in axis-rt-transport-http-hc3/src/main/java/org/apache/axis/transport/http/CommonsHTTPSender.java [119:308]
public void invoke(MessageContext msgContext) throws AxisFault {
HttpMethodBase method = null;
if (log.isDebugEnabled()) {
log.debug(Messages.getMessage("enter00",
"CommonsHTTPSender::invoke"));
}
try {
URL targetURL =
new URL(msgContext.getStrProp(MessageContext.TRANS_URL));
// no need to retain these, as the cookies/credentials are
// stored in the message context across multiple requests.
// the underlying connection manager, however, is retained
// so sockets get recycled when possible.
HttpClient httpClient = new HttpClient(this.connectionManager);
// the timeout value for allocation of connections from the pool
httpClient.getParams().setConnectionManagerTimeout(this.clientProperties.getConnectionPoolTimeout());
HostConfiguration hostConfiguration =
getHostConfiguration(httpClient, msgContext, targetURL);
boolean posting = true;
// If we're SOAP 1.2, allow the web method to be set from the
// MessageContext.
if (msgContext.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS) {
String webMethod = msgContext.getStrProp(SOAP12Constants.PROP_WEBMETHOD);
if (webMethod != null) {
posting = webMethod.equals(HTTPConstants.HEADER_POST);
}
}
if (posting) {
Message reqMessage = msgContext.getRequestMessage();
method = new PostMethod(targetURL.toString());
// set false as default, addContetInfo can overwrite
method.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE,
false);
addContextInfo(method, httpClient, msgContext, targetURL);
MessageRequestEntity requestEntity = null;
if (msgContext.isPropertyTrue(HTTPConstants.MC_GZIP_REQUEST)) {
requestEntity = new GzipMessageRequestEntity(method, reqMessage, httpChunkStream);
} else {
requestEntity = new MessageRequestEntity(method, reqMessage, httpChunkStream);
}
((PostMethod)method).setRequestEntity(requestEntity);
} else {
method = new GetMethod(targetURL.toString());
addContextInfo(method, httpClient, msgContext, targetURL);
}
String httpVersion =
msgContext.getStrProp(MessageContext.HTTP_TRANSPORT_VERSION);
if (httpVersion != null) {
if (httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_V10)) {
method.getParams().setVersion(HttpVersion.HTTP_1_0);
}
// assume 1.1
}
// don't forget the cookies!
// Cookies need to be set on HttpState, since HttpMethodBase
// overwrites the cookies from HttpState
if (msgContext.getMaintainSession()) {
HttpState state = httpClient.getState();
method.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
String host = hostConfiguration.getHost();
String path = targetURL.getPath();
boolean secure = hostConfiguration.getProtocol().isSecure();
fillHeaders(msgContext, state, HTTPConstants.HEADER_COOKIE, host, path, secure);
fillHeaders(msgContext, state, HTTPConstants.HEADER_COOKIE2, host, path, secure);
httpClient.setState(state);
}
int returnCode = httpClient.executeMethod(hostConfiguration, method, null);
String contentType =
getHeader(method, HTTPConstants.HEADER_CONTENT_TYPE);
String contentLocation =
getHeader(method, HTTPConstants.HEADER_CONTENT_LOCATION);
String contentLength =
getHeader(method, HTTPConstants.HEADER_CONTENT_LENGTH);
if ((returnCode > 199) && (returnCode < 300)) {
// 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.equals("text/html")
&& ((returnCode > 499) && (returnCode < 600))) {
// SOAP Fault should be in here - so fall through
} else {
String statusMessage = method.getStatusText();
AxisFault fault = new AxisFault("HTTP",
"(" + returnCode + ")"
+ statusMessage, null,
null);
try {
fault.setFaultDetailString(
Messages.getMessage("return01",
"" + returnCode,
method.getResponseBodyAsString()));
fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_HTTPERRORCODE,
Integer.toString(returnCode));
throw fault;
} finally {
method.releaseConnection(); // release connection back to pool.
}
}
// wrap the response body stream so that close() also releases
// the connection back to the pool.
InputStream releaseConnectionOnCloseStream =
createConnectionReleasingInputStream(method);
Header contentEncoding =
method.getResponseHeader(HTTPConstants.HEADER_CONTENT_ENCODING);
if (contentEncoding != null) {
if (contentEncoding.getValue().
equalsIgnoreCase(HTTPConstants.COMPRESSION_GZIP)) {
releaseConnectionOnCloseStream =
new GZIPInputStream(releaseConnectionOnCloseStream);
} else {
AxisFault fault = new AxisFault("HTTP",
"unsupported content-encoding of '"
+ contentEncoding.getValue()
+ "' found", null, null);
throw fault;
}
}
Message outMsg = new Message(releaseConnectionOnCloseStream,
false, contentType, contentLocation);
// Transfer HTTP headers of HTTP message to MIME headers of SOAP message
Header[] responseHeaders = method.getResponseHeaders();
MimeHeaders responseMimeHeaders = outMsg.getMimeHeaders();
for (int i = 0; i < responseHeaders.length; i++) {
Header responseHeader = responseHeaders[i];
responseMimeHeaders.addHeader(responseHeader.getName(),
responseHeader.getValue());
}
outMsg.setMessageType(Message.RESPONSE);
msgContext.setResponseMessage(outMsg);
if (log.isDebugEnabled()) {
if (null == contentLength) {
log.debug("\n"
+ Messages.getMessage("no00", "Content-Length"));
}
log.debug("\n" + Messages.getMessage("xmlRecd00"));
log.debug("-----------------------------------------------");
log.debug(outMsg.getSOAPPartAsString());
}
// if we are maintaining session state,
// handle cookies (if any)
if (msgContext.getMaintainSession()) {
Header[] headers = method.getResponseHeaders();
for (int i = 0; i < headers.length; i++) {
if (headers[i].getName().equalsIgnoreCase(HTTPConstants.HEADER_SET_COOKIE)) {
handleCookie(HTTPConstants.HEADER_COOKIE, headers[i].getValue(), msgContext);
} else if (headers[i].getName().equalsIgnoreCase(HTTPConstants.HEADER_SET_COOKIE2)) {
handleCookie(HTTPConstants.HEADER_COOKIE2, headers[i].getValue(), msgContext);
}
}
}
// always release the connection back to the pool if
// it was one way invocation
if (msgContext.isPropertyTrue("axis.one.way")) {
method.releaseConnection();
}
} catch (Exception e) {
log.debug(e);
throw AxisFault.makeFault(e);
}
if (log.isDebugEnabled()) {
log.debug(Messages.getMessage("exit00",
"CommonsHTTPSender::invoke"));
}
}