package org.oauth2.client4j.http; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.Consts; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.oauth2.client4j.common.OAuth; import org.oauth2.client4j.exception.OAuthException; import org.oauth2.client4j.http.client.HttpClient; import org.oauth2.client4j.http.response.OAuthResponse; import org.oauth2.client4j.utils.OAuthKit; public class HttpClient4 implements HttpClient { private org.apache.http.impl.client.CloseableHttpClient client; public HttpClient4() { client = HttpClients.createDefault(); } public HttpClient4(org.apache.http.impl.client.CloseableHttpClient client) { this.client = client; } public void close() { try { client.close(); } catch (IOException e) { client = null; } } public <T extends OAuthResponse> T execute(Request request, Class<T> responseClass) { try { URI location = new URI(request.getLocationUri()); HttpRequestBase req = null; if (!OAuthKit.isEmpty(request.getRequestType()) && OAuth.HttpMethod.POST.equals(request.getRequestType())) { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> body : request .getBodyNameValuePair().entrySet()) { formparams.add(new BasicNameValuePair(body.getKey(), body .getValue())); } HttpEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); req = new HttpPost(location); ((HttpPost) req).setEntity(entity); } else { req = new HttpGet(location); } if (request.getHeaders() != null) { for (Map.Entry<String, String> header : request.getHeaders() .entrySet()) { req.setHeader(header.getKey(), header.getValue()); } } HttpResponse response = client.execute(req); Header contentTypeHeader = null; Header encodingHeader = null; String responseBody = ""; HttpEntity entity = response.getEntity(); if (entity != null) { responseBody = EntityUtils.toString(entity); contentTypeHeader = entity.getContentType(); encodingHeader = entity.getContentEncoding(); } String contentType = null; String characterEncoding = null; if (contentTypeHeader != null) { contentType = contentTypeHeader.toString(); } if (encodingHeader != null) { characterEncoding = encodingHeader.toString(); } return OAuthKit.createCustomResponse(responseBody, contentType, characterEncoding, response.getStatusLine().getStatusCode(), responseClass); } catch (URISyntaxException e) { throw new OAuthException(e); } catch (ClientProtocolException e) { throw new OAuthException(e); } catch (IOException e) { throw new OAuthException(e); } } }