Example usage for org.apache.http.impl.client.cache CachingHttpClient CachingHttpClient

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

Introduction

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

Prototype

public CachingHttpClient(HttpClient client, HttpCacheStorage storage, CacheConfig config) 

Source Link

Document

Constructs a CachingHttpClient with the given caching options that stores cache entries in the provided storage backend and uses the given HttpClient for backend requests.

Usage

From source file:de.otto.jsonhome.client.HttpJsonHomeClient.java

/**
 * Constructs a default HttpJsonHomeClient build on top of a CachingHttpClient with in-memory storage.
 *///ww  w.j  a  v a 2  s. co  m
public HttpJsonHomeClient() {
    final CacheConfig cacheConfig = new CacheConfig();
    cacheConfig.setMaxCacheEntries(100);
    cacheConfig.setMaxObjectSize(50000);
    this.cacheStorage = new BasicHttpCacheStorage(cacheConfig);
    this.httpClient = new CachingHttpClient(new DefaultHttpClient(), cacheStorage, cacheConfig);
}

From source file:org.apache.abdera2.common.protocol.BasicCachingClient.java

protected HttpClient initClient(String useragent, DefaultHttpClient client) {
    inner = client != null ? client : (DefaultHttpClient) super.initClient(useragent);
    CacheConfig cacheConfig = new CacheConfig();
    cacheConfig.setMaxCacheEntries(1000);
    cacheConfig.setMaxObjectSizeBytes(8192);
    cacheConfig.setHeuristicCachingEnabled(true);

    return store != null ? new CachingHttpClient(inner, store, cacheConfig)
            : new CachingHttpClient(inner, cacheConfig);
}

From source file:de.otto.jsonhome.client.HttpJsonHomeClient.java

/**
 * Constructs a HttpJsonHomeClient using a HttpClient and a CacheConfig.
 * <p/>//from  w  w w .  ja  v a 2s. c o m
 * Internally, these two are used to build a CachingHttpClient using a BasicHttpCacheStorage.
 *
 * @param httpClient non-caching HttpClient used to get resources.
 * @param cacheConfig configuration of the HttpCacheStorage
 */
public HttpJsonHomeClient(final HttpClient httpClient, final CacheConfig cacheConfig) {
    this.cacheStorage = new BasicHttpCacheStorage(cacheConfig);
    this.httpClient = new CachingHttpClient(httpClient, cacheStorage, cacheConfig);
}

From source file:de.otto.jsonhome.client.HttpJsonHomeClient.java

/**
 * Constructs a caching HttpJsonHomeClient using a HttpClient, HttpCacheStorage and a CacheConfig.
 *
 * @param httpClient non-caching HttpClient used to get resources.
 * @param storage the HttpCacheStorage used to cache HTTP responses.
 * @param cacheConfig configuration of the HttpCacheStorage
 *///  w w w  . ja  v a 2  s  .com
public HttpJsonHomeClient(final HttpClient httpClient, final HttpCacheStorage storage,
        final CacheConfig cacheConfig) {
    this.cacheStorage = storage;
    this.httpClient = new CachingHttpClient(httpClient, cacheStorage, cacheConfig);
}

From source file:gr.wavesoft.webng.io.web.WebStreams.java

public static void Initialize() {

    schemeRegistry = new SchemeRegistry();

    // Register HTTP
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    // Register HTTPS with WebNG Extensions
    try {/*from   w  w w  .  ja  v  a2 s . c  o m*/
        schemeRegistry.register(new Scheme("https", 443, new SSLSocketFactory(WebNGKeyStore.getKeyStore())));

    } catch (NoSuchAlgorithmException ex) {
        systemLogger.except(ex);
    } catch (KeyManagementException ex) {
        systemLogger.except(ex);
    } catch (KeyStoreException ex) {
        systemLogger.except(ex);
    } catch (UnrecoverableKeyException ex) {
        systemLogger.except(ex);
    }

    // Setup connection manager
    connectionManager = new PoolingClientConnectionManager(schemeRegistry);

    // Increase max total connection to 200
    connectionManager.setMaxTotal(200);

    // Increase default max connection per route to 20
    connectionManager.setDefaultMaxPerRoute(20);

    // Increase max connections for localhost:80 to 50
    HttpHost localhost = new HttpHost("locahost", 80);
    connectionManager.setMaxPerRoute(new HttpRoute(localhost), 50);

    // Setup http client
    httpClient = new DefaultHttpClient(connectionManager);

    // Setup cookie cache
    //((DefaultHttpClient)httpClient).setCookieStore(WebNGCache.instanceCookieCache());

    // Setup cache
    CacheConfig cacheConfig = new CacheConfig();
    cacheConfig.setMaxCacheEntries(WebNGCache.config.MAX_ENTRIES_MEM);
    cacheConfig.setMaxObjectSize(WebNGCache.config.MAX_OBJECT_SIZE);

    cachingClient = new CachingHttpClient(httpClient, WebNGCache.instanceDiskCacheStorage(), cacheConfig);

}

From source file:com.grendelscan.commons.http.apache_overrides.client.CustomHttpClient.java

/**
 * /* www.  j av  a  2s. c  o m*/
 * @param conman
 * @param params
 */
public CustomHttpClient(final ClientConnectionManager conman, final HttpParams params,
        final HttpContext contextTemplate) {
    super(conman, params);
    this.contextTemplate = contextTemplate;

    apacheCacheConfig = new CacheConfig();
    apacheCacheConfig.setMaxCacheEntries(0);
    apacheCacheConfig.setMaxObjectSizeBytes(50000);
    CacheConfiguration ehCacheConfig = new CacheConfiguration("Cobra Cache", 50);
    ehCacheConfig.setOverflowToDisk(true);
    ehCacheConfig.setMaxElementsInMemory(50);
    ehCacheConfig.setDiskStorePath(Scan.getInstance().getOutputDirectory());

    Ehcache ehCache = new Cache(ehCacheConfig);
    ehCache.initialise();
    HttpCacheStorage storage = new EhcacheHttpCacheStorage(ehCache);
    cachingClient = new CachingHttpClient(this, storage, apacheCacheConfig);
}

From source file:fr.ippon.wip.http.hc.HttpClientResourceManager.java

private HttpClientResourceManager() {
    perUserClientMap = Collections.synchronizedMap(new HashMap<String, HttpClient>());
    perUserCookieStoreMap = Collections.synchronizedMap(new HashMap<String, CookieStore>());
    perUserWindowCredentialProviderMap = Collections
            .synchronizedMap(new HashMap<String, CredentialsProvider>());
    currentPortletRequest = new ThreadLocal<PortletRequest>();
    currentPortletResponse = new ThreadLocal<PortletResponse>();
    currentRequest = new ThreadLocal<RequestBuilder>();

    try {/*from w w w  . j a va  2s .  com*/
        SSLSocketFactory ssf = new SSLSocketFactory(new TrustSelfSignedStrategy(),
                new AllowAllHostnameVerifier());
        Scheme httpsScheme = new Scheme("https", 443, ssf);
        PlainSocketFactory psf = new PlainSocketFactory();
        Scheme httpScheme = new Scheme("http", 80, psf);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(httpsScheme);
        registry.register(httpScheme);
        connectionManager = new PoolingClientConnectionManager(registry);
        connectionManager.setDefaultMaxPerRoute(10);
        connectionManager.setMaxTotal(100);

        DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connectionManager);

        // automatically redirects all HEAD, GET and POST requests
        defaultHttpClient.setRedirectStrategy(new LaxRedirectStrategy());

        CacheConfig cacheConfig = createAndConfigureCache();

        URL ehCacheConfig = getClass().getResource("/ehcache.xml");
        cacheManager = CacheManager.create(ehCacheConfig);
        Ehcache ehcache = cacheManager.getEhcache("public");
        EhcacheHttpCacheStorage httpCacheStorage = new EhcacheHttpCacheStorage(ehcache);

        CachingHttpClient sharedCacheClient = new CachingHttpClient(defaultHttpClient, httpCacheStorage,
                cacheConfig);
        HttpClientDecorator decoratedClient = new HttpClientDecorator(sharedCacheClient);

        decoratedClient.addPreProcessor(new LtpaRequestInterceptor());
        decoratedClient.addPreProcessor(new StaleIfErrorRequestInterceptor(staleIfErrorTime));
        decoratedClient.addFilter(new IgnoreHttpRequestFilter());

        decoratedClient.addPostProcessor(new TransformerResponseInterceptor());

        rootClient = decoratedClient;

    } catch (Exception e) {
        throw new RuntimeException("Could not initialize connection manager", e);
    }

}

From source file:fr.ippon.wip.http.hc.HttpClientResourceManager.java

/**
 * Get or create an instance of org.apache.http.client.HttpClient. If
 * private cache is disabled for this portlet windowID, the instance
 * returned is always the same. If it is enabled, a specific instance is
 * returned for each windowID/sessionID. The instance returned wraps
 * multiple decorators on a unique/*from  w  ww  . j  a v a 2  s.  c o  m*/
 * org.apache.http.impl.client.DefaultHttpClient instance:
 * <ol>
 * <li>org.apache.http.impl.client.cache.CachingHttpClient for shared cache
 * (unique instance)</li>
 * <li>fr.ippon.wip.http.hc.HttpClientDecorator to inject specific
 * pre-precessor and post-processor interceptors: LtpaRequestInterceptor and
 * TransformerResponseInterceptor (unique instance)</li>
 * <li>org.apache.http.impl.client.cache.CachingHttpClient for private cache
 * (optional, one instance per windowID/sessionID</li>
 * </ol>
 * 
 * @param request
 *            Gives access to javax.portlet.PortletSession and windowID
 * @return
 */
public HttpClient getHttpClient(PortletRequest request) {
    String userSessionId = request.getPortletSession().getId();
    HttpClient client;
    synchronized (perUserClientMap) {
        client = perUserClientMap.get(userSessionId);
        if (client == null) {
            WIPConfiguration config = WIPUtil.getConfiguration(request);
            if (!config.isPageCachePrivate()) {
                client = rootClient;
            } else {
                CacheConfig cacheConfig = createAndConfigureCache();

                URL ehCacheConfig = getClass().getResource("/ehcache.xml");
                cacheManager = CacheManager.create(ehCacheConfig);
                Ehcache ehcache = cacheManager.getEhcache("private");
                EhcacheHttpCacheStorage httpCacheStorage = new EhcacheHttpCacheStorage(ehcache);

                client = new CachingHttpClient(rootClient, httpCacheStorage, cacheConfig);
            }

            perUserClientMap.put(userSessionId, client);
        }
    }
    return client;
}

From source file:org.apache.marmotta.platform.core.services.http.HttpClientServiceImpl.java

@PostConstruct
protected void initialize() {
    try {/*from w w  w  .ja  v  a 2 s  .c o m*/
        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();
    }
}