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:com.tremolosecurity.provisioning.customTasks.CallRemoteWorkflow.java

@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {

    HashMap<String, Object> newRequest = new HashMap<String, Object>();
    for (String name : this.fromRequest) {
        newRequest.put(name, request.get(name));
    }//from w  ww .  ja v a  2  s . c  o  m

    for (String key : this.staticRequest.keySet()) {
        newRequest.put(key, this.staticRequest.get(key));
    }

    WFCall wfCall = new WFCall();
    wfCall.setName(this.workflowName);
    wfCall.setRequestParams(newRequest);
    wfCall.setUser(new TremoloUser());
    wfCall.getUser().setUid(user.getUserID());
    wfCall.getUser().setUserPassword(user.getPassword());
    wfCall.getUser().setGroups(user.getGroups());
    wfCall.getUser().setAttributes(new ArrayList<Attribute>());
    wfCall.getUser().getAttributes().addAll(user.getAttribs().values());
    wfCall.setUidAttributeName(uidAttributeName);
    wfCall.setReason(task.getWorkflow().getUser().getRequestReason());
    if (task.getWorkflow().getRequester() != null) {
        wfCall.setRequestor(task.getWorkflow().getRequester().getUserID());
    } else {
        wfCall.setRequestor(this.lastMileUser);
    }

    DateTime notBefore = new DateTime();
    notBefore = notBefore.minusSeconds(timeSkew);
    DateTime notAfter = new DateTime();
    notAfter = notAfter.plusSeconds(timeSkew);

    com.tremolosecurity.lastmile.LastMile lastmile = null;

    try {
        lastmile = new com.tremolosecurity.lastmile.LastMile(this.uri, notBefore, notAfter, 0, "oauth2");

    } catch (URISyntaxException e) {
        throw new ProvisioningException("Could not generate lastmile", e);
    }

    Attribute attrib = new Attribute(this.lastMileUid, this.lastMileUser);
    lastmile.getAttributes().add(attrib);
    String encryptedXML = null;

    try {
        encryptedXML = lastmile
                .generateLastMileToken(this.task.getConfigManager().getSecretKey(this.lastmileKeyName));
    } catch (Exception e) {
        throw new ProvisioningException("Could not generate lastmile", e);
    }

    StringBuffer header = new StringBuffer();
    header.append("Bearer ").append(encryptedXML);

    BasicHttpClientConnectionManager bhcm = null;
    CloseableHttpClient http = null;

    try {
        bhcm = new BasicHttpClientConnectionManager(this.task.getConfigManager().getHttpClientSocketRegistry());

        RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setRedirectsEnabled(false)
                .build();

        http = HttpClients.custom().setConnectionManager(bhcm).setDefaultRequestConfig(rc).build();

        HttpPost post = new HttpPost(this.url);
        post.addHeader(new BasicHeader("Authorization", header.toString()));

        Gson gson = new Gson();
        StringEntity str = new StringEntity(gson.toJson(wfCall), ContentType.APPLICATION_JSON);
        post.setEntity(str);

        HttpResponse resp = http.execute(post);
        if (resp.getStatusLine().getStatusCode() != 200) {
            throw new ProvisioningException("Call failed");
        }

    } catch (IOException e) {
        throw new ProvisioningException("Could not make call", e);
    } finally {
        if (http != null) {
            try {
                http.close();
            } catch (IOException e) {
                logger.warn(e);
            }
        }

        if (bhcm != null) {
            bhcm.close();
        }

    }

    return true;
}

From source file:tradeok.HttpTool.java

@SuppressWarnings("deprecation")
public static String invokeGet(String url, Map<String, String> params, String encode, int connectTimeout)
        throws Exception {
    String responseString = null;
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(connectTimeout)
            .setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectTimeout)
            .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();

    StringBuilder sb = new StringBuilder();
    sb.append(url);//from   ww  w  . ja va2 s. c  om
    int i = 0;
    if (params != null) {
        for (Entry<String, String> entry : params.entrySet()) {
            if (i == 0 && !url.contains("?")) {
                sb.append("?");
            } else {
                sb.append("&");
            }
            sb.append(entry.getKey());
            sb.append("=");
            String value = entry.getValue();
            try {
                sb.append(URLEncoder.encode(value, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                System.out.printf("\nwarn:encode http get params error, value is " + value, e);
                sb.append(URLEncoder.encode(value));
            }
            i++;
        }
    }
    //        System.out.printf("\ninfo:[HttpUtils Get] begin invoke:"
    //                + sb.toString());
    HttpGet get = new HttpGet(sb.toString());
    get.setConfig(requestConfig);
    get.setHeader("Connection", "keep-alive");

    try {
        CloseableHttpResponse response = httpclient.execute(get);
        try {
            HttpEntity entity = response.getEntity();
            try {
                if (entity != null) {
                    responseString = EntityUtils.toString(entity, encode);
                }
            } finally {
                if (entity != null) {
                    entity.getContent().close();
                }
            }
        } finally {
            if (response != null) {
                response.close();
            }
        }
    } finally {
        get.releaseConnection();
    }
    return responseString;
}

From source file:org.drftpd.protocol.speedtest.net.slave.SpeedTestHandler.java

private float getUploadSpeed(String url) {
    long totalTime = 0L;
    long totalBytes = 0L;

    long startTime = System.currentTimeMillis();

    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(5000)
            .setConnectionRequestTimeout(5000).build();

    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("content-type", "application/x-www-form-urlencoded");
    httpPost.setConfig(requestConfig);/* www .  j a v a  2 s  . c  o m*/

    String payload = _payload; // Initial payload

    StopWatch watch = new StopWatch();

    SpeedTestCallable[] speedTestCallables = new SpeedTestCallable[_upThreads];
    for (int i = 0; i < _upThreads; i++) {
        speedTestCallables[i] = new SpeedTestCallable();
    }

    ExecutorService executor = Executors.newFixedThreadPool(_upThreads);
    List<Future<Long>> threadList;
    Set<Callable<Long>> callables = new HashSet<Callable<Long>>();

    boolean limitReached = false;

    int i = 2;
    while (true) {
        if ((System.currentTimeMillis() - startTime) > _upTime) {
            break;
        }

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("content1", payload));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        } catch (UnsupportedEncodingException e) {
            logger.error("Unsupported encoding of payload for speedtest upload: " + e.getMessage());
            close(executor, callables);
            return 0;
        }

        callables.clear();
        for (int k = 0; k < _upThreads; k++) {
            speedTestCallables[k].setHttpPost(httpPost);
            callables.add(speedTestCallables[k]);
        }

        for (int j = 0; j < _payloadLoop; j++) {
            try {
                watch.reset();
                Thread.sleep(_sleep);
                watch.start();
                threadList = executor.invokeAll(callables);
                for (Future<Long> fut : threadList) {
                    Long bytes = fut.get();
                    totalBytes += bytes;
                }
                watch.stop();
                totalTime += watch.getTime();
            } catch (InterruptedException e) {
                logger.error(e.getMessage());
                close(executor, callables);
                return 0;
            } catch (ExecutionException e) {
                if (e.getMessage().contains("Error code 413")) {
                    limitReached = true;
                    payload = StringUtils.repeat(_payload, i - 2);
                } else {
                    logger.error(e.getMessage());
                    close(executor, callables);
                    return 0;
                }
            }
            if ((System.currentTimeMillis() - startTime) > _upTime) {
                break;
            }
        }

        if (!limitReached) { // Increase payload size if not too big
            payload = StringUtils.repeat(_payload, i);
            i++;
        }
    }

    if (totalBytes == 0L || totalTime == 0L) {
        close(executor, callables);
        return 0;
    }

    close(executor, callables);

    return (float) (((totalBytes * 8) / totalTime) * 1000) / 1000000;
}

From source file:org.ow2.proactive.http.HttpClientBuilderTest.java

@Test
public void testSetDefaultRequestConfig() throws Exception {
    RequestConfig config = RequestConfig.custom().setConnectTimeout(42).build();

    httpClientBuilder.setDefaultRequestConfig(config);
    httpClientBuilder.build();//from  w w w .ja  va2  s . co  m

    Mockito.verify(internalHttpClientBuilder).build();
    Mockito.verify(internalHttpClientBuilder).setDefaultRequestConfig(config);
}

From source file:com.crosstreelabs.cognitio.gumshoe.transport.HttpTransport.java

private void buildHttpClient() {
    requestConfig = RequestConfig.custom().setExpectContinueEnabled(false).setCookieSpec(CookieSpecs.DEFAULT)
            .setRedirectsEnabled(false).setSocketTimeout(5000).setConnectTimeout(5000)
            .setConnectionRequestTimeout(5000).setStaleConnectionCheckEnabled(true).build();

    RegistryBuilder<ConnectionSocketFactory> connRegistryBuilder = RegistryBuilder.create();
    connRegistryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE);
    try { // Fixing: https://code.google.com/p/crawler4j/issues/detail?id=174
          // By always trusting the ssl certificate
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
            @Override// www.ja va  2s. com
            public boolean isTrusted(final X509Certificate[] chain, String authType) {
                return true;
            }
        }).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        connRegistryBuilder.register("https", sslsf);
    } catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException e) {
        LOGGER.warn("Exception thrown while trying to register https");
        LOGGER.debug("Stacktrace", e);
    }

    Registry<ConnectionSocketFactory> connRegistry = connRegistryBuilder.build();
    connectionManager = new PoolingHttpClientConnectionManager(connRegistry);
    connectionManager.setMaxTotal(5);
    connectionManager.setDefaultMaxPerRoute(5);

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    clientBuilder.setDefaultRequestConfig(requestConfig);
    clientBuilder.setConnectionManager(connectionManager);
    clientBuilder.setUserAgent("Cognitio");

    httpClient = clientBuilder.build();
}

From source file:org.tinymediamanager.scraper.util.Url.java

/**
 * Gets the input stream.//from w  w  w .  ja  v  a  2  s.c  o m
 * 
 * @return the input stream
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
public InputStream getInputStream() throws IOException, InterruptedException {
    // workaround for local files
    if (url.startsWith("file:")) {
        String newUrl = url.replace("file:/", "");
        File file = new File(newUrl);
        return new FileInputStream(file);
    }

    BasicHttpContext localContext = new BasicHttpContext();
    ByteArrayInputStream is = null;

    // replace our API keys for logging...
    String logUrl = url.replaceAll("api_key=\\w+", "api_key=<API_KEY>").replaceAll("api/\\d+\\w+",
            "api/<API_KEY>");
    LOGGER.debug("getting " + logUrl);
    HttpGet httpget = new HttpGet(uri);
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000)
            .build();
    httpget.setConfig(requestConfig);

    // set custom headers
    for (Header header : headersRequest) {
        httpget.addHeader(header);
    }

    CloseableHttpResponse response = null;
    try {
        response = client.execute(httpget, localContext);
        headersResponse = response.getAllHeaders();
        entity = response.getEntity();
        responseStatus = response.getStatusLine();
        if (entity != null) {
            is = new ByteArrayInputStream(EntityUtils.toByteArray(entity));
        }
        EntityUtils.consume(entity);
    } catch (InterruptedIOException e) {
        LOGGER.info("aborted request (" + e.getMessage() + "): " + logUrl);
        throw e;
    } catch (UnknownHostException e) {
        LOGGER.error("proxy or host not found/reachable", e);
        throw e;
    } catch (Exception e) {
        LOGGER.error("Exception getting url " + logUrl, e);
    } finally {
        if (response != null) {
            response.close();
        }
    }
    return is;
}

From source file:org.kontalk.client.DownloadClient.java

private static CloseableHttpClient createHTTPClient(PrivateKey privateKey, X509Certificate certificate,
        boolean validateCertificate) {
    //HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    HttpClientBuilder clientBuilder = HttpClients.custom();
    try {/*  w w w  .  ja va2s . c  o  m*/
        SSLContext sslContext = TrustUtils.getCustomSSLContext(privateKey, certificate, validateCertificate);
        clientBuilder.setSslcontext(sslContext);
    } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException
            | KeyManagementException | UnrecoverableKeyException | NoSuchProviderException ex) {
        LOGGER.log(Level.WARNING, "unable to set SSL context", ex);
        return null;
    }

    RequestConfig.Builder rcBuilder = RequestConfig.custom();
    // handle redirects :)
    rcBuilder.setRedirectsEnabled(true);
    // HttpClient bug caused by Lighttpd
    rcBuilder.setExpectContinueEnabled(false);
    clientBuilder.setDefaultRequestConfig(rcBuilder.build());

    // create connection manager
    //ClientConnectionManager connMgr = new SingleClientConnManager(params, registry);

    //return new DefaultHttpClient(connMgr, params);
    return clientBuilder.build();
}

From source file:com.baidu.rigel.biplatform.ac.util.HttpRequest.java

/**
 * ?HttpClientheadapplication/json// w  w w  .j a  va 2s  .c o m
 * 
 * @return HttpClient
 */
public static HttpClient getDefaultHttpClient(Map<String, String> params) {

    if (client == null) {
        CookieSpecProvider cookieSpecProvider = new CookieSpecProvider() {

            @Override
            public CookieSpec create(HttpContext context) {
                return new BrowserCompatSpec() {

                    @Override
                    public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException {
                        //no check cookie
                    }
                };
            }
        };

        Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
                .register(NO_CHECK_COOKIES, cookieSpecProvider).build();
        String socketTimeout = "50000";
        String connTimeout = "1000";
        if (params != null) {
            if (params.containsKey(SOCKET_TIME_OUT)) {
                socketTimeout = params.get(SOCKET_TIME_OUT);
            }
            if (params.containsKey(CONNECTION_TIME_OUT)) {
                socketTimeout = params.get(CONNECTION_TIME_OUT);
            }
        }
        // cookie?
        RequestConfig requestConfigBuilder = RequestConfig.custom().setCookieSpec(NO_CHECK_COOKIES)
                .setSocketTimeout(Integer.valueOf(socketTimeout)) // ms ???
                .setConnectTimeout(Integer.valueOf(connTimeout)) // ms???
                .build();
        client = HttpClients.custom().setDefaultCookieSpecRegistry(cookieSpecRegistry)
                .setDefaultRequestConfig(requestConfigBuilder).build();
    }

    return client;
}

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

public void setLoved(final ScratchSession session, final boolean loved) throws ScratchProjectException {
    final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();

    final CookieStore cookieStore = new BasicCookieStore();
    final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en");
    final BasicClientCookie sessid = new BasicClientCookie("scratchsessionsid", session.getSessionID());
    final BasicClientCookie token = new BasicClientCookie("scratchcsrftoken", session.getCSRFToken());
    final BasicClientCookie debug = new BasicClientCookie("DEBUG", "true");
    lang.setDomain(".scratch.mit.edu");
    lang.setPath("/");
    sessid.setDomain(".scratch.mit.edu");
    sessid.setPath("/");
    token.setDomain(".scratch.mit.edu");
    token.setPath("/");
    debug.setDomain(".scratch.mit.edu");
    debug.setPath("/");
    cookieStore.addCookie(lang);//from w w w  .  j a v a  2 s  .  c  o  m
    cookieStore.addCookie(sessid);
    cookieStore.addCookie(token);
    cookieStore.addCookie(debug);

    final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
            .setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64)"
                    + " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/" + "537.36")
            .setDefaultCookieStore(cookieStore).build();
    CloseableHttpResponse resp;

    final HttpUriRequest update = RequestBuilder.put()
            .setUri("https://scratch.mit.edu/site-api/users/lovers/" + this.getProjectID() + "/"
                    + (loved ? "add" : "remove") + "/?usernames=" + session.getUsername())
            .addHeader("Accept", "application/json, text/javascript, */*; q=0.01").addHeader("DNT", "1")
            .addHeader("Referer", "https://scratch.mit.edu/projects/" + this.getProjectID() + "/")
            .addHeader("Origin", "https://scratch.mit.edu/").addHeader("Accept-Encoding", "gzip, deflate, sdch")
            .addHeader("Accept-Language", "en-US,en;q=0.8").addHeader("Content-Type", "application/json")
            .addHeader("X-Requested-With", "XMLHttpRequest").addHeader("Cookie", "scratchsessionsid="
                    + session.getSessionID() + "; scratchcsrftoken=" + session.getCSRFToken())
            .addHeader("X-CSRFToken", session.getCSRFToken()).build();
    try {
        resp = httpClient.execute(update);
        if (resp.getStatusLine().getStatusCode() != 200)
            throw new ScratchProjectException();
        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);
    } catch (final IOException e) {
        e.printStackTrace();
        throw new ScratchProjectException();
    }

    BufferedReader rd;
    try {
        rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
    } catch (UnsupportedOperationException | IOException e) {
        e.printStackTrace();
        throw new ScratchProjectException();
    }
}