in bindings/servicemix-http/src/main/java/org/apache/servicemix/http/processors/ProviderProcessor.java [85:193]
public void process(MessageExchange exchange) throws Exception {
if (exchange.getStatus() == ExchangeStatus.DONE || exchange.getStatus() == ExchangeStatus.ERROR) {
PostMethod method = methods.remove(exchange.getExchangeId());
if (method != null) {
method.releaseConnection();
}
return;
}
boolean txSync = exchange.isTransacted() && Boolean.TRUE.equals(exchange.getProperty(JbiConstants.SEND_SYNC));
txSync |= endpoint.isSynchronous();
NormalizedMessage nm = exchange.getMessage("in");
if (nm == null) {
throw new IllegalStateException("Exchange has no input message");
}
String locationURI = endpoint.getLocationURI();
// Incorporated because of JIRA SM-695
Object newDestinationURI = nm.getProperty(JbiConstants.HTTP_DESTINATION_URI);
if (newDestinationURI != null) {
locationURI = (String) newDestinationURI;
logger.debug("Location URI overridden: {}", locationURI);
}
PostMethod method = new PostMethod(getRelUri(locationURI));
SoapMessage soapMessage = new SoapMessage();
soapHelper.getJBIMarshaler().fromNMS(soapMessage, nm);
Context context = soapHelper.createContext(soapMessage);
soapHelper.onSend(context);
SoapWriter writer = soapHelper.getSoapMarshaler().createWriter(soapMessage);
copyHeaderInformation(nm, method);
RequestEntity entity = writeMessage(writer);
// remove content-type header that may have been part of the in message
if (!endpoint.isWantContentTypeHeaderFromExchangeIntoHttpRequest()) {
method.removeRequestHeader(HEADER_CONTENT_TYPE);
method.addRequestHeader(HEADER_CONTENT_TYPE, entity.getContentType());
}
if (entity.getContentLength() < 0) {
method.removeRequestHeader(HEADER_CONTENT_LENGTH);
} else {
method.setRequestHeader(HEADER_CONTENT_LENGTH, Long.toString(entity.getContentLength()));
}
if (endpoint.isSoap() && method.getRequestHeader(HEADER_SOAP_ACTION) == null) {
if (endpoint.getSoapAction() != null) {
method.setRequestHeader(HEADER_SOAP_ACTION, endpoint.getSoapAction());
} else {
method.setRequestHeader(HEADER_SOAP_ACTION, "\"\"");
}
}
method.setRequestEntity(entity);
boolean close = true;
try {
// Set the retry handler
int retries = getConfiguration().isStreamingEnabled() ? 0 : getConfiguration().getRetryCount();
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(retries, true));
// Set authentication
HostConfiguration hostConfig = getHostConfiguration(locationURI, exchange, nm);
if (endpoint.getBasicAuthentication() != null) {
if (getConfiguration().isUseHostPortForAuthScope()) {
endpoint.getBasicAuthentication().applyCredentials(getClient(), exchange, nm, hostConfig);
} else {
endpoint.getBasicAuthentication().applyCredentials(getClient(), exchange, nm, null);
}
}
// Execute the HTTP method
int response = getClient().executeMethod(getHostConfiguration(locationURI, exchange, nm), method);
Header contentType = method.getResponseHeader(HEADER_CONTENT_TYPE);
// if true check if xml is contained (it is actually not enough) and if not it will throw an exception
// with the complete response is logged.
if (endpoint.isResponseContentTypeCheck() && !contentType.toExternalForm().contains("xml")) {
throw new Exception(method.getResponseBodyAsString());
}
if (response != HttpStatus.SC_OK && response != HttpStatus.SC_ACCEPTED) {
if (!(exchange instanceof InOnly)) {
SoapReader reader = soapHelper.getSoapMarshaler().createReader();
soapMessage = reader.read(method.getResponseBodyAsStream(),
contentType != null ? contentType.getValue() : null);
context.setFaultMessage(soapMessage);
soapHelper.onAnswer(context);
Fault fault = exchange.createFault();
fault.setProperty(JbiConstants.PROTOCOL_HEADERS, getHeaders(method));
soapHelper.getJBIMarshaler().toNMS(fault, soapMessage);
exchange.setFault(fault);
if (txSync) {
channel.sendSync(exchange);
} else {
methods.put(exchange.getExchangeId(), method);
channel.send(exchange);
close = false;
}
return;
} else {
throw new Exception("Invalid status response: " + response);
}
}
if (exchange instanceof InOut) {
close = processInOut(exchange, method, context, txSync, close);
} else if (exchange instanceof InOptionalOut) {
close = processInOptionalOut(method, exchange, context, txSync, close);
} else {
exchange.setStatus(ExchangeStatus.DONE);
channel.send(exchange);
}
} finally {
if (close) {
method.releaseConnection();
}
}
}