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