Search in sources :

Example 1 with ProxyHost

use of org.apache.commons.httpclient.ProxyHost in project tdq-studio-se by Talend.

the class EcosystemProxyAdapter method adapt.

/**
 * DOC bZhou Comment method "adapt".
 *
 * @param httpclient
 * @param url
 */
public static void adapt(HttpClient httpclient, String url) {
    IProxyService proxyService = EcosPlugin.getDefault().getProxyService();
    IProxyData proxyData = null;
    try {
        IProxyData[] proxyDatas = proxyService.select(new URI(url));
        if (proxyDatas != null && proxyDatas.length > 0) {
            proxyData = proxyDatas[0];
        }
    } catch (URISyntaxException e) {
        log.error(e, e);
    }
    if (proxyData == null) {
        proxyData = proxyService.getProxyData(IProxyData.HTTP_PROXY_TYPE);
    }
    if (proxyData != null & StringUtils.isNotEmpty(proxyData.getHost())) {
        // use proxy to connect
        ProxyHost host = new ProxyHost(proxyData.getHost(), proxyData.getPort());
        httpclient.getHostConfiguration().setProxyHost(host);
        httpclient.getParams().setAuthenticationPreemptive(true);
        String userId = proxyData.getUserId();
        if (StringUtils.isNotEmpty(userId)) {
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userId, proxyData.getPassword());
            httpclient.getState().setProxyCredentials(AuthScope.ANY, credentials);
        }
    }
}
Also used : IProxyData(org.eclipse.core.net.proxy.IProxyData) URISyntaxException(java.net.URISyntaxException) ProxyHost(org.apache.commons.httpclient.ProxyHost) URI(java.net.URI) IProxyService(org.eclipse.core.net.proxy.IProxyService) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 2 with ProxyHost

use of org.apache.commons.httpclient.ProxyHost in project jaggery by wso2.

the class XMLHttpRequestHostObject method executeRequest.

private static void executeRequest(Context cx, XMLHttpRequestHostObject xhr) throws ScriptException {
    try {
        String host = xhr.method.getURI().getHost();
        ProxyHost proxyHost = getProxyConfig(host);
        xhr.httpClient.getHostConfiguration().setProxyHost(proxyHost);
        xhr.httpClient.executeMethod(xhr.method);
        xhr.statusLine = xhr.method.getStatusLine();
        xhr.responseHeaders = xhr.method.getResponseHeaders();
        updateReadyState(cx, xhr, HEADERS_RECEIVED);
        byte[] response = xhr.method.getResponseBody();
        if (response != null) {
            if (response.length > 0) {
                xhr.responseText = new String(response);
            }
        }
        Header contentType = xhr.method.getResponseHeader("Content-Type");
        if (contentType != null) {
            xhr.responseType = contentType.getValue();
        }
        updateReadyState(cx, xhr, DONE);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new ScriptException(e);
    } finally {
        xhr.method.releaseConnection();
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) Header(org.apache.commons.httpclient.Header) ProxyHost(org.apache.commons.httpclient.ProxyHost) IOException(java.io.IOException)

Example 3 with ProxyHost

use of org.apache.commons.httpclient.ProxyHost in project jaggery by wso2.

the class XMLHttpRequestHostObject method getProxyConfig.

private static ProxyHost getProxyConfig(String host) {
    ProxyHost proxyConfig = null;
    String proxyHost = System.getProperty(HTTP_PROXY_HOST);
    String proxyPortStr = System.getProperty(HTTP_PROXY_PORT);
    String nonProxyHosts = System.getProperty(HTTP_NON_PROXY_HOSTS);
    if (isHostInNonProxyList(host, nonProxyHosts)) {
        return null;
    }
    int proxyPort = -1;
    if (proxyHost != null) {
        proxyHost = proxyHost.trim();
    }
    if (proxyPortStr != null) {
        try {
            proxyPort = Integer.parseInt(proxyPortStr);
            if (!proxyHost.isEmpty()) {
                proxyConfig = new ProxyHost(proxyHost, proxyPort);
            }
        } catch (NumberFormatException e) {
            log.error(e.getMessage(), e);
        }
    }
    return proxyConfig;
}
Also used : ProxyHost(org.apache.commons.httpclient.ProxyHost)

Example 4 with ProxyHost

use of org.apache.commons.httpclient.ProxyHost in project zaproxy by zaproxy.

the class HttpSender method executeMethod.

public int executeMethod(HttpMethod method, HttpState state) throws IOException {
    int responseCode = -1;
    String hostName;
    hostName = method.getURI().getHost();
    method.setDoAuthentication(true);
    HostConfiguration hc = null;
    HttpClient requestClient;
    if (isConnectionUpgrade(method)) {
        requestClient = new HttpClient(new ZapHttpConnectionManager());
        if (param.isUseProxy(hostName)) {
            requestClient.getHostConfiguration().setProxy(param.getProxyChainName(), param.getProxyChainPort());
            setProxyAuth(requestClient);
        }
    } else if (param.isUseProxy(hostName)) {
        requestClient = clientViaProxy;
    } else {
        requestClient = client;
    }
    if (this.initiator == CHECK_FOR_UPDATES_INITIATOR) {
        // Use the 'strict' SSLConnector, i.e. one that performs all the usual cert checks
        // The 'standard' one 'trusts' everything
        // This is to ensure that all 'check-for update' calls are made to the expected https
        // urls
        // without this is would be possible to intercept and change the response which could
        // result
        // in the user downloading and installing a malicious add-on
        hc = new HostConfiguration() {

            @Override
            public synchronized void setHost(URI uri) {
                try {
                    setHost(new HttpHost(uri.getHost(), uri.getPort(), getProtocol()));
                } catch (URIException e) {
                    throw new IllegalArgumentException(e.toString());
                }
            }
        };
        hc.setHost(hostName, method.getURI().getPort(), new Protocol("https", (ProtocolSocketFactory) new SSLConnector(false), 443));
        if (param.isUseProxy(hostName)) {
            hc.setProxyHost(new ProxyHost(param.getProxyChainName(), param.getProxyChainPort()));
            setProxyAuth(requestClient);
        }
    }
    method.getParams().setBooleanParameter(HttpMethodDirector.PARAM_RESOLVE_HOSTNAME, param.shouldResolveRemoteHostname(hostName));
    // ZAP: Check if a custom state is being used
    if (state != null) {
        // Make sure cookies are enabled
        method.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        setProxyAuth(state);
    }
    responseCode = requestClient.executeMethod(hc, method, state);
    return responseCode;
}
Also used : HostConfiguration(org.apache.commons.httpclient.HostConfiguration) ZapHttpConnectionManager(org.zaproxy.zap.ZapHttpConnectionManager) URI(org.apache.commons.httpclient.URI) ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) URIException(org.apache.commons.httpclient.URIException) HttpHost(org.apache.commons.httpclient.HttpHost) HttpClient(org.apache.commons.httpclient.HttpClient) ProxyHost(org.apache.commons.httpclient.ProxyHost) Protocol(org.apache.commons.httpclient.protocol.Protocol)

Aggregations

ProxyHost (org.apache.commons.httpclient.ProxyHost)4 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Header (org.apache.commons.httpclient.Header)1 HostConfiguration (org.apache.commons.httpclient.HostConfiguration)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 HttpHost (org.apache.commons.httpclient.HttpHost)1 URI (org.apache.commons.httpclient.URI)1 URIException (org.apache.commons.httpclient.URIException)1 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)1 Protocol (org.apache.commons.httpclient.protocol.Protocol)1 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)1 IProxyData (org.eclipse.core.net.proxy.IProxyData)1 IProxyService (org.eclipse.core.net.proxy.IProxyService)1 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)1 ZapHttpConnectionManager (org.zaproxy.zap.ZapHttpConnectionManager)1