abstracthttpsender.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 572 行 · 第 1/2 页
JAVA
572 行
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.axis2.transport.http;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMOutputFormat;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.OperationContext;
import org.apache.axis2.description.TransportOutDescription;
import org.apache.axis2.i18n.Messages;
import org.apache.axis2.transport.MessageFormatter;
import org.apache.axis2.transport.TransportUtils;
import org.apache.axis2.util.JavaUtils;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.xml.namespace.QName;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
public abstract class AbstractHTTPSender {
protected static final String ANONYMOUS = "anonymous";
protected static final String PROXY_HOST_NAME = "proxy_host";
protected static final String PROXY_PORT = "proxy_port";
protected boolean chunked = false;
protected String httpVersion = HTTPConstants.HEADER_PROTOCOL_11;
private static final Log log = LogFactory.getLog(AbstractHTTPSender.class);
protected static final String PROTOCOL_HTTP = "http";
protected static final String PROTOCOL_HTTPS = "https";
/**
* proxydiscription
*/
protected TransportOutDescription proxyOutSetting = null;
protected OMOutputFormat format = new OMOutputFormat();
/**
* isAllowedRetry will be using to check where the
* retry should be allowed or not.
*/
protected boolean isAllowedRetry = false;
public void setChunked(boolean chunked) {
this.chunked = chunked;
}
public void setHttpVersion(String version) throws AxisFault {
if (version != null) {
if (HTTPConstants.HEADER_PROTOCOL_11.equals(version)) {
this.httpVersion = HTTPConstants.HEADER_PROTOCOL_11;
} else if (HTTPConstants.HEADER_PROTOCOL_10.equals(version)) {
this.httpVersion = HTTPConstants.HEADER_PROTOCOL_10;
// chunked is not possible with HTTP/1.0
this.chunked = false;
} else {
throw new AxisFault(
"Parameter " + HTTPConstants.PROTOCOL_VERSION
+ " Can have values only HTTP/1.0 or HTTP/1.1");
}
}
}
/**
* Collect the HTTP header information and set them in the message context
*
* @param method HttpMethodBase from which to get information
* @param msgContext the MessageContext in which to place the information... OR NOT!
* @throws AxisFault if problems occur
*/
protected void obtainHTTPHeaderInformation(HttpMethodBase method,
MessageContext msgContext) throws AxisFault {
// Set RESPONSE properties onto the REQUEST message context. They will need to be copied off the request context onto
// the response context elsewhere, for example in the OutInOperationClient.
Map transportHeaders = new CommonsTransportHeaders(method.getResponseHeaders());
msgContext.setProperty(MessageContext.TRANSPORT_HEADERS, transportHeaders);
msgContext.setProperty(HTTPConstants.MC_HTTP_STATUS_CODE, new Integer(method.getStatusCode()));
Header header = method.getResponseHeader(HTTPConstants.HEADER_CONTENT_TYPE);
if (header != null) {
HeaderElement[] headers = header.getElements();
MessageContext inMessageContext = msgContext.getOperationContext().getMessageContext(
WSDLConstants.MESSAGE_LABEL_IN_VALUE);
Object contentType = header.getValue();
Object charSetEnc = null;
for (int i = 0; i < headers.length; i++) {
NameValuePair charsetEnc = headers[i].getParameterByName(
HTTPConstants.CHAR_SET_ENCODING);
if (charsetEnc != null) {
charSetEnc = charsetEnc.getValue();
}
}
if (inMessageContext != null) {
inMessageContext
.setProperty(Constants.Configuration.CONTENT_TYPE, contentType);
inMessageContext
.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
} else {
// Transport details will be stored in a HashMap so that anybody interested can
// retrieve them
HashMap transportInfoMap = new HashMap();
transportInfoMap.put(Constants.Configuration.CONTENT_TYPE, contentType);
transportInfoMap.put(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
//the HashMap is stored in the outgoing message.
msgContext.setProperty(Constants.Configuration.TRANSPORT_INFO_MAP,
transportInfoMap);
}
}
String sessionCookie = null;
// Process old style headers first
Header[] cookieHeaders = method.getResponseHeaders(HTTPConstants.HEADER_SET_COOKIE);
String customCoookiId = (String) msgContext.getProperty(Constants.CUSTOM_COOKIE_ID);
for (int i = 0; i < cookieHeaders.length; i++) {
HeaderElement[] elements = cookieHeaders[i].getElements();
for (int e = 0; e < elements.length; e++) {
HeaderElement element = elements[e];
if (Constants.SESSION_COOKIE.equalsIgnoreCase(element.getName()) ||
Constants.SESSION_COOKIE_JSESSIONID.equalsIgnoreCase(element.getName())) {
sessionCookie = processCookieHeader(element);
}
if (customCoookiId != null && customCoookiId.equalsIgnoreCase(element.getName())) {
sessionCookie = processCookieHeader(element);
}
}
}
// Overwrite old style cookies with new style ones if present
cookieHeaders = method.getResponseHeaders(HTTPConstants.HEADER_SET_COOKIE2);
for (int i = 0; i < cookieHeaders.length; i++) {
HeaderElement[] elements = cookieHeaders[i].getElements();
for (int e = 0; e < elements.length; e++) {
HeaderElement element = elements[e];
if (Constants.SESSION_COOKIE.equalsIgnoreCase(element.getName()) ||
Constants.SESSION_COOKIE_JSESSIONID.equalsIgnoreCase(element.getName())) {
sessionCookie = processCookieHeader(element);
}
if(customCoookiId!=null&&customCoookiId.equalsIgnoreCase(element.getName())){
sessionCookie = processCookieHeader(element);
}
}
}
if (sessionCookie != null) {
msgContext.getServiceContext().setProperty(HTTPConstants.COOKIE_STRING, sessionCookie);
}
}
private String processCookieHeader(HeaderElement element) {
String cookie = element.getName() + "=" + element.getValue();
NameValuePair[] parameters = element.getParameters();
for (int j = 0; j < parameters.length; j++) {
NameValuePair parameter = parameters[j];
cookie = cookie + "; " + parameter.getName() + "=" + parameter.getValue();
}
return cookie;
}
protected void processResponse(HttpMethodBase httpMethod,
MessageContext msgContext)
throws IOException {
obtainHTTPHeaderInformation(httpMethod, msgContext);
InputStream in = httpMethod.getResponseBodyAsStream();
if (in == null) {
throw new AxisFault(Messages.getMessage("canNotBeNull", "InputStream"));
}
Header contentEncoding =
httpMethod.getResponseHeader(HTTPConstants.HEADER_CONTENT_ENCODING);
if (contentEncoding != null) {
if (contentEncoding.getValue().
equalsIgnoreCase(HTTPConstants.COMPRESSION_GZIP)) {
in = new GZIPInputStream(in);
} else {
throw new AxisFault("HTTP :" + "unsupported content-encoding of '"
+ contentEncoding.getValue() + "' found");
}
}
OperationContext opContext = msgContext.getOperationContext();
if (opContext != null) {
opContext.setProperty(MessageContext.TRANSPORT_IN, in);
}
}
public abstract void send(MessageContext msgContext, URL url, String soapActionString)
throws IOException;
/**
* getting host configuration to support standard http/s, proxy and NTLM support
*
* @param client active HttpClient
* @param msgCtx active MessageContext
* @param targetURL the target URL
* @return a HostConfiguration set up with proxy information
* @throws AxisFault if problems occur
*/
protected HostConfiguration getHostConfiguration(HttpClient client,
MessageContext msgCtx,
URL targetURL)throws AxisFault {
boolean isAuthenticationEnabled = isAuthenticationEnabled(msgCtx);
int port = targetURL.getPort();
String protocol = targetURL.getProtocol();
if (port == -1) {
if (PROTOCOL_HTTP.equals(protocol)) {
port = 80;
} else if (PROTOCOL_HTTPS.equals(protocol)) {
port = 443;
}
}
// to see the host is a proxy and in the proxy list - available in axis2.xml
HostConfiguration config = new HostConfiguration();
// one might need to set his own socket factory. Let's allow that case as well.
Protocol protocolHandler =
(Protocol)msgCtx.getOptions().getProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER);
// setting the real host configuration
// I assume the 90% case, or even 99% case will be no protocol handler case.
if (protocolHandler == null) {
config.setHost(targetURL.getHost(), port, targetURL.getProtocol());
} else {
config.setHost(targetURL.getHost(), port, protocolHandler);
}
if (isAuthenticationEnabled) {
// Basic, Digest, NTLM and custom authentications.
this.setAuthenticationInfo(client, msgCtx, config);
}
// proxy configuration
if (ProxyConfiguration.isProxyEnabled(msgCtx,targetURL)) {
log.debug("ProxyConfiguration");
ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
proxyConfiguration.configure(msgCtx,client,config);
}
return config;
}
protected boolean isAuthenticationEnabled(MessageContext msgCtx) {
return (msgCtx.getProperty(HTTPConstants.AUTHENTICATE) != null);
}
/*
This will handle server Authentication, It could be either NTLM, Digest or Basic Authentication.
Apart from that user can change the priory or add a custom authentication scheme.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?