private HttpResponse postProcess(final HttpResponse response, HttpContext context, final Task task) { requestsExecuted.incrementAndGet(); task.resetProgress(); task.updateMessage("retrieving response"); if (response.getEntity() != null) { boolean cachedResponse; if (context != null) { CacheResponseStatus cacheRespStatus = (CacheResponseStatus) context.getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS); // To report download progress, the entity is wrapped in a MonitoredHttpEntity. cachedResponse = cacheRespStatus != null && cacheRespStatus != CacheResponseStatus.CACHE_MISS; } else { cachedResponse = false; } response.setEntity(new MonitoredHttpEntity(response.getEntity(), task, cachedResponse ? bytesFromCache : bytesReceived)); } else { task.endTask(); } return response; }
/** * Creates a HTTP client. * * @param parent * Owning object. Used for logging. * @param maxCacheEntries * the maximum number of cache entries the cache will retain. Set to 0 for no caching. */ public SimpleHttpClient(Object parent, int maxCacheEntries) { logger = Logger.getLogger(this.getClass().getName() + "-" + instanceCounter.incrementAndGet() + " (" + parent.getClass().getSimpleName() + ")"); isShutdown = new AtomicBoolean(); PoolingClientConnectionManager conman = new PoolingClientConnectionManager(); // increase max number of connections per host and total, defaults are too low to take advantage of multiple // threads conman.setDefaultMaxPerRoute(50); // default is 2 conman.setMaxTotal(100); // default is 20 defaultClient = new DefaultHttpClient(conman); if (maxCacheEntries > 0) { CacheConfig cacheConfig = new CacheConfig(); cacheConfig.setSharedCache(false); cacheConfig.setMaxCacheEntries(maxCacheEntries); cachingClient = new CachingHttpClient(defaultClient, cacheConfig); theClient = cachingClient; } else { cachingClient = null; theClient = defaultClient; } logger.info("Created"); }
@PostConstruct protected void initialize() { try { lock.writeLock().lock(); httpParams = new BasicHttpParams(); String userAgentString = "Apache Marmotta/" + configurationService.getStringConfiguration("kiwi.version") + " (running at " + configurationService.getServerUri() + ")" + " lmf-core/" + configurationService.getStringConfiguration("kiwi.version"); userAgentString = configurationService.getStringConfiguration("core.http.user_agent", userAgentString); httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, configurationService.getIntConfiguration("core.http.so_timeout", 60000)); httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, configurationService.getIntConfiguration("core.http.connection_timeout", 10000)); httpParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true); httpParams.setIntParameter(ClientPNames.MAX_REDIRECTS, 3); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); cm.setMaxTotal(configurationService.getIntConfiguration(CoreOptions.HTTP_MAX_CONNECTIONS, 20)); cm.setDefaultMaxPerRoute(configurationService.getIntConfiguration(CoreOptions.HTTP_MAX_CONNECTIONS_PER_ROUTE, 10)); final DefaultHttpClient hc = new DefaultHttpClient(cm, httpParams); hc.setRedirectStrategy(new LMFRedirectStrategy()); hc.setHttpRequestRetryHandler(new LMFHttpRequestRetryHandler()); hc.removeRequestInterceptorByClass(org.apache.http.protocol.RequestUserAgent.class); hc.addRequestInterceptor(new LMFRequestUserAgent(userAgentString)); if (configurationService.getBooleanConfiguration(CoreOptions.HTTP_CLIENT_CACHE_ENABLE, true)) { CacheConfig cacheConfig = new CacheConfig(); // FIXME: Hardcoded constants - is this useful? cacheConfig.setMaxCacheEntries(1000); cacheConfig.setMaxObjectSize(81920); final HttpCacheStorage cacheStore = new MapHttpCacheStorage(httpCache); this.httpClient = new MonitoredHttpClient(new CachingHttpClient(hc, cacheStore, cacheConfig)); } else { this.httpClient = new MonitoredHttpClient(hc); } bytesSent.set(0); bytesReceived.set(0); requestsExecuted.set(0); idleConnectionMonitorThread = new IdleConnectionMonitorThread(httpClient.getConnectionManager()); idleConnectionMonitorThread.start(); StatisticsProvider stats = new StatisticsProvider(cm); statisticsService.registerModule(HttpClientService.class.getSimpleName(), stats); } finally { lock.writeLock().unlock(); } }
public HttpCache() { CacheConfig cacheCfg =new CacheConfig(); cacheCfg.setMaxCacheEntries(1000); cacheCfg.setMaxObjectSize(8192); cachingClient = new CachingHttpClient(new DefaultHttpClient(), cacheCfg); }