Example usage for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT

List of usage examples for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT.

Prototype

String USER_AGENT

To view the source code for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT.

Click Source Link

Usage

From source file:davmail.caldav.TestCaldav.java

public void testPropfindAddressBook() throws IOException, DavException {
    DavPropertyNameSet davPropertyNameSet = new DavPropertyNameSet();
    //davPropertyNameSet.add(DavPropertyName.create("getctag", Namespace.getNamespace("http://calendarserver.org/ns/")));
    davPropertyNameSet.add(DavPropertyName.create("getetag", Namespace.getNamespace("DAV:")));
    PropFindMethod method = new PropFindMethod("/users/" + session.getEmail() + "/addressbook/",
            davPropertyNameSet, 1);//from w w  w .  j  a  v a  2 s  . co  m
    httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT,
            "Address%20Book/883 CFNetwork/454.12.4 Darwin/10.8.0 (i386) (MacBookPro3%2C1)");
    httpClient.executeMethod(method);
    assertEquals(HttpStatus.SC_MULTI_STATUS, method.getStatusCode());
    method.getResponseBodyAsMultiStatus();
}

From source file:com.eucalyptus.webui.server.ConfigurationWebBackend.java

private static String getExternalIpAddress() {
    String ipAddr = null;//  w w  w .j a  va 2 s  .  c  o m
    HttpClient httpClient = new HttpClient();

    //set User-Agent
    String clientVersion = (String) httpClient.getParams().getDefaults()
            .getParameter(HttpMethodParams.USER_AGENT);
    String javaVersion = System.getProperty("java.version");
    String osName = System.getProperty("os.name");
    String osArch = System.getProperty("os.arch");
    String eucaVersion = System.getProperty("euca.version");
    String extraVersion = System.getProperty("euca.extra_version");

    // Jakarta Commons-HttpClient/3.1 (java 1.6.0_24; Linux amd64) Eucalyptus/3.1.0-1.el6
    String userAgent = clientVersion + " (java " + javaVersion + "; " + osName + " " + osArch + ") Eucalyptus/"
            + eucaVersion;
    if (extraVersion != null) {
        userAgent = userAgent + "-" + extraVersion;
    }

    httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);

    //support for http proxy
    if (HttpServerBootstrapper.httpProxyHost != null && (HttpServerBootstrapper.httpProxyHost.length() > 0)) {
        String proxyHost = HttpServerBootstrapper.httpProxyHost;
        if (HttpServerBootstrapper.httpProxyPort != null
                && (HttpServerBootstrapper.httpProxyPort.length() > 0)) {
            int proxyPort = Integer.parseInt(HttpServerBootstrapper.httpProxyPort);
            httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
        } else {
            httpClient.getHostConfiguration().setProxyHost(new ProxyHost(proxyHost));
        }
    }
    // Use Rightscale's "whoami" service
    String whoamiUrl = WebProperties.getProperty(WebProperties.RIGHTSCALE_WHOAMI_URL,
            WebProperties.RIGHTSCALE_WHOAMI_URL_DEFAULT);
    GetMethod method = new GetMethod(whoamiUrl);
    Integer timeoutMs = new Integer(3 * 1000); // TODO: is this working?
    method.getParams().setSoTimeout(timeoutMs);

    try {
        httpClient.executeMethod(method);
        String str = "";
        InputStream in = method.getResponseBodyAsStream();
        byte[] readBytes = new byte[1024];
        int bytesRead = -1;
        while ((bytesRead = in.read(readBytes)) > 0) {
            str += new String(readBytes, 0, bytesRead);
        }
        Matcher matcher = Pattern.compile(".*your ip is (.*)").matcher(str);
        if (matcher.find()) {
            ipAddr = matcher.group(1);
        }

    } catch (MalformedURLException e) {
        LOG.warn("Malformed URL exception: " + e.getMessage());
        LOG.debug(e, e);
    } catch (IOException e) {
        LOG.warn("I/O exception: " + e.getMessage());
        LOG.debug(e, e);
    } finally {
        method.releaseConnection();
    }

    return ipAddr;
}

From source file:com.amazonaws.a2s.AmazonA2SClient.java

/**
 * Configure HttpClient with set of defaults as well as configuration
 * from AmazonA2SConfig instance/* w w w . ja  va2s.co m*/
 *
 */
private HttpClient configureHttpClient() {

    /* Set http client parameters */
    HttpClientParams httpClientParams = new HttpClientParams();
    httpClientParams.setParameter(HttpMethodParams.USER_AGENT, config.getUserAgent());
    httpClientParams.setParameter(HttpClientParams.RETRY_HANDLER, new HttpMethodRetryHandler() {
        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (executionCount > 3) {
                log.debug("Maximum Number of Retry attempts reached, will not retry");
                return false;
            }
            log.debug("Retrying request. Attempt " + executionCount);
            if (exception instanceof NoHttpResponseException) {
                log.debug("Retrying on NoHttpResponseException");
                return true;
            }
            if (!method.isRequestSent()) {
                log.debug("Retrying on failed sent request");
                return true;
            }
            return false;
        }
    });

    /* Set host configuration */
    HostConfiguration hostConfiguration = new HostConfiguration();

    /* Set connection manager parameters */
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    connectionManagerParams.setConnectionTimeout(50000);
    connectionManagerParams.setSoTimeout(50000);
    connectionManagerParams.setStaleCheckingEnabled(true);
    connectionManagerParams.setTcpNoDelay(true);
    connectionManagerParams.setMaxTotalConnections(3);
    connectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, 3);

    /* Set connection manager */
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);

    /* Set http client */
    httpClient = new HttpClient(httpClientParams, connectionManager);

    /* Set proxy if configured */
    if (config.isSetProxyHost() && config.isSetProxyPort()) {
        log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + "Proxy Port: "
                + config.getProxyPort());
        hostConfiguration.setProxy(config.getProxyHost(), config.getProxyPort());

    }

    httpClient.setHostConfiguration(hostConfiguration);
    return httpClient;
}

From source file:net.ushkinaz.storm8.http.GameRequestor.java

@Override
protected void initHttpClient(HttpClient httpClient) {
    super.initHttpClient(httpClient); //To change body of overridden methods use File | Settings | File Templates.
    HttpState initialState = new HttpState();

    for (Map.Entry<String, String> cookieEntry : player.getCookies().entrySet()) {
        Cookie cookie = new Cookie(player.getGame().getDomain(), cookieEntry.getKey(), cookieEntry.getValue(),
                "/", null, false);
        initialState.addCookie(cookie);/*from  w w w.jav  a 2 s .  com*/
    }

    httpClient.setState(initialState);
    httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
}

From source file:net.ushkinaz.storm8.http.HttpService.java

protected void initHttpClient(HttpClient httpClient) {
    httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT,
            "Mozilla/5.0 (Linux; U; Android 2.1; ru-ru; HTC Legend Build/ERD79) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
}

From source file:org.andrewberman.sync.InheritMe.java

void init() throws Exception {
    httpclient = new HttpClient();
    //      MultiThreadedHttpConnectionManager hcm = new MultiThreadedHttpConnectionManager();
    //      httpclient.setHttpConnectionManager(hcm);
    httpclient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    httpclient.getParams().setBooleanParameter("http.protocol.single-cookie-header", true);
    //      httpclient.getParams().setIntParameter("http.socket.timeout", 20000);
    Header h = new Header("Connection", "close");
    ArrayList<Header> headers = new ArrayList<Header>();
    headers.add(h);// w  ww  .  ja  va 2 s .  c o m
    //       httpclient.getParams().setParameter("http.default-headers", headers);
    httpclient.getParams().setParameter(HttpMethodParams.USER_AGENT, "SyncUThink " + this.getVersionString());

    String proxyHost = null;
    int proxyPort = -1;
    try {
        proxyHost = System.getProperty("https.proxyHost");
        proxyPort = Integer.parseInt(System.getProperty("https.proxyPort"));
        if (proxyHost == null || proxyPort < 0)
            throw new Exception();
    } catch (Exception e) {
        proxyHost = null;
        proxyPort = -1;
        try {
            proxyHost = System.getProperty("http.proxyHost");
            proxyPort = Integer.parseInt(System.getProperty("http.proxyPort"));
        } catch (Exception e1) {
            proxyHost = null;
            proxyPort = -1;
        }
    }
    if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
        status("Found proxy: " + proxyHost + ":" + proxyPort);
        httpclient.getHostConfiguration().setProxy(proxyHost, proxyPort);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.apache.ivy.util.url.HttpClientHandler.java

private HttpClient getClient() {
    if (httpClient == null) {
        final MultiThreadedHttpConnectionManager connManager = new MultiThreadedHttpConnectionManager();
        httpClient = new HttpClient(connManager);

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            public void run() {
                connManager.shutdown();/*from   w w  w .  j ava2 s  . com*/
            }
        }));

        List authPrefs = new ArrayList(3);
        authPrefs.add(AuthPolicy.DIGEST);
        authPrefs.add(AuthPolicy.BASIC);
        authPrefs.add(AuthPolicy.NTLM); // put it at the end to give less priority (IVY-213)
        httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

        if (useProxy()) {
            httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
            if (useProxyAuthentication()) {
                httpClient.getState().setProxyCredentials(
                        new AuthScope(proxyHost, proxyPort, AuthScope.ANY_REALM),
                        createCredentials(proxyUserName, proxyPasswd));
            }
        }

        // user-agent
        httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, getUserAgent());

        // authentication
        httpClient.getParams().setParameter(CredentialsProvider.PROVIDER, new IvyCredentialsProvider());
    }

    return httpClient;
}

From source file:org.apache.maven.doxia.linkcheck.validation.OnlineHTTPLinkValidator.java

/** {@inheritDoc} */
public LinkValidationResult validateLink(LinkValidationItem lvi) {
    if (this.cl == null) {
        initHttpClient();// ww w  . j a v a  2  s. c o m
    }

    if (this.http.getHttpClientParameters() != null) {
        for (Map.Entry<Object, Object> entry : this.http.getHttpClientParameters().entrySet()) {
            if (entry.getValue() != null) {
                System.setProperty(entry.getKey().toString(), entry.getValue().toString());
            }
        }
    }

    // Some web servers don't allow the default user-agent sent by httpClient
    System.setProperty(HttpMethodParams.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    this.cl.getParams().setParameter(HttpMethodParams.USER_AGENT,
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");

    String link = lvi.getLink();
    String anchor = "";
    int idx = link.indexOf('#');
    if (idx != -1) {
        anchor = link.substring(idx + 1);
        link = link.substring(0, idx);
    }

    try {
        if (link.startsWith("/")) {
            if (getBaseURL() == null) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn("Cannot check link [" + link + "] in page [" + lvi.getSource()
                            + "], as no base URL has been set!");
                }

                return new LinkValidationResult(LinkcheckFileResult.WARNING_LEVEL, false,
                        "No base URL specified");
            }

            link = getBaseURL() + link;
        }

        HttpMethod hm = null;
        try {
            hm = checkLink(link, 0);
        } catch (Throwable t) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Received: [" + t + "] for [" + link + "] in page [" + lvi.getSource() + "]", t);
            }

            return new LinkValidationResult(LinkcheckFileResult.ERROR_LEVEL, false,
                    t.getClass().getName() + " : " + t.getMessage());
        }

        if (hm == null) {
            return new LinkValidationResult(LinkcheckFileResult.ERROR_LEVEL, false,
                    "Cannot retreive HTTP Status");
        }

        if (hm.getStatusCode() == HttpStatus.SC_OK) {
            // lets check if the anchor is present
            if (anchor.length() > 0) {
                String content = hm.getResponseBodyAsString();

                if (!Anchors.matchesAnchor(content, anchor)) {
                    return new HTTPLinkValidationResult(LinkcheckFileResult.VALID_LEVEL, false,
                            "Missing anchor '" + anchor + "'");
                }
            }
            return new HTTPLinkValidationResult(LinkcheckFileResult.VALID_LEVEL, true, hm.getStatusCode(),
                    hm.getStatusText());
        }

        String msg = "Received: [" + hm.getStatusCode() + "] for [" + link + "] in page [" + lvi.getSource()
                + "]";
        // If there's a redirection ... add a warning
        if (hm.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY
                || hm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY
                || hm.getStatusCode() == HttpStatus.SC_TEMPORARY_REDIRECT) {
            LOG.warn(msg);

            return new HTTPLinkValidationResult(LinkcheckFileResult.WARNING_LEVEL, true, hm.getStatusCode(),
                    hm.getStatusText());
        }

        LOG.debug(msg);

        return new HTTPLinkValidationResult(LinkcheckFileResult.ERROR_LEVEL, false, hm.getStatusCode(),
                hm.getStatusText());
    } catch (Throwable t) {
        String msg = "Received: [" + t + "] for [" + link + "] in page [" + lvi.getSource() + "]";
        if (LOG.isDebugEnabled()) {
            LOG.debug(msg, t);
        } else {
            LOG.error(msg);
        }

        return new LinkValidationResult(LinkcheckFileResult.ERROR_LEVEL, false, t.getMessage());
    } finally {
        System.getProperties().remove(HttpMethodParams.USER_AGENT);

        if (this.http.getHttpClientParameters() != null) {
            for (Map.Entry<Object, Object> entry : this.http.getHttpClientParameters().entrySet()) {
                if (entry.getValue() != null) {
                    System.getProperties().remove(entry.getKey().toString());
                }
            }
        }
    }
}

From source file:org.apache.maven.plugin.doap.DoapUtil.java

/**
 * Fetch an URL//from   w  w w .jav  a  2  s.  c  o m
 *
 * @param settings the user settings used to fetch the url with an active proxy, if defined.
 * @param url the url to fetch
 * @throws IOException if any
 * @see #DEFAULT_TIMEOUT
 * @since 1.1
 */
public static void fetchURL(Settings settings, URL url) throws IOException {
    if (url == null) {
        throw new IllegalArgumentException("The url is null");
    }

    if ("file".equals(url.getProtocol())) {
        InputStream in = null;
        try {
            in = url.openStream();
        } finally {
            IOUtil.close(in);
        }

        return;
    }

    // http, https...
    HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_TIMEOUT);
    httpClient.getHttpConnectionManager().getParams().setSoTimeout(DEFAULT_TIMEOUT);
    httpClient.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);

    // Some web servers don't allow the default user-agent sent by httpClient
    httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT,
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");

    if (settings != null && settings.getActiveProxy() != null) {
        Proxy activeProxy = settings.getActiveProxy();

        ProxyInfo proxyInfo = new ProxyInfo();
        proxyInfo.setNonProxyHosts(activeProxy.getNonProxyHosts());

        if (StringUtils.isNotEmpty(activeProxy.getHost())
                && !ProxyUtils.validateNonProxyHosts(proxyInfo, url.getHost())) {
            httpClient.getHostConfiguration().setProxy(activeProxy.getHost(), activeProxy.getPort());

            if (StringUtils.isNotEmpty(activeProxy.getUsername()) && activeProxy.getPassword() != null) {
                Credentials credentials = new UsernamePasswordCredentials(activeProxy.getUsername(),
                        activeProxy.getPassword());

                httpClient.getState().setProxyCredentials(AuthScope.ANY, credentials);
            }
        }
    }

    GetMethod getMethod = new GetMethod(url.toString());
    try {
        int status;
        try {
            status = httpClient.executeMethod(getMethod);
        } catch (SocketTimeoutException e) {
            // could be a sporadic failure, one more retry before we give up
            status = httpClient.executeMethod(getMethod);
        }

        if (status != HttpStatus.SC_OK) {
            throw new FileNotFoundException(url.toString());
        }
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.maven.plugin.docck.AbstractCheckDocumentationMojo.java

protected AbstractCheckDocumentationMojo() {
    String httpUserAgent = "maven-docck-plugin/1.x" + " (Java " + System.getProperty("java.version") + "; "
            + System.getProperty("os.name") + " " + System.getProperty("os.version") + ")";

    httpClient = new HttpClient();

    final int connectionTimeout = 5000;
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
    httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, httpUserAgent);
}