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:org.exoplatform.services.common.HttpClientImpl.java

private void setHost(String protocol, String host, int port) throws Exception {
    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams para = new HttpConnectionManagerParams();
    para.setConnectionTimeout(HTTP_TIMEOUT);
    para.setDefaultMaxConnectionsPerHost(10);
    para.setMaxTotalConnections(20);//from   www  . ja va  2 s.c  o m
    para.setStaleCheckingEnabled(true);
    manager.setParams(para);
    http = new HttpClient(manager);
    http.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
    http.getParams().setParameter("http.socket.timeout", new Integer(HTTP_TIMEOUT));
    http.getParams().setParameter("http.protocol.content-charset", "UTF-8");
    http.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    http.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    if (port < 0)
        port = 80;
    HostConfiguration hostConfig = http.getHostConfiguration();
    hostConfig.setHost(host, port, protocol);

    String proxyHost = System.getProperty("httpclient.proxy.host");
    if (proxyHost == null || proxyHost.trim().length() < 1)
        return;
    String proxyPort = System.getProperty("httpclient.proxy.port");
    hostConfig.setProxy(proxyHost, Integer.parseInt(proxyPort));

    String username = System.getProperty("httpclient.proxy.username");
    String password = System.getProperty("httpclient.proxy.password");
    String ntlmHost = System.getProperty("httpclient.proxy.ntlm.host");
    String ntlmDomain = System.getProperty("httpclient.proxy.ntlm.domain");

    Credentials ntCredentials;
    if (ntlmHost == null || ntlmDomain == null) {
        ntCredentials = new UsernamePasswordCredentials(username, password);
    } else {
        ntCredentials = new NTCredentials(username, password, ntlmHost, ntlmDomain);
    }
    http.getState().setProxyCredentials(AuthScope.ANY, ntCredentials);
}

From source file:org.fao.geonet.lib.NetLib.java

/** Setup proxy for http client
  *//*from   w  w w.jav  a  2  s  .c  o m*/
public void setupProxy(SettingManager sm, HttpClient client) {
    boolean enabled = sm.getValueAsBool(ENABLED, false);
    String host = sm.getValue(HOST);
    String port = sm.getValue(PORT);
    String username = sm.getValue(USERNAME);
    String password = sm.getValue(PASSWORD);

    if (enabled) {
        if (!Lib.type.isInteger(port)) {
            Log.error(Geonet.GEONETWORK, "Proxy port is not an integer : " + port);
        } else {
            HostConfiguration config = client.getHostConfiguration();
            if (config == null)
                config = new HostConfiguration();
            config.setProxy(host, Integer.parseInt(port));
            client.setHostConfiguration(config);

            if (username.trim().length() != 0) {
                Credentials cred = new UsernamePasswordCredentials(username, password);
                AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);

                client.getState().setProxyCredentials(scope, cred);
            }
            List authPrefs = new ArrayList(2);
            authPrefs.add(AuthPolicy.DIGEST);
            authPrefs.add(AuthPolicy.BASIC);
            // This will exclude the NTLM authentication scheme
            client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
        }
    }
}

From source file:org.geotools.data.ows.MultithreadedHttpClient.java

private void applySystemProxySettings() {
    final String proxyHost = System.getProperty("http.proxyHost");
    final int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort", "80"));
    // String nonProxyHost = System.getProperty("http.nonProxyHosts");

    if (proxyHost != null) {
        LOGGER.fine(//from  w  w  w.  j a v  a2  s  .c  o m
                "Found 'http.proxyHost' Java System property. Using it as proxy server. Port: " + proxyPort);
        HostConfiguration hostConfig = client.getHostConfiguration();
        hostConfig.setProxy(proxyHost, proxyPort);
    }

    final String proxyUser = System.getProperty("http.proxyUser");
    final String proxyPassword = System.getProperty("http.proxyPassword");
    if (proxyUser != null) {
        if (proxyPassword == null || proxyPassword.length() == 0) {
            LOGGER.warning("System property http.proxyUser provided but http.proxyPassword "
                    + "not provided or empty. Proxy auth credentials will be passed as is anyway.");
        } else {
            LOGGER.fine("System property http.proxyUser and http.proxyPassword found,"
                    + " setting proxy auth credentials");
        }
        HttpState state = client.getState();
        if (state == null) {
            state = new HttpState();
            client.setState(state);
        }
        AuthScope authscope = AuthScope.ANY;
        Credentials credentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
        state.setProxyCredentials(authscope, credentials);
    }
}

From source file:org.hubiquitus.hfacebook.publics.GetLikeFacebook.java

/**
 * Constructor//from   w w w .  ja  v a  2 s  . c om
 * @param proxyHost your proxy host or null
 * @param proxyPort your proxy port or null
 * @param pageName the name of the page e.g. "cocacola" so we will fetch data on https://graph.facebook.com/cocacola
 * @param roundTime the refresh rate in ms
 */
public GetLikeFacebook(String proxyHost, int proxyPort, String pageName, long roundTime) {
    super();

    this.roundTime = roundTime;
    url = END_POINT_LIKE_FACEBOOK_PAGE + pageName;

    if (proxyHost != null && proxyPort > 0) {
        HostConfiguration config = client.getHostConfiguration();
        config.setProxy(proxyHost, proxyPort);
        log.info("proxy used : " + proxyHost + ":" + proxyPort);
    }

    log.info("url scanned : " + url + " (every " + roundTime + " ms)");

}

From source file:org.hubiquitus.hubiquitus4j.hgoogleplus.HGooglePlus.java

/**
 * Constructor//  w w w.  jav  a2 s. co m
 * @param proxyHost your proxy host or null
 * @param proxyPort your proxy port or null
 * @param pageName the name of the page e.g. " " +cocacola" so we will fetch data on https://www.googleapis.com/plus/v1/people/ 
 * @param roundTime the refresh rate in ms
 */
public HGooglePlus(String proxyHost, int proxyPort, String pageName, long roundTime, String APIKey) {
    super();

    this.roundTime = roundTime;
    url = END_POINT_GOOGLE_PLUS_PAGE + pageName + "?key=" + APIKey;

    if (proxyHost != null && proxyPort > 0) {
        HostConfiguration config = client.getHostConfiguration();
        config.setProxy(proxyHost, proxyPort);
        log.info("proxy used : " + proxyHost + ":" + proxyPort);
    }

    log.info("url scanned : " + url + " (every " + roundTime + " ms)");

}

From source file:org.hubiquitus.hubiquitus4j.hgoogleplus.HGooglePlusActivity.java

/**
 * //from  w  ww  .  ja va 2 s.  c o m
 * @param proxyHost your proxy host or null
 * @param proxyPort your proxy port or 0
 * @param query  your tag so we will fetch data at google API activity
 * @param language  ie (fr, en, ..) null if else
 * @param maxResults number between (0 -20)
 * @param orderBy  Acceptable values are:"best" or "recent"
 * @param roundTime refresh time on milliseconds
 * @param APIKey your google API key
 */
public HGooglePlusActivity(String proxyHost, int proxyPort, String query, String language, int maxResults,
        String orderBy, long roundTime, String APIKey) {
    super();

    this.roundTime = roundTime;
    url = END_POINT_GOOGLE_PLUS_ACTIVITY + query + buildUrlOptionalParams(language, maxResults, orderBy)
            + "&key=" + APIKey;

    if (proxyHost != null && proxyPort > 0) {
        HostConfiguration config = client.getHostConfiguration();
        config.setProxy(proxyHost, proxyPort);
        log.info("proxy used : " + proxyHost + ":" + proxyPort);
    }

    log.info("url scanned : " + url + " (every " + roundTime + " ms)");

}

From source file:org.hubiquitus.hubiquitus4j.HInstagramAPI.GetInstagramTags.java

/**
 * Constructor// w ww  . j a  v a2  s . c  om
 * @param proxyHost your proxy host or null
 * @param proxyPort your proxy port or null
 * @param tag,  e.g. "cocacola" so we will fetch data on https://api.instagram.com/v1/tags/
  * @param options : full or ligth, full fetch all the data for a tag, and ligth fetches only the number of citation
  * for the given tag.
  * @param clientID is the instagram client id
 * @param roundTime the refresh rate in ms
 */
public GetInstagramTags(String proxyHost, int proxyPort, String tag, String option, String clientID,
        long roundTime) {
    super();

    this.roundTime = roundTime;
    url = END_POINT_TAGS_INSTAGRAM + buildParamsURL(tag, clientID, option);

    if (proxyHost != null && proxyPort > 0) {
        HostConfiguration config = client.getHostConfiguration();
        config.setProxy(proxyHost, proxyPort);
        log.info("proxy used : " + proxyHost + ":" + proxyPort);
    }

    log.info("url scanned : " + url + " (every " + roundTime + " ms)");

}

From source file:org.jivesoftware.openfire.update.UpdateManager.java

/**
 * Queries the igniterealtime.org server for new server and plugin updates.
 *
 * @param notificationsEnabled true if admins will be notified when new updates are found.
 * @throws Exception if some error happens during the query.
 *//*www.j av a2s. c  o  m*/
public synchronized void checkForServerUpdate(boolean notificationsEnabled) throws Exception {
    // Get the XML request to include in the HTTP request
    String requestXML = getServerUpdateRequest();
    // Send the request to the server
    HttpClient httpClient = new HttpClient();
    // Check if a proxy should be used
    if (isUsingProxy()) {
        HostConfiguration hc = new HostConfiguration();
        hc.setProxy(getProxyHost(), getProxyPort());
        httpClient.setHostConfiguration(hc);
    }
    PostMethod postMethod = new PostMethod(updateServiceURL);
    NameValuePair[] data = { new NameValuePair("type", "update"), new NameValuePair("query", requestXML) };
    postMethod.setRequestBody(data);
    if (httpClient.executeMethod(postMethod) == 200) {
        // Process answer from the server
        String responseBody = postMethod.getResponseBodyAsString();
        processServerUpdateResponse(responseBody, notificationsEnabled);
    }
}

From source file:org.jivesoftware.openfire.update.UpdateManager.java

public synchronized void checkForPluginsUpdates(boolean notificationsEnabled) throws Exception {
    // Get the XML request to include in the HTTP request
    String requestXML = getAvailablePluginsUpdateRequest();
    // Send the request to the server
    HttpClient httpClient = new HttpClient();
    // Check if a proxy should be used
    if (isUsingProxy()) {
        HostConfiguration hc = new HostConfiguration();
        hc.setProxy(getProxyHost(), getProxyPort());
        httpClient.setHostConfiguration(hc);
    }/*from w  w  w.  ja v a2 s .c  o m*/
    PostMethod postMethod = new PostMethod(updateServiceURL);
    NameValuePair[] data = { new NameValuePair("type", "available"), new NameValuePair("query", requestXML) };
    postMethod.setRequestBody(data);
    if (httpClient.executeMethod(postMethod) == 200) {
        // Process answer from the server
        String responseBody = postMethod.getResponseBodyAsString();
        processAvailablePluginsResponse(responseBody, notificationsEnabled);
    }
}

From source file:org.jivesoftware.openfire.update.UpdateManager.java

/**
 * Download and install latest version of plugin.
 *
 * @param url the URL of the latest version of the plugin.
 * @return true if the plugin was successfully downloaded and installed.
 *//*  www . ja va2 s .  c om*/
public boolean downloadPlugin(String url) {
    boolean installed = false;
    // Download and install new version of plugin
    HttpClient httpClient = new HttpClient();
    // Check if a proxy should be used
    if (isUsingProxy()) {
        HostConfiguration hc = new HostConfiguration();
        hc.setProxy(getProxyHost(), getProxyPort());
        httpClient.setHostConfiguration(hc);
    }
    GetMethod getMethod = new GetMethod(url);
    //execute the method
    try {
        int statusCode = httpClient.executeMethod(getMethod);
        if (statusCode == 200) {
            //get the resonse as an InputStream
            InputStream in = getMethod.getResponseBodyAsStream();
            String pluginFilename = url.substring(url.lastIndexOf("/") + 1);
            installed = XMPPServer.getInstance().getPluginManager().installPlugin(in, pluginFilename);
            in.close();
            if (installed) {
                // Remove the plugin from the list of plugins to update
                for (Update update : pluginUpdates) {
                    if (update.getURL().equals(url)) {
                        update.setDownloaded(true);
                    }
                }
                // Save response in a file for later retrieval
                saveLatestServerInfo();
            }
        }
    } catch (IOException e) {
        Log.warn("Error downloading new plugin version", e);
    }
    return installed;
}