📄 mulehttpsender.java
字号:
/* * $Id: MuleHttpSender.java 10489 2008-01-23 17:53:38Z dfeist $ * -------------------------------------------------------------------------------------- * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com * * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */package org.mule.transport.soap.axis.extensions;import org.mule.transport.soap.SoapConstants;import org.mule.util.StringUtils;import org.mule.util.SystemUtils;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import java.net.URL;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import javax.xml.soap.MimeHeader;import javax.xml.soap.MimeHeaders;import javax.xml.soap.SOAPException;import org.apache.axis.AxisFault;import org.apache.axis.Constants;import org.apache.axis.Message;import org.apache.axis.MessageContext;import org.apache.axis.client.Call;import org.apache.axis.components.logger.LogFactory;import org.apache.axis.components.net.BooleanHolder;import org.apache.axis.components.net.DefaultSocketFactory;import org.apache.axis.components.net.SocketFactory;import org.apache.axis.components.net.SocketFactoryFactory;import org.apache.axis.encoding.Base64;import org.apache.axis.handlers.BasicHandler;import org.apache.axis.soap.SOAP12Constants;import org.apache.axis.soap.SOAPConstants;import org.apache.axis.transport.http.ChunkedInputStream;import org.apache.axis.transport.http.ChunkedOutputStream;import org.apache.axis.transport.http.HTTPConstants;import org.apache.axis.transport.http.HTTPSender;import org.apache.axis.transport.http.SocketHolder;import org.apache.axis.transport.http.SocketInputStream;import org.apache.axis.utils.Messages;import org.apache.axis.utils.TeeOutputStream;import org.apache.commons.io.output.ByteArrayOutputStream;import org.apache.commons.logging.Log;/** * <code>MuleHttpSender</code> is a rewrite of the Axis HttpSender. Unfortunately, * the Axis implementation is not extensible so this class is a copy of it with * modifications. The enhancements made are to allow for asynchronous Http method * calls which Mule initiates when the endpoint is asynchronous. * * @deprecated Use the UniversalSender instead */public class MuleHttpSender extends BasicHandler{ /** * Serial version */ private static final long serialVersionUID = -1730816323289419500L; protected static final Log log = LogFactory.getLog(HTTPSender.class.getName()); private static final String ACCEPT_HEADERS = HTTPConstants.HEADER_ACCEPT // limit to the types that are // meaningful to us. + ": " + HTTPConstants.HEADER_ACCEPT_APPL_SOAP + ", " + HTTPConstants.HEADER_ACCEPT_APPLICATION_DIME + ", " + HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED + ", " + HTTPConstants.HEADER_ACCEPT_TEXT_ALL + "\r\n" + HTTPConstants.HEADER_USER_AGENT // Tell // who // we // are. + ": " + Messages.getMessage("axisUserAgent") + "\r\n"; private static final String CACHE_HEADERS = HTTPConstants.HEADER_CACHE_CONTROL // stop caching proxies from caching // SOAP request. + ": " + HTTPConstants.HEADER_CACHE_CONTROL_NOCACHE + "\r\n" + HTTPConstants.HEADER_PRAGMA + ": " + HTTPConstants.HEADER_CACHE_CONTROL_NOCACHE + "\r\n"; private static final String CHUNKED_HEADER = HTTPConstants.HEADER_TRANSFER_ENCODING + ": " + HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED + "\r\n"; private static final String HEADER_CONTENT_TYPE_LC = HTTPConstants.HEADER_CONTENT_TYPE.toLowerCase(); private static final String HEADER_LOCATION_LC = HTTPConstants.HEADER_LOCATION.toLowerCase(); private static final String HEADER_CONTENT_LOCATION_LC = HTTPConstants.HEADER_CONTENT_LOCATION.toLowerCase(); private static final String HEADER_CONTENT_LENGTH_LC = HTTPConstants.HEADER_CONTENT_LENGTH.toLowerCase(); private static final String HEADER_TRANSFER_ENCODING_LC = HTTPConstants.HEADER_TRANSFER_ENCODING.toLowerCase(); /** * the url; used for error reporting */ URL targetURL; /** * invoke creates a socket connection, sends the request SOAP message and then * reads the response SOAP message back from the SOAP server * * @param msgContext the messsage context * @throws AxisFault */ public void invoke(MessageContext msgContext) throws AxisFault { if (log.isDebugEnabled()) { log.debug(Messages.getMessage("enter00", "HTTPSender::invoke")); } try { Call call = (Call)msgContext.getProperty("call_object"); String transURL = msgContext.getStrProp(MessageContext.TRANS_URL); String uri = transURL; if (call != null && call.useSOAPAction()) { uri = call.getSOAPActionURI(); } msgContext.setProperty(SoapConstants.SOAP_ACTION_PROPERTY_CAPS, uri); BooleanHolder useFullURL = new BooleanHolder(false); StringBuffer otherHeaders = new StringBuffer(64); targetURL = new URL(transURL); String host = targetURL.getHost(); int port = targetURL.getPort(); SocketHolder socketHolder = new SocketHolder(null); // Send the SOAP request to the server InputStream inp = writeToSocket(socketHolder, msgContext, targetURL, otherHeaders, host, port, msgContext.getTimeout(), useFullURL); if (msgContext.isClient() && call != null) { if (Boolean.TRUE.equals(call.getProperty("axis.one.way"))) { return; } } // Read the response back from the server Hashtable headers = new Hashtable(); inp = readHeadersFromSocket(socketHolder, msgContext, inp, headers); readFromSocket(socketHolder, msgContext, inp, headers); } catch (Exception e) { log.debug(e); throw AxisFault.makeFault(e); } if (log.isDebugEnabled()) { log.debug(Messages.getMessage("exit00", "HTTPDispatchHandler::invoke")); } } /** * Creates a socket connection to the SOAP server * * @param protocol "http" for standard, "https" for ssl. * @param host host name * @param port port to connect to * @param otherHeaders buffer for storing additional headers that need to be sent * @param useFullURL flag to indicate if the complete URL has to be sent * @throws java.io.IOException */ protected void getSocket(SocketHolder sockHolder, MessageContext msgContext, String protocol, String host, int port, int timeout, StringBuffer otherHeaders, BooleanHolder useFullURL) throws Exception { Hashtable options = getOptions(); if (timeout > 0) { if (options == null) { options = new Hashtable(); } options.put(DefaultSocketFactory.CONNECT_TIMEOUT, Integer.toString(timeout)); } SocketFactory factory = SocketFactoryFactory.getFactory(protocol, options); if (factory == null) { throw new IOException(Messages.getMessage("noSocketFactory", protocol)); } // log.fatal("Axis client: connect on socket: " + host + ":" + port); Socket sock = null; try { sock = factory.create(host, port, otherHeaders, useFullURL); } catch (Exception e) { Thread.sleep(1000); try { sock = factory.create(host, port, otherHeaders, useFullURL); } catch (Exception e1) { log.fatal("Axis client Failed: connect on socket: " + host + ":" + port, e); throw e; } } if (timeout > 0) { sock.setSoTimeout(timeout); } sockHolder.setSocket(sock); } /** * Send the soap request message to the server * * @param msgContext message context * @param tmpURL url to connect to * @param otherHeaders other headers if any * @param host host name * @param port port * @param useFullURL flag to indicate if the whole url needs to be sent * @throws IOException */ private InputStream writeToSocket(SocketHolder sockHolder, MessageContext msgContext, URL tmpURL, StringBuffer otherHeaders, String host, int port, int timeout, BooleanHolder useFullURL) throws Exception { String userID = msgContext.getUsername(); String passwd = msgContext.getPassword(); // Get SOAPAction, default to "" String action = msgContext.useSOAPAction() ? msgContext.getSOAPActionURI() : ""; if (action == null) { action = ""; } // if UserID is not part of the context, but is in the URL, use // the one in the URL. if ((userID == null) && (tmpURL.getUserInfo() != null)) { String info = tmpURL.getUserInfo(); int sep = info.indexOf(':'); if ((sep >= 0) && (sep + 1 < info.length())) { userID = info.substring(0, sep); passwd = info.substring(sep + 1); } else { userID = info; } } if (userID != null) { StringBuffer tmpBuf = new StringBuffer(64); tmpBuf.append(userID).append(":").append((passwd == null) ? "" : passwd); otherHeaders.append(HTTPConstants.HEADER_AUTHORIZATION).append(": Basic ").append( Base64.encode(tmpBuf.toString().getBytes())).append("\r\n"); } // don't forget the cookies! // mmm... cookies if (msgContext.getMaintainSession()) { String cookie = msgContext.getStrProp(HTTPConstants.HEADER_COOKIE); String cookie2 = msgContext.getStrProp(HTTPConstants.HEADER_COOKIE2); if (cookie != null) { otherHeaders.append(HTTPConstants.HEADER_COOKIE).append(": ").append(cookie).append("\r\n"); } if (cookie2 != null) { otherHeaders.append(HTTPConstants.HEADER_COOKIE2).append(": ").append(cookie2).append("\r\n"); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -