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

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

Introduction

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

Prototype

public void setProxy(String paramString, int paramInt)

Source Link

Usage

From source file:com.twinsoft.convertigo.engine.ProxyManager.java

public void setProxy(HostConfiguration hostConfiguration, HttpState httpState, URL url) throws EngineException {
    // Proxy configuration
    Boolean needProxy = true;//from   ww w .  j  av  a  2 s.  c o  m
    String[] bpDomains = getBypassDomains();
    String urlHost = url.getHost();

    for (String domain : bpDomains) {
        if (domain.equals(urlHost)) {
            needProxy = false;
        }
    }

    hostConfiguration.getParams().setParameter("hostConfId", this.hostConfId);

    if (isEnabled()) {
        if (needProxy) {
            if (proxyMode == ProxyMode.manual) {
                if (!proxyServer.equals("")) {
                    hostConfiguration.setProxy(proxyServer, proxyPort);
                    Engine.logProxyManager
                            .debug("(ProxyManager) Using proxy: " + proxyServer + ":" + proxyPort);
                } else {
                    disableProxy(hostConfiguration);
                }
            } else if (proxyMode == ProxyMode.auto) {
                //               String result = pacUtils.evaluate(url.toString(), url.getHost());
                //               
                //               if (result.startsWith("PROXY")) {
                //                  result = result.replaceAll("PROXY\\s*", "");
                //                  String pacServer = result.split(":")[0];
                //                  int pacPort =  Integer.parseInt(result.split(":")[1]);
                //                  this.proxyServer = pacServer;
                //                  this.proxyPort = pacPort;
                //                  hostConfiguration.setProxy(pacServer, pacPort);
                //                  Engine.logProxyManager.debug("(ProxyManager) Using proxy from auto configuration file: " + proxyServer + ":" + proxyPort);
                //               }
                //               else {
                //                  hostConfiguration.setProxyHost(null);
                //               }

                PacInfos pacInfos = getPacInfos(url.toString(), url.getHost());
                if (pacInfos != null) {
                    proxyServer = pacInfos.getServer();
                    proxyPort = pacInfos.getPort();
                    hostConfiguration.setProxy(proxyServer, proxyPort);
                    Engine.logProxyManager.debug("(ProxyManager) Using proxy from auto configuration file: "
                            + proxyServer + ":" + proxyPort);
                } else {
                    disableProxy(hostConfiguration);
                }
            }

            if (proxyMethod == ProxyMethod.basic) {
                setBasicAuth(httpState);
            } else if (proxyMethod == ProxyMethod.ntlm) {
                int indexSlash = this.proxyUser.indexOf("\\");
                if (indexSlash != -1) {
                    setNtlmAuth(httpState);
                } else {
                    throw new EngineException(
                            "\nWrong username, please indicate the domain name for ntlm authentication. (eg: domain\\user)\n");
                }
            } else {
                setAnonymAuth(httpState);
            }
        } else {
            disableProxy(hostConfiguration);
        }
    }
}

From source file:com.orange.mmp.net.http.HttpConnection.java

/**
 * Inner method used to execute request// w  ww  . j  av a2 s .  c om
 * 
 * @param dataStream The data stream to send in request (null for GET only)
 * @throws MMPNetException 
 */
@SuppressWarnings("unchecked")
protected void doExecute(InputStream dataStream) throws MMPNetException {
    try {
        this.currentHttpClient = HttpConnectionManager.httpClientPool.take();
    } catch (InterruptedException ie) {
        throw new MMPNetException("Corrupted HTTP client pool", ie);
    }

    if (this.httpConnectionProperties.containsKey(HttpConnectionParameters.PARAM_IN_CREDENTIALS_USER)) {
        this.currentHttpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
                (String) this.httpConnectionProperties.get(HttpConnectionParameters.PARAM_IN_CREDENTIALS_USER),
                (String) this.httpConnectionProperties
                        .get(HttpConnectionParameters.PARAM_IN_CREDENTIALS_PASSWORD)));
        this.currentHttpClient.getParams().setAuthenticationPreemptive(true);
    }

    // Config
    HostConfiguration config = new HostConfiguration();
    if (this.timeout > 0)
        this.currentHttpClient.getParams().setParameter(HttpConnectionParameters.PARAM_IN_HTTP_SOCKET_TIMEOUT,
                this.timeout);
    if (HttpConnectionManager.proxyHost != null
            && (this.httpConnectionProperties.get(HttpConnectionParameters.PARAM_IN_HTTP_USE_PROXY) != null
                    && this.httpConnectionProperties.get(HttpConnectionParameters.PARAM_IN_HTTP_USE_PROXY)
                            .toString().equals("true"))
            || (this.httpConnectionProperties.get(HttpConnectionParameters.PARAM_IN_HTTP_USE_PROXY) == null
                    && this.useProxy)) {
        config.setProxy(HttpConnectionManager.proxyHost, HttpConnectionManager.proxyPort);
    } else {
        config.setProxyHost(null);
    }
    this.currentHttpClient.setHostConfiguration(config);
    this.currentHttpClient.getHostConfiguration().setHost(new HttpHost(this.endPointUrl.getHost()));

    String methodStr = (String) this.httpConnectionProperties
            .get(HttpConnectionParameters.PARAM_IN_HTTP_METHOD);

    if (methodStr == null || methodStr.equals(HttpConnectionParameters.HTTP_METHOD_GET)) {
        this.method = new GetMethod(endPointUrl.toString().replace(" ", "+"));
    } else if (methodStr.equals(HttpConnectionParameters.HTTP_METHOD_POST)) {
        this.method = new PostMethod((this.endPointUrl.getQuery() == null) ? this.endPointUrl.getPath()
                : this.endPointUrl.getPath() + "?" + endPointUrl.getQuery());
        if (dataStream != null) {
            InputStreamRequestEntity inputStreamRequestEntity = new InputStreamRequestEntity(dataStream);
            ((PostMethod) this.method).setRequestEntity(inputStreamRequestEntity);
        }
    } else if (methodStr.equals(HttpConnectionParameters.HTTP_METHOD_PUT)) {
        this.method = new PutMethod((this.endPointUrl.getQuery() == null) ? this.endPointUrl.getPath()
                : this.endPointUrl.getPath() + "?" + endPointUrl.getQuery());
        if (dataStream != null) {
            InputStreamRequestEntity inputStreamRequestEntity = new InputStreamRequestEntity(dataStream);
            ((PutMethod) this.method).setRequestEntity(inputStreamRequestEntity);
        }
    } else if (methodStr.equals(HttpConnectionParameters.HTTP_METHOD_DEL)) {
        this.method = new DeleteMethod((this.endPointUrl.getQuery() == null) ? this.endPointUrl.getPath()
                : this.endPointUrl.getPath() + "?" + endPointUrl.getQuery());
    } else
        throw new MMPNetException("HTTP method not supported");

    //Add headers
    if (this.httpConnectionProperties != null) {
        for (Object headerName : this.httpConnectionProperties.keySet()) {
            if (!((String) headerName).startsWith("http.")) {
                this.method.addRequestHeader((String) headerName,
                        this.httpConnectionProperties.get(headerName).toString());
            }
        }
    }
    // Set Connection/Proxy-Connection close to avoid TIME_WAIT sockets
    this.method.addRequestHeader("Connection", "close");
    this.method.addRequestHeader("Proxy-Connection", "close");

    try {
        int httpCode = this.currentHttpClient.executeMethod(config, method);
        this.currentStatusCode = httpCode;

        for (org.apache.commons.httpclient.Header responseHeader : method.getResponseHeaders()) {
            this.httpConnectionProperties.put(responseHeader.getName(), responseHeader.getValue());
        }

        if (this.currentStatusCode >= 400) {
            throw new MMPNetException("HTTP " + this.currentStatusCode + " on '" + endPointUrl + "'");
        } else {
            this.inDataStream = this.method.getResponseBodyAsStream();
        }
    } catch (IOException ioe) {
        throw new MMPNetException("I/O error on " + this.endPointUrl + " : " + ioe.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.iflytek.spider.protocol.httpclient.Http.java

/**
 * Configures the HTTP client/* ww  w .j  a  va2  s .c  om*/
 */
private void configureClient() {

    // Set up an HTTPS socket factory that accepts self-signed certs.
    Protocol https = new Protocol("https", new DummySSLProtocolSocketFactory(), 443);
    Protocol.registerProtocol("https", https);

    HttpConnectionManagerParams params = connectionManager.getParams();
    params.setConnectionTimeout(timeout);
    params.setSoTimeout(timeout);
    params.setSendBufferSize(BUFFER_SIZE);
    params.setReceiveBufferSize(BUFFER_SIZE);
    params.setMaxTotalConnections(maxThreadsTotal);

    params.setDefaultMaxConnectionsPerHost(maxThreadsTotal);

    // executeMethod(HttpMethod) seems to ignore the connection timeout on
    // the connection manager.
    // set it explicitly on the HttpClient.
    client.getParams().setConnectionManagerTimeout(timeout);

    HostConfiguration hostConf = client.getHostConfiguration();
    ArrayList headers = new ArrayList();
    // Set the User Agent in the header
    headers.add(new Header("User-Agent", userAgent));
    // prefer English
    headers.add(new Header("Accept-Language", acceptLanguage));
    // prefer UTF-8
    headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7"));
    // prefer understandable formats
    headers.add(new Header("Accept",
            "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"));
    // accept gzipped content
    headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate"));
    hostConf.getParams().setParameter("http.default-headers", headers);

    // HTTP proxy server details
    if (useProxy) {
        hostConf.setProxy(proxyHost, proxyPort);

        if (proxyUsername.length() > 0) {

            AuthScope proxyAuthScope = getAuthScope(this.proxyHost, this.proxyPort, this.proxyRealm);

            NTCredentials proxyCredentials = new NTCredentials(this.proxyUsername, this.proxyPassword,
                    this.agentHost, this.proxyRealm);

            client.getState().setProxyCredentials(proxyAuthScope, proxyCredentials);
        }
    }

}

From source file:com.iflytek.spider.protocol.httpclient.HttpSimply.java

/**
 * Configures the HTTP client/*from   w w w .  j a  va 2s .com*/
 */
private void configureClient() {

    // Set up an HTTPS socket factory that accepts self-signed certs.
    Protocol https = new Protocol("https", new DummySSLProtocolSocketFactory(), 443);
    Protocol.registerProtocol("https", https);

    HttpConnectionManagerParams params = connectionManager.getParams();
    params.setConnectionTimeout(timeout);
    //params.setSoTimeout(timeout); timeouttimeout
    params.setSendBufferSize(BUFFER_SIZE);
    params.setReceiveBufferSize(BUFFER_SIZE);
    params.setMaxTotalConnections(maxThreadsTotal);

    params.setDefaultMaxConnectionsPerHost(maxThreadsTotal);

    // executeMethod(HttpMethod) seems to ignore the connection timeout on
    // the connection manager.
    // set it explicitly on the HttpClient.
    client.getParams().setConnectionManagerTimeout(timeout);

    HostConfiguration hostConf = client.getHostConfiguration();
    ArrayList headers = new ArrayList();
    // Set the User Agent in the header
    headers.add(new Header("User-Agent", userAgent));
    // prefer English
    headers.add(new Header("Accept-Language", acceptLanguage));
    // prefer UTF-8
    headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7"));
    // prefer understandable formats
    headers.add(new Header("Accept",
            "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"));
    // accept gzipped content
    headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate"));
    hostConf.getParams().setParameter("http.default-headers", headers);

    // HTTP proxy server details
    if (useProxy) {
        hostConf.setProxy(proxyHost, proxyPort);

        if (proxyUsername.length() > 0) {

            AuthScope proxyAuthScope = getAuthScope(this.proxyHost, this.proxyPort, this.proxyRealm);

            NTCredentials proxyCredentials = new NTCredentials(this.proxyUsername, this.proxyPassword,
                    this.agentHost, this.proxyRealm);

            client.getState().setProxyCredentials(proxyAuthScope, proxyCredentials);
        }
    }

}

From source file:de.juwimm.cms.common.http.HttpClientWrapper.java

public void setHostConfiguration(HttpClient client, URL targetURL) {
    int port = targetURL.getPort();
    String host = targetURL.getHost();
    HostConfiguration config = hostMap.get(host + ":" + port);
    if (config == null) {
        config = new HostConfiguration();
        if (port == -1) {
            if (targetURL.getProtocol().equalsIgnoreCase("https")) {
                port = 443;/*from w w w . j  a v  a 2s  .c  om*/
            } else {
                port = 80;
            }
        }
        config.setHost(host, port, targetURL.getProtocol());
    }
    // in the meantime HttpProxyUser and HttpProxyPasword might have changed
    // (DlgUsernamePassword) or now trying NTLM instead of BASE
    // authentication
    if (getHttpProxyHost() != null && getHttpProxyPort() != null && getHttpProxyHost().length() > 0
            && getHttpProxyPort().length() > 0) {
        client.getParams().setAuthenticationPreemptive(true);
        int proxyPort = new Integer(getHttpProxyPort()).intValue();
        config.setProxy(getHttpProxyHost(), proxyPort);
        if (getHttpProxyUser() != null && getHttpProxyUser().length() > 0) {
            Credentials proxyCred = null;
            if (isUseNTproxy()) {
                proxyCred = new NTCredentials(getHttpProxyUser(), getHttpProxyPassword(), getHttpProxyHost(),
                        "");
            } else {
                proxyCred = new UsernamePasswordCredentials(getHttpProxyUser(), getHttpProxyPassword());
            }
            client.getState().setProxyCredentials(AUTHSCOPE_ANY, proxyCred);

        }
    }
    hostMap.put(host + ":" + port, config);
    client.setHostConfiguration(config);

}

From source file:com.liferay.portal.util.HttpImpl.java

public HostConfiguration getHostConfiguration(String location) throws IOException {

    if (_log.isDebugEnabled()) {
        _log.debug("Location is " + location);
    }//from   ww w .  j  a va 2 s .  c o m

    HostConfiguration hostConfiguration = new HostConfiguration();

    hostConfiguration.setHost(new URI(location, false));

    if (isProxyHost(hostConfiguration.getHost())) {
        hostConfiguration.setProxy(_PROXY_HOST, _PROXY_PORT);
    }

    HttpConnectionManager httpConnectionManager = _httpClient.getHttpConnectionManager();

    HttpConnectionManagerParams httpConnectionManagerParams = httpConnectionManager.getParams();

    int defaultMaxConnectionsPerHost = httpConnectionManagerParams.getMaxConnectionsPerHost(hostConfiguration);

    int maxConnectionsPerHost = GetterUtil.getInteger(PropsUtil.get(
            HttpImpl.class.getName() + ".max.connections.per.host", new Filter(hostConfiguration.getHost())));

    if ((maxConnectionsPerHost > 0) && (maxConnectionsPerHost != defaultMaxConnectionsPerHost)) {

        httpConnectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, maxConnectionsPerHost);
    }

    int timeout = GetterUtil.getInteger(
            PropsUtil.get(HttpImpl.class.getName() + ".timeout", new Filter(hostConfiguration.getHost())));

    if (timeout > 0) {
        HostParams hostParams = hostConfiguration.getParams();

        hostParams.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, timeout);
        hostParams.setIntParameter(HttpConnectionParams.SO_TIMEOUT, timeout);
    }

    return hostConfiguration;
}

From source file:com.cordys.coe.ac.httpconnector.config.ServerConnection.java

/**
 * @see com.cordys.coe.ac.httpconnector.config.IServerConnection#open()
 *///  w w  w .  ja v  a2s  .c  o  m
@Override
public void open() {
    m_connManager = new MultiThreadedHttpConnectionManager();
    m_client = new HttpClient(m_connManager);

    // Set the host information in the client.
    HostConfiguration hostConfig = m_client.getHostConfiguration();
    String protoName = m_url.getProtocol();
    String hostName = m_url.getHost();
    int port;

    port = m_url.getPort();

    if (port <= 0) {
        port = m_url.getDefaultPort();
    }

    Protocol protocol;
    if (HTTPS_PROTOCOL_PREFIX.equals(protoName)) {
        if (m_checkServerCertificate) {
            if (LOG.isInfoEnabled()) {
                LOG.info(Messages.USING_CORDYS_SOCKET_FACTORY);
            }
            protocol = new Protocol(HTTPS_PROTOCOL_PREFIX, createSocketFactory(), port);

        } else {
            if (LOG.isInfoEnabled()) {
                LOG.info(Messages.USING_DUMMY_SOCKET_FACTORY);
            }
            protocol = new Protocol("https", (ProtocolSocketFactory) new DummySSLSocketFactory(), port);

        }
    } else {
        protocol = Protocol.getProtocol(protoName);
    }
    hostConfig.setHost(hostName, port, protocol);

    if (m_proxyHost != null) {
        hostConfig.setProxy(m_proxyHost, m_proxyPort);

        if (m_proxyUsername != null) {
            Credentials defaultcreds = new UsernamePasswordCredentials(m_proxyUsername, m_proxyPassword);

            m_client.getState().setProxyCredentials(new AuthScope(hostName, port, AuthScope.ANY_REALM),
                    defaultcreds);
        }
    }

    if (m_username != null) {
        if (m_authenticateAlways) {
            m_client.getParams().setAuthenticationPreemptive(true);
        }

        Credentials defaultcreds = new UsernamePasswordCredentials(m_username, m_password);

        m_client.getState().setCredentials(new AuthScope(hostName, port, AuthScope.ANY_REALM), defaultcreds);
    }
}

From source file:com.twinsoft.convertigo.engine.servlets.ReverseProxyServlet.java

/**
 * Executes the {@link HttpMethod} passed in and sends the proxy response
 * back to the client via the given {@link HttpServletResponse}
 * /*from w w w . j a v  a 2 s. c o  m*/
 * @param httpMethodProxyRequest
 *            An object representing the proxy request to be made
 * @param httpServletResponse
 *            An object by which we can send the proxied response back to
 *            the client
 * @throws IOException
 *             Can be thrown by the {@link HttpClient}.executeMethod
 * @throws ServletException
 *             Can be thrown to indicate that another error has occurred
 * @throws EngineException
 */
private void doRequest(HttpMethodType httpMethodType, HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse) throws IOException, ServletException {
    try {
        Engine.logEngine.debug("(ReverseProxyServlet) Starting request handling");

        if (Boolean.parseBoolean(EnginePropertiesManager.getProperty(PropertyName.SSL_DEBUG))) {
            System.setProperty("javax.net.debug", "all");
            Engine.logEngine.trace("(ReverseProxyServlet) Enabling SSL debug mode");
        } else {
            System.setProperty("javax.net.debug", "");
            Engine.logEngine.debug("(ReverseProxyServlet) Disabling SSL debug mode");
        }

        String baseUrl;
        String projectName;
        String connectorName;
        String contextName;
        String extraPath;

        {
            String requestURI = httpServletRequest.getRequestURI();
            Engine.logEngine.trace("(ReverseProxyServlet) Requested URI : " + requestURI);
            Matcher m = reg_fields.matcher(requestURI);
            if (m.matches() && m.groupCount() >= 5) {
                baseUrl = m.group(1);
                projectName = m.group(2);
                connectorName = m.group(3);
                contextName = m.group(4);
                extraPath = m.group(5);
            } else {
                throw new MalformedURLException(
                        "The request doesn't contains needed fields : projectName, connectorName and contextName");
            }
        }

        String sessionID = httpServletRequest.getSession().getId();

        Engine.logEngine.debug("(ReverseProxyServlet) baseUrl : " + baseUrl + " ; projectName : " + projectName
                + " ; connectorName : " + connectorName + " ; contextName : " + contextName + " ; extraPath : "
                + extraPath + " ; sessionID : " + sessionID);

        Context context = Engine.theApp.contextManager.get(null, contextName, sessionID, null, projectName,
                connectorName, null);

        Project project = Engine.theApp.databaseObjectsManager.getProjectByName(projectName);
        context.projectName = projectName;
        context.project = project;

        ProxyHttpConnector proxyHttpConnector = (ProxyHttpConnector) project.getConnectorByName(connectorName);
        context.connector = proxyHttpConnector;
        context.connectorName = proxyHttpConnector.getName();

        HostConfiguration hostConfiguration = proxyHttpConnector.hostConfiguration;

        // Proxy configuration
        String proxyServer = Engine.theApp.proxyManager.getProxyServer();
        String proxyUser = Engine.theApp.proxyManager.getProxyUser();
        String proxyPassword = Engine.theApp.proxyManager.getProxyPassword();
        int proxyPort = Engine.theApp.proxyManager.getProxyPort();

        if (!proxyServer.equals("")) {
            hostConfiguration.setProxy(proxyServer, proxyPort);
            Engine.logEngine.debug("(ReverseProxyServlet) Using proxy: " + proxyServer + ":" + proxyPort);
        } else {
            // Remove old proxy configuration
            hostConfiguration.setProxyHost(null);
        }

        String targetHost = proxyHttpConnector.getServer();
        Engine.logEngine.debug("(ReverseProxyServlet) Target host: " + targetHost);
        int targetPort = proxyHttpConnector.getPort();
        Engine.logEngine.debug("(ReverseProxyServlet) Target port: " + targetPort);

        // Configuration SSL
        Engine.logEngine.debug("(ReverseProxyServlet) Https: " + proxyHttpConnector.isHttps());
        CertificateManager certificateManager = proxyHttpConnector.certificateManager;
        boolean trustAllServerCertificates = proxyHttpConnector.isTrustAllServerCertificates();

        if (proxyHttpConnector.isHttps()) {
            Engine.logEngine.debug("(ReverseProxyServlet) Setting up SSL properties");
            certificateManager.collectStoreInformation(context);

            Engine.logEngine.debug(
                    "(ReverseProxyServlet) CertificateManager has changed: " + certificateManager.hasChanged);
            if (certificateManager.hasChanged || (!targetHost.equalsIgnoreCase(hostConfiguration.getHost()))
                    || (hostConfiguration.getPort() != targetPort)) {
                Engine.logEngine
                        .debug("(ReverseProxyServlet) Using MySSLSocketFactory for creating the SSL socket");
                Protocol myhttps = new Protocol("https",
                        MySSLSocketFactory.getSSLSocketFactory(certificateManager.keyStore,
                                certificateManager.keyStorePassword, certificateManager.trustStore,
                                certificateManager.trustStorePassword, trustAllServerCertificates),
                        targetPort);

                hostConfiguration.setHost(targetHost, targetPort, myhttps);
            }

            Engine.logEngine.debug("(ReverseProxyServlet) Updated host configuration for SSL purposes");
        } else {
            hostConfiguration.setHost(targetHost, targetPort);
        }

        HttpMethod httpMethodProxyRequest;

        String targetPath = proxyHttpConnector.getBaseDir() + extraPath;

        // Handle the query string
        if (httpServletRequest.getQueryString() != null) {
            targetPath += "?" + httpServletRequest.getQueryString();
        }
        Engine.logEngine.debug("(ReverseProxyServlet) Target path: " + targetPath);

        Engine.logEngine.debug("(ReverseProxyServlet) Requested method: " + httpMethodType);

        if (httpMethodType == HttpMethodType.GET) {
            // Create a GET request
            httpMethodProxyRequest = new GetMethod();
        } else if (httpMethodType == HttpMethodType.POST) {
            // Create a standard POST request
            httpMethodProxyRequest = new PostMethod();
            ((PostMethod) httpMethodProxyRequest)
                    .setRequestEntity(new InputStreamRequestEntity(httpServletRequest.getInputStream()));
        } else {
            throw new IllegalArgumentException("Unknown HTTP method: " + httpMethodType);
        }

        String charset = httpMethodProxyRequest.getParams().getUriCharset();
        URI targetURI;
        try {
            targetURI = new URI(targetPath, true, charset);
        } catch (URIException e) {
            // Bugfix #1484
            String newTargetPath = "";
            for (String part : targetPath.split("&")) {
                if (!newTargetPath.equals("")) {
                    newTargetPath += "&";
                }
                String[] pair = part.split("=");
                try {
                    newTargetPath += URLDecoder.decode(pair[0], "UTF-8") + "="
                            + (pair.length > 1 ? URLEncoder.encode(URLDecoder.decode(pair[1], "UTF-8"), "UTF-8")
                                    : "");
                } catch (UnsupportedEncodingException ee) {
                    newTargetPath = targetPath;
                }
            }

            targetURI = new URI(newTargetPath, true, charset);
        }
        httpMethodProxyRequest.setURI(targetURI);

        // Tells the method to automatically handle authentication.
        httpMethodProxyRequest.setDoAuthentication(true);

        HttpState httpState = getHttpState(proxyHttpConnector, context);

        String basicUser = proxyHttpConnector.getAuthUser();
        String basicPassword = proxyHttpConnector.getAuthPassword();
        String givenBasicUser = proxyHttpConnector.getGivenAuthUser();
        String givenBasicPassword = proxyHttpConnector.getGivenAuthPassword();

        // Basic authentication configuration
        String realm = null;
        if (!basicUser.equals("") || (basicUser.equals("") && (givenBasicUser != null))) {
            String userName = ((givenBasicUser == null) ? basicUser : givenBasicUser);
            String userPassword = ((givenBasicPassword == null) ? basicPassword : givenBasicPassword);
            httpState.setCredentials(new AuthScope(targetHost, targetPort, realm),
                    new UsernamePasswordCredentials(userName, userPassword));
            Engine.logEngine.debug("(ReverseProxyServlet) Credentials: " + userName + ":******");
        }

        // Setting basic authentication for proxy
        if (!proxyServer.equals("") && !proxyUser.equals("")) {
            httpState.setProxyCredentials(new AuthScope(proxyServer, proxyPort),
                    new UsernamePasswordCredentials(proxyUser, proxyPassword));
            Engine.logEngine.debug("(ReverseProxyServlet) Proxy credentials: " + proxyUser + ":******");
        }

        // Forward the request headers
        setProxyRequestHeaders(httpServletRequest, httpMethodProxyRequest, proxyHttpConnector);

        // Use the CEMS HttpClient
        HttpClient httpClient = Engine.theApp.httpClient;
        httpMethodProxyRequest.setFollowRedirects(false);

        // Execute the request
        int intProxyResponseCode = httpClient.executeMethod(hostConfiguration, httpMethodProxyRequest,
                httpState);

        // Check if the proxy response is a redirect
        // The following code is adapted from
        // org.tigris.noodle.filters.CheckForRedirect
        // Hooray for open source software
        if (intProxyResponseCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */
                && intProxyResponseCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) {
            String stringStatusCode = Integer.toString(intProxyResponseCode);
            String stringLocation = httpMethodProxyRequest.getResponseHeader(STRING_LOCATION_HEADER).getValue();
            if (stringLocation == null) {
                throw new ServletException("Received status code: " + stringStatusCode + " but no "
                        + STRING_LOCATION_HEADER + " header was found in the response");
            }
            // Modify the redirect to go to this proxy servlet rather that
            // the
            // proxied host
            String redirect = handleRedirect(stringLocation, baseUrl, proxyHttpConnector);
            httpServletResponse.sendRedirect(redirect);
            Engine.logEngine.debug("(ReverseProxyServlet) Send redirect (" + redirect + ")");
            return;
        } else if (intProxyResponseCode == HttpServletResponse.SC_NOT_MODIFIED) {
            // 304 needs special handling. See:
            // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304
            // We get a 304 whenever passed an 'If-Modified-Since'
            // header and the data on disk has not changed; server
            // responds w/ a 304 saying I'm not going to send the
            // body because the file has not changed.
            httpServletResponse.setIntHeader(STRING_CONTENT_LENGTH_HEADER_NAME, 0);
            httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            Engine.logEngine.debug("(ReverseProxyServlet) NOT MODIFIED (304)");
            return;
        }

        // Pass the response code back to the client
        httpServletResponse.setStatus(intProxyResponseCode);

        // Pass response headers back to the client
        Engine.logEngine.debug("(ReverseProxyServlet) Response headers back to the client:");
        Header[] headerArrayResponse = httpMethodProxyRequest.getResponseHeaders();
        for (Header header : headerArrayResponse) {
            String headerName = header.getName();
            String headerValue = header.getValue();
            if (!headerName.equalsIgnoreCase("Transfer-Encoding")
                    && !headerName.equalsIgnoreCase("Set-Cookie")) {
                httpServletResponse.setHeader(headerName, headerValue);
                Engine.logEngine.debug("   " + headerName + "=" + headerValue);
            }
        }

        String contentType = null;
        Header[] contentTypeHeaders = httpMethodProxyRequest.getResponseHeaders("Content-Type");
        for (Header contentTypeHeader : contentTypeHeaders) {
            contentType = contentTypeHeader.getValue();
            break;
        }

        String pageCharset = "UTF-8";
        if (contentType != null) {
            int iCharset = contentType.indexOf("charset=");
            if (iCharset != -1) {
                pageCharset = contentType.substring(iCharset + "charset=".length()).trim();
            }
            Engine.logEngine.debug("(ReverseProxyServlet) Using charset: " + pageCharset);
        }

        InputStream siteIn = httpMethodProxyRequest.getResponseBodyAsStream();

        // Handle gzipped content
        Header[] contentEncodingHeaders = httpMethodProxyRequest.getResponseHeaders("Content-Encoding");
        boolean bGZip = false, bDeflate = false;
        for (Header contentEncodingHeader : contentEncodingHeaders) {
            HeaderElement[] els = contentEncodingHeader.getElements();
            for (int j = 0; j < els.length; j++) {
                if ("gzip".equals(els[j].getName())) {
                    Engine.logBeans.debug("(ReverseProxyServlet) Decode GZip stream");
                    siteIn = new GZIPInputStream(siteIn);
                    bGZip = true;
                } else if ("deflate".equals(els[j].getName())) {
                    Engine.logBeans.debug("(ReverseProxyServlet) Decode Deflate stream");
                    siteIn = new InflaterInputStream(siteIn, new Inflater(true));
                    bDeflate = true;
                }
            }
        }

        byte[] bytesDataResult;

        ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);

        // String resourceUrl = projectName + targetPath;

        String t = context.statistics.start(EngineStatistics.APPLY_USER_REQUEST);

        try {
            // Read either from the cache, either from the remote server
            // InputStream is = proxyCacheManager.getResource(resourceUrl);
            // if (is != null) {
            // Engine.logEngine.debug("(ReverseProxyServlet) Getting data from cache");
            // siteIn = is;
            // }
            int c = siteIn.read();
            while (c > -1) {
                baos.write(c);
                c = siteIn.read();
            }
            // if (is != null) is.close();
        } finally {
            context.statistics.stop(t, true);
        }

        bytesDataResult = baos.toByteArray();
        baos.close();
        Engine.logEngine.debug("(ReverseProxyServlet) Data retrieved!");

        // if (isDynamicContent(httpServletRequest.getPathInfo(),
        // proxyHttpConnector.getDynamicContentFiles())) {
        Engine.logEngine.debug("(ReverseProxyServlet) Dynamic content");

        bytesDataResult = handleStringReplacements(baseUrl, contentType, pageCharset, proxyHttpConnector,
                bytesDataResult);

        String billingClassName = context.getConnector().getBillingClassName();
        if (billingClassName != null) {
            try {
                Engine.logContext.debug("Billing class name required: " + billingClassName);
                AbstractBiller biller = (AbstractBiller) Class.forName(billingClassName).newInstance();
                Engine.logContext.debug("Executing the biller");
                biller.insertBilling(context);
            } catch (Throwable e) {
                Engine.logContext.warn("Unable to execute the biller (the billing is thus ignored): ["
                        + e.getClass().getName() + "] " + e.getMessage());
            }
        }
        // }
        // else {
        // Engine.logEngine.debug("(ReverseProxyServlet) Static content: " +
        // contentType);
        //            
        // // Determine if the resource has already been cached or not
        // CacheEntry cacheEntry =
        // proxyCacheManager.getCacheEntry(resourceUrl);
        // if (cacheEntry instanceof FileCacheEntry) {
        // FileCacheEntry fileCacheEntry = (FileCacheEntry) cacheEntry;
        // File file = new File(fileCacheEntry.fileName);
        // if (!file.exists())
        // proxyCacheManager.removeCacheEntry(cacheEntry);
        // cacheEntry = null;
        // }
        // if (cacheEntry == null) {
        // bytesDataResult = handleStringReplacements(contentType,
        // proxyHttpConnector, bytesDataResult);
        //
        // if (intProxyResponseCode == 200) {
        // Engine.logEngine.debug("(ReverseProxyServlet) Resource stored: "
        // + resourceUrl);
        // cacheEntry = proxyCacheManager.storeResponse(resourceUrl,
        // bytesDataResult);
        // cacheEntry.contentLength = bytesDataResult.length;
        // cacheEntry.contentType = contentType;
        // Engine.logEngine.debug("(ReverseProxyServlet) Cache entry: " +
        // cacheEntry);
        // }
        // }
        // }

        // Send the content to the client
        if (Engine.logEngine.isDebugEnabled() && MimeType.Html.is(contentType)) {
            Engine.logEngine.debug("Data proxied:\n" + new String(bytesDataResult, pageCharset));
        }

        if (bGZip || bDeflate) {
            baos = new ByteArrayOutputStream();
            OutputStream compressedOutputStream = bGZip ? new GZIPOutputStream(baos)
                    : new DeflaterOutputStream(baos,
                            new Deflater(Deflater.DEFAULT_COMPRESSION | Deflater.DEFAULT_STRATEGY, true));
            compressedOutputStream.write(bytesDataResult);
            compressedOutputStream.close();
            bytesDataResult = baos.toByteArray();
            baos.close();
        }

        httpServletResponse.setContentLength(bytesDataResult.length);
        OutputStream outputStreamClientResponse = httpServletResponse.getOutputStream();
        outputStreamClientResponse.write(bytesDataResult);

        Engine.logEngine.debug("(ReverseProxyServlet) End of document retransmission");
    } catch (Exception e) {
        Engine.logEngine.error("Error while trying to proxy page", e);
        throw new ServletException("Error while trying to proxy page", e);
    }
}

From source file:flex.messaging.services.http.HTTPProxyAdapter.java

private void initHttpConnectionManagerParams(HTTPConnectionManagerSettings settings) {
    connectionParams = new HttpConnectionManagerParams();
    connectionParams.setMaxTotalConnections(settings.getMaxTotalConnections());
    connectionParams.setDefaultMaxConnectionsPerHost(settings.getDefaultMaxConnectionsPerHost());

    if (!settings.getCookiePolicy().equals(CookiePolicy.DEFAULT)) {
        HttpClientParams httpClientParams = (HttpClientParams) connectionParams.getDefaults();
        httpClientParams.setCookiePolicy(settings.getCookiePolicy());
    }/*from w w  w. j a v a 2 s .  c o  m*/

    if (settings.getConnectionTimeout() >= 0)
        connectionParams.setConnectionTimeout(settings.getConnectionTimeout());

    if (settings.getSocketTimeout() >= 0)
        connectionParams.setSoTimeout(settings.getSocketTimeout());

    connectionParams.setStaleCheckingEnabled(settings.isStaleCheckingEnabled());

    if (settings.getSendBufferSize() > 0)
        connectionParams.setSendBufferSize(settings.getSendBufferSize());

    if (settings.getReceiveBufferSize() > 0)
        connectionParams.setReceiveBufferSize(settings.getReceiveBufferSize());

    connectionParams.setTcpNoDelay(settings.isTcpNoDelay());
    connectionParams.setLinger(settings.getLinger());

    if (settings.getMaxConnectionsPerHost() != null) {
        Iterator it = settings.getMaxConnectionsPerHost().iterator();
        while (it.hasNext()) {
            HostConfigurationSettings hcs = (HostConfigurationSettings) it.next();
            HostConfiguration hostConfig = new HostConfiguration();

            if (hcs.getProtocol() != null) {
                Protocol protocol = Protocol.getProtocol(hcs.getProtocol());
                hostConfig.setHost(hcs.getHost(), hcs.getPort(), protocol);
            } else if (hcs.getProtocolFactory() != null) {
                Protocol protocol = hcs.getProtocolFactory().getProtocol();
                if (hcs.getPort() > 0)
                    hostConfig.setHost(hcs.getHost(), hcs.getPort(), protocol);
                else
                    hostConfig.setHost(hcs.getHost(), protocol.getDefaultPort(), protocol);
            } else {
                if (hcs.getPort() > 0)
                    hostConfig.setHost(hcs.getHost(), hcs.getPort());
                else
                    hostConfig.setHost(hcs.getHost());
            }

            if (hcs.getVirtualHost() != null) {
                HostParams params = hostConfig.getParams();
                if (params != null)
                    params.setVirtualHost(hcs.getVirtualHost());
            }

            if (hcs.getProxyHost() != null) {
                hostConfig.setProxy(hcs.getProxyHost(), hcs.getProxyPort());
            }

            try {
                InetAddress addr = InetAddress.getByName(hcs.getLocalAddress());
                hostConfig.setLocalAddress(addr);
            } catch (UnknownHostException ex) {
            }
            connectionParams.setMaxConnectionsPerHost(hostConfig, hcs.getMaximumConnections());
        }
    }
}

From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java

/**
 * Setup proxy, based on attributes in CrawlURI and settings, 
 * in the given HostConfiguration/* w  w  w  .  j a va  2s .c  om*/
 */
private void configureProxy(CrawlURI curi, HostConfiguration config) {
    String proxy = (String) getAttributeEither(curi, ATTR_HTTP_PROXY_HOST);
    int port = -1;
    if (proxy.length() == 0) {
        proxy = null;
    } else {
        String portString = (String) getAttributeEither(curi, ATTR_HTTP_PROXY_PORT);
        port = portString.length() > 0 ? Integer.parseInt(portString) : -1;
    }
    if (proxy != null) {
        config.setProxy(proxy, port);
    }
}