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