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;
25 import org.apache.http.Header;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.util.EntityUtils;
30 * An immutable class that encapsulates an API response.
35 public class APIResponse {
36 private final int statusCode;
37 private final byte[] responseBody;
38 private final HttpHeader[] headers;
41 * Internal method used to create http headers using the specified
42 * HttpResponse object.
44 * @param httpResponse used to create headers
45 * @return http headers
47 private HttpHeader[] buildHeaders(final HttpResponse httpResponse) {
48 final Header[] headers = httpResponse.getAllHeaders();
50 HttpHeader[] httpHeaders = new HttpHeader[headers.length];
51 for (int i = 0; i < headers.length; ++i) {
52 final Header header = headers[i];
53 final String name = header.getName();
54 final String value = header.getValue();
55 final HttpHeader httpHeader = new HttpHeader(name, value);
56 httpHeaders[i] = httpHeader;
63 * Create an APIResponse object using the specified HttpResponse object.
65 * @param httpResponse used to create the APIResponse
67 * @throws RESTException if unable to read from the HttpResponse object
69 public APIResponse(final HttpResponse httpResponse) throws RESTException {
71 this.statusCode = httpResponse.getStatusLine().getStatusCode();
73 if (httpResponse.getEntity() == null)
75 this.responseBody = null;
79 this.responseBody = EntityUtils.toByteArray(httpResponse.getEntity());
82 this.headers = buildHeaders(httpResponse);
83 } catch (IOException ioe) {
84 throw new RESTException(ioe);
89 * Gets the http status code returned by the api server.
91 * For example, status code 200 represents 'OK.'
95 public int getStatusCode() {
96 return this.statusCode;
100 * Gets the http response body as a byte array.
102 * @return http response body
104 public byte[] getResponseBodyAsByteArray() {
105 return this.responseBody;
109 * Gets the http response body as a string.
111 * @return http response body
113 public String getResponseBodyAsString() {
114 if (this.responseBody != null) {
115 return new String(this.responseBody);
122 * Gets a list of all the headers returned by the API response.
124 * @return an array of all the HttpHeaders
126 public HttpHeader[] getAllHeaders() {
127 // avoid exposing internals, create copy
128 HttpHeader[] copy = new HttpHeader[this.headers.length];
129 for (int i = 0; i < this.headers.length; ++i) {
130 copy[i] = headers[i];
135 public String getFirstHeader(String name) {
136 for (HttpHeader header : headers) {
137 if (header.getName().equals(name)) {
138 return header.getValue();