Example usage for org.apache.http.impl.client.cache CachingHttpClientBuilder create

List of usage examples for org.apache.http.impl.client.cache CachingHttpClientBuilder create

Introduction

In this page you can find the example usage for org.apache.http.impl.client.cache CachingHttpClientBuilder create.

Prototype

public static CachingHttpClientBuilder create() 

Source Link

Usage

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

private HttpClient getCachingHttpClient() {
    return configureHttpClientBuilder(configureHttpCacheClientBuilder(CachingHttpClientBuilder.create()))
            .build();
}

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

private HttpClient getNewBasicAuthHttpClient(final URL url, final String user, final String password) {
    return configureHttpClientBuilder(configureHttpCacheClientBuilder(CachingHttpClientBuilder.create()))
            .setDefaultCredentialsProvider(getCredentialsProvider(url, user, password)).build();
}

From source file:com.moviejukebox.tools.YamjHttpClientBuilder.java

@SuppressWarnings("resource")
private static YamjHttpClient buildHttpClient() {
    LOG.trace("Create new YAMJ http client");

    // create proxy
    HttpHost proxy = null;//from  www  . java  2s.  c o  m
    CredentialsProvider credentialsProvider = null;

    if (StringUtils.isNotBlank(PROXY_HOST) && PROXY_PORT > 0) {
        proxy = new HttpHost(PROXY_HOST, PROXY_PORT);

        if (StringUtils.isNotBlank(PROXY_USERNAME) && StringUtils.isNotBlank(PROXY_PASSWORD)) {
            credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(PROXY_HOST, PROXY_PORT),
                    new UsernamePasswordCredentials(PROXY_USERNAME, PROXY_PASSWORD));
        }
    }

    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(TIMEOUT_SOCKET).build());
    connManager.setMaxTotal(20);
    connManager.setDefaultMaxPerRoute(2);

    CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000).setMaxObjectSize(8192).build();

    HttpClientBuilder builder = CachingHttpClientBuilder.create().setCacheConfig(cacheConfig)
            .setConnectionManager(connManager).setProxy(proxy)
            .setDefaultCredentialsProvider(credentialsProvider)
            .setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(TIMEOUT_READ)
                    .setConnectTimeout(TIMEOUT_CONNECT).setSocketTimeout(TIMEOUT_SOCKET)
                    .setCookieSpec(CookieSpecs.IGNORE_COOKIES).setProxy(proxy).build());

    // show status
    showStatus();

    // build the client
    YamjHttpClient wrapper = new YamjHttpClient(builder.build(), connManager);
    wrapper.setUserAgentSelector(new WebBrowserUserAgentSelector());
    wrapper.addGroupLimit(".*", 1); // default limit, can be overwritten

    // First we have to read/create the rules
    String maxDownloadSlots = PropertiesUtil.getProperty("mjb.MaxDownloadSlots");
    if (StringUtils.isNotBlank(maxDownloadSlots)) {
        LOG.debug("Using download limits: {}", maxDownloadSlots);

        Pattern pattern = Pattern.compile(",?\\s*([^=]+)=(\\d+)");
        Matcher matcher = pattern.matcher(maxDownloadSlots);
        while (matcher.find()) {
            String group = matcher.group(1);
            try {
                final Integer maxResults = Integer.valueOf(matcher.group(2));
                wrapper.addGroupLimit(group, maxResults);
                LOG.trace("Added download slot '{}' with max results {}", group, maxResults);
            } catch (NumberFormatException error) {
                LOG.debug("Rule '{}' is no valid regexp, ignored", group);
            }
        }
    }

    return wrapper;
}

From source file:com.machinepublishers.jbrowserdriver.StreamConnectionClient.java

StreamConnectionClient() {
    File cacheDirTmp = SettingsManager.settings().cacheDir();
    FileRemover shutdownHookTmp = null;//ww  w  . j a v a 2s .c om
    try {
        cacheDirTmp = cacheDirTmp == null ? Files.createTempDirectory("jbd_webcache_").toFile() : cacheDirTmp;
        if (SettingsManager.settings().cacheDir() == null) {
            shutdownHookTmp = new FileRemover(cacheDirTmp);
            Runtime.getRuntime().addShutdownHook(shutdownHookTmp);
        } else {
            cacheDirTmp.mkdirs();
        }
    } catch (Throwable t) {
        Util.handleException(t);
    }
    shutdownHook = shutdownHookTmp;
    cacheDir = cacheDirTmp;
    httpCache = new HttpCache(cacheDirTmp);

    cacheConfig = CacheConfig.custom().setSharedCache(false)
            .setMaxCacheEntries(SettingsManager.settings().cacheEntries())
            .setMaxObjectSize(SettingsManager.settings().cacheEntrySize()).build();
    ConnectionSocketFactory sslSocketFactory = SettingsManager.settings().hostnameVerification()
            ? new SslSocketFactory(sslContext())
            : new SslSocketWithoutHostnameVerificationFactory(sslContext());
    registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslSocketFactory)
            .register("http", new SocketFactory()).build();
    manager = new PoolingHttpClientConnectionManager(registry);
    manager.setDefaultMaxPerRoute(SettingsManager.settings().maxRouteConnections());
    manager.setMaxTotal(SettingsManager.settings().maxConnections());
    client = clientBuilderHelper(HttpClientBuilder.create(), manager);
    cachingClient = clientBuilderHelper(
            CachingHttpClientBuilder.create().setCacheConfig(cacheConfig).setHttpCacheStorage(httpCache),
            manager);
}

From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java

License:asdf

@Test
public void testDescribeRdfCached() throws IOException {
    try (final CloseableHttpClient cachClient = CachingHttpClientBuilder.create().setCacheConfig(DEFAULT)
            .build()) {//from  w ww .  ja v a 2s. c om
        final String location = getLocation(postObjMethod());
        try (final CloseableHttpResponse response = cachClient.execute(new HttpGet(location))) {
            assertEquals("Client didn't return a OK!", OK.getStatusCode(), getStatus(response));
            logger.debug("Found HTTP headers:\n{}", asList(response.getAllHeaders()));
            assertTrue("Didn't find Last-Modified header!", response.containsHeader("Last-Modified"));
            final String lastModed = response.getFirstHeader("Last-Modified").getValue();
            final String etag = response.getFirstHeader("ETag").getValue();
            final HttpGet getObjMethod2 = new HttpGet(location);
            getObjMethod2.setHeader("If-Modified-Since", lastModed);
            getObjMethod2.setHeader("If-None-Match", etag);
            assertEquals("Client didn't get a NOT_MODIFIED!", NOT_MODIFIED.getStatusCode(),
                    getStatus(getObjMethod2));
        }
    }
}