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, CacheConfig config) 

Source Link

Document

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

Usage

From source file:com.riksof.a320.http.CoreHttpClient.java

/**
 * This method is used to execute Get Http Request
 * /*from   w  ww.j av  a 2 s. co  m*/
 * @param url
 * @return
 * @throws ServerException
 */
public String executeGet(String url) throws ServerException {

    // Response String
    String responseString = null;

    headers = new HashMap<String, String>();

    try {

        CacheConfig cacheConfig = new CacheConfig();
        cacheConfig.setMaxCacheEntries(1000);
        cacheConfig.setMaxObjectSizeBytes(1024 * 1024);

        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOut);
        HttpConnectionParams.setSoTimeout(httpParameters, socketTimeOut);

        // Create http client object
        DefaultHttpClient realClient = new DefaultHttpClient(httpParameters);
        realClient.addResponseInterceptor(MakeCacheable.INSTANCE, 0);
        CachingHttpClient httpclient = new CachingHttpClient(realClient, cacheConfig);

        // Create http context object
        HttpContext localContext = new BasicHttpContext();

        // Check for cached data against URL
        if (Cache.getInstance().get(url) == null) {

            // Execute HTTP Get Request
            HttpGet httpget = new HttpGet(url);

            // Get http response
            HttpResponse response = httpclient.execute(httpget, localContext);

            for (Header h : response.getAllHeaders()) {
                headers.put(h.getName(), h.getValue());
            }

            // Create http entity object
            HttpEntity entity = response.getEntity();

            // Create and convert stream into string
            InputStream inStream = entity.getContent();
            responseString = convertStreamToString(inStream);

            // Cache url data
            Cache.getInstance().put(url, responseString);

        } else {

            // Returned cached data
            responseString = Cache.getInstance().get(url);
        }

    } catch (ClientProtocolException e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("ClientProtocolException");
    } catch (ConnectTimeoutException e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("ConnectTimeoutException");
    } catch (IOException e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("Request Timeout");
    } catch (Exception e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("Exception");
    } finally {
    }
    return responseString;
}