📄 httpsender.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.axis.transport.http;import org.apache.axis.AxisFault;import org.apache.axis.Message;import org.apache.axis.MessageContext;import org.apache.axis.Constants;import org.apache.axis.components.logger.LogFactory;import org.apache.axis.components.net.BooleanHolder;import org.apache.axis.components.net.SocketFactory;import org.apache.axis.components.net.SocketFactoryFactory;import org.apache.axis.components.net.DefaultSocketFactory;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.utils.Messages;import org.apache.axis.utils.TeeOutputStream;import org.apache.commons.logging.Log;import javax.xml.soap.MimeHeader;import javax.xml.soap.MimeHeaders;import javax.xml.soap.SOAPException;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;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 java.util.ArrayList;import java.util.Collection;/** * This is meant to be used on a SOAP Client to call a SOAP server. * * @author Doug Davis (dug@us.ibm.com) * @author Davanum Srinivas (dims@yahoo.com) */public class HTTPSender extends BasicHandler { protected static 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 reqeuest. ": " + 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")); } SocketHolder socketHolder = new SocketHolder(null); try { BooleanHolder useFullURL = new BooleanHolder(false); StringBuffer otherHeaders = new StringBuffer(); targetURL = new URL(msgContext.getStrProp(MessageContext.TRANS_URL)); String host = targetURL.getHost(); int port = targetURL.getPort(); // Send the SOAP request to the server InputStream inp = writeToSocket(socketHolder, msgContext, targetURL, otherHeaders, host, port, msgContext.getTimeout(), useFullURL); // 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); try { if (socketHolder.getSocket() != null ) { socketHolder.getSocket().close(); } } catch (IOException ie) { // we shouldn't get here. } 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 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)); } Socket sock = factory.create(host, port, otherHeaders, useFullURL); 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(); 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()) { fillHeaders(msgContext, HTTPConstants.HEADER_COOKIE, otherHeaders); fillHeaders(msgContext, HTTPConstants.HEADER_COOKIE2, otherHeaders); } StringBuffer header2 = new StringBuffer(); String webMethod = null; boolean posting = true; Message reqMessage = msgContext.getRequestMessage(); boolean http10 = true; //True if this is to use HTTP 1.0 / false HTTP 1.1 boolean httpChunkStream = false; //Use HTTP chunking or not. boolean httpContinueExpected = false; //Under HTTP 1.1 if false you *MAY* need to wait for a 100 rc, // if true the server MUST reply with 100 continue. String httpConnection = null; String httpver = msgContext.getStrProp(MessageContext.HTTP_TRANSPORT_VERSION); if (null == httpver) { httpver = HTTPConstants.HEADER_PROTOCOL_V10; } httpver = httpver.trim(); if (httpver.equals(HTTPConstants.HEADER_PROTOCOL_V11)) { http10 = false; } //process user defined headers for information. Hashtable userHeaderTable = (Hashtable) msgContext. getProperty(HTTPConstants.REQUEST_HEADERS); if (userHeaderTable != null) { if (null == otherHeaders) { otherHeaders = new StringBuffer(1024); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -