74df0e6ecb9ad3de4169b300f4a8d9ff5f701193
[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 © 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.apache.log4j.Logger;
45 import org.codehaus.jackson.map.ObjectMapper;
46
47 import com.woorea.openstack.base.client.OpenStackResponse;
48
49 import java.io.ByteArrayInputStream;
50 import java.io.ByteArrayOutputStream;
51 import java.io.IOException;
52 import java.io.InputStream;
53 import java.util.HashMap;
54 import java.util.Map;
55
56 public class HttpClientResponse implements OpenStackResponse {
57
58         private static Logger LOGGER = Logger.getLogger(HttpClientConnector.class);
59         
60     private HttpResponse response = null;
61     private String entityBody = null;
62
63     public HttpClientResponse(HttpResponse response)
64     {
65         this.response = response;
66         
67         // Read the body so InputStream can be closed
68         if (response.getEntity() == null) {
69                 // No body
70                 LOGGER.debug ("No Response Body");
71                 return;
72         }
73         
74                 ByteArrayOutputStream responseBody = new ByteArrayOutputStream();
75                 try {
76                         response.getEntity().writeTo(responseBody);
77                 } catch (IOException e) {
78                         throw new HttpClientException ("Error Reading Response Body", e);
79                 }
80                 entityBody = responseBody.toString();
81                 LOGGER.debug (entityBody);
82     }
83
84     
85     @Override
86         public <T> T getEntity (Class<T> returnType) {
87         // Get appropriate mapper, based on existence of a root element
88                 ObjectMapper mapper = HttpClientConnector.getObjectMapper (returnType);
89
90                 T resp = null;
91                 try {
92                         resp = mapper.readValue(entityBody, returnType);
93                 } catch (Exception e) {
94                         throw new HttpClientException ("Caught exception in getEntity", e);
95                 }
96                 return resp;
97     }
98
99     @Override
100     public <T> T getErrorEntity(Class<T> returnType) {
101         return getEntity(returnType);
102     }
103
104     @Override
105     public InputStream getInputStream() {
106                 return new ByteArrayInputStream (entityBody.getBytes());
107     }
108
109     @Override
110     public String header(String name) {
111         return response.getFirstHeader(name).getValue();
112     }
113
114     @Override
115     public Map<String, String> headers() {
116         Map<String, String> headers = new HashMap<String, String>();
117
118         Header responseHeaders[] = response.getAllHeaders();
119         for (Header h : responseHeaders) {
120             headers.put(h.getName(), h.getValue());
121         }
122
123         return headers;
124     }
125
126 }