Example usage for org.apache.commons.httpclient.methods InputStreamRequestEntity InputStreamRequestEntity

List of usage examples for org.apache.commons.httpclient.methods InputStreamRequestEntity InputStreamRequestEntity

Introduction

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

Prototype

public InputStreamRequestEntity(InputStream paramInputStream, String paramString) 

Source Link

Usage

From source file:org.geoserver.wps.Execute.java

/**
 * Executes/* ww  w. j av a 2s  .co  m*/
 * 
 * @param ref
 * @return
 */
Object executeRemoteRequest(InputReferenceType ref, ComplexPPIO ppio, String inputId) throws Exception {
    URL destination = new URL(ref.getHref());

    HttpMethod method = null;
    GetMethod refMethod = null;
    InputStream input = null;
    InputStream refInput = null;

    // execute the request
    try {
        if ("http".equalsIgnoreCase(destination.getProtocol())) {
            // setup the client
            HttpClient client = new HttpClient();
            // setting timeouts (30 seconds, TODO: make this configurable)
            HttpConnectionManagerParams params = new HttpConnectionManagerParams();
            params.setSoTimeout(connectionTimeout);
            params.setConnectionTimeout(connectionTimeout);
            // TODO: make the http client a well behaved http client, no more than x connections
            // per server (x admin configurable maybe), persistent connections and so on
            HttpConnectionManager manager = new SimpleHttpConnectionManager();
            manager.setParams(params);
            client.setHttpConnectionManager(manager);

            // prepare either a GET or a POST request
            if (ref.getMethod() == null || ref.getMethod() == MethodType.GET_LITERAL) {
                GetMethod get = new GetMethod(ref.getHref());
                get.setFollowRedirects(true);
                method = get;
            } else {
                String encoding = ref.getEncoding();
                if (encoding == null) {
                    encoding = "UTF-8";
                }

                PostMethod post = new PostMethod(ref.getHref());
                Object body = ref.getBody();
                if (body == null) {
                    if (ref.getBodyReference() != null) {
                        URL refDestination = new URL(ref.getBodyReference().getHref());
                        if ("http".equalsIgnoreCase(refDestination.getProtocol())) {
                            // open with commons http client
                            refMethod = new GetMethod(ref.getBodyReference().getHref());
                            refMethod.setFollowRedirects(true);
                            client.executeMethod(refMethod);
                            refInput = refMethod.getResponseBodyAsStream();
                        } else {
                            // open with the built-in url management
                            URLConnection conn = refDestination.openConnection();
                            conn.setConnectTimeout(connectionTimeout);
                            conn.setReadTimeout(connectionTimeout);
                            refInput = conn.getInputStream();
                        }
                        post.setRequestEntity(new InputStreamRequestEntity(refInput, ppio.getMimeType()));
                    } else {
                        throw new WPSException("A POST request should contain a non empty body");
                    }
                } else if (body instanceof String) {
                    post.setRequestEntity(new StringRequestEntity((String) body, ppio.getMimeType(), encoding));
                } else {
                    throw new WPSException("The request body should be contained in a CDATA section, "
                            + "otherwise it will get parsed as XML instead of being preserved as is");

                }
                method = post;
            }
            // add eventual extra headers
            if (ref.getHeader() != null) {
                for (Iterator it = ref.getHeader().iterator(); it.hasNext();) {
                    HeaderType header = (HeaderType) it.next();
                    method.setRequestHeader(header.getKey(), header.getValue());
                }
            }
            int code = client.executeMethod(method);

            if (code == 200) {
                input = method.getResponseBodyAsStream();
            } else {
                throw new WPSException("Error getting remote resources from " + ref.getHref() + ", http error "
                        + code + ": " + method.getStatusText());
            }
        } else {
            // use the normal url connection methods then...
            URLConnection conn = destination.openConnection();
            conn.setConnectTimeout(connectionTimeout);
            conn.setReadTimeout(connectionTimeout);
            input = conn.getInputStream();
        }

        // actually parse teh data
        if (input != null) {
            return ppio.decode(input);
        } else {
            throw new WPSException("Could not find a mean to read input " + inputId);
        }
    } finally {
        // make sure to close the connection and streams no matter what
        if (input != null) {
            input.close();
        }
        if (method != null) {
            method.releaseConnection();
        }
        if (refMethod != null) {
            refMethod.releaseConnection();
        }
    }
}

From source file:org.geoserver.wps.executor.RemoteRequestInputProvider.java

@Override
protected Object getValueInternal(ProgressListener listener) throws Exception {
    InputReferenceType ref = input.getReference();
    URL destination = new URL(ref.getHref());

    HttpMethod method = null;/*from   w w w .j a v a  2s .c  om*/
    GetMethod refMethod = null;
    InputStream input = null;
    InputStream refInput = null;

    // execute the request
    listener.started();
    try {
        if ("file".equalsIgnoreCase(destination.getProtocol())) {
            File file = DataUtilities.urlToFile(destination);
            if (maxSize > 0 && maxSize < file.length()) {
                throw new WPSException("Input " + getInputId() + " size " + file.length()
                        + " exceeds maximum allowed size of " + maxSize, "NoApplicableCode", getInputId());
            }

            input = new FileInputStream(file);
        } else if ("http".equalsIgnoreCase(destination.getProtocol())) {
            // setup the client
            HttpClient client = new HttpClient();
            // setting timeouts (30 seconds, TODO: make this configurable)
            HttpConnectionManagerParams params = new HttpConnectionManagerParams();
            params.setSoTimeout(timeout);
            params.setConnectionTimeout(timeout);
            // TODO: make the http client a well behaved http client, no more than x connections
            // per server (x admin configurable maybe), persistent connections and so on
            HttpConnectionManager manager = new SimpleHttpConnectionManager();
            manager.setParams(params);
            client.setHttpConnectionManager(manager);

            // prepare either a GET or a POST request
            if (ref.getMethod() == null || ref.getMethod() == MethodType.GET_LITERAL) {
                GetMethod get = new GetMethod(ref.getHref());
                get.setFollowRedirects(true);
                method = get;
            } else {
                String encoding = ref.getEncoding();
                if (encoding == null) {
                    encoding = "UTF-8";
                }

                PostMethod post = new PostMethod(ref.getHref());
                Object body = ref.getBody();
                if (body == null) {
                    if (ref.getBodyReference() != null) {
                        URL refDestination = new URL(ref.getBodyReference().getHref());
                        if ("http".equalsIgnoreCase(refDestination.getProtocol())) {
                            // open with commons http client
                            refMethod = new GetMethod(ref.getBodyReference().getHref());
                            refMethod.setFollowRedirects(true);
                            client.executeMethod(refMethod);
                            refInput = refMethod.getResponseBodyAsStream();
                        } else {
                            // open with the built-in url management
                            URLConnection conn = refDestination.openConnection();
                            conn.setConnectTimeout(timeout);
                            conn.setReadTimeout(timeout);
                            refInput = conn.getInputStream();
                        }
                        post.setRequestEntity(
                                new InputStreamRequestEntity(refInput, complexPPIO.getMimeType()));
                    } else {
                        throw new WPSException("A POST request should contain a non empty body");
                    }
                } else if (body instanceof String) {
                    post.setRequestEntity(
                            new StringRequestEntity((String) body, complexPPIO.getMimeType(), encoding));
                } else {
                    throw new WPSException("The request body should be contained in a CDATA section, "
                            + "otherwise it will get parsed as XML instead of being preserved as is");

                }
                method = post;
            }
            // add eventual extra headers
            if (ref.getHeader() != null) {
                for (Iterator it = ref.getHeader().iterator(); it.hasNext();) {
                    HeaderType header = (HeaderType) it.next();
                    method.setRequestHeader(header.getKey(), header.getValue());
                }
            }
            int code = client.executeMethod(method);

            if (code == 200) {
                try {
                    Header length = method.getResponseHeader("Content-Lenght");
                    if (maxSize > 0 && length != null && Long.parseLong(length.getValue()) > maxSize) {
                        throw new WPSException(
                                "Input " + getInputId() + " size " + length.getValue()
                                        + " exceeds maximum allowed size of " + maxSize
                                        + " according to HTTP Content-Lenght response header",
                                "NoApplicableCode", getInputId());
                    }
                } catch (NumberFormatException e) {
                    LOGGER.log(Level.FINE, "Failed to parse content lenght to check input limits respect, "
                            + "moving on and checking data as it comes in", e);
                }
                input = method.getResponseBodyAsStream();
                if (maxSize > 0) {
                    input = new MaxSizeInputStream(input, getInputId(), maxSize);
                }
            } else {
                throw new WPSException("Error getting remote resources from " + ref.getHref() + ", http error "
                        + code + ": " + method.getStatusText());
            }
        } else {
            // use the normal url connection methods then...
            URLConnection conn = destination.openConnection();
            conn.setConnectTimeout(timeout);
            conn.setReadTimeout(timeout);
            input = conn.getInputStream();

            if (maxSize > 0) {
                input = new MaxSizeInputStream(input, getInputId(), maxSize);
            }
        }

        // actually parse the data
        if (input != null) {
            CancellingInputStream is = new CancellingInputStream(input, listener);
            return complexPPIO.decode(is);
        } else {
            throw new WPSException("Could not find a mean to read input " + inputId);
        }
    } finally {
        listener.progress(100);
        listener.complete();
        // make sure to close the connection and streams no matter what
        if (refInput != null) {
            refInput.close();
        }
        if (input != null) {
            input.close();
        }
        if (method != null) {
            method.releaseConnection();
        }
        if (refMethod != null) {
            refMethod.releaseConnection();
        }
    }
}

From source file:org.geoserver.wps.executor.SimpleInputProvider.java

/**
 * Executes/*  ww w .j a  va 2  s . c o  m*/
 * 
 * @param ref
 * @return
 */
Object executeRemoteRequest(InputReferenceType ref, ComplexPPIO ppio, String inputId) throws Exception {
    URL destination = new URL(ref.getHref());

    HttpMethod method = null;
    GetMethod refMethod = null;
    InputStream input = null;
    InputStream refInput = null;

    // execute the request
    try {
        if ("http".equalsIgnoreCase(destination.getProtocol())) {
            // setup the client
            HttpClient client = new HttpClient();
            // setting timeouts (30 seconds, TODO: make this configurable)
            HttpConnectionManagerParams params = new HttpConnectionManagerParams();
            params.setSoTimeout(executor.getConnectionTimeout());
            params.setConnectionTimeout(executor.getConnectionTimeout());
            // TODO: make the http client a well behaved http client, no more than x connections
            // per server (x admin configurable maybe), persistent connections and so on
            HttpConnectionManager manager = new SimpleHttpConnectionManager();
            manager.setParams(params);
            client.setHttpConnectionManager(manager);

            // prepare either a GET or a POST request
            if (ref.getMethod() == null || ref.getMethod() == MethodType.GET_LITERAL) {
                GetMethod get = new GetMethod(ref.getHref());
                get.setFollowRedirects(true);
                method = get;
            } else {
                String encoding = ref.getEncoding();
                if (encoding == null) {
                    encoding = "UTF-8";
                }

                PostMethod post = new PostMethod(ref.getHref());
                Object body = ref.getBody();
                if (body == null) {
                    if (ref.getBodyReference() != null) {
                        URL refDestination = new URL(ref.getBodyReference().getHref());
                        if ("http".equalsIgnoreCase(refDestination.getProtocol())) {
                            // open with commons http client
                            refMethod = new GetMethod(ref.getBodyReference().getHref());
                            refMethod.setFollowRedirects(true);
                            client.executeMethod(refMethod);
                            refInput = refMethod.getResponseBodyAsStream();
                        } else {
                            // open with the built-in url management
                            URLConnection conn = refDestination.openConnection();
                            conn.setConnectTimeout(executor.getConnectionTimeout());
                            conn.setReadTimeout(executor.getConnectionTimeout());
                            refInput = conn.getInputStream();
                        }
                        post.setRequestEntity(new InputStreamRequestEntity(refInput, ppio.getMimeType()));
                    } else {
                        throw new WPSException("A POST request should contain a non empty body");
                    }
                } else if (body instanceof String) {
                    post.setRequestEntity(new StringRequestEntity((String) body, ppio.getMimeType(), encoding));
                } else {
                    throw new WPSException("The request body should be contained in a CDATA section, "
                            + "otherwise it will get parsed as XML instead of being preserved as is");

                }
                method = post;
            }
            // add eventual extra headers
            if (ref.getHeader() != null) {
                for (Iterator it = ref.getHeader().iterator(); it.hasNext();) {
                    HeaderType header = (HeaderType) it.next();
                    method.setRequestHeader(header.getKey(), header.getValue());
                }
            }
            int code = client.executeMethod(method);

            if (code == 200) {
                input = method.getResponseBodyAsStream();
            } else {
                throw new WPSException("Error getting remote resources from " + ref.getHref() + ", http error "
                        + code + ": " + method.getStatusText());
            }
        } else {
            // use the normal url connection methods then...
            URLConnection conn = destination.openConnection();
            conn.setConnectTimeout(executor.getConnectionTimeout());
            conn.setReadTimeout(executor.getConnectionTimeout());
            input = conn.getInputStream();
        }

        // actually parse teh data
        if (input != null) {
            return ppio.decode(input);
        } else {
            throw new WPSException("Could not find a mean to read input " + inputId);
        }
    } finally {
        // make sure to close the connection and streams no matter what
        if (input != null) {
            input.close();
        }
        if (method != null) {
            method.releaseConnection();
        }
        if (refMethod != null) {
            refMethod.releaseConnection();
        }
    }
}

From source file:org.helio.taverna.helio_taverna_suite.common.UWSCaller.java

/**
 * @param filename//ww w  .j av  a  2s  . co  m
 * @param strURL
 * @param append
 * @return
 * @throws Exception
 */
private static String getDetails(File input, String strURL, String append) throws Exception {
    // Prepare HTTP post
    PostMethod post = new PostMethod(strURL);
    // Request content will be retrieved directly
    // from the input stream
    // Per default, the request content needs to be buffered
    // in order to determine its length.
    // Request body buffering can be avoided when
    // content length is explicitly specified

    post.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(input), input.length()));
    post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");

    post.setFollowRedirects(false);
    // Get HTTP client
    HttpClient httpclient = new HttpClient();

    try {
        //httpclient.
        int result = httpclient.executeMethod(post);
        int statuscode = post.getStatusCode();

        if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                || (statuscode == HttpStatus.SC_SEE_OTHER)
                || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {

            Header header = post.getResponseHeader("location");
            //System.out.println("   :  Header  :  "+header);
            if (header != null) {
                String newuri = header.getValue();
                if ((newuri == null) || (newuri.equals("")))
                    newuri = "/";
                return newuri;
            } else {
                return null;
            }
        } else {
            //System.out.println(post.getResponseBodyAsString());
        }
    } finally {
        // Release current connection to the connection pool 
        // once you are done
        post.releaseConnection();
    }
    return null;
}

From source file:org.mule.transport.http.transformers.ObjectToHttpClientMethodRequest.java

protected void setupEntityMethod(Object src, String encoding, MuleMessage msg, EntityEnclosingMethod postMethod)
        throws UnsupportedEncodingException, TransformerException {
    // Dont set a POST payload if the body is a Null Payload.
    // This way client calls can control if a POST body is posted explicitly
    if (!(msg.getPayload() instanceof NullPayload)) {
        String outboundMimeType = (String) msg.getProperty(HttpConstants.HEADER_CONTENT_TYPE,
                PropertyScope.OUTBOUND);
        if (outboundMimeType == null) {
            outboundMimeType = (getEndpoint() != null ? getEndpoint().getMimeType() : null);
        }/*from   w  w  w .  j a  va2 s .  co  m*/
        if (outboundMimeType == null) {
            outboundMimeType = HttpConstants.DEFAULT_CONTENT_TYPE;
            logger.info("Content-Type not set on outgoing request, defaulting to: " + outboundMimeType);
        }

        if (encoding != null && !"UTF-8".equals(encoding.toUpperCase())
                && outboundMimeType.indexOf("charset") == -1) {
            outboundMimeType += "; charset=" + encoding;
        }

        // Ensure that we have a cached representation of the message if we're
        // using HTTP 1.0
        final String httpVersion = msg.getOutboundProperty(HttpConnector.HTTP_VERSION_PROPERTY,
                HttpConstants.HTTP11);
        if (HttpConstants.HTTP10.equals(httpVersion)) {
            try {
                src = msg.getPayloadAsBytes();
            } catch (final Exception e) {
                throw new TransformerException(this, e);
            }
        }

        if (msg.getOutboundAttachmentNames() != null && msg.getOutboundAttachmentNames().size() > 0) {
            try {
                postMethod.setRequestEntity(createMultiPart(msg, postMethod));
                return;
            } catch (final Exception e) {
                throw new TransformerException(this, e);
            }
        }
        if (src instanceof String) {
            postMethod.setRequestEntity(new StringRequestEntity(src.toString(), outboundMimeType, encoding));
            return;
        }

        if (src instanceof InputStream) {
            postMethod.setRequestEntity(new InputStreamRequestEntity((InputStream) src, outboundMimeType));
        } else if (src instanceof byte[]) {
            postMethod.setRequestEntity(new ByteArrayRequestEntity((byte[]) src, outboundMimeType));
        } else if (src instanceof OutputHandler) {
            final MuleEvent event = RequestContext.getEvent();
            postMethod.setRequestEntity(new StreamPayloadRequestEntity((OutputHandler) src, event));
        } else {
            final byte[] buffer = SerializationUtils.serialize((Serializable) src);
            postMethod.setRequestEntity(new ByteArrayRequestEntity(buffer, outboundMimeType));
        }
    } else if (msg.getOutboundAttachmentNames() != null && msg.getOutboundAttachmentNames().size() > 0) {
        try {
            postMethod.setRequestEntity(createMultiPart(msg, postMethod));
        } catch (Exception e) {
            throw new TransformerException(this, e);
        }
    }
}

From source file:org.openehealth.ipf.commons.test.http.client.Client.java

public void execute(InputStream input) throws Exception {
    PostMethod method = new PostMethod(serverUrl.toString());

    method.setRequestEntity(new InputStreamRequestEntity(input, contentType));
    client.executeMethod(method);/*from w ww  .  jav  a2s. com*/

    try {
        InputStream responseStream = method.getResponseBodyAsStream();
        handler.handleResponse(responseStream);
        IOUtils.closeQuietly(responseStream);
    } finally {
        method.releaseConnection();
    }

}

From source file:org.openhab.binding.nest.internal.messages.AbstractRequest.java

/**
 * Executes the given <code>url</code> with the given <code>httpMethod</code>. In the case of httpMethods that do
 * not support automatic redirection, manually handle the HTTP temporary redirect (307) and retry with the new URL.
 * // www  .j  av a2s . com
 * @param httpMethod
 *            the HTTP method to use
 * @param url
 *            the url to execute (in milliseconds)
 * @param contentString
 *            the content to be sent to the given <code>url</code> or <code>null</code> if no content should be
 *            sent.
 * @param contentType
 *            the content type of the given <code>contentString</code>
 * @return the response body or <code>NULL</code> when the request went wrong
 */
protected final String executeUrl(final String httpMethod, final String url, final String contentString,
        final String contentType) {

    HttpClient client = new HttpClient();

    HttpMethod method = HttpUtil.createHttpMethod(httpMethod, url);
    method.getParams().setSoTimeout(HTTP_REQUEST_TIMEOUT);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    for (String httpHeaderKey : HTTP_HEADERS.stringPropertyNames()) {
        method.addRequestHeader(new Header(httpHeaderKey, HTTP_HEADERS.getProperty(httpHeaderKey)));
    }

    // add content if a valid method is given ...
    if (method instanceof EntityEnclosingMethod && contentString != null) {
        EntityEnclosingMethod eeMethod = (EntityEnclosingMethod) method;
        InputStream content = new ByteArrayInputStream(contentString.getBytes());
        eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType));
    }

    if (logger.isDebugEnabled()) {
        try {
            logger.trace("About to execute '" + method.getURI().toString() + "'");
        } catch (URIException e) {
            logger.trace(e.getMessage());
        }
    }

    try {

        int statusCode = client.executeMethod(method);
        if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_ACCEPTED) {
            // perfectly fine but we cannot expect any answer...
            return null;
        }

        // Manually handle 307 redirects with a little tail recursion
        if (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
            Header[] headers = method.getResponseHeaders("Location");
            String newUrl = headers[headers.length - 1].getValue();
            return executeUrl(httpMethod, newUrl, contentString, contentType);
        }

        if (statusCode != HttpStatus.SC_OK) {
            logger.warn("Method failed: " + method.getStatusLine());
        }

        InputStream tmpResponseStream = method.getResponseBodyAsStream();
        Header encodingHeader = method.getResponseHeader("Content-Encoding");
        if (encodingHeader != null) {
            for (HeaderElement ehElem : encodingHeader.getElements()) {
                if (ehElem.toString().matches(".*gzip.*")) {
                    tmpResponseStream = new GZIPInputStream(tmpResponseStream);
                    logger.trace("GZipped InputStream from {}", url);
                } else if (ehElem.toString().matches(".*deflate.*")) {
                    tmpResponseStream = new InflaterInputStream(tmpResponseStream);
                    logger.trace("Deflated InputStream from {}", url);
                }
            }
        }

        String responseBody = IOUtils.toString(tmpResponseStream);
        if (!responseBody.isEmpty()) {
            logger.trace(responseBody);
        }

        return responseBody;
    } catch (HttpException he) {
        logger.error("Fatal protocol violation: {}", he.toString());
    } catch (IOException ioe) {
        logger.error("Fatal transport error: {}", ioe.toString());
    } finally {
        method.releaseConnection();
    }

    return null;
}

From source file:org.openhab.io.net.http.HttpUtil.java

/**
 * Executes the given <code>url</code> with the given <code>httpMethod</code>
 * /*w  w  w.  j a v a2 s  .c  om*/
 * @param httpMethod the HTTP method to use
 * @param url the url to execute (in milliseconds)
 * @param httpHeaders optional HTTP headers which has to be set on request
 * @param content the content to be send to the given <code>url</code> or 
 * <code>null</code> if no content should be send.
 * @param contentType the content type of the given <code>content</code>
 * @param timeout the socket timeout to wait for data
 * @param proxyHost the hostname of the proxy
 * @param proxyPort the port of the proxy
 * @param proxyUser the username to authenticate with the proxy
 * @param proxyPassword the password to authenticate with the proxy
 * @param nonProxyHosts the hosts that won't be routed through the proxy
 * @return the response body or <code>NULL</code> when the request went wrong
 */
public static String executeUrl(String httpMethod, String url, Properties httpHeaders, InputStream content,
        String contentType, int timeout, String proxyHost, Integer proxyPort, String proxyUser,
        String proxyPassword, String nonProxyHosts) {

    HttpClient client = new HttpClient();

    // only configure a proxy if a host is provided
    if (StringUtils.isNotBlank(proxyHost) && proxyPort != null && shouldUseProxy(url, nonProxyHosts)) {
        client.getHostConfiguration().setProxy(proxyHost, proxyPort);
        if (StringUtils.isNotBlank(proxyUser)) {
            client.getState().setProxyCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(proxyUser, proxyPassword));
        }
    }

    HttpMethod method = HttpUtil.createHttpMethod(httpMethod, url);
    method.getParams().setSoTimeout(timeout);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    if (httpHeaders != null) {
        for (String httpHeaderKey : httpHeaders.stringPropertyNames()) {
            method.addRequestHeader(new Header(httpHeaderKey, httpHeaders.getProperty(httpHeaderKey)));
        }
    }
    // add content if a valid method is given ...
    if (method instanceof EntityEnclosingMethod && content != null) {
        EntityEnclosingMethod eeMethod = (EntityEnclosingMethod) method;
        eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType));
    }

    Credentials credentials = extractCredentials(url);
    if (credentials != null) {
        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(AuthScope.ANY, credentials);
    }

    if (logger.isDebugEnabled()) {
        try {
            logger.debug("About to execute '" + method.getURI().toString() + "'");
        } catch (URIException e) {
            logger.debug(e.getMessage());
        }
    }

    try {

        int statusCode = client.executeMethod(method);
        if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_ACCEPTED) {
            // perfectly fine but we cannot expect any answer...
            return null;
        }

        if (statusCode != HttpStatus.SC_OK) {
            logger.warn("Method failed: " + method.getStatusLine());
        }

        InputStream tmpResponseStream = method.getResponseBodyAsStream();
        Header encodingHeader = method.getResponseHeader("Content-Encoding");
        if (encodingHeader != null) {
            for (HeaderElement ehElem : encodingHeader.getElements()) {
                if (ehElem.toString().matches(".*gzip.*")) {
                    tmpResponseStream = new GZIPInputStream(tmpResponseStream);
                    logger.debug("GZipped InputStream from {}", url);
                } else if (ehElem.toString().matches(".*deflate.*")) {
                    tmpResponseStream = new InflaterInputStream(tmpResponseStream);
                    logger.debug("Deflated InputStream from {}", url);
                }
            }
        }

        String responseBody = IOUtils.toString(tmpResponseStream);
        if (!responseBody.isEmpty()) {
            logger.debug(responseBody);
        }

        return responseBody;
    } catch (HttpException he) {
        logger.error("Fatal protocol violation: {}", he.toString());
    } catch (IOException ioe) {
        logger.error("Fatal transport error: {}", ioe.toString());
    } finally {
        method.releaseConnection();
    }

    return null;
}

From source file:org.pentaho.di.trans.steps.httppost.HTTPPOST.java

private Object[] callHTTPPOST(Object[] rowData) throws KettleException {
    // get dynamic url ?
    if (meta.isUrlInField()) {
        data.realUrl = data.inputRowMeta.getString(rowData, data.indexOfUrlField);
    }//w  ww .j a  v a 2  s  .  c o  m

    FileInputStream fis = null;
    try {
        if (isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "HTTPPOST.Log.ConnectingToURL", data.realUrl));
        }

        // Prepare HTTP POST
        //
        HttpClient HTTPPOSTclient = SlaveConnectionManager.getInstance().createHttpClient();
        PostMethod post = new PostMethod(data.realUrl);
        // post.setFollowRedirects(false);

        // Set timeout
        if (data.realConnectionTimeout > -1) {
            HTTPPOSTclient.getHttpConnectionManager().getParams()
                    .setConnectionTimeout(data.realConnectionTimeout);
        }
        if (data.realSocketTimeout > -1) {
            HTTPPOSTclient.getHttpConnectionManager().getParams().setSoTimeout(data.realSocketTimeout);
        }

        if (!Const.isEmpty(data.realHttpLogin)) {
            HTTPPOSTclient.getParams().setAuthenticationPreemptive(true);
            Credentials defaultcreds = new UsernamePasswordCredentials(data.realHttpLogin,
                    data.realHttpPassword);
            HTTPPOSTclient.getState().setCredentials(AuthScope.ANY, defaultcreds);
        }

        HostConfiguration hostConfiguration = new HostConfiguration();
        if (!Const.isEmpty(data.realProxyHost)) {
            hostConfiguration.setProxy(data.realProxyHost, data.realProxyPort);
        }
        // Specify content type and encoding
        // If content encoding is not explicitly specified
        // ISO-8859-1 is assumed by the POSTMethod
        if (!data.contentTypeHeaderOverwrite) { // can be overwritten now
            if (Const.isEmpty(data.realEncoding)) {
                post.setRequestHeader(CONTENT_TYPE, CONTENT_TYPE_TEXT_XML);
                if (isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.HeaderValue", CONTENT_TYPE,
                            CONTENT_TYPE_TEXT_XML));
                }
            } else {
                post.setRequestHeader(CONTENT_TYPE, CONTENT_TYPE_TEXT_XML + "; " + data.realEncoding);
                if (isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.HeaderValue", CONTENT_TYPE,
                            CONTENT_TYPE_TEXT_XML + "; " + data.realEncoding));
                }
            }
        }

        // HEADER PARAMETERS
        if (data.useHeaderParameters) {
            // set header parameters that we want to send
            for (int i = 0; i < data.header_parameters_nrs.length; i++) {
                post.addRequestHeader(data.headerParameters[i].getName(),
                        data.inputRowMeta.getString(rowData, data.header_parameters_nrs[i]));
                if (isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.HeaderValue",
                            data.headerParameters[i].getName(),
                            data.inputRowMeta.getString(rowData, data.header_parameters_nrs[i])));
                }
            }
        }

        // BODY PARAMETERS
        if (data.useBodyParameters) {
            // set body parameters that we want to send
            for (int i = 0; i < data.body_parameters_nrs.length; i++) {
                data.bodyParameters[i]
                        .setValue(data.inputRowMeta.getString(rowData, data.body_parameters_nrs[i]));
                if (isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.BodyValue",
                            data.bodyParameters[i].getName(),
                            data.inputRowMeta.getString(rowData, data.body_parameters_nrs[i])));
                }
            }
            post.setRequestBody(data.bodyParameters);
        }

        // QUERY PARAMETERS
        if (data.useQueryParameters) {
            for (int i = 0; i < data.query_parameters_nrs.length; i++) {
                data.queryParameters[i]
                        .setValue(data.inputRowMeta.getString(rowData, data.query_parameters_nrs[i]));
                if (isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.QueryValue",
                            data.queryParameters[i].getName(),
                            data.inputRowMeta.getString(rowData, data.query_parameters_nrs[i])));
                }
            }
            post.setQueryString(data.queryParameters);
        }

        // Set request entity?
        if (data.indexOfRequestEntity >= 0) {
            String tmp = data.inputRowMeta.getString(rowData, data.indexOfRequestEntity);
            // Request content will be retrieved directly
            // from the input stream
            // Per default, the request content needs to be buffered
            // in order to determine its length.
            // Request body buffering can be avoided when
            // content length is explicitly specified

            if (meta.isPostAFile()) {
                File input = new File(tmp);
                fis = new FileInputStream(input);
                post.setRequestEntity(new InputStreamRequestEntity(fis, input.length()));
            } else {
                if ((data.realEncoding != null) && (data.realEncoding.length() > 0)) {
                    post.setRequestEntity(new InputStreamRequestEntity(
                            new ByteArrayInputStream(tmp.getBytes(data.realEncoding)), tmp.length()));
                } else {
                    post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(tmp.getBytes()),
                            tmp.length()));
                }
            }
        }

        // Execute request
        //
        InputStreamReader inputStreamReader = null;
        Object[] newRow = null;
        if (rowData != null) {
            newRow = rowData.clone();
        }
        try {
            // used for calculating the responseTime
            long startTime = System.currentTimeMillis();

            // Execute the POST method
            int statusCode = HTTPPOSTclient.executeMethod(hostConfiguration, post);

            // calculate the responseTime
            long responseTime = System.currentTimeMillis() - startTime;

            if (isDetailed()) {
                logDetailed(
                        BaseMessages.getString(PKG, "HTTPPOST.Log.ResponseTime", responseTime, data.realUrl));
            }

            // Display status code
            if (isDebug()) {
                logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.ResponseCode", String.valueOf(statusCode)));
            }
            String body = null;
            if (statusCode != -1) {
                if (statusCode == 204) {
                    body = "";
                } else {
                    // if the response is not 401: HTTP Authentication required
                    if (statusCode != 401) {

                        // Use request encoding if specified in component to avoid strange response encodings
                        // See PDI-3815
                        String encoding = data.realEncoding;

                        // Try to determine the encoding from the Content-Type value
                        //
                        if (Const.isEmpty(encoding)) {
                            String contentType = post.getResponseHeader("Content-Type").getValue();
                            if (contentType != null && contentType.contains("charset")) {
                                encoding = contentType.replaceFirst("^.*;\\s*charset\\s*=\\s*", "")
                                        .replace("\"", "").trim();
                            }
                        }

                        // Get the response, but only specify encoding if we've got one
                        // otherwise the default charset ISO-8859-1 is used by HttpClient
                        if (Const.isEmpty(encoding)) {
                            if (isDebug()) {
                                logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.Encoding", "ISO-8859-1"));
                            }
                            inputStreamReader = new InputStreamReader(post.getResponseBodyAsStream());
                        } else {
                            if (isDebug()) {
                                logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.Encoding", encoding));
                            }
                            inputStreamReader = new InputStreamReader(post.getResponseBodyAsStream(), encoding);
                        }

                        StringBuffer bodyBuffer = new StringBuffer();

                        int c;
                        while ((c = inputStreamReader.read()) != -1) {
                            bodyBuffer.append((char) c);
                        }
                        inputStreamReader.close();

                        // Display response
                        body = bodyBuffer.toString();

                        if (isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.ResponseBody", body));
                        }
                    } else { // the status is a 401
                        throw new KettleStepException(
                                BaseMessages.getString(PKG, "HTTPPOST.Exception.Authentication", data.realUrl));

                    }
                }
            }
            int returnFieldsOffset = data.inputRowMeta.size();
            if (!Const.isEmpty(meta.getFieldName())) {
                newRow = RowDataUtil.addValueData(newRow, returnFieldsOffset, body);
                returnFieldsOffset++;
            }

            if (!Const.isEmpty(meta.getResultCodeFieldName())) {
                newRow = RowDataUtil.addValueData(newRow, returnFieldsOffset, new Long(statusCode));
                returnFieldsOffset++;
            }
            if (!Const.isEmpty(meta.getResponseTimeFieldName())) {
                newRow = RowDataUtil.addValueData(newRow, returnFieldsOffset, new Long(responseTime));
            }
        } finally {
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            // Release current connection to the connection pool once you are done
            post.releaseConnection();
            if (data.realcloseIdleConnectionsTime > -1) {
                HTTPPOSTclient.getHttpConnectionManager()
                        .closeIdleConnections(data.realcloseIdleConnectionsTime);
            }
        }
        return newRow;
    } catch (UnknownHostException uhe) {
        throw new KettleException(
                BaseMessages.getString(PKG, "HTTPPOST.Error.UnknownHostException", uhe.getMessage()));
    } catch (Exception e) {
        throw new KettleException(BaseMessages.getString(PKG, "HTTPPOST.Error.CanNotReadURL", data.realUrl), e);

    } finally {
        if (fis != null) {
            BaseStep.closeQuietly(fis);
        }
    }
}

From source file:org.red5.server.net.rtmpt.RTMPTClientConnector.java

public void run() {
    try {/*from   ww w. jav a2 s  .c om*/
        RTMPTClientConnection connection = openConnection();

        while (!connection.isClosing() && !stopRequested) {
            IoBuffer toSend = connection.getPendingMessages(SEND_TARGET_SIZE);
            PostMethod post;
            if (toSend != null && toSend.limit() > 0) {
                post = makePost("send");
                post.setRequestEntity(new InputStreamRequestEntity(toSend.asInputStream(), CONTENT_TYPE));
            } else {
                post = makePost("idle");
                post.setRequestEntity(ZERO_REQUEST_ENTITY);
            }
            httpClient.executeMethod(post);

            checkResponseCode(post);

            byte[] received = post.getResponseBody();

            IoBuffer data = IoBuffer.allocate(received.length);
            data.put(received);
            data.flip();
            data.skip(1); // XXX: polling interval lies in this byte
            List<?> messages = connection.decode(data);

            if (messages == null || messages.isEmpty()) {
                try {
                    // XXX handle polling delay
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    if (stopRequested)
                        break;
                }
                continue;
            }

            for (Object message : messages) {
                try {
                    client.messageReceived(connection, connection.getState(), message);
                } catch (Exception e) {
                    log.error("Could not process message.", e);
                }
            }
        }

        finalizeConnection();

        client.connectionClosed(connection, connection.getState());

    } catch (Throwable e) {
        log.debug("RTMPT handling exception", e);
        client.handleException(e);
    }
}