2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.cloudify.connector.http;
25 import org.apache.http.Header;
26 import org.apache.http.HttpResponse;
27 import com.fasterxml.jackson.databind.ObjectMapper;
29 import org.onap.so.cloudify.base.client.CloudifyResponse;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
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;
40 public class HttpClientResponse implements CloudifyResponse {
42 private static Logger logger = LoggerFactory.getLogger(HttpClientResponse.class);
44 private HttpResponse response = null;
45 private String entityBody = null;
47 public HttpClientResponse(HttpResponse response)
49 this.response = response;
51 // Read the body so InputStream can be closed
52 if (response.getEntity() == null) {
54 logger.debug("No Response Body");
58 ByteArrayOutputStream responseBody = new ByteArrayOutputStream();
60 response.getEntity().writeTo(responseBody);
61 } catch (IOException e) {
62 throw new HttpClientException ("Error Reading Response Body", e);
64 entityBody = responseBody.toString();
65 logger.debug(entityBody);
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);
76 resp = mapper.readValue(entityBody, returnType);
77 } catch (Exception e) {
78 throw new HttpClientException ("Caught exception in getEntity", e);
84 public <T> T getErrorEntity(Class<T> returnType) {
85 return getEntity(returnType);
89 public InputStream getInputStream() {
90 return new ByteArrayInputStream (entityBody.getBytes());
94 public String getHeader(String name) {
95 return response.getFirstHeader(name).getValue();
99 public Map<String, String> headers() {
100 Map<String, String> headers = new HashMap<>();
102 Header responseHeaders[] = response.getAllHeaders();
103 for (Header h : responseHeaders) {
104 headers.put(h.getName(), h.getValue());