Skip to content

Instantly share code, notes, and snippets.

@cfalzone
Created September 16, 2013 22:29
Show Gist options
  • Save cfalzone/6587462 to your computer and use it in GitHub Desktop.
Save cfalzone/6587462 to your computer and use it in GitHub Desktop.
Solution to pulling dotCMS's /JSONContent with Apache Commons HttpClient
String stVarName = "UXThursdayEvents";
String url = "http://aquent.com/JSONContent?type=json&limit=1000&offset=0&q=";
try {
url = url + URLEncoder.encode("+structureName:"+stVarName+" +live:true +deleted:false", "UTF-8");
} catch (Exception e) {
failed.put(stVarName, "Unable to encode the url to fetch the data: "+e.getMessage());
throw new Exception();
}
Logger.info(this, "The url we are fetching: "+url);
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new DefaultRedirectStrategy() {
private static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
//get the location header to find out where to redirect to
Header locationHeader = response.getFirstHeader("location");
if (locationHeader == null) {
// got a redirect response, but no location header
throw new ProtocolException("Received redirect response " + response.getStatusLine() + " but no location header");
}
// The magic happens here:
String location = locationHeader.getValue().replaceAll(":","%3A").replaceAll("\\+","%2B").replaceAll (" ", "%20");
Logger.info(this, "The url we are redirecting to: "+location);
URI uri;
try {
uri = new URI(location);
} catch (Exception ex) {
throw new ProtocolException("Invalid redirect URI: " + location, ex);
}
HttpParams params = response.getParams();
// rfc2616 demands the location value be a complete URI
// Location = "Location" ":" absoluteURI
if (!uri.isAbsolute()) {
if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
}
// Adjust location URI
HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (target == null) {
throw new IllegalStateException("Target host not available in the HTTP context");
}
Logger.info(this, "The target Host is "+target.getHostName());
try {
URI requestURI = new URI(request.getRequestLine().getUri());
URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
uri = URIUtils.resolve(absoluteRequestURI, uri);
} catch (Exception ex) {
throw new ProtocolException(ex.getMessage(), ex);
}
}
if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {
RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);
if (redirectLocations == null) {
redirectLocations = new RedirectLocations();
context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
}
URI redirectURI;
if (uri.getFragment() != null) {
try {
HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
redirectURI = URIUtils.rewriteURI(uri, target, true);
} catch (Exception ex) {
throw new ProtocolException(ex.getMessage(), ex);
}
} else {
redirectURI = uri;
}
if (redirectLocations.contains(redirectURI)) {
throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'");
} else {
redirectLocations.add(redirectURI);
}
}
return uri;
}
});
HttpGet method = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(method);
} catch (Exception e) {
Logger.error(this, "Unable to fetch the data ("+url+")", e);
failed.put(stVarName, "Unable to fetch the data ("+url+"): "+e.getMessage());
throw new Exception();
}
InputStream inStream = response.getEntity().getContent();
StringWriter writer = new StringWriter();
IOUtils.copy(inStream, writer, "UTF-8");
String dataString = writer.toString();
JSONObject data;
JSONArray contentlets;
try {
data = new JSONObject(dataString);
contentlets = data.getJSONArray("contentlets");
} catch (Exception e) {
failed.put(stVarName, "Unable to get the data from the json: "+e.getMessage());
throw new Exception();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment