PostMethod method = new PostMethod(relUri);
String locationURI = endpoint.getLocationURI();   Object newDestinationURI = nm.getProperty(JbiConstants.HTTP_DESTINATION_URI); if (newDestinationURI != null) { locationURI = (String) newDestinationURI; log.debug("Location URI overridden: " + locationURI); }  PostMethod method = new PostMethod(getRelUri(locationURI));
    public void process(MessageExchange exchange) throws Exception {        if (exchange.getStatus() == ExchangeStatus.DONE ||             exchange.getStatus() == ExchangeStatus.ERROR) {            PostMethod method = (PostMethod) methods.remove(exchange.getExchangeId());            if (method != null) {                method.releaseConnection();            }            return;        }        boolean txSync = exchange.isTransacted() && Boolean.TRUE.equals(exchange.getProperty(JbiConstants.SEND_SYNC));        NormalizedMessage nm = exchange.getMessage("in");        if (nm == null) {            throw new IllegalStateException("Exchange has no input message");        }        PostMethod method = new PostMethod(relUri);        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 {            // Uncomment to avoid the http request being sent several times.            // Can be useful when debugging            //================================            //method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {            //    public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {            //        return false;            //    }            //});            if (endpoint.getBasicAuthentication() != null) {                endpoint.getBasicAuthentication().applyCredentials( getClient() );            }            int response = getClient().executeMethod(host, 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);                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);                    msg.setProperty(JbiConstants.PROTOCOL_HEADERS, getHeaders(method));                    soapHelper.getJBIMarshaler().toNMS(msg, soapMessage);                    ((InOptionalOut) exchange).setOutMessage(msg);                    if (txSync) {                        channel.sendSync(exchange);                    } else {                        methods.put(exchange.getExchangeId(), method);                        channel.send(exchange);                        close = false;                    }                }            } else {                exchange.setStatus(ExchangeStatus.DONE);                channel.send(exchange);            }        } finally {            if (close) {                method.releaseConnection();            }        }    }