⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mulehttpsender.java

📁 提供ESB 应用mule源代码 提供ESB 应用mule源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            finally            {                // Flush ONLY once.                out.flush();            }        }        if (log.isDebugEnabled() && baos != null)        {            log.debug(header + new String(baos.toByteArray()));        }        return inp;    }    private InputStream readHeadersFromSocket(SocketHolder sockHolder,                                              MessageContext msgContext,                                              InputStream inp,                                              Hashtable headers) throws IOException    {        byte b = 0;        int len = 0;        int colonIndex = -1;        String name, value;        int returnCode = 0;        if (null == inp)        {            inp = new BufferedInputStream(sockHolder.getSocket().getInputStream());        }        if (headers == null)        {            headers = new Hashtable();        }        // Should help performance. Temporary fix only till its all stream        // oriented.        // Need to add logic for getting the version # and the return code        // but that's for tomorrow!        /* Logic to read HTTP response headers */        boolean readTooMuch = false;        for (ByteArrayOutputStream buf = new ByteArrayOutputStream(4097);;)        {            if (!readTooMuch)            {                b = (byte)inp.read();            }            if (b == -1)            {                break;            }            readTooMuch = false;            if ((b != '\r') && (b != '\n'))            {                if ((b == ':') && (colonIndex == -1))                {                    colonIndex = len;                }                len++;                buf.write(b);            }            else if (b == '\r')            {                continue;            }            else            { // b== '\n'                if (len == 0)                {                    break;                }                b = (byte)inp.read();                readTooMuch = true;                // A space or tab at the begining of a line means the header                // continues.                if ((b == ' ') || (b == '\t'))                {                    continue;                }                buf.close();                byte[] hdata = buf.toByteArray();                buf.reset();                if (colonIndex != -1)                {                    name = new String(hdata, 0, colonIndex, HTTPConstants.HEADER_DEFAULT_CHAR_ENCODING);                    value = new String(hdata, colonIndex + 1, len - 1 - colonIndex,                        HTTPConstants.HEADER_DEFAULT_CHAR_ENCODING);                    colonIndex = -1;                }                else                {                    name = new String(hdata, 0, len, HTTPConstants.HEADER_DEFAULT_CHAR_ENCODING);                    value = "";                }                if (log.isDebugEnabled())                {                    log.debug(name + value);                }                if (msgContext.getProperty(HTTPConstants.MC_HTTP_STATUS_CODE) == null)                {                    // Reader status code                    int start = name.indexOf(' ') + 1;                    String tmp = name.substring(start).trim();                    int end = tmp.indexOf(' ');                    if (end != -1)                    {                        tmp = tmp.substring(0, end);                    }                    returnCode = Integer.parseInt(tmp);                    msgContext.setProperty(HTTPConstants.MC_HTTP_STATUS_CODE, new Integer(returnCode));                    msgContext.setProperty(HTTPConstants.MC_HTTP_STATUS_MESSAGE, name.substring(start + end                                                                                                + 1));                }                else                {                    headers.put(name.toLowerCase(), value);                }                len = 0;            }        }        return inp;    }    /**     * Reads the SOAP response back from the server     *      * @param msgContext message context     * @throws IOException     */    private InputStream readFromSocket(SocketHolder socketHolder,                                       MessageContext msgContext,                                       InputStream inp,                                       Hashtable headers) throws IOException    {        Message outMsg = null;        byte b;        Integer rc = (Integer)msgContext.getProperty(HTTPConstants.MC_HTTP_STATUS_CODE);        int returnCode = 0;        if (rc != null)        {            returnCode = rc.intValue();        }        else        {            // No return code?? Should have one by now.        }        /* All HTTP headers have been read. */        String contentType = (String)headers.get(HEADER_CONTENT_TYPE_LC);        contentType = (null == contentType) ? null : contentType.trim();        String location = (String)headers.get(HEADER_LOCATION_LC);        location = (null == location) ? null : location.trim();        if ((returnCode > 199) && (returnCode < 300))        {            if (returnCode == 202)            {                return inp;            }            // SOAP return is OK - so fall through        }        else if (msgContext.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS)        {            // For now, if we're SOAP 1.2, fall through, since the range of            // valid result codes is much greater        }        else if ((contentType != null) && !contentType.startsWith("text/html")                 && ((returnCode > 499) && (returnCode < 600)))        {            // SOAP Fault should be in here - so fall through        }        else if ((location != null) && ((returnCode == 302) || (returnCode == 307)))        {            // Temporary Redirect (HTTP: 302/307)            // close old connection            inp.close();            socketHolder.getSocket().close();            // remove former result and set new target url            msgContext.removeProperty(HTTPConstants.MC_HTTP_STATUS_CODE);            msgContext.setProperty(MessageContext.TRANS_URL, location);            // next try            invoke(msgContext);            return inp;        }        else if (returnCode == 100)        {            msgContext.removeProperty(HTTPConstants.MC_HTTP_STATUS_CODE);            msgContext.removeProperty(HTTPConstants.MC_HTTP_STATUS_MESSAGE);            readHeadersFromSocket(socketHolder, msgContext, inp, headers);            return readFromSocket(socketHolder, msgContext, inp, headers);        }        else        {            // Unknown return code - so wrap up the content into a            // SOAP Fault.            ByteArrayOutputStream buf = new ByteArrayOutputStream(4097);            while (-1 != (b = (byte)inp.read()))            {                buf.write(b);            }            String statusMessage = msgContext.getStrProp(HTTPConstants.MC_HTTP_STATUS_MESSAGE);            AxisFault fault = new AxisFault("HTTP", "(" + returnCode + ")" + statusMessage, null, null);            fault.setFaultDetailString(Messages.getMessage("return01", String.valueOf(returnCode), buf.toString()));            fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_HTTPERRORCODE, Integer.toString(returnCode));            throw fault;        }        String contentLocation = (String)headers.get(HEADER_CONTENT_LOCATION_LC);        contentLocation = (null == contentLocation) ? null : contentLocation.trim();        String contentLength = (String)headers.get(HEADER_CONTENT_LENGTH_LC);        contentLength = (null == contentLength) ? null : contentLength.trim();        String transferEncoding = (String)headers.get(HEADER_TRANSFER_ENCODING_LC);        if (null != transferEncoding)        {            transferEncoding = transferEncoding.trim().toLowerCase();            if (transferEncoding.equals(HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED))            {                inp = new ChunkedInputStream(inp);            }        }        outMsg = new Message(new SocketInputStream(inp, socketHolder.getSocket()), false, contentType,            contentLocation);        // Transfer HTTP headers of HTTP message to MIME headers of SOAP message        MimeHeaders mimeHeaders = outMsg.getMimeHeaders();        for (Enumeration e = headers.keys(); e.hasMoreElements();)        {            String key = (String)e.nextElement();            mimeHeaders.addHeader(key, ((String)headers.get(key)).trim());        }        outMsg.setMessageType(Message.RESPONSE);        msgContext.setResponseMessage(outMsg);        if (log.isDebugEnabled())        {            if (null == contentLength)            {                log.debug(SystemUtils.LINE_SEPARATOR + Messages.getMessage("no00", "Content-Length"));            }            log.debug(SystemUtils.LINE_SEPARATOR + Messages.getMessage("xmlRecd00"));            log.debug("-----------------------------------------------");            log.debug(outMsg.getSOAPEnvelope().toString());        }        // if we are maintaining session state,        // handle cookies (if any)        if (msgContext.getMaintainSession())        {            handleCookie(HTTPConstants.HEADER_COOKIE, HTTPConstants.HEADER_SET_COOKIE, headers, msgContext);            handleCookie(HTTPConstants.HEADER_COOKIE2, HTTPConstants.HEADER_SET_COOKIE2, headers, msgContext);        }        return inp;    }    /**     * little helper function for cookies     *      * @param cookieName     * @param setCookieName     * @param headers     * @param msgContext     */    public void handleCookie(String cookieName,                             String setCookieName,                             Hashtable headers,                             MessageContext msgContext)    {        if (headers.containsKey(setCookieName.toLowerCase()))        {            String cookie = (String)headers.get(setCookieName.toLowerCase());            cookie = cookie.trim();            // chop after first ; a la Apache SOAP (see HTTPUtils.java there)            int index = cookie.indexOf(';');            if (index != -1)            {                cookie = cookie.substring(0, index);            }            msgContext.setProperty(cookieName, cookie);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -