static CloseableHttpClient defaultHttpClient() { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS); connectionManager.setMaxTotal(DEFAULT_MAX_CONNECTIONS); RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectTimeout(((int) TimeUnit.SECONDS.toMillis(5))); requestConfigBuilder.setSocketTimeout((int) TimeUnit.SECONDS.toMillis(10)); CacheConfig cacheConfig = CacheConfig.custom() .setMaxCacheEntries(128) .setMaxObjectSize(2048) // keys (.pem) are small .setHeuristicCachingEnabled(false) .setSharedCache(false) .setAsynchronousWorkersMax(2) .build(); return CachingHttpClients.custom() .setCacheConfig(cacheConfig) .setDefaultRequestConfig(requestConfigBuilder.build()) .setConnectionManager(connectionManager) .useSystemProperties() .setRedirectStrategy(DefaultRedirectStrategy.INSTANCE) .build(); } }
private CloseableHttpClient createDefaultClient(HttpClientConnectionManager fClientCm) { final CacheConfig cacheConfig = CacheConfig.custom() .setMaxCacheEntries(DEFAULT_CACHE_MAX_ENTRIES) .setMaxObjectSize(DEFAULT_CACHE_MAX_OBJ_SIZE) .setHeuristicCachingEnabled(true) .setHeuristicDefaultLifetime(DEFAULT_CACHE_TTL_IN_SECS) .build(); final RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(DEFAULT_REQUEST_CONN_TIMEOUT_IN_MS) .setSocketTimeout(DEFAULT_REQUEST_SOCK_TIMEOUT_IN_MS) .build(); return CachingHttpClients.custom() .setCacheConfig(cacheConfig) .setConnectionManager(fClientCm) .setDefaultRequestConfig(requestConfig) .build(); }
.setMaxObjectSize(cacheMaxObjectSize != null ? cacheMaxObjectSize : DEFAULT_CACHE_MAX_OBJECT_SIZE) .setSharedCache(false) .build();
public static final CacheConfig DEFAULT = new Builder().build();
.setNeverCacheHTTP10ResponsesWithQueryString(false) .setMaxObjectSize(options.getMaxCacheObjectSize()) .build();
.setMaxObjectSize(1024 * 128).build();
.setMaxCacheEntries(SettingsManager.settings().cacheEntries()) .setMaxObjectSize(SettingsManager.settings().cacheEntrySize()) .build(); ConnectionSocketFactory sslSocketFactory = SettingsManager.settings() .hostnameVerification() ? new SslSocketFactory(sslContext()) : new SslSocketWithoutHostnameVerificationFactory(sslContext());
public static CacheConfig createCacheConfig(Properties properties) { // Heuristic caching boolean heuristicCachingEnabled = Parameters.HEURISTIC_CACHING_ENABLED.getValue(properties); float heuristicCoefficient = Parameters.HEURISTIC_COEFFICIENT.getValue(properties); long heuristicDefaultLifetimeSecs = Parameters.HEURISTIC_DEFAULT_LIFETIME_SECS.getValue(properties); int maxCacheEntries = Parameters.MAX_CACHE_ENTRIES.getValue(properties); long maxObjectSize = Parameters.MAX_OBJECT_SIZE.getValue(properties); // Asynchronous revalidation int minAsynchronousWorkers = Parameters.MIN_ASYNCHRONOUS_WORKERS.getValue(properties); int maxAsynchronousWorkers = Parameters.MAX_ASYNCHRONOUS_WORKERS.getValue(properties); int asynchronousWorkerIdleLifetimeSecs = Parameters.ASYNCHRONOUS_WORKER_IDLE_LIFETIME_SECS.getValue(properties); int maxUpdateRetries = Parameters.MAX_UPDATE_RETRIES.getValue(properties); int revalidationQueueSize = Parameters.REVALIDATION_QUEUE_SIZE.getValue(properties); CacheConfig.Builder builder = CacheConfig.custom(); builder.setHeuristicCachingEnabled(heuristicCachingEnabled); builder.setHeuristicCoefficient(heuristicCoefficient); builder.setHeuristicDefaultLifetime(heuristicDefaultLifetimeSecs); builder.setMaxCacheEntries(maxCacheEntries); long usedMaxObjectSize = Long.MAX_VALUE; if (maxObjectSize > 0) { usedMaxObjectSize = maxObjectSize; } builder.setMaxObjectSize(usedMaxObjectSize); builder.setAsynchronousWorkersCore(minAsynchronousWorkers); builder.setAsynchronousWorkersMax(maxAsynchronousWorkers); builder.setAsynchronousWorkerIdleLifetimeSecs(asynchronousWorkerIdleLifetimeSecs); builder.setMaxUpdateRetries(maxUpdateRetries).setRevalidationQueueSize(revalidationQueueSize); builder.setSharedCache(true); return builder.build(); }
public HttpEndpoint(URI endpoint, Config cfg, HttpClientContextFactory clientContextFactory) { if (endpoint == null) { throw new IllegalArgumentException("Endpoint is required"); } if (cfg == null) { cfg = new ConfigurationBuilder().build(); } CacheConfig cacheConfig = CacheConfig.custom() .setMaxCacheEntries(cfg.getMaxCacheEntries()) .setMaxObjectSize(cfg.getMaxCacheObjectSize()) .build(); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(1000 * cfg.getConnectTimeOutSeconds()) .setSocketTimeout(1000 * cfg.getSocketTimeOutSeconds()) .build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(cfg.getMaxConections()); this.endpoint = endpoint; this.httpClient = CachingHttpClients.custom() .setCacheConfig(cacheConfig) .setDefaultRequestConfig(requestConfig) .setRetryHandler(new StandardHttpRequestRetryHandler()) .setConnectionManager(cm) .build(); this.clientContextFactory = clientContextFactory; initPingThread(cfg.getPingSeconds()); }
/******************************************************************************************************************* * * * ******************************************************************************************************************/ @PostConstruct /* VisibleForTesting */ void initialize() { connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(200); connectionManager.setDefaultMaxPerRoute(20); cacheConfig = CacheConfig.custom() .setAllow303Caching(true) .setMaxCacheEntries(Integer.MAX_VALUE) .setMaxObjectSize(Integer.MAX_VALUE) .setSharedCache(false) .setHeuristicCachingEnabled(true) .build(); httpClient = CachingHttpClients.custom() .setHttpCacheStorage(cacheStorage) .setCacheConfig(cacheConfig) .setRedirectStrategy(dontFollowRedirect) .setUserAgent("blueMarine (fabrizio.giudici@tidalwave.it)") .setDefaultHeaders(Arrays.asList(new BasicHeader("Accept", "application/n3"))) .setConnectionManager(connectionManager) .addInterceptorFirst(killCacheHeaders) // FIXME: only if explicitly configured .build(); }
@Test public void contextClassLoader() throws Exception { final URL nestedJar = getClass().getResource("/nested.jar"); assertNotNull(nestedJar); final ClassLoader cl = new URLClassLoader(new URL[] { nestedJar }); final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000) .setMaxObjectSize(1024 * 128).build(); final JarCacheStorage storage = new JarCacheStorage(cl, cacheConfig); Thread.currentThread().setContextClassLoader(cl); final HttpClient httpClient = createTestHttpClient(cacheConfig, storage); final HttpGet get = new HttpGet("http://nonexisting.example.com/nested/hello"); final HttpResponse resp = httpClient.execute(get); assertEquals("application/json", resp.getEntity().getContentType().getValue()); final String str = IOUtils.toString(resp.getEntity().getContent(), "UTF-8"); assertEquals("{ \"Hello\": \"World!\" }", str.trim()); }
@Test public void customClassPath() throws Exception { final URL nestedJar = getClass().getResource("/nested.jar"); final ClassLoader cl = new URLClassLoader(new URL[] { nestedJar }); final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000) .setMaxObjectSize(1024 * 128).build(); final JarCacheStorage storage = new JarCacheStorage(cl, cacheConfig); final HttpClient httpClient = createTestHttpClient(cacheConfig, storage); final HttpGet get = new HttpGet("http://nonexisting.example.com/nested/hello"); final HttpResponse resp = httpClient.execute(get); assertEquals("application/json", resp.getEntity().getContentType().getValue()); final String str = IOUtils.toString(resp.getEntity().getContent(), "UTF-8"); assertEquals("{ \"Hello\": \"World!\" }", str.trim()); }
/** * Constructs a default HttpJsonHomeClient build on top of a CachingHttpClient with in-memory storage. */ public HttpJsonHomeClient() { final CacheConfig cacheConfig = CacheConfig.custom() .setMaxCacheEntries(100) .setMaxObjectSize(50000) .build(); this.cacheStorage = new BasicHttpCacheStorage(cacheConfig); this.httpClient = CachingHttpClientBuilder.create() .setHttpCacheStorage(cacheStorage) .setCacheConfig(cacheConfig) .build(); }
@Test public void cacheHit() throws Exception { final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000) .setMaxObjectSize(1024 * 128).build(); final JarCacheStorage storage = new JarCacheStorage(null, cacheConfig); final HttpClient httpClient = createTestHttpClient(cacheConfig, storage); final HttpGet get = new HttpGet("http://nonexisting.example.com/context"); final HttpResponse resp = httpClient.execute(get); assertEquals("application/ld+json", resp.getEntity().getContentType().getValue()); final String str = IOUtils.toString(resp.getEntity().getContent(), "UTF-8"); assertTrue(str.contains("ex:datatype")); }
@Test public void systemClassLoader() throws Exception { final URL nestedJar = getClass().getResource("/nested.jar"); assertNotNull(nestedJar); final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000) .setMaxObjectSize(1024 * 128).build(); final JarCacheStorage storage = new JarCacheStorage(null, cacheConfig); final HttpClient httpClient = createTestHttpClient(cacheConfig, storage); final HttpGet get = new HttpGet("http://nonexisting.example.com/context"); final HttpResponse resp = httpClient.execute(get); assertEquals("application/ld+json", resp.getEntity().getContentType().getValue()); }
@Test public void doubleLoad() throws Exception { final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000) .setMaxObjectSize(1024 * 128).build(); final JarCacheStorage storage = new JarCacheStorage(null, cacheConfig); final HttpClient httpClient = createTestHttpClient(cacheConfig, storage); final HttpGet get = new HttpGet("http://nonexisting.example.com/context"); HttpResponse resp = httpClient.execute(get); resp = httpClient.execute(get); // Ensure second load through the cached jarcache list works assertEquals("application/ld+json", resp.getEntity().getContentType().getValue()); }
@Test(expected = IOException.class) public void cacheMiss() throws Exception { final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000) .setMaxObjectSize(1024 * 128).build(); final JarCacheStorage storage = new JarCacheStorage(null, cacheConfig); final HttpClient httpClient = createTestHttpClient(cacheConfig, storage); final HttpGet get = new HttpGet("http://nonexisting.example.com/notfound"); // Should throw an IOException as the DNS name // nonexisting.example.com does not exist httpClient.execute(get); }
public static CacheConfig createDefaultCacheConfig() { return CacheConfig.custom().setMaxCacheEntries(500).setMaxObjectSize(1024 * 256) .setSharedCache(false).setHeuristicCachingEnabled(true) .setHeuristicDefaultLifetime(86400).build(); }
public static CacheConfig createDefaultCacheConfig() { return CacheConfig.custom().setMaxCacheEntries(500).setMaxObjectSize(1024 * 256) .setSharedCache(false).setHeuristicCachingEnabled(true) .setHeuristicDefaultLifetime(86400).build(); }