2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.rest;
23 import java.io.IOException;
24 import java.util.Arrays;
26 import org.apache.http.Header;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.util.EntityUtils;
31 * An immutable class that encapsulates an API response.
36 public class APIResponse {
37 private final int statusCode;
38 private final byte[] responseBody;
39 private final HttpHeader[] headers;
42 * Internal method used to create http headers using the specified
43 * HttpResponse object.
45 * @param httpResponse used to create headers
46 * @return http headers
48 private static HttpHeader[] buildHeaders(final HttpResponse httpResponse) {
49 final Header[] headers = httpResponse.getAllHeaders();
51 HttpHeader[] httpHeaders = new HttpHeader[headers.length];
52 for (int i = 0; i < headers.length; ++i) {
53 final Header header = headers[i];
54 final String name = header.getName();
55 final String value = header.getValue();
56 final HttpHeader httpHeader = new HttpHeader(name, value);
57 httpHeaders[i] = httpHeader;
64 * Create an APIResponse object using the specified HttpResponse object.
66 * @param httpResponse used to create the APIResponse
68 * @throws RESTException if unable to read from the HttpResponse object
70 public APIResponse(final HttpResponse httpResponse) throws RESTException {
72 this.statusCode = httpResponse.getStatusLine().getStatusCode();
74 if (httpResponse.getEntity() == null)
76 this.responseBody = null;
80 this.responseBody = EntityUtils.toByteArray(httpResponse.getEntity());
83 this.headers = buildHeaders(httpResponse);
84 } catch (IOException ioe) {
85 throw new RESTException(ioe);
90 * Gets the http status code returned by the api server.
92 * For example, status code 200 represents 'OK.'
96 public int getStatusCode() {
97 return this.statusCode;
101 * Gets the http response body as a byte array.
103 * @return http response body
105 public byte[] getResponseBodyAsByteArray() {
106 // avoid exposing internals, create copy
107 if (this.responseBody != null) {
108 return Arrays.copyOf(this.responseBody, this.responseBody.length);
115 * Gets the http response body as a string.
117 * @return http response body
119 public String getResponseBodyAsString() {
120 if (this.responseBody != null) {
121 return new String(this.responseBody);
128 * Gets a list of all the headers returned by the API response.
130 * @return an array of all the HttpHeaders
132 public HttpHeader[] getAllHeaders() {
133 // avoid exposing internals, create copy
134 return Arrays.copyOf(this.headers, this.headers.length);
137 public String getFirstHeader(String name) {
138 for (HttpHeader header : headers) {
139 if (header.getName().equals(name)) {
140 return header.getValue();