1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.amber.oauth2.httpclient4;
23
24 import java.net.URI;
25 import java.util.Map;
26
27 import org.apache.amber.oauth2.client.HttpClient;
28 import org.apache.amber.oauth2.client.request.OAuthClientRequest;
29 import org.apache.amber.oauth2.client.response.OAuthClientResponse;
30 import org.apache.amber.oauth2.client.response.OAuthClientResponseFactory;
31 import org.apache.amber.oauth2.common.OAuth;
32 import org.apache.amber.oauth2.common.exception.OAuthProblemException;
33 import org.apache.amber.oauth2.common.exception.OAuthSystemException;
34 import org.apache.amber.oauth2.common.utils.OAuthUtils;
35 import org.apache.http.Header;
36 import org.apache.http.HttpEntity;
37 import org.apache.http.HttpResponse;
38 import org.apache.http.client.methods.HttpGet;
39 import org.apache.http.client.methods.HttpPost;
40 import org.apache.http.client.methods.HttpRequestBase;
41 import org.apache.http.conn.ClientConnectionManager;
42 import org.apache.http.entity.StringEntity;
43 import org.apache.http.impl.client.DefaultHttpClient;
44 import org.apache.http.util.EntityUtils;
45
46
47
48
49
50
51
52
53
54 public class HttpClient4 implements HttpClient {
55
56 private org.apache.http.client.HttpClient client;
57
58 public HttpClient4() {
59 client = new DefaultHttpClient();
60 }
61
62 public HttpClient4(org.apache.http.client.HttpClient client) {
63 this.client = client;
64 }
65
66 public void shutdown() {
67 if (client != null) {
68 ClientConnectionManager connectionManager = client.getConnectionManager();
69 if (connectionManager != null) {
70 connectionManager.shutdown();
71 }
72 }
73 }
74
75 public <T extends OAuthClientResponse> T execute(OAuthClientRequest request,
76 Map<String, String> headers,
77 String requestMethod,
78 Class<T> responseClass)
79 throws OAuthSystemException, OAuthProblemException {
80
81 try {
82 URI location = new URI(request.getLocationUri());
83 HttpRequestBase req = null;
84 String responseBody = "";
85
86 if (!OAuthUtils.isEmpty(requestMethod) && OAuth.HttpMethod.POST.equals(requestMethod)) {
87 req = new HttpPost(location);
88 HttpEntity entity = new StringEntity(request.getBody());
89 ((HttpPost)req).setEntity(entity);
90 } else {
91 req = new HttpGet(location);
92 }
93 if (headers != null && !headers.isEmpty()) {
94 for (Map.Entry<String, String> header : headers.entrySet()) {
95 req.setHeader(header.getKey(), header.getValue());
96 }
97 }
98 if (request.getHeaders() != null) {
99 for (Map.Entry<String, String> header : request.getHeaders().entrySet()) {
100 req.setHeader(header.getKey(), header.getValue());
101 }
102 }
103 HttpResponse response = client.execute(req);
104 Header contentTypeHeader = null;
105 HttpEntity entity = response.getEntity();
106 if (entity != null) {
107 responseBody = EntityUtils.toString(entity);
108 contentTypeHeader = entity.getContentType();
109 }
110 String contentType = null;
111 if (contentTypeHeader != null) {
112 contentType = contentTypeHeader.toString();
113 }
114
115 return OAuthClientResponseFactory
116 .createCustomResponse(responseBody, contentType, response.getStatusLine().getStatusCode(),
117 responseClass);
118 } catch (Exception e) {
119 throw new OAuthSystemException(e);
120 }
121
122 }
123 }