public HttpRequest getRequest()

in modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/Axis2HttpRequest.java [168:272]


    public HttpRequest getRequest() throws IOException {

        String httpMethod = (String) msgContext.getProperty(Constants.Configuration.HTTP_METHOD);
        if (httpMethod == null) {
            httpMethod = "POST";
        }
        endpointURLPrefix = (String) msgContext.getProperty(NhttpConstants.ENDPOINT_PREFIX);
        HttpRequest httpRequest;

        if ("POST".equals(httpMethod) || "PUT".equals(httpMethod)) {

            URL url = new URL(epr.getAddress());
            httpRequest = new BasicHttpEntityEnclosingRequest(
                    httpMethod,
                    msgContext.isPropertyTrue(NhttpConstants.POST_TO_URI) ?
                            epr.getAddress() : url.getPath()
                            + (url.getQuery() != null ? "?" + url.getQuery() : ""),
                    msgContext.isPropertyTrue(NhttpConstants.FORCE_HTTP_1_0) ?
                            HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1);

            BasicHttpEntity entity = new BasicHttpEntity();

            if (msgContext.isPropertyTrue(NhttpConstants.FORCE_HTTP_1_0)) {
                setStreamAsTempData(entity);
            } else {
                entity.setChunked(chunked);
                if (!chunked) {
                    setStreamAsTempData(entity);
                }
            }
            ((BasicHttpEntityEnclosingRequest) httpRequest).setEntity(entity);

            httpRequest.setHeader(
                    HTTP.CONTENT_TYPE,
                    messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction()));

        } else if ("GET".equals(httpMethod) || "DELETE".equals(httpMethod)) {

            URL reqURI = messageFormatter.getTargetAddress(
                    msgContext, format, new URL(epr.getAddress()));
            String path = (msgContext.isPropertyTrue(NhttpConstants.POST_TO_URI) ?
                    reqURI.toString() : reqURI.getPath() +
                    (reqURI.getQuery() != null ? "?" + reqURI.getQuery() : ""));

            httpRequest = new BasicHttpRequest(httpMethod, path,
                    msgContext.isPropertyTrue(NhttpConstants.FORCE_HTTP_1_0) ?
                            HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1);

            httpRequest.setHeader(HTTP.CONTENT_TYPE, messageFormatter.getContentType(
                    msgContext, format, msgContext.getSoapAction()));

        } else {
            URL reqURI = new URL(epr.getAddress());
            httpRequest = new BasicHttpRequest(
                    httpMethod,
                    msgContext.isPropertyTrue(NhttpConstants.POST_TO_URI) ?
                            epr.getAddress() : reqURI.getPath()
                            + (reqURI.getQuery() != null ? "?" + reqURI.getQuery() : ""),
                    msgContext.isPropertyTrue(NhttpConstants.FORCE_HTTP_1_0) ?
                            HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1);
        }

        // set any transport headers
        Object o = msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
        if (o != null && o instanceof Map) {
            Map headers = (Map) o;
            for (Object header : headers.keySet()) {
                Object value = headers.get(header);
                if (header instanceof String && value != null && value instanceof String) {
                    if (!HTTPConstants.HEADER_HOST.equalsIgnoreCase((String) header)) {
                        httpRequest.setHeader((String) header, (String) value);
                    }
                }
            }
        }

        // if the message is SOAP 11 (for which a SOAPAction is *required*), and
        // the msg context has a SOAPAction or a WSA-Action (give pref to SOAPAction)
        // use that over any transport header that may be available
        String soapAction = msgContext.getSoapAction();
        if (soapAction == null) {
            soapAction = msgContext.getWSAAction();
        }
        if (soapAction == null) {
            msgContext.getAxisOperation().getInputAction();
        }

        if (msgContext.isSOAP11() && soapAction != null &&
                soapAction.length() >= 0) {
            Header existingHeader =
                    httpRequest.getFirstHeader(HTTPConstants.HEADER_SOAP_ACTION);
            if (existingHeader != null) {
                httpRequest.removeHeader(existingHeader);
            }
            httpRequest.setHeader(HTTPConstants.HEADER_SOAP_ACTION,
                    messageFormatter.formatSOAPAction(msgContext, null, soapAction));
        }

        if (NHttpConfiguration.getInstance().isKeepAliveDisabled() ||
                msgContext.isPropertyTrue(NhttpConstants.NO_KEEPALIVE)) {
            httpRequest.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
        }

        return httpRequest;
    }