Change the header to SO
[so.git] / bpmn / MSORESTClient / src / main / java / org / openecomp / mso / rest / APIResponse.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.rest;
22
23 import java.io.IOException;
24 import java.util.Arrays;
25
26 import org.apache.http.Header;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.util.EntityUtils;
29
30 /**
31  * An immutable class that encapsulates an API response.
32  * 
33  * @version 1.0
34  * @since 1.0
35  */
36 public class APIResponse {
37     private final int statusCode;
38     private final byte[] responseBody;
39     private final HttpHeader[] headers;
40
41     /**
42      * Internal method used to create http headers using the specified
43      * HttpResponse object.
44      *
45      * @param httpResponse used to create headers
46      * @return http headers
47      */
48     private static HttpHeader[] buildHeaders(final HttpResponse httpResponse) {
49         final Header[] headers = httpResponse.getAllHeaders();
50
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;
58         } 
59
60         return httpHeaders;
61     }
62
63     /**
64      * Create an APIResponse object using the specified HttpResponse object.
65      *
66      * @param httpResponse used to create the APIResponse
67      *
68      * @throws RESTException if unable to read from the HttpResponse object
69      */
70     public APIResponse(final HttpResponse httpResponse) throws RESTException {
71         try {
72             this.statusCode = httpResponse.getStatusLine().getStatusCode();
73
74             if (httpResponse.getEntity() == null)
75             {
76                 this.responseBody = null;
77             }
78             else
79             {
80                 this.responseBody = EntityUtils.toByteArray(httpResponse.getEntity());
81             }
82
83             this.headers = buildHeaders(httpResponse);
84         } catch (IOException ioe) {
85             throw new RESTException(ioe);
86         }
87     }
88
89     /**
90      * Gets the http status code returned by the api server.
91      * <p>
92      * For example, status code 200 represents 'OK.' 
93      *
94      * @return status code
95      */
96     public int getStatusCode() {
97         return this.statusCode;
98     }
99
100     /**
101      * Gets the http response body as a byte array.
102      *
103      * @return http response body
104      */
105     public byte[] getResponseBodyAsByteArray() {
106         // avoid exposing internals, create copy
107         if (this.responseBody != null) {
108             return Arrays.copyOf(this.responseBody, this.responseBody.length);
109         } else {
110             return null;
111         }
112     }
113
114     /**
115      * Gets the http response body as a string.
116      *
117      * @return http response body
118      */
119     public String getResponseBodyAsString() {
120         if (this.responseBody != null) {
121             return new String(this.responseBody);
122         } else {
123             return "";
124         }
125     }
126
127     /**
128      * Gets a list of all the headers returned by the API response.
129      *
130      * @return an array of all the HttpHeaders 
131      */
132     public HttpHeader[] getAllHeaders() {
133         // avoid exposing internals, create copy
134         return Arrays.copyOf(this.headers, this.headers.length);
135     }
136
137     public String getFirstHeader(String name) {
138         for (HttpHeader header : headers) {
139             if (header.getName().equals(name)) {
140                 return header.getValue();
141             }
142         }
143         return null;
144     }
145 }