Example usage for org.apache.http.client.config RequestConfig custom

List of usage examples for org.apache.http.client.config RequestConfig custom

Introduction

In this page you can find the example usage for org.apache.http.client.config RequestConfig custom.

Prototype

public static RequestConfig.Builder custom() 

Source Link

Usage

From source file:mobi.jenkinsci.server.core.net.PooledHttpClientFactory.java

private RequestConfig getRequestConfig() {
    return RequestConfig.custom().setConnectTimeout(config.getHttpConnectionTimeoutMsec())
            .setConnectionRequestTimeout(config.getHttpConnectionTimeoutMsec())
            .setCircularRedirectsAllowed(false).setRedirectsEnabled(true)
            .setSocketTimeout(config.getHttpReadTimeoutMsec()).setStaleConnectionCheckEnabled(true).build();
}

From source file:sk.datalan.solr.impl.HttpClientUtil.java

public static HttpClientBuilder configureClient(final HttpClientConfiguration config) {
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();

    // max total connections
    if (config.isSetMaxConnections()) {
        clientBuilder.setMaxConnTotal(config.getMaxConnections());
    }/*from  w  ww . j  a  v a  2  s  .c  o  m*/

    // max connections per route
    if (config.isSetMaxConnectionsPerRoute()) {
        clientBuilder.setMaxConnPerRoute(config.getMaxConnectionsPerRoute());
    }

    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH)
            .setExpectContinueEnabled(true).setStaleConnectionCheckEnabled(true);

    // connection timeout
    if (config.isSetConnectionTimeout()) {
        requestConfigBuilder.setConnectTimeout(config.getConnectionTimeout());
    }

    // soucket timeout
    if (config.isSetSocketTimeout()) {
        requestConfigBuilder.setSocketTimeout(config.getSocketTimeout());
    }

    // soucket timeout
    if (config.isSetFollowRedirects()) {
        requestConfigBuilder.setRedirectsEnabled(config.getFollowRedirects());
    }
    clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());

    if (config.isSetUseRetry()) {
        if (config.getUseRetry()) {
            clientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler());
        } else {
            clientBuilder.setRetryHandler(NO_RETRY);
        }
    }

    // basic authentication
    if (config.isSetBasicAuthUsername() && config.isSetBasicAuthPassword()) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(config.getBasicAuthUsername(), config.getBasicAuthPassword()));
    }

    if (config.isSetAllowCompression()) {
        clientBuilder.addInterceptorFirst(new UseCompressionRequestInterceptor());
        clientBuilder.addInterceptorFirst(new UseCompressionResponseInterceptor());
    }

    // SSL context for secure connections can be created either based on
    // system or application specific properties.
    SSLContext sslcontext = SSLContexts.createSystemDefault();
    // Use custom hostname verifier to customize SSL hostname verification.
    X509HostnameVerifier hostnameVerifier = new BrowserCompatHostnameVerifier();

    // Create a registry of custom connection socket factories for supported
    // protocol schemes.
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", new SSLConnectionSocketFactory(sslcontext, hostnameVerifier)).build();

    clientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry));

    return clientBuilder;
}

From source file:com.mirth.connect.client.core.ConnectServiceUtil.java

public static void registerUser(String serverId, String mirthVersion, User user, String[] protocols,
        String[] cipherSuites) throws ClientException {
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse httpResponse = null;
    NameValuePair[] params = { new BasicNameValuePair("serverId", serverId),
            new BasicNameValuePair("version", mirthVersion),
            new BasicNameValuePair("user", ObjectXMLSerializer.getInstance().serialize(user)) };

    HttpPost post = new HttpPost();
    post.setURI(URI.create(URL_CONNECT_SERVER + URL_REGISTRATION_SERVLET));
    post.setEntity(new UrlEncodedFormEntity(Arrays.asList(params), Charset.forName("UTF-8")));
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(TIMEOUT)
            .setConnectionRequestTimeout(TIMEOUT).setSocketTimeout(TIMEOUT).build();

    try {/*w  ww  .  jav  a 2  s.  co m*/
        HttpClientContext postContext = HttpClientContext.create();
        postContext.setRequestConfig(requestConfig);
        httpClient = getClient(protocols, cipherSuites);
        httpResponse = httpClient.execute(post, postContext);
        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if ((statusCode != HttpStatus.SC_OK) && (statusCode != HttpStatus.SC_MOVED_TEMPORARILY)) {
            throw new Exception("Failed to connect to update server: " + statusLine);
        }
    } catch (Exception e) {
        throw new ClientException(e);
    } finally {
        HttpClientUtils.closeQuietly(httpResponse);
        HttpClientUtils.closeQuietly(httpClient);
    }
}

From source file:com.ibm.subway.NewYorkSubway.java

public StationList getTrainList(String stationId, Locale locale, boolean translate) throws Exception {
    logger.debug("Station {}", stationId);
    StationList returnedList = new StationList();
    WatsonTranslate watson = new WatsonTranslate(locale);

    try {//from ww w  .jav a2  s .  c  o  m
        if (stationId != null) {
            RequestConfig config = RequestConfig.custom().setSocketTimeout(10 * 1000)
                    .setConnectTimeout(10 * 1000).build();
            CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(config).build();

            URIBuilder builder = new URIBuilder();
            builder.setScheme("http").setHost(url).setPath("/by-id/" + stationId);
            URI uri = builder.build();
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setHeader("Content-Type", "text/plain");

            HttpResponse httpResponse = httpclient.execute(httpGet);

            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                BufferedReader rd = new BufferedReader(
                        new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));

                // Read all the trains from the list
                ObjectMapper mapper = new ObjectMapper();
                returnedList = mapper.readValue(rd, StationList.class);

                // Set the direction of the trains
                ArrayList<Station> stations = returnedList.getStationList();
                for (Station station : stations) {
                    String northString = "North";
                    String southString = "South";

                    if (translate) {
                        northString = watson.translate(northString);
                        southString = watson.translate(southString);
                    }

                    ArrayList<Train> north = station.getNorthTrains();
                    for (Train train : north) {
                        train.setDirection(northString);
                    }
                    ArrayList<Train> south = station.getSouthTrains();
                    for (Train train : south) {
                        train.setDirection(southString);
                    }
                }
            } else {
                logger.error("could not get list from MTA http code {}",
                        httpResponse.getStatusLine().getStatusCode());
            }
        }
    } catch (Exception e) {
        logger.error("could not get list from MTA {}", e.getMessage());
        throw e;
    }

    return returnedList;
}

From source file:cc.gospy.example.google.GoogleSpider.java

public Collection<String> getResultLinks(final String keyword, final int pageFrom, final int pageTo) {
    if (pageFrom < 1)
        throw new IllegalArgumentException(pageFrom + "<" + 1);
    if (pageFrom >= pageTo)
        throw new IllegalArgumentException(pageFrom + ">=" + pageTo);

    final AtomicInteger currentPage = new AtomicInteger(pageFrom);
    final AtomicBoolean returned = new AtomicBoolean(false);
    final Collection<String> links = new LinkedHashSet<>();
    Gospy googleSpider = Gospy.custom()//  w ww .  j  a  v a 2s. c  o m
            .setScheduler(
                    Schedulers.VerifiableScheduler.custom().setExitCallback(() -> returned.set(true)).build())
            .addFetcher(Fetchers.HttpFetcher.custom()
                    .before(request -> request.setConfig(
                            RequestConfig.custom().setProxy(new HttpHost("127.0.0.1", 8118)).build()))
                    .build())
            .addProcessor(Processors.XPathProcessor.custom()
                    .extract("//*[@id='ires']/ol/div/h3/a/@href", (task, resultList, returnedData) -> {
                        resultList.forEach(a -> {
                            if (a.startsWith("/url?q=")) {
                                String link = a.substring(7);
                                int end = link.indexOf("&sa=");
                                links.add(link.substring(0, end != -1 ? end : link.length()));
                            }
                        });
                        currentPage.incrementAndGet();
                        if (pageFrom <= currentPage.get() && currentPage.get() < pageTo) {
                            return Arrays.asList(
                                    new Task(String.format("http://www.google.com/search?q=%s&start=%d&*",
                                            keyword, (currentPage.get() - 1) * 10)));
                        } else {
                            return Arrays.asList();
                        }
                    }).build())
            .build().addTask(String.format("http://www.google.com/search?q=%s&start=%d&*", keyword, pageFrom));
    googleSpider.start();
    while (!returned.get())
        ; // block until spider returned
    googleSpider.stop();
    return links;
}

From source file:com.linkedin.multitenant.db.RocksdbDatabase.java

public RocksdbDatabase() {
    RequestConfig rq = RequestConfig.custom().setStaleConnectionCheckEnabled(false).build();

    m_client = HttpClients.custom().setDefaultRequestConfig(rq).setMaxConnTotal(2).build();
    m_handler = new MyResponseHandler();
}

From source file:org.elasticsearch.plugin.SitePluginTests.java

public HttpRequestBuilder httpClient() {
    RequestConfig.Builder builder = RequestConfig.custom().setRedirectsEnabled(false);
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(builder.build()).build();
    return new HttpRequestBuilder(httpClient)
            .httpTransport(internalCluster().getDataNodeInstance(HttpServerTransport.class));
}

From source file:edu.mit.scratch.Scratch.java

public static ScratchSession createSession(final String username, String password)
        throws ScratchLoginException {
    try {/*  ww w.java 2  s  . c o m*/
        final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT) // Changed due to deprecation
                .build();

        final CookieStore cookieStore = new BasicCookieStore();
        final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en");
        lang.setDomain(".scratch.mit.edu");
        lang.setPath("/");
        cookieStore.addCookie(lang);

        final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setUserAgent(Scratch.USER_AGENT).setDefaultCookieStore(cookieStore).build();
        CloseableHttpResponse resp;

        final HttpUriRequest csrf = RequestBuilder.get().setUri("https://scratch.mit.edu/csrf_token/")
                .addHeader("Accept", "*/*").addHeader("Referer", "https://scratch.mit.edu")
                .addHeader("X-Requested-With", "XMLHttpRequest").build();
        resp = httpClient.execute(csrf);
        resp.close();

        String csrfToken = null;
        for (final Cookie c : cookieStore.getCookies())
            if (c.getName().equals("scratchcsrftoken"))
                csrfToken = c.getValue();

        final JSONObject loginObj = new JSONObject();
        loginObj.put("username", username);
        loginObj.put("password", password);
        loginObj.put("captcha_challenge", "");
        loginObj.put("captcha_response", "");
        loginObj.put("embed_captcha", false);
        loginObj.put("timezone", "America/New_York");
        loginObj.put("csrfmiddlewaretoken", csrfToken);
        final HttpUriRequest login = RequestBuilder.post().setUri("https://scratch.mit.edu/accounts/login/")
                .addHeader("Accept", "application/json, text/javascript, */*; q=0.01")
                .addHeader("Referer", "https://scratch.mit.edu").addHeader("Origin", "https://scratch.mit.edu")
                .addHeader("Accept-Encoding", "gzip, deflate").addHeader("Accept-Language", "en-US,en;q=0.8")
                .addHeader("Content-Type", "application/json").addHeader("X-Requested-With", "XMLHttpRequest")
                .addHeader("X-CSRFToken", csrfToken).setEntity(new StringEntity(loginObj.toString())).build();
        resp = httpClient.execute(login);
        password = null;
        final BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));

        final StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null)
            result.append(line);
        final JSONObject jsonOBJ = new JSONObject(
                result.toString().substring(1, result.toString().length() - 1));
        if ((int) jsonOBJ.get("success") != 1)
            throw new ScratchLoginException();
        String ssi = null;
        String sct = null;
        String e = null;
        final Header[] headers = resp.getAllHeaders();
        for (final Header header : headers)
            if (header.getName().equals("Set-Cookie")) {
                final String value = header.getValue();
                final String[] split = value.split(Pattern.quote("; "));
                for (final String s : split) {
                    if (s.contains("=")) {
                        final String[] split2 = s.split(Pattern.quote("="));
                        final String key = split2[0];
                        final String val = split2[1];
                        if (key.equals("scratchsessionsid"))
                            ssi = val;
                        else if (key.equals("scratchcsrftoken"))
                            sct = val;
                        else if (key.equals("expires"))
                            e = val;
                    }
                }
            }
        resp.close();

        return new ScratchSession(ssi, sct, e, username);
    } catch (final IOException e) {
        e.printStackTrace();
        throw new ScratchLoginException();
    }
}

From source file:cc.gospy.example.google.GoogleScholarSpider.java

public Collection<String> getResultLinks(final String keyword, final int pageFrom, final int pageTo) {
    if (pageFrom < 1)
        throw new IllegalArgumentException(pageFrom + "<" + 1);
    if (pageFrom >= pageTo)
        throw new IllegalArgumentException(pageFrom + ">=" + pageTo);

    final AtomicInteger currentPage = new AtomicInteger(pageFrom);
    final AtomicBoolean returned = new AtomicBoolean(false);
    final Collection<String> links = new LinkedHashSet<>();
    Gospy googleScholarSpider = Gospy.custom()
            .setScheduler(//from   w ww.j av a  2  s.c o  m
                    Schedulers.VerifiableScheduler.custom().setExitCallback(() -> returned.set(true)).build())
            .addFetcher(Fetchers.HttpFetcher.custom()
                    .before(request -> request.setConfig(
                            RequestConfig.custom().setProxy(new HttpHost("127.0.0.1", 8118)).build()))
                    .build())
            .addProcessor(Processors.XPathProcessor.custom().extract(
                    "//*[@id='gs_ccl_results']/div/div/h3/a/@href", (task, resultList, returnedData) -> {
                        links.addAll(resultList);
                        currentPage.incrementAndGet();
                        if (pageFrom <= currentPage.get() && currentPage.get() < pageTo) {
                            return Arrays.asList(
                                    new Task(String.format("https://scholar.google.com/scholar?start=%d&q=%s",
                                            (currentPage.get() - 1) * 10, keyword)));
                        } else {
                            return Arrays.asList();
                        }
                    }).build())
            .build()
            .addTask(String.format("https://scholar.google.com/scholar?start=%d&q=%s", pageFrom, keyword));
    googleScholarSpider.start();
    while (!returned.get())
        ; // block until spider returned
    googleScholarSpider.stop();
    return links;
}

From source file:io.cloudslang.content.httpclient.build.RequestConfigBuilder.java

public RequestConfig buildRequestConfig() {
    HttpHost proxy = null;/*from  w  w  w  . j  av  a  2s  . com*/
    final int proxyPortNumber;
    if (proxyHost != null && !proxyHost.isEmpty()) {
        proxyPortNumber = Utils.validatePortNumber(proxyPort);
        proxy = new HttpHost(proxyHost, proxyPortNumber);
    }
    int connectionTimeout = Integer.parseInt(this.connectionTimeout);
    int socketTimeout = Integer.parseInt(this.socketTimeout);
    //todo should we also allow user to enable redirects prohibited by the HTTP specification (on POST and PUT)? See 'LaxRedirectStrategy'
    return RequestConfig.custom()
            .setConnectTimeout(connectionTimeout <= 0 ? connectionTimeout : connectionTimeout * 1000)
            .setSocketTimeout(socketTimeout <= 0 ? socketTimeout : socketTimeout * 1000).setProxy(proxy)
            .setRedirectsEnabled(Boolean.parseBoolean(followRedirects)).build();
}