27 December, 2013

Java REST API HTTP Helper

To ease up on HTTP calls I've build a helper that takes care of their inner workings.

package com.milagaia.helper;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

public class HttpHelper {

 protected static final String UTF8 = "UTF-8";
 protected static final String CONTENT_TYPE = "application/json; charset=utf-8";
 
 public InputStream Post(String url, String content, int timeoutMillis) 
   throws ConnectTimeoutException, UnsupportedEncodingException, ClientProtocolException, IOException {

  InputStream result = null;

  DefaultHttpClient httpClient = buildHttpClient(timeoutMillis);
  HttpUriRequest req = buildPost(url, content);

  HttpResponse httpResponse = httpClient.execute(req);

  HttpEntity httpEntity = httpResponse.getEntity();
  result = httpEntity.getContent();

  return result;
 }

 public boolean Put(String url, String content, int timeoutMillis) 
   throws ConnectTimeoutException, UnsupportedEncodingException, ClientProtocolException, IOException {

  DefaultHttpClient httpClient = buildHttpClient(timeoutMillis);
  HttpUriRequest req = buildPut(url, content);

  HttpResponse httpResponse = httpClient.execute(req);

  int statusCode = httpResponse.getStatusLine().getStatusCode();

  return statusCode == HttpStatus.SC_OK;
 }

 public InputStream Get(String url, int timeoutMillis) 
   throws ConnectTimeoutException, UnsupportedEncodingException, ClientProtocolException, IOException {

  InputStream result = null;

  DefaultHttpClient httpClient = buildHttpClient(timeoutMillis);
  HttpUriRequest req = buildGet(url);

  HttpResponse httpResponse = httpClient.execute(req);

  HttpEntity httpEntity = httpResponse.getEntity();
  result = httpEntity.getContent();

  return result;
 }

 public InputStream Delete(String url, int timeoutMillis) 
   throws ConnectTimeoutException, UnsupportedEncodingException, ClientProtocolException, IOException {

  InputStream result = null;

  DefaultHttpClient httpClient = buildHttpClient(timeoutMillis);
  HttpUriRequest req = buildDelete(url);

  HttpResponse httpResponse = httpClient.execute(req);

  int statusCode = httpResponse.getStatusLine().getStatusCode();

  if (statusCode == HttpStatus.SC_OK) {

   HttpEntity httpEntity = httpResponse.getEntity();
   result = httpEntity.getContent();
  }

  return result;
 }

 private DefaultHttpClient buildHttpClient(int timeoutMillis) {

  DefaultHttpClient httpClient = new DefaultHttpClient();
  HttpParams httpParameters = httpClient.getParams();
  HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutMillis);
  HttpConnectionParams.setSoTimeout(httpParameters, timeoutMillis);

  return httpClient;
 }

 private HttpPost buildPost(String url, String content) 
   throws UnsupportedEncodingException {

  HttpPost httpPost = new HttpPost(url);
  ByteArrayEntity baEntity = new ByteArrayEntity(content.getBytes(UTF8));
  baEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, CONTENT_TYPE));
  httpPost.setEntity(baEntity);

  return httpPost;
 }

 private HttpGet buildGet(String url) 
   throws UnsupportedEncodingException {

  HttpGet httpGet = new HttpGet(url);

  return httpGet;
 }

 private HttpPut buildPut(String url, String content) 
   throws UnsupportedEncodingException {

  HttpPut httpPut = new HttpPut(url);
  ByteArrayEntity baEntity = new ByteArrayEntity(content.getBytes(UTF8));
  baEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, CONTENT_TYPE));
  httpPut.setEntity(baEntity);

  return httpPut;
 }

 private HttpDelete buildDelete(String url) 
   throws UnsupportedEncodingException {

  HttpDelete httpDelete = new HttpDelete(url);

  return httpDelete;
 }
}
And when i need more advanced features such as authentication I either extend or make the necessary changes. 
Hope this helps someone else.

No comments:

Post a Comment