468e859141f228cbbbd5c90814c79e561014d278
[so/libs.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * ============LICENSE_END=========================================================
15  */
16
17
18 package com.woorea.openstack.connector;
19
20 import org.apache.http.Header;
21 import org.apache.http.HttpResponse;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.woorea.openstack.base.client.OpenStackResponse;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 public class HttpClientResponse implements OpenStackResponse {
36
37     private static Logger LOGGER = LoggerFactory.getLogger(HttpClientConnector.class);
38
39     private HttpResponse response = null;
40     private String entityBody = null;
41
42     public HttpClientResponse(HttpResponse response)
43     {
44         this.response = response;
45
46         // Read the body so InputStream can be closed
47         if (response.getEntity() == null) {
48             // No body
49             LOGGER.debug ("No Response Body");
50             return;
51         }
52
53         ByteArrayOutputStream responseBody = new ByteArrayOutputStream();
54         try {
55             response.getEntity().writeTo(responseBody);
56         } catch (IOException e) {
57             throw new HttpClientException ("Error Reading Response Body", e);
58         }
59         entityBody = responseBody.toString();
60         LOGGER.debug (entityBody);
61     }
62
63
64     @Override
65     public <T> T getEntity (Class<T> returnType) {
66         // Get appropriate mapper, based on existence of a root element
67         ObjectMapper mapper = HttpClientConnector.getObjectMapper (returnType);
68
69         T resp = null;
70         try {
71             resp = mapper.readValue(entityBody, returnType);
72         } catch (Exception e) {
73             throw new HttpClientException ("Caught exception in getEntity", e);
74         }
75         return resp;
76     }
77
78     @Override
79     public <T> T getErrorEntity(Class<T> returnType) {
80         return getEntity(returnType);
81     }
82
83     @Override
84     public InputStream getInputStream() {
85         return new ByteArrayInputStream (entityBody.getBytes());
86     }
87
88     @Override
89     public String header(String name) {
90         return response.getFirstHeader(name).getValue();
91     }
92
93     @Override
94     public Map<String, String> headers() {
95         Map<String, String> headers = new HashMap<String, String>();
96
97         Header responseHeaders[] = response.getAllHeaders();
98         for (Header h : responseHeaders) {
99             headers.put(h.getName(), h.getValue());
100         }
101
102         return headers;
103     }
104
105 }