public
Last active

A study in expressive programming

  • Download Gist
HttpCourier.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
 
/**
* HttpCourier - utility class to retrieve info from the web.
*
* @author Nicholas Vaidyanathan
*
*/
final class HttpCourier {
/**
* the log.
*/
private static Log log = LogFactory.getLog(HttpCourier.class);
/**
* Don't instantiate.
*/
private HttpCourier() {
}
 
/**
* connect - connects to a url, if the
* content is not previously cached. Otherwise,
* returns the cached content.
* @param toGo
* - a url to connect to.
* @return - the response body as a string.
*/
public static String connect(final String toGo) {
String result = "";
if (TravelLog.visitedRecently(toGo)) {
result = TravelLog.memoriesOf(toGo);
} else {
result = buildURI(toGo);
TravelLog.rememberVisit(toGo, result);
}
return result;
}
/**
* buildURI - creates a URI from a string and tries to connect
* @param toGo - the string to create a URI from
* @return the results of the expedition
*/
private static String buildURI(final String toGo) {
URI toVisit;
try {
toVisit = new URI(toGo);
} catch (final URISyntaxException e) {
log.info("Could not construct URI from " + toGo + " got back "
+ e.getLocalizedMessage());
return "";
}
return retrieveFrom(toVisit);
}
 
/**
* retrieveFrom - makes a HTTP GET request to the provided URL. Gets the
* response as a string.
*
* @param toGo
* - a URL to make a GET request to.
* @return the response body
*/
private static String retrieveFrom(final URI toGo) {
String responseBody = "";
final HttpClient httpclient = new DefaultHttpClient();
 
try {
final HttpGet httpget = new HttpGet(toGo);
 
// Create a response handler
final ResponseHandler<String> hand = new BasicResponseHandler();
responseBody = httpclient.execute(httpget, hand);
} catch (final ClientProtocolException e) {
log.info("Called " + toGo + " got back " + e.getLocalizedMessage());
} catch (final IOException e) {
log.info("Called " + toGo + " got back " + e.getLocalizedMessage());
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
return responseBody;
}
}
TravelLog.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
 
/**
* TravelLog - A cache for the HttpCourier.
* Reduces unnecessary trips by drawing from memory,
* if the time since the last trip hasn't been too long.
* @author Nicholas Vaidyanathan
*
*/
final class TravelLog {
/**
* A cache of old journeys
* to reduce latency.
*/
private static Map<String,String> cache = Collections.emptyMap();
/**
* tells when the last trip was taken, so the cache doesn't
* get stale and can be evicted.
*/
private static Date lastTrip;
/**
* visitedRecently - tells whether a location
* has been visited and if the last visit was
* too long ago.
* @param location -the location to check.
* @return true if the location was visited not too long ago.
*/
public static boolean visitedRecently(String location) {
return hasVisited(location) && tripTakenRecently();
}
/**
* tripTakenRecently - the cache staleness indicator
* set for one week.
* @return true if a trip was taken less than a week ago.
*/
private static boolean tripTakenRecently() {
Calendar oneWeekBeforeToday = Calendar.getInstance();
oneWeekBeforeToday.setTime(new Date());
oneWeekBeforeToday.add(Calendar.DATE, -7);
Calendar lastRecorded = Calendar.getInstance();
lastRecorded.setTime(lastTrip);
return oneWeekBeforeToday.before(lastRecorded);
}
 
/**
* hasVisited - tells whether a new adventure is necessary.
* @param location the place to go
* @return true if the location has been visited before.
*/
public static boolean hasVisited(String location) {
return cache.containsKey(location);
}
/**
* rememberVisit - stores result of trip in cache.
* Also notes that this trip was taken last, to avoid
* unnecessary trips.
* @param place - the location visited.
* @param experience - the result of the trip.
*/
public static void rememberVisit(String place, String experience) {
cache.put(place, experience);
lastTrip = new Date();
}
/**
* memoriesOf - gets the contents of the cache
* for a given place.
* @param location - the place to remember.
* @return memories if present, otherwise an empty string.
*/
public static String memoriesOf(String location) {
String result = "";
if(hasVisited(location)) {
result = cache.get(location);
}
return result;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.