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