Example usage for org.apache.commons.httpclient HttpConnection HttpConnection

List of usage examples for org.apache.commons.httpclient HttpConnection HttpConnection

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpConnection HttpConnection.

Prototype

public HttpConnection(String proxyHost, int proxyPort, String host, int port) 

Source Link

Document

Creates a new HTTP connection for the given host and port via the given proxy host and port using the default protocol.

Usage

From source file:org.apache.cocoon.components.language.markup.xsp.SOAPHelper.java

public XScriptObject invoke() throws ProcessingException {
    HttpConnection conn = null;//from  w w w.j  av a  2 s  .co  m

    try {
        if (action == null || action.length() == 0) {
            action = "\"\"";
        }

        String host = url.getHost();
        int port = url.getPort();

        if (System.getProperty("http.proxyHost") != null) {
            String proxyHost = System.getProperty("http.proxyHost");
            int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort"));
            conn = new HttpConnection(proxyHost, proxyPort, host, port);
        } else {
            conn = new HttpConnection(host, port);
        }

        PostMethod method = new PostMethod(url.getFile());
        String request;

        try {
            // Write the SOAP request body
            if (xscriptObject instanceof XScriptObjectInlineXML) {
                // Skip overhead
                request = ((XScriptObjectInlineXML) xscriptObject).getContent();
            } else {
                StringBuffer bodyBuffer = new StringBuffer();
                InputSource saxSource = xscriptObject.getInputSource();

                Reader r = null;
                // Byte stream or character stream?
                if (saxSource.getByteStream() != null) {
                    r = new InputStreamReader(saxSource.getByteStream());
                } else {
                    r = saxSource.getCharacterStream();
                }

                try {
                    char[] buffer = new char[1024];
                    int len;
                    while ((len = r.read(buffer)) > 0)
                        bodyBuffer.append(buffer, 0, len);
                } finally {
                    if (r != null) {
                        r.close();
                    }
                }

                request = bodyBuffer.toString();
            }

        } catch (Exception ex) {
            throw new ProcessingException("Error assembling request", ex);
        }

        method.setRequestHeader(new Header("Content-type", "text/xml; charset=\"utf-8\""));
        method.setRequestHeader(new Header("SOAPAction", action));
        method.setRequestBody(request);

        if (authorization != null && !authorization.equals("")) {
            method.setRequestHeader(
                    new Header("Authorization", "Basic " + SourceUtil.encodeBASE64(authorization)));
        }

        method.execute(new HttpState(), conn);

        String ret = method.getResponseBodyAsString();
        int startOfXML = ret.indexOf("<?xml");
        if (startOfXML == -1) { // No xml?!
            throw new ProcessingException("Invalid response - no xml");
        }

        return new XScriptObjectInlineXML(xscriptManager, ret.substring(startOfXML));
    } catch (Exception ex) {
        throw new ProcessingException("Error invoking remote service: " + ex, ex);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception ex) {
        }
    }
}