All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.tno.bim.nmd.services.PersistentHttpCache Maven / Gradle / Ivy

The newest version!
package nl.tno.bim.nmd.services;

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blogspot.mydailyjava.guava.cache.overflow.FileSystemCacheBuilder;
import com.google.common.cache.Cache;

/**
 * Createa a persistent cache for http GET request by using the hash of the http
 * request
 * 
 * @author vijj
 *
 */
public class PersistentHttpCache {

	Logger log = LoggerFactory.getLogger(PersistentHttpCache.class);
	File persistenceDirectory = new File(System.getProperty("java.io.tmpdir"), "NMD3_cache/");
	Cache responseCache;

	public PersistentHttpCache() {
		responseCache = FileSystemCacheBuilder.newBuilder()
				.persistenceDirectory(persistenceDirectory)
				.maximumSize(1L)
				.build();
	}

	public String MD5(HttpUriRequest md5) {
		try {
			java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
			byte[] array = md.digest(md5.getURI().toString().getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
			}
			return sb.toString();
		} catch (java.security.NoSuchAlgorithmException e) {
		}
		return null;
	}

	public HttpResponse get(HttpUriRequest key) {
		String md5key = MD5(key);
		HttpResponse response = null;
		try {
			String entity = responseCache.getIfPresent(md5key);
			
			if (entity == null || entity.contains("Access_TokenExpired")) {
				remove(key);
				return null;
			}
			
			response = new BasicHttpResponse(
					new BasicStatusLine(
							new ProtocolVersion("HTTP", 1, 1),
							200,
							"from cache"));
			response.setEntity(new StringEntity(entity));

		} catch (Exception e) {
			log.warn(String.format("Could not retrieve %s from cache", md5key));
		}

		return response;
	}

	public HttpResponse put(HttpUriRequest key, HttpResponse value) {
		String md5key = MD5(key);
		try {
			String entity = EntityUtils.toString(value.getEntity());
			if (entity.contains("Access_TokenExpired")) {
				throw new IOException("Trying to cache expired response.");
			}
			
			responseCache.put(md5key, entity);
			HttpResponse response = new BasicHttpResponse(
					new BasicStatusLine(
							new ProtocolVersion("HTTP", 1, 1),
							200,
							"cached response"));
			response.setEntity(new StringEntity(entity));
			log.info(String.format("stored %s in cache", md5key));
			return response;
		} catch (ParseException | IOException e) {
			log.warn(String.format("failed to store %s in cache", md5key));
		}
		return null;
	}

	public void remove(HttpUriRequest key) {
		String md5key = MD5(key);
		log.warn(String.format("removing %s from cache", md5key));
		responseCache.invalidate(MD5(key));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy