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

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

Introduction

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

Prototype

public void setLocalAddress(InetAddress paramInetAddress)

Source Link

Usage

From source file:org.opentides.util.WidgetUtil.java

/**
 * Returns the HTML code of the original engine. Takes the URL to connect to
 * the engine. Also takes encoding type that overrides default if not null
 * "UTF8" is typical encoding type/* w w w  . j  av a  2s .  c  om*/
 * 
 * @param queryURL
 *            - URL of engine to retrieve
 * @param request
 *            - request object
 * @param param
 *            - additional parameters
 *              - Valid parameters are:
 *              - methodName - Either "POST" or "GET". Default is "POST"   
 *            - forwardCookie - if true, will forward cookies found on request object
 *            - IPAddress - if specified, this IP will be used for the request
 *        
 */
public static final UrlResponseObject getPage(final String queryURL, final HttpServletRequest request,
        final Map<String, Object> param) {
    // determine if get or post method
    HttpMethodBase httpMethodBase;
    Boolean forwardCookie = false;
    InetAddress IPAddress = null;

    if (param != null) {
        if (param.get("forwardCookie") != null)
            forwardCookie = (Boolean) param.get("forwardCookie");

        if (param.get("IPAddress") != null) {
            String IPString = (String) param.get("IPAddress");
            if (!StringUtil.isEmpty(IPString)) {
                IPAddress = UrlUtil.convertIPString(IPString);
            }
        }
    }

    if (param != null && "GET".equals((String) param.get("methodName"))) {
        httpMethodBase = new GetMethod(queryURL);
    } else {
        httpMethodBase = new PostMethod(queryURL);
    }

    try {
        // declare the connection objects
        HttpClient client = new HttpClient();
        HostConfiguration hostConfig = new HostConfiguration();
        String userAgent = request.getHeader("User-Agent");

        // for debugging
        if (_log.isDebugEnabled())
            _log.debug("Retrieving page from " + queryURL);

        // initialize the connection settings
        client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);
        client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        httpMethodBase.addRequestHeader("accept", "*/*");
        httpMethodBase.addRequestHeader("accept-language", "en-us");
        httpMethodBase.addRequestHeader("user-agent", userAgent);

        if (forwardCookie) {
            // get cookies from request
            Cookie[] cookies = request.getCookies();
            String cookieString = "";
            for (Cookie c : cookies) {
                cookieString += c.getName() + "=" + c.getValue() + "; ";
            }

            // forward cookies to httpMethod
            httpMethodBase.setRequestHeader("Cookie", cookieString);
        }

        if (IPAddress != null) {
            hostConfig.setLocalAddress(IPAddress);
        }

        // Setup for 3 retry  
        httpMethodBase.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        // now let's retrieve the data
        client.executeMethod(hostConfig, httpMethodBase);

        // Read the response body.
        UrlResponseObject response = new UrlResponseObject();
        response.setResponseBody(httpMethodBase.getResponseBody());
        Header contentType = httpMethodBase.getResponseHeader("Content-Type");
        if (contentType != null)
            response.setResponseType(contentType.getValue());
        else
            response.setResponseType("html");
        return response;

    } catch (Exception ex) {
        _log.error("Failed to request from URL: [" + queryURL + "]", ex);
        return null;
    } finally {
        try {
            httpMethodBase.releaseConnection();
        } catch (Exception ignored) {
        }
    }
}