Example usage for org.apache.commons.httpclient HttpClient getParams

List of usage examples for org.apache.commons.httpclient HttpClient getParams

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpClient getParams.

Prototype

public HttpClientParams getParams() 

Source Link

Usage

From source file:com.rometools.fetcher.impl.HttpClientFeedFetcher.java

@Override
public SyndFeed retrieveFeed(final String userAgent, final URL feedUrl)
        throws IllegalArgumentException, IOException, FeedException, FetcherException {

    if (feedUrl == null) {
        throw new IllegalArgumentException("null is not a valid URL");
    }/*w w w. j  av  a2  s.c o  m*/

    final HttpClient client = new HttpClient(httpClientParams);

    if (credentialSupplier != null) {

        final HttpClientParams params = client.getParams();
        params.setAuthenticationPreemptive(true);

        final String host = feedUrl.getHost();
        final Credentials credentials = credentialSupplier.getCredentials(null, host);
        if (credentials != null) {
            final AuthScope authScope = new AuthScope(host, -1);
            final HttpState state = client.getState();
            state.setCredentials(authScope, credentials);
        }

    }

    System.setProperty("httpclient.useragent", userAgent);

    final String urlStr = feedUrl.toString();
    final HttpMethod method = new GetMethod(urlStr);

    if (customRequestHeaders == null) {
        method.addRequestHeader("Accept-Encoding", "gzip");
        method.addRequestHeader("User-Agent", userAgent);

    } else {
        for (final Map.Entry<String, String> entry : customRequestHeaders.entrySet()) {
            method.addRequestHeader(entry.getKey(), entry.getValue());
        }
        if (!customRequestHeaders.containsKey("Accept-Encoding")) {
            method.addRequestHeader("Accept-Encoding", "gzip");
        }
        if (!customRequestHeaders.containsKey("User-Agent")) {
            method.addRequestHeader("User-Agent", userAgent);
        }
    }

    method.setFollowRedirects(true);

    if (httpClientMethodCallback != null) {
        synchronized (httpClientMethodCallback) {
            httpClientMethodCallback.afterHttpClientMethodCreate(method);
        }
    }

    final FeedFetcherCache cache = getFeedInfoCache();

    if (cache != null) {
        // retrieve feed
        try {

            if (isUsingDeltaEncoding()) {
                method.setRequestHeader("A-IM", "feed");
            }

            // try to get the feed info from the cache
            SyndFeedInfo syndFeedInfo = cache.getFeedInfo(feedUrl);

            if (syndFeedInfo != null) {

                method.setRequestHeader("If-None-Match", syndFeedInfo.getETag());

                final Object lastModifiedHeader = syndFeedInfo.getLastModified();
                if (lastModifiedHeader instanceof String) {
                    method.setRequestHeader("If-Modified-Since", (String) lastModifiedHeader);
                }

            }

            final int statusCode = client.executeMethod(method);
            fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr);
            handleErrorCodes(statusCode);

            SyndFeed feed = getFeed(syndFeedInfo, urlStr, method, statusCode);

            syndFeedInfo = buildSyndFeedInfo(feedUrl, urlStr, method, feed, statusCode);

            cache.setFeedInfo(feedUrl, syndFeedInfo);

            // the feed may have been modified to pick up cached values
            // (eg - for delta encoding)
            feed = syndFeedInfo.getSyndFeed();

            return feed;

        } finally {

            method.releaseConnection();

        }

    } else {

        // cache is not in use
        try {

            final int statusCode = client.executeMethod(method);
            fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr);
            handleErrorCodes(statusCode);

            return getFeed(null, urlStr, method, statusCode);

        } finally {

            method.releaseConnection();

        }

    }

}

From source file:com.liferay.portal.search.solr.server.BasicAuthSolrServer.java

public BasicAuthSolrServer(AuthScope authScope, String username, String password, String url)
        throws MalformedURLException {

    _username = username;/*from   w w  w  .j ava 2  s  . co m*/
    _password = password;

    _multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();

    HttpClient httpClient = new HttpClient(_multiThreadedHttpConnectionManager);

    if ((_username != null) && (_password != null)) {
        if (authScope == null) {
            authScope = AuthScope.ANY;
        }

        HttpState httpState = httpClient.getState();

        httpState.setCredentials(authScope, new UsernamePasswordCredentials(_username, _password));

        HttpClientParams httpClientParams = httpClient.getParams();

        httpClientParams.setAuthenticationPreemptive(true);
    }

    _server = new CommonsHttpSolrServer(url, httpClient);
}

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

protected HttpMethodBase invoke(URL targetURL, String userName, String password) throws URIException {
    HttpMethodBase method = null;/* w  ww. ja v a  2  s  .c o m*/
    if (log.isDebugEnabled())
        log.debug(targetURL.toExternalForm());
    method = new GetMethod(targetURL.toExternalForm());
    HttpClient httpClient = getNewHttpClient();
    if (userName != null) {
        // Credentials for destination URL
        Credentials cred = new UsernamePasswordCredentials(userName, password);
        httpClient.getParams().setAuthenticationPreemptive(true);
        httpClient.getState().setCredentials(AUTHSCOPE_ANY, cred);
    }
    setHostConfiguration(httpClient, targetURL);
    String returnMessage = null;
    int returnCode = 500;
    try {
        returnCode = httpClient.executeMethod(method);
    } catch (InvalidCredentialsException exe) {
        if (log.isInfoEnabled())
            log.info("Invalid credentials trying to authenticate: " + exe.getMessage());
    } catch (HttpException exe) {
        log.error("while connection to: " + targetURL.getHost() + " an unknown error occured (HttpException): "
                + exe.getMessage());
    } catch (SSLPeerUnverifiedException exe) {
        returnCode = 516;
        returnMessage = exe.getMessage();
    } catch (IOException exe) {
        log.error("while connection to: " + targetURL.getHost() + " using Proxy: " + this.isUsingProxy()
                + " host: " + this.getHttpProxyHost() + " on Port: " + this.httpProxyPort + " together: "
                + this.getProxyServer() + " an unknown error occured (IOException): "
                + targetURL.toExternalForm() + " " + exe.getMessage());
    } catch (Exception exe) {
        log.error("while connection to: " + targetURL.getHost() + " an unknown error occured: "
                + exe.getMessage());
    }

    if ((returnCode > 199) && (returnCode < 300)) {
        // return is OK - so fall through
        if (log.isDebugEnabled())
            log.debug("good return code: " + returnCode);
    } else if (returnCode == 401) {
        returnMessage = HttpMessages.getString("HttpClientWrapper.401_authRequired");
    } else if (returnCode == 404) {
        returnMessage = HttpMessages.getString("HttpClientWrapper.404_notFound");
    } else if (returnCode == 407) {
        returnMessage = HttpMessages.getString("HttpClientWrapper.407_proxyAuthRequired");
    } else if (returnCode == 403) {
        returnMessage = HttpMessages.getString("HttpClientWrapper.403_Forbidden");
    } else if (returnCode == 503) {
        returnMessage = HttpMessages.getString("HttpClientWrapper.503_ServiceUnavailable");
    } else if (returnCode == 504) {
        returnMessage = HttpMessages.getString("HttpClientWrapper.504_ProxyTimeout");
    } else if (returnCode == 516) {
        returnMessage = HttpMessages.getString("HttpClientWrapper.516_SSLPeerUnverified", returnMessage);
    } else {
        returnMessage = "Unknown error with return code " + returnCode;
    }
    if (returnMessage != null) {
        throw new URIException(returnCode, returnMessage);
    }
    return method;
}

From source file:it.geosolutions.geofence.gui.server.service.impl.InstancesManagerServiceImpl.java

private void setAuth(HttpClient client, String url, String username, String pw) throws MalformedURLException {
    URL u = new URL(url);
    if (username != null && pw != null) {
        Credentials defaultcreds = new UsernamePasswordCredentials(username, pw);
        client.getState().setCredentials(new AuthScope(u.getHost(), u.getPort()), defaultcreds);
        client.getParams().setAuthenticationPreemptive(true); // GS2 by default always requires authentication
    } else {/* w  w  w. java 2  s  .  co  m*/
        if (logger.isDebugEnabled()) {
            logger.debug("Not setting credentials to access to " + url);
        }
    }
}

From source file:com.htmlhifive.tools.jslint.engine.download.AbstractDownloadEngineSupport.java

/**
 * ?url????eclipse?????HttpClient??.<br>
 * ??//from   w  ww.  jav  a2s  .c  om
 * {@link AbstractDownloadEngineSupport#getConnectionTimeout()}.<br>
 * ? {@link AbstractDownloadEngineSupport#getRetryTimes()}
 * 
 * @param url URL
 * @return HttpClient
 */
private HttpClient createHttpClient(String url) {
    ServiceTracker<IProxyService, IProxyService> proxyTracker = new ServiceTracker<IProxyService, IProxyService>(
            JSLintPlugin.getDefault().getBundle().getBundleContext(), IProxyService.class, null);
    boolean useProxy = false;
    String proxyHost = null;
    int proxyPort = 0;
    String userId = null;
    String password = null;
    try {
        proxyTracker.open();
        IProxyService service = proxyTracker.getService();
        IProxyData[] datas = service.select(new URI(url));
        for (IProxyData proxyData : datas) {
            if (proxyData.getHost() != null) {
                useProxy = true;
                proxyHost = proxyData.getHost();
                proxyPort = proxyData.getPort();
                userId = proxyData.getUserId();
                password = proxyData.getPassword();
            }
        }
    } catch (URISyntaxException e) {
        throw new RuntimeException(Messages.EM0100.getText(), e);
    } finally {
        proxyTracker.close();
    }
    HttpClient client = new HttpClient();
    if (useProxy) {
        client.getHostConfiguration().setProxy(proxyHost, proxyPort);
        if (StringUtils.isNotEmpty(userId)) {
            // ?????
            client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort, "realm"),
                    new UsernamePasswordCredentials(userId, password));
        }
    }
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(getRetryTimes(), true));
    client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, getConnectionTimeout());
    return client;
}

From source file:com.aptana.jira.core.JiraManager.java

/**
 * Adds an attachment to a JIRA ticket./*  ww w  .  j a  va  2  s  . c o m*/
 * 
 * @param path
 *            the path of the file to be attached
 * @param issue
 *            the JIRA ticket
 * @throws JiraException
 */
public void addAttachment(IPath path, JiraIssue issue) throws JiraException {
    if (path == null || issue == null) {
        return;
    }
    if (user == null) {
        throw new JiraException(Messages.JiraManager_ERR_NotLoggedIn);
    }

    // Use Apache HTTPClient to POST the file
    HttpClient httpclient = new HttpClient();
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user.getUsername(), user.getPassword());
    httpclient.getState().setCredentials(new AuthScope(HOST_NAME, 443), creds);
    httpclient.getParams().setAuthenticationPreemptive(true);
    PostMethod filePost = null;
    try {
        filePost = new PostMethod(createAttachmentURL(issue));
        File file = path.toFile();
        // MUST USE "file" AS THE NAME!!!
        Part[] parts = { new FilePart("file", file) }; //$NON-NLS-1$
        filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
        filePost.setContentChunked(true);
        filePost.setDoAuthentication(true);
        // Special header to tell JIRA not to do XSFR checking
        filePost.setRequestHeader("X-Atlassian-Token", "nocheck"); //$NON-NLS-1$ //$NON-NLS-2$

        int responseCode = httpclient.executeMethod(filePost);
        if (responseCode != HttpURLConnection.HTTP_OK && responseCode != HttpURLConnection.HTTP_CREATED) {
            // TODO This is a JSON response that we should parse out "errorMessages" value(s) (its an array of
            // strings).
            throw new JiraException(filePost.getResponseBodyAsString());
        }
        String json = filePost.getResponseBodyAsString();
        IdeLog.logInfo(JiraCorePlugin.getDefault(), json);
    } catch (JiraException e) {
        throw e;
    } catch (Exception e) {
        throw new JiraException(e.getMessage(), e);
    } finally {
        if (filePost != null) {
            filePost.releaseConnection();
        }
    }
}

From source file:com.sforce.cd.apexUnit.client.codeCoverage.WebServiceInvoker.java

public HashMap<String, String> doPost(String relativeServiceURL) {

    PostMethod post = null;//from   w  ww  . jav a  2 s.c om
    HttpClient httpclient = new HttpClient();
    String requestString = "";
    HashMap<String, String> responseMap = new HashMap<String, String>();

    try {
        // the client id and secret is applicable across all dev orgs
        requestString = generateRequestString();
        String authorizationServerURL = CommandLineArguments.getOrgUrl() + relativeServiceURL;

        httpclient.getParams().setSoTimeout(0);
        post = new PostMethod(authorizationServerURL);
        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        post.addRequestHeader("X-PrettyPrint", "1");
        post.setRequestEntity(
                new StringRequestEntity(requestString, "application/x-www-form-urlencoded", "UTF-8"));
        httpclient.executeMethod(post);

        Gson json = new Gson();
        // obtain the result map from the response body and get the access
        // token
        responseMap = json.fromJson(post.getResponseBodyAsString(), new TypeToken<HashMap<String, String>>() {
        }.getType());

    } catch (Exception ex) {
        ApexUnitUtils.shutDownWithDebugLog(ex, "Exception during post method: " + ex);
        if (LOG.isDebugEnabled()) {
            ex.printStackTrace();
        }
    } finally {
        post.releaseConnection();
    }

    return responseMap;

}

From source file:com.eucalyptus.imaging.backend.ImagingTaskStateManager.java

private boolean doesManifestExist(final String manifestUrl) throws Exception {
    // validate urls per EUCA-9144
    final UrlValidator urlValidator = new UrlValidator();
    if (!urlValidator.isEucalyptusUrl(manifestUrl))
        throw new RuntimeException("Manifest's URL is not in the Eucalyptus format: " + manifestUrl);
    final HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
    client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000);
    client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 30000);
    GetMethod method = new GetMethod(manifestUrl);
    String manifest = null;//from www  .  ja v a2 s.c  om
    try {
        // avoid TCP's CLOSE_WAIT  
        method.setRequestHeader("Connection", "close");
        client.executeMethod(method);
        manifest = method.getResponseBodyAsString(ImageConfiguration.getInstance().getMaxManifestSizeBytes());
        if (manifest == null) {
            return false;
        } else if (manifest.contains("<Code>NoSuchKey</Code>")
                || manifest.contains("The specified key does not exist")) {
            return false;
        }
    } catch (final Exception ex) {
        return false;
    } finally {
        method.releaseConnection();
    }
    final List<String> partsUrls = getPartsHeadUrl(manifest);
    for (final String url : partsUrls) {
        if (!urlValidator.isEucalyptusUrl(url))
            throw new RuntimeException("Manifest's URL is not in the Eucalyptus format: " + url);
        HeadMethod partCheck = new HeadMethod(url);
        int res = client.executeMethod(partCheck);
        if (res != HttpStatus.SC_OK) {
            return false;
        }
    }
    return true;
}

From source file:com.tasktop.c2c.server.common.service.http.MultiUserClientHttpRequestFactory.java

protected void setCredentials(HttpClient httpClient, Authentication authentication) {
    UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
    String username = token.getName();
    String password = token.getCredentials() == null ? "" : token.getCredentials().toString();

    // FIXME: review, should probably not be AuthScope.ANY
    Credentials credentials = new UsernamePasswordCredentials(username, password);
    Object auth = httpClient.getParams().getParameter(PARAM_AUTH_KEY);
    if (auth == null || !auth.equals(credentials)) {
        clearAuthState(httpClient);// www . j  av a 2 s  .  c o m

        httpClient.getParams().setParameter(PARAM_AUTH_KEY, credentials);
        httpClient.getState().setCredentials(AuthScope.ANY, credentials);
    }

    // this seems to be necessary for correct operation.
    // it seems that with keepalives a response to an auth challenge is issued
    // without first reading the whole response, making the 2nd request read the wrong
    // data for the HTTP status line.
    httpClient.getParams().setAuthenticationPreemptive(authenticationPreemptive);
    if (cookiePolicy != null) {
        httpClient.getParams().setCookiePolicy(cookiePolicy);
    }
}

From source file:com.googlecode.fascinator.indexer.SolrWrapperQueueConsumer.java

/**
 * Initialize a Solr core object.//from  ww  w.j  ava  2s .com
 * 
 * @param coreName : The core to initialize
 * @return SolrServer : The initialized core
 */
private SolrServer initCore(String core) {
    boolean isEmbedded = globalConfig.getBoolean(false, "indexer", core, "embedded");
    try {
        // Embedded Solr
        if (isEmbedded) {
            // Solr over HTTP - Needed to run commits
            // so the core web server sees them.
            String uri = globalConfig.getString(null, "indexer", core, "uri");
            if (uri == null) {
                log.error("No URI provided for core: '{}'", core);
                return null;
            }
            URI solrUri = new URI(uri);
            commit = new CommonsHttpSolrServer(solrUri.toURL());
            username = globalConfig.getString(null, "indexer", core, "username");
            password = globalConfig.getString(null, "indexer", core, "password");
            if (username != null && password != null) {
                UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
                HttpClient hc = ((CommonsHttpSolrServer) solr).getHttpClient();
                hc.getParams().setAuthenticationPreemptive(true);
                hc.getState().setCredentials(AuthScope.ANY, credentials);
            }

            // First time execution
            if (coreContainer == null) {
                String home = globalConfig.getString(DEFAULT_SOLR_HOME, "indexer", "home");
                log.info("Embedded Solr Home = {}", home);
                File homeDir = new File(home);
                if (!homeDir.exists()) {
                    log.error("Solr directory does not exist!");
                    return null;
                }
                System.setProperty("solr.solr.home", homeDir.getAbsolutePath());
                File coreXmlFile = new File(homeDir, "solr.xml");
                coreContainer = new CoreContainer(homeDir.getAbsolutePath(), coreXmlFile);
                for (SolrCore aCore : coreContainer.getCores()) {
                    log.info("Loaded core: {}", aCore.getName());
                }
            }
            String coreName = globalConfig.getString(null, "indexer", core, "coreName");
            if (coreName == null) {
                log.error("No 'coreName' node for core: '{}'", core);
                return null;
            }
            return new EmbeddedSolrServer(coreContainer, coreName);

            // Solr over HTTP
        } else {
            String uri = globalConfig.getString(null, "indexer", core, "uri");
            if (uri == null) {
                log.error("No URI provided for core: '{}'", core);
                return null;
            }

            URI solrUri = new URI(uri);
            CommonsHttpSolrServer thisCore = new CommonsHttpSolrServer(solrUri.toURL());
            username = globalConfig.getString(null, "indexer", core, "username");
            password = globalConfig.getString(null, "indexer", core, "password");
            if (username != null && password != null) {
                UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
                HttpClient hc = thisCore.getHttpClient();
                hc.getParams().setAuthenticationPreemptive(true);
                hc.getState().setCredentials(AuthScope.ANY, credentials);
            }
            return thisCore;
        }

    } catch (MalformedURLException mue) {
        log.error(core + " : Malformed URL", mue);
    } catch (URISyntaxException urise) {
        log.error(core + " : Invalid URI", urise);
    } catch (IOException ioe) {
        log.error(core + " : Failed to read Solr configuration", ioe);
    } catch (ParserConfigurationException pce) {
        log.error(core + " : Failed to parse Solr configuration", pce);
    } catch (SAXException saxe) {
        log.error(core + " : Failed to load Solr configuration", saxe);
    }
    return null;
}