Java tutorial
/* * Copyright (c) 2013 HollowSoft @IgorMorais * * 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. * */ package com.hollowsoft.library.utility.request; import java.io.IOException; import java.util.List; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; 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.utils.URLEncodedUtils; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.hollowsoft.library.utility.utility.Constants; /** * @author Igor Morais * @author mor41s.1gor@gmail.com */ public class HttpRequest { /** * */ private final DefaultHttpClient httpClient = new DefaultHttpClient(); /** * * @param url * @param nameValuePairList * @return * @throws RequestException */ public JsonNode doGet(final String url, final List<NameValuePair> nameValuePairList) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } final String parameterString = URLEncodedUtils.format(nameValuePairList, Constants.DEFAULT_CHARSET.name()); try { final HttpGet httpGet = new HttpGet(url + "?" + parameterString); final HttpEntity httpEntity = httpClient.execute(httpGet).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } /** * * @param url * @param nameValuePairList * @param headerArray * @return * @throws RequestException */ public JsonNode doGet(final String url, final List<NameValuePair> nameValuePairList, final Header... headerArray) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } final String parameterString = URLEncodedUtils.format(nameValuePairList, Constants.DEFAULT_CHARSET.name()); try { final HttpGet httpGet = new HttpGet(url + "?" + parameterString); httpGet.setHeaders(headerArray); final HttpEntity httpEntity = httpClient.execute(httpGet).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } /** * * @param url * @param json * @return * @throws RequestException */ public JsonNode doPost(final String url, final String json) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } if (json == null || json.isEmpty()) { throw new IllegalArgumentException("The json cannot be null or empty."); } try { final HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(json, Constants.DEFAULT_CHARSET.name())); final HttpEntity httpEntity = httpClient.execute(httpPost).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } /** * * @param url * @param json * @param headerArray * @return * @throws RequestException */ public JsonNode doPost(final String url, final String json, final Header... headerArray) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } if (json == null || json.isEmpty()) { throw new IllegalArgumentException("The json cannot be null or empty."); } try { final HttpPost httpPost = new HttpPost(url); httpPost.setHeaders(headerArray); httpPost.setEntity(new StringEntity(json, Constants.DEFAULT_CHARSET.name())); final HttpEntity httpEntity = httpClient.execute(httpPost).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } /** * * @param url * @param jsonNode * @return * @throws RequestException */ public JsonNode doPost(final String url, final JsonNode jsonNode) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } if (jsonNode == null || jsonNode.isNull()) { throw new IllegalArgumentException("The jsonNode cannot be null or empty."); } try { final HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(jsonNode.toString(), Constants.DEFAULT_CHARSET.name())); final HttpEntity httpEntity = httpClient.execute(httpPost).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } /** * * @param url * @param jsonNode * @param headerArray * @return * @throws RequestException */ public JsonNode doPost(final String url, final JsonNode jsonNode, final Header... headerArray) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } if (jsonNode == null || jsonNode.isNull()) { throw new IllegalArgumentException("The jsonNode cannot be null or empty."); } try { final HttpPost httpPost = new HttpPost(url); httpPost.setHeaders(headerArray); httpPost.setEntity(new StringEntity(jsonNode.toString(), Constants.DEFAULT_CHARSET.name())); final HttpEntity httpEntity = httpClient.execute(httpPost).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } /** * * @param url * @param json * @return * @throws RequestException */ public JsonNode doPut(final String url, final String json) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } if (json == null || json.isEmpty()) { throw new IllegalArgumentException("The json cannot be null or empty."); } try { final HttpPut httpPut = new HttpPut(url); httpPut.setEntity(new StringEntity(json, Constants.DEFAULT_CHARSET.name())); final HttpEntity httpEntity = httpClient.execute(httpPut).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } /** * * @param url * @param json * @param headerArray * @return * @throws RequestException */ public JsonNode doPut(final String url, final String json, final Header... headerArray) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } if (json == null || json.isEmpty()) { throw new IllegalArgumentException("The json cannot be null or empty."); } try { final HttpPut httpPut = new HttpPut(url); httpPut.setHeaders(headerArray); httpPut.setEntity(new StringEntity(json, Constants.DEFAULT_CHARSET.name())); final HttpEntity httpEntity = httpClient.execute(httpPut).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } /** * * @param url * @param jsonNode * @return * @throws RequestException */ public JsonNode doPut(final String url, final JsonNode jsonNode) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } if (jsonNode == null || jsonNode.isNull()) { throw new IllegalArgumentException("The jsonNode cannot be null or empty."); } try { final HttpPut httpPut = new HttpPut(url); httpPut.setEntity(new StringEntity(jsonNode.toString(), Constants.DEFAULT_CHARSET.name())); final HttpEntity httpEntity = httpClient.execute(httpPut).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } /** * * @param url * @param jsonNode * @param headerArray * @return * @throws RequestException */ public JsonNode doPut(final String url, final JsonNode jsonNode, final Header... headerArray) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } if (jsonNode == null || jsonNode.isNull()) { throw new IllegalArgumentException("The jsonNode cannot be null or empty."); } try { final HttpPut httpPut = new HttpPut(url); httpPut.setHeaders(headerArray); httpPut.setEntity(new StringEntity(jsonNode.toString(), Constants.DEFAULT_CHARSET.name())); final HttpEntity httpEntity = httpClient.execute(httpPut).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } /** * * @param url * @param nameValuePairList * @return * @throws RequestException */ public JsonNode doDelete(final String url, final List<NameValuePair> nameValuePairList) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } final String parameterString = URLEncodedUtils.format(nameValuePairList, Constants.DEFAULT_CHARSET.name()); try { final HttpDelete httpDelete = new HttpDelete(url + "?" + parameterString); final HttpEntity httpEntity = httpClient.execute(httpDelete).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } /** * * @param url * @param nameValuePairList * @param headerArray * @return * @throws RequestException */ public JsonNode doDelete(final String url, final List<NameValuePair> nameValuePairList, final Header... headerArray) throws RequestException { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The url cannot be null or empty."); } final String parameterString = URLEncodedUtils.format(nameValuePairList, Constants.DEFAULT_CHARSET.name()); try { final HttpDelete httpDelete = new HttpDelete(url + "?" + parameterString); httpDelete.setHeaders(headerArray); final HttpEntity httpEntity = httpClient.execute(httpDelete).getEntity(); return new ObjectMapper().reader().readTree(httpEntity.getContent()); } catch (final ClientProtocolException e) { throw new RequestException(e); } catch (final JsonProcessingException e) { throw new RequestException(e); } catch (final IllegalStateException e) { throw new RequestException(e); } catch (final IOException e) { throw new RequestException(e); } } }