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