2b1820e6e42195ee19cc0b1be70809cde196d24a
[so/libs.git] /
1 /*-
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
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
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=========================================================
15  */
16
17 package com.woorea.openstack.connector;
18
19 /*
20  * Modifications copyright (c) 2017 AT&T Intellectual Property
21  */
22
23 import java.io.InputStream;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import javax.ws.rs.core.Response;
28
29 import com.woorea.openstack.base.client.OpenStackResponse;
30 import com.woorea.openstack.base.client.OpenStackResponseException;
31
32 public class JaxRs20Response implements OpenStackResponse {
33     
34     private Response response;
35     
36     public JaxRs20Response(Response response) {
37         this.response = response;
38     }
39
40     @Override
41     public <T> T getEntity(Class<T> returnType) {
42         if(response.getStatus() >= 400) {
43             throw new OpenStackResponseException(response.getStatusInfo().getReasonPhrase(),
44                     response.getStatusInfo().getStatusCode(), this);
45         }
46         return response.readEntity(returnType);
47     }
48
49     @Override
50     public <T> T getErrorEntity(Class<T> returnType) {
51         if(response.getStatus() >= 400 && response.hasEntity()) {
52             return response.readEntity(returnType);
53         }
54         return null;
55     }
56     
57
58     @Override
59     public InputStream getInputStream() {
60         return (InputStream) response.getEntity();
61     }
62
63     @Override
64     public String header(String name) {
65         return response.getHeaderString(name);
66     }
67
68     @Override
69     public Map<String, String> headers() {
70         Map<String, String> headers = new HashMap<>();
71         for(String k : response.getHeaders().keySet()) {
72             headers.put(k, response.getHeaderString(k));
73         }
74         return headers;
75     }
76
77 }