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
7 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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
26 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
35 * ECOMP and OpenECOMP are trademarks
36 * and service marks of AT&T Intellectual Property.
40 package com.woorea.openstack.connector;
42 import org.apache.http.Header;
43 import org.apache.http.HttpResponse;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.woorea.openstack.base.client.OpenStackResponse;
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;
57 public class HttpClientResponse implements OpenStackResponse {
59 private static Logger LOGGER = LoggerFactory.getLogger(HttpClientConnector.class);
61 private HttpResponse response = null;
62 private String entityBody = null;
64 public HttpClientResponse(HttpResponse response)
66 this.response = response;
68 // Read the body so InputStream can be closed
69 if (response.getEntity() == null) {
71 LOGGER.debug ("No Response Body");
75 ByteArrayOutputStream responseBody = new ByteArrayOutputStream();
77 response.getEntity().writeTo(responseBody);
78 } catch (IOException e) {
79 throw new HttpClientException ("Error Reading Response Body", e);
81 entityBody = responseBody.toString();
82 LOGGER.debug (entityBody);
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);
93 resp = mapper.readValue(entityBody, returnType);
94 } catch (Exception e) {
95 throw new HttpClientException ("Caught exception in getEntity", e);
101 public <T> T getErrorEntity(Class<T> returnType) {
102 return getEntity(returnType);
106 public InputStream getInputStream() {
107 return new ByteArrayInputStream (entityBody.getBytes());
111 public String header(String name) {
112 return response.getFirstHeader(name).getValue();
116 public Map<String, String> headers() {
117 Map<String, String> headers = new HashMap<String, String>();
119 Header responseHeaders[] = response.getAllHeaders();
120 for (Header h : responseHeaders) {
121 headers.put(h.getName(), h.getValue());