Java tutorial
// // RestRequestClient.java // ProctorservApi // // Copyright 2013 ProctorCam, Inc // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Created by John Tabone on 6/6/13. // package com.proctorcam.proctorserv; //Standard Java lib import java.util.*; import java.io.UnsupportedEncodingException; import java.io.IOException; import java.net.URLEncoder; //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; } }