View Javadoc

1   /**
2    *       Copyright 2010 Newcastle University
3    *
4    *          http://research.ncl.ac.uk/smart/
5    *
6    * Licensed to the Apache Software Foundation (ASF) under one or more
7    * contributor license agreements.  See the NOTICE file distributed with
8    * this work for additional information regarding copyright ownership.
9    * The ASF licenses this file to You under the Apache License, Version 2.0
10   * (the "License"); you may not use this file except in compliance with
11   * the License.  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
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   * Exemplar HttpClient4
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 }