}
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);
Map headers = (Map) nm.getProperty(JbiConstants.PROTOCOL_HEADERS);
if (headers != null) {
for (Iterator it = headers.keySet().iterator(); it.hasNext();) {
String name = (String) it.next();
String value = (String) headers.get(name);
method.addRequestHeader(name, value);
}
}
RequestEntity entity = writeMessage(writer);
// remove content-type header that may have been part of the in message
method.removeRequestHeader(Constants.HEADER_CONTENT_TYPE);
method.addRequestHeader(Constants.HEADER_CONTENT_TYPE, entity.getContentType());
if (entity.getContentLength() < 0) {
method.removeRequestHeader(Constants.HEADER_CONTENT_LENGTH);
} else {
method.setRequestHeader(Constants.HEADER_CONTENT_LENGTH, Long.toString(entity.getContentLength()));
}
if (endpoint.isSoap() && method.getRequestHeader(Constants.HEADER_SOAP_ACTION) == null) {
if (endpoint.getSoapAction() != null) {
method.setRequestHeader(Constants.HEADER_SOAP_ACTION, endpoint.getSoapAction());
} else {
method.setRequestHeader(Constants.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
if (endpoint.getBasicAuthentication() != null) {
endpoint.getBasicAuthentication().applyCredentials( getClient() );
}
// Execute the HTTP method
int response = getClient().executeMethod(getHostConfiguration(locationURI), method);
if (response != HttpStatus.SC_OK && response != HttpStatus.SC_ACCEPTED) {
if (exchange instanceof InOnly == false) {
SoapReader reader = soapHelper.getSoapMarshaler().createReader();
Header contentType = method.getResponseHeader(Constants.HEADER_CONTENT_TYPE);
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) {
NormalizedMessage msg = exchange.createMessage();
SoapReader reader = soapHelper.getSoapMarshaler().createReader();
Header contentType = method.getResponseHeader(Constants.HEADER_CONTENT_TYPE);
soapMessage = reader.read(method.getResponseBodyAsStream(),
contentType != null ? contentType.getValue() : null);
context.setOutMessage(soapMessage);
soapHelper.onAnswer(context);
if (getConfiguration().isWantHeadersFromHttpIntoExchange()) {
msg.setProperty(JbiConstants.PROTOCOL_HEADERS, getHeaders(method));
}
soapHelper.getJBIMarshaler().toNMS(msg, soapMessage);
((InOut) exchange).setOutMessage(msg);
if (txSync) {
channel.sendSync(exchange);
} else {
methods.put(exchange.getExchangeId(), method);
channel.send(exchange);
close = false;
}
} else if (exchange instanceof InOptionalOut) {
if (method.getResponseContentLength() == 0) {
exchange.setStatus(ExchangeStatus.DONE);
channel.send(exchange);
} else {
NormalizedMessage msg = exchange.createMessage();
SoapReader reader = soapHelper.getSoapMarshaler().createReader();
soapMessage = reader.read(method.getResponseBodyAsStream(),
method.getResponseHeader(Constants.HEADER_CONTENT_TYPE).getValue());
context.setOutMessage(soapMessage);
soapHelper.onAnswer(context);
if (getConfiguration().isWantHeadersFromHttpIntoExchange()) {
msg.setProperty(JbiConstants.PROTOCOL_HEADERS, getHeaders(method));
}
soapHelper.getJBIMarshaler().toNMS(msg, soapMessage);