abstracthttpsender.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 572 行 · 第 1/2 页
JAVA
572 行
*/
protected void setAuthenticationInfo(HttpClient agent,
MessageContext msgCtx,
HostConfiguration config) throws AxisFault {
HttpTransportProperties.Authenticator authenticator;
Object obj = msgCtx.getProperty(HTTPConstants.AUTHENTICATE);
if (obj != null) {
if (obj instanceof HttpTransportProperties.Authenticator) {
authenticator = (HttpTransportProperties.Authenticator) obj;
String username = authenticator.getUsername();
String password = authenticator.getPassword();
String host = authenticator.getHost();
String domain = authenticator.getDomain();
int port = authenticator.getPort();
String realm = authenticator.getRealm();
/* If retrying is available set it first */
isAllowedRetry = authenticator.isAllowedRetry();
Credentials creds;
agent.getParams()
.setAuthenticationPreemptive(authenticator.getPreemptiveAuthentication());
if (host != null) {
if (domain != null) {
/*Credentials for NTLM Authentication*/
creds = new NTCredentials(username, password, host, domain);
} else {
/*Credentials for Digest and Basic Authentication*/
creds = new UsernamePasswordCredentials(username, password);
}
agent.getState().setCredentials(new AuthScope(host, port, realm), creds);
} else {
if (domain != null) {
/*Credentials for NTLM Authentication when host is ANY_HOST*/
creds = new NTCredentials(username, password, AuthScope.ANY_HOST, domain);
agent.getState().setCredentials(
new AuthScope(AuthScope.ANY_HOST, port, realm), creds);
} else {
/*Credentials only for Digest and Basic Authentication*/
creds = new UsernamePasswordCredentials(username, password);
agent.getState().setCredentials(new AuthScope(AuthScope.ANY), creds);
}
}
/* Customizing the priority Order */
List schemes = authenticator.getAuthSchemes();
if (schemes != null && schemes.size() > 0) {
List authPrefs = new ArrayList(3);
for (int i = 0; i < schemes.size(); i++) {
if (schemes.get(i) instanceof AuthPolicy) {
authPrefs.add(schemes.get(i));
continue;
}
String scheme = (String) schemes.get(i);
if (HttpTransportProperties.Authenticator.BASIC.equals(scheme)) {
authPrefs.add(AuthPolicy.BASIC);
} else if (HttpTransportProperties.Authenticator.NTLM.equals(scheme)) {
authPrefs.add(AuthPolicy.NTLM);
} else if (HttpTransportProperties.Authenticator.DIGEST.equals(scheme)) {
authPrefs.add(AuthPolicy.DIGEST);
}
}
agent.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY,
authPrefs);
}
} else {
throw new AxisFault("HttpTransportProperties.Authenticator class cast exception");
}
}
}
/**
* Method used to copy all the common properties
*
* @param msgContext - The messageContext of the request message
* @param url - The target URL
* @param httpMethod - The http method used to send the request
* @param httpClient - The httpclient used to send the request
* @param soapActionString - The soap action atring of the request message
* @return MessageFormatter - The messageFormatter for the relavent request message
* @throws AxisFault - Thrown in case an exception occurs
*/
protected MessageFormatter populateCommonProperties(MessageContext msgContext, URL url,
HttpMethodBase httpMethod,
HttpClient httpClient,
String soapActionString)
throws AxisFault {
if (isAuthenticationEnabled(msgContext)) {
httpMethod.setDoAuthentication(true);
}
MessageFormatter messageFormatter = TransportUtils.getMessageFormatter(
msgContext);
url = messageFormatter.getTargetAddress(msgContext, format, url);
httpMethod.setPath(url.getPath());
httpMethod.setQueryString(url.getQuery());
httpMethod.setRequestHeader(HTTPConstants.HEADER_CONTENT_TYPE,
messageFormatter.getContentType(msgContext, format,
soapActionString));
httpMethod.setRequestHeader(HTTPConstants.HEADER_HOST, url.getHost());
if (msgContext.getOptions() != null && msgContext.getOptions().isManageSession()) {
// setting the cookie in the out path
Object cookieString = msgContext.getProperty(HTTPConstants.COOKIE_STRING);
if (cookieString != null) {
StringBuffer buffer = new StringBuffer();
buffer.append(cookieString);
httpMethod.setRequestHeader(HTTPConstants.HEADER_COOKIE, buffer.toString());
}
}
if (httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_10)) {
httpClient.getParams().setVersion(HttpVersion.HTTP_1_0);
}
return messageFormatter;
}
/**
* This is used to get the dynamically set time out values from the
* message context. If the values are not available or invalid then
* the default values or the values set by the configuration will be used
*
* @param msgContext the active MessageContext
* @param httpClient
*/
protected void initializeTimeouts(MessageContext msgContext, HttpClient httpClient) {
// If the SO_TIMEOUT of CONNECTION_TIMEOUT is set by dynamically the
// override the static config
Integer tempSoTimeoutProperty =
(Integer) msgContext.getProperty(HTTPConstants.SO_TIMEOUT);
Integer tempConnTimeoutProperty =
(Integer) msgContext
.getProperty(HTTPConstants.CONNECTION_TIMEOUT);
long timeout = msgContext.getOptions().getTimeOutInMilliSeconds();
if (tempConnTimeoutProperty != null) {
int connectionTimeout = tempConnTimeoutProperty.intValue();
// timeout for initial connection
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
} else {
// set timeout in client
if (timeout > 0) {
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout((int) timeout);
}
}
if (tempSoTimeoutProperty != null) {
int soTimeout = tempSoTimeoutProperty.intValue();
// SO_TIMEOUT -- timeout for blocking reads
httpClient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);
httpClient.getParams().setSoTimeout(soTimeout);
} else {
// set timeout in client
if (timeout > 0) {
httpClient.getHttpConnectionManager().getParams().setSoTimeout((int) timeout);
httpClient.getParams().setSoTimeout((int) timeout);
}
}
}
public void setFormat(OMOutputFormat format) {
this.format = format;
}
protected HttpClient getHttpClient(MessageContext msgContext) {
HttpClient httpClient;
Object reuse = msgContext.getOptions().getProperty(HTTPConstants.REUSE_HTTP_CLIENT);
if (reuse == null) {
reuse = msgContext.getConfigurationContext().getProperty(HTTPConstants.REUSE_HTTP_CLIENT);
}
if (reuse != null && JavaUtils.isTrueExplicitly(reuse)) {
httpClient = (HttpClient) msgContext.getOptions().getProperty(HTTPConstants.CACHED_HTTP_CLIENT);
if (httpClient == null) {
httpClient = (HttpClient) msgContext.getConfigurationContext()
.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);
}
if (httpClient != null)
return httpClient;
MultiThreadedHttpConnectionManager connectionManager =
new MultiThreadedHttpConnectionManager();
httpClient = new HttpClient(connectionManager);
msgContext.getConfigurationContext()
.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
} else {
HttpConnectionManager connManager =
(HttpConnectionManager) msgContext.getProperty(
HTTPConstants.MUTTITHREAD_HTTP_CONNECTION_MANAGER);
if(connManager != null){
httpClient = new HttpClient(connManager);
} else {
//Multi threaded http connection manager has set as the default
connManager = new MultiThreadedHttpConnectionManager();
httpClient = new HttpClient(connManager);
}
}
// Get the timeout values set in the runtime
initializeTimeouts(msgContext, httpClient);
return httpClient;
}
protected void executeMethod(HttpClient httpClient, MessageContext msgContext, URL url,
HttpMethod method) throws IOException {
HostConfiguration config = this.getHostConfiguration(httpClient, msgContext, url);
msgContext.setProperty(HTTPConstants.HTTP_METHOD, method);
// set the custom headers, if available
addCustomHeaders(method, msgContext);
// add compression headers if needed
if (msgContext.isPropertyTrue(HTTPConstants.MC_ACCEPT_GZIP)) {
method.addRequestHeader(HTTPConstants.HEADER_ACCEPT_ENCODING,
HTTPConstants.COMPRESSION_GZIP);
}
if (msgContext.isPropertyTrue(HTTPConstants.MC_GZIP_REQUEST)) {
method.addRequestHeader(HTTPConstants.HEADER_CONTENT_ENCODING,
HTTPConstants.COMPRESSION_GZIP);
}
httpClient.executeMethod(config, method);
}
public void addCustomHeaders(HttpMethod method, MessageContext msgContext) {
boolean isCustomUserAgentSet = false;
// set the custom headers, if available
Object httpHeadersObj = msgContext.getProperty(HTTPConstants.HTTP_HEADERS);
if (httpHeadersObj != null && httpHeadersObj instanceof ArrayList) {
ArrayList httpHeaders = (ArrayList) httpHeadersObj;
Header header;
for (int i = 0; i < httpHeaders.size(); i++) {
header = (Header) httpHeaders.get(i);
if (HTTPConstants.HEADER_USER_AGENT.equals(header.getName())) {
isCustomUserAgentSet = true;
}
method.addRequestHeader(header);
}
}
if (!isCustomUserAgentSet) {
String userAgentString = getUserAgent(msgContext);
method.setRequestHeader(HTTPConstants.HEADER_USER_AGENT, userAgentString);
}
}
private String getUserAgent(MessageContext messageContext) {
String userAgentString = "Axis2";
boolean locked = false;
if (messageContext.getParameter(HTTPConstants.USER_AGENT) != null) {
OMElement userAgentElement =
messageContext.getParameter(HTTPConstants.USER_AGENT).getParameterElement();
userAgentString = userAgentElement.getText().trim();
OMAttribute lockedAttribute = userAgentElement.getAttribute(new QName("locked"));
if (lockedAttribute != null) {
if (lockedAttribute.getAttributeValue().equalsIgnoreCase("true")) {
locked = true;
}
}
}
// Runtime overing part
if (!locked) {
if (messageContext.getProperty(HTTPConstants.USER_AGENT) != null) {
userAgentString = (String) messageContext.getProperty(HTTPConstants.USER_AGENT);
}
}
return userAgentString;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?