Skip to content

Instantly share code, notes, and snippets.

@jtabone16
Created June 23, 2013 00:08
Show Gist options
  • Save jtabone16/5843187 to your computer and use it in GitHub Desktop.
Save jtabone16/5843187 to your computer and use it in GitHub Desktop.
Updated RestRequest client using Apache HttpComponents
package com.proctorcam.proctorserv;
//Standard Java lib
import java.util.*;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
//Apache libs for HTTP POST/GET
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.commons.io.IOUtils;
//GSON lib for JSON parsing
import com.google.gson.Gson;
public class RestRequestClient
{
public RestRequestClient()
{
}
/**
* makeGetRequest calls HashedAuthenticator.applyReverseGuidAndSign() to mutate params
* into a properly signed request. The GET request to the queryURL (String) created by
* buildURL is made using the Apache HttpComponents library (HttpClient and HttpCore)
* included in the lib directory. A response (HttpResponse) is returned from which the
* response body and status code can be extracted.
*/
public HttpResponse makeGetRequest(String url, String customer_identifier, String shared_secret, HashMap<String,Object> params) throws IOException
{
HashedAuthenticator.applyReverseGuidAndSign(params, customer_identifier, shared_secret);
HttpClient client = new DefaultHttpClient();
String queryURL = buildURL(url, params);
HttpGet request = new HttpGet(queryURL);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(request);
return response;
}
/**
* makePostRequest calls HashedAuthenticator.applyReverseGuidAndSign() to mutate params
* into a properly signed request. The POST request to the url (String) is made using
* the Apache HttpComponents library (HttpClient and HttpCore) included in the lib
* directory. A response (HttpResponse) is returned from which the response body and
* status code can be extracted.
*
* The GSON library (provided in the lib directory) parses params (HashMap) into a JSON
* formatted String that can correctly be posted to Proctorserve.
*/
public HttpResponse makePostRequest(String url, String customer_identifier, String shared_secret, HashMap<String,Object> params) throws IOException, UnsupportedEncodingException
{
HashedAuthenticator.applyReverseGuidAndSign(params, customer_identifier, shared_secret);
Gson gson = new Gson();
String json = gson.toJson(params);
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setEntity(new StringEntity(json, "UTF-8"));
HttpResponse response = client.execute(request);
return response;
}
/**
* Creates a URL using url (String) and appending to it the key-value pairs
* found in params (HashMap). The resulting String is used to make GET requests
* in makeGetRequest,
*/
protected String buildURL (String url, HashMap<String,Object> params) {
StringBuilder query = new StringBuilder(url).append("?");
for (String key : params.keySet()) {
String data = String.valueOf(params.get(key));
query.append(key).append("=").append(data).append("&");
}
query.deleteCharAt(query.length()-1);
String queryURL = query.toString();
return queryURL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment