Created
November 28, 2012 10:42
-
-
Save leadVisionary/4160430 to your computer and use it in GitHub Desktop.
A study in expressive programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment