Example usage for org.apache.commons.httpclient HostConfiguration toString

List of usage examples for org.apache.commons.httpclient HostConfiguration toString

Introduction

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

Prototype

public String toString() 

Source Link

Usage

From source file:org.tuckey.web.filters.urlrewrite.RequestProxy.java

/**
 * This method performs the proxying of the request to the target address.
 *
 * @param target     The target address. Has to be a fully qualified address. The request is send as-is to this address.
 * @param hsRequest  The request data which should be send to the
 * @param hsResponse The response data which will contain the data returned by the proxied request to target.
 * @throws java.io.IOException Passed on from the connection logic.
 *//* ww  w  .  j ava  2 s. co m*/
public static void execute(final String target, final HttpServletRequest hsRequest,
        final HttpServletResponse hsResponse) throws IOException {
    if (log.isInfoEnabled()) {
        log.info("execute, target is " + target);
        log.info("response commit state: " + hsResponse.isCommitted());
    }

    if (StringUtils.isBlank(target)) {
        log.error("The target address is not given. Please provide a target address.");
        return;
    }

    log.info("checking url");
    final URL url;
    try {
        url = new URL(target);
    } catch (MalformedURLException e) {
        log.error("The provided target url is not valid.", e);
        return;
    }

    log.info("seting up the host configuration");

    final HostConfiguration config = new HostConfiguration();

    ProxyHost proxyHost = getUseProxyServer((String) hsRequest.getAttribute("use-proxy"));
    if (proxyHost != null)
        config.setProxyHost(proxyHost);

    final int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort();
    config.setHost(url.getHost(), port, url.getProtocol());

    if (log.isInfoEnabled())
        log.info("config is " + config.toString());

    final HttpMethod targetRequest = setupProxyRequest(hsRequest, url);
    if (targetRequest == null) {
        log.error("Unsupported request method found: " + hsRequest.getMethod());
        return;
    }

    //perform the reqeust to the target server
    final HttpClient client = new HttpClient(new SimpleHttpConnectionManager());
    if (log.isInfoEnabled()) {
        log.info("client state" + client.getState());
        log.info("client params" + client.getParams().toString());
        log.info("executeMethod / fetching data ...");
    }

    final int result;
    if (targetRequest instanceof EntityEnclosingMethod) {
        final RequestProxyCustomRequestEntity requestEntity = new RequestProxyCustomRequestEntity(
                hsRequest.getInputStream(), hsRequest.getContentLength(), hsRequest.getContentType());
        final EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) targetRequest;
        entityEnclosingMethod.setRequestEntity(requestEntity);
        result = client.executeMethod(config, entityEnclosingMethod);

    } else {
        result = client.executeMethod(config, targetRequest);
    }

    //copy the target response headers to our response
    setupResponseHeaders(targetRequest, hsResponse);

    InputStream originalResponseStream = targetRequest.getResponseBodyAsStream();
    //the body might be null, i.e. for responses with cache-headers which leave out the body
    if (originalResponseStream != null) {
        OutputStream responseStream = hsResponse.getOutputStream();
        copyStream(originalResponseStream, responseStream);
    }

    log.info("set up response, result code was " + result);
}