diff --git a/src/main/java/io/github/rhkiswani/javaff/httpclient/ApacheHttpClient2.java b/src/main/java/io/github/rhkiswani/javaff/httpclient/ApacheHttpClient2.java new file mode 100644 index 0000000..034df8a --- /dev/null +++ b/src/main/java/io/github/rhkiswani/javaff/httpclient/ApacheHttpClient2.java @@ -0,0 +1,181 @@ +/* + * Copyright 2016 Mohamed Kiswani. + * + * 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 io.github.rhkiswani.javaff.httpclient; + +import io.github.rhkiswani.javaff.exceptions.SmartException; +import io.github.rhkiswani.javaff.httpclient.exceptions.HttpClientException; +import io.github.rhkiswani.javaff.httpclient.util.IOUtil; +import io.github.rhkiswani.javaff.json.JsonHandlerFactory; +import io.github.rhkiswani.javaff.reflection.ReflectionUtil; +import org.apache.http.Header; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.*; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.message.BasicNameValuePair; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author Mohamed Kiswani + * @since 0.0.20 + * + */ +public class ApacheHttpClient2 implements HttpClient{ + + @Override + public String postJson(String url, String json, Map headers) throws HttpClientException { + return doJsonRequest(new HttpPost(url), json, headers); + } + + @Override + public String putJson(String url, String json, Map headers) throws HttpClientException { + return doJsonRequest(new HttpPut(url), json, headers); + } + + private String doJsonRequest(HttpRequestBase method, String json, Map headers) { + try { + if (json == null){ + throw new HttpClientException(SmartException.NULL_VAL, "Json Object"); + } + CloseableHttpClient c = HttpClientBuilder.create().build(); + ((HttpEntityEnclosingRequestBase) method).setEntity(new StringEntity(json)); + method.setHeader("Accept", "application/json"); + method.setHeader("Content-type", "application/json"); + setHeaders(method, headers); + return handleResponse(method, c); + } catch (Exception e) { + throw new HttpClientException(e.getMessage()); + } + } + + private String handleResponse(HttpRequestBase method, CloseableHttpClient c) { + HashMap hashMap = new HashMap<>(); + try { + HttpResponse response = c.execute(method); + hashMap.put("response", IOUtil.inputStreamToString(response.getEntity().getContent())); + hashMap.put("status", response.getStatusLine().getStatusCode()); + hashMap.put("headers", IOUtil.headersToMap(response.getAllHeaders())); + } catch (Exception ex) { + hashMap.put("status", HttpStatus.SC_BAD_REQUEST ); + hashMap.put("errors", ReflectionUtil.getErrorsAsArray(ex)); + } + return JsonHandlerFactory.getJsonHandler(HashMap.class).toJson(hashMap); + } + + @Override + public String post(String url, Map params, Map headers) throws HttpClientException { + return doRequest(new HttpPost(url), params, headers); + } + + @Override + public String put(String url, Map params, Map headers) throws HttpClientException { + return doRequest(new HttpPut(url), params, headers); + } + + private String doRequest(HttpRequestBase method, Map params, Map headers) { + try { + CloseableHttpClient c = prepareRequest(method, params); + setHeaders(method, headers); + return handleResponse(method, c); + } catch (Exception e) { + throw new HttpClientException(e.getMessage()); + } + } + + private CloseableHttpClient prepareRequest(HttpRequestBase method, Map params) throws UnsupportedEncodingException { + CloseableHttpClient c = HttpClientBuilder.create().build(); + if(params != null){ + List urlParameters = new ArrayList<>(); + for (String key : params.keySet()) { + + urlParameters.add(new BasicNameValuePair(key, params.get(key))); + } + if (method instanceof HttpEntityEnclosingRequestBase){ + ((HttpEntityEnclosingRequestBase) method).setEntity(new UrlEncodedFormEntity(urlParameters)); + } + } + return c; + } + + private void setHeaders(HttpRequestBase method, Map headers) { + if(headers != null){ + for (String header : headers.keySet()) { + method.setHeader(header, headers.get(header)); + } + } + } + + @Override + public String get(String url, Map params, Map headers) throws HttpClientException { + String urlWithParams = paramsToString(url, params); + return doRequest(new HttpGet(urlWithParams), params, headers); + } + + private String paramsToString(String url, Map params) { + String urlWithParams = url; + if(params != null){ + urlWithParams += "?"; + for (String key : params.keySet()) { + urlWithParams += key + "=" + params.get(key) +"&"; + } + } + return urlWithParams; + } + + @Override + public Map head(String url, Map params, Map headers) throws HttpClientException { + try { + HttpHead method = new HttpHead(url); + CloseableHttpClient client = prepareRequest(method, params); + setHeaders(method, headers); + HttpResponse response = client.execute(method); + Header[] s = response.getAllHeaders(); + Map returnedHeaders = new HashMap<>(); + for (Header header : s) { + returnedHeaders.put(header.getName(), header.getValue()); + } + return returnedHeaders; + } catch (Exception e) { + throw new HttpClientException(e.getMessage()); + } + } + + @Override + public String delete(String url, Map params, Map headers) throws HttpClientException { + String urlWithParams = paramsToString(url, params); + return doRequest(new HttpDelete(urlWithParams), params, headers); + } + + @Override + public Map options(String url, Map params, Map headers) throws HttpClientException { + return head(url, params, headers); + } + + + @Override + public String patchJson(String url, String json, Map headers) throws HttpClientException { + return doJsonRequest(new HttpPatch(url), json, headers); + } +}