Example usage for org.apache.http.impl.client.cache.ehcache EhcacheHttpCacheStorage EhcacheHttpCacheStorage

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

Introduction

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

Prototype

public EhcacheHttpCacheStorage(Ehcache cache) 

Source Link

Document

Constructs a storage backend using the provided Ehcache with default configuration options.

Usage

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

/**
 * /*  w w  w  . j  a va  2s.c  om*/
 * @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 ww.ja  va 2  s  .  c  o  m*/
        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   www .  j ava  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;
}