dc57d881c93bed37318229023cc0fe0948ea728f
[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 org.jboss.resteasy.client.ClientRequest;
24 import org.jboss.resteasy.client.ClientResponse;
25 import com.woorea.openstack.base.client.OpenStackResponse;
26
27 import javax.ws.rs.core.MultivaluedMap;
28 import java.io.InputStream;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 public class RESTEasyResponse implements OpenStackResponse {
33
34     private ClientRequest client;
35
36     private ClientResponse response;
37
38     public RESTEasyResponse(ClientRequest client, ClientResponse response) {
39         this.client = client;
40         this.response = response;
41     }
42
43     @Override
44     public <T> T getEntity(Class<T> returnType) {
45         return (T) response.getEntity(returnType);
46     }
47
48     @Override
49     public <T> T getErrorEntity(Class<T> returnType) {
50         return (T) response.getEntity(returnType);
51     }
52
53     @Override
54     public InputStream getInputStream() {
55                 return new RESTEasyInputStream((InputStream) response.getEntity(InputStream.class), client.getExecutor());
56     }
57
58     @Override
59     public String header(String name) {
60         return response.getHeaders().getFirst(name).toString();
61     }
62
63     @Override
64     public Map<String, String> headers() {
65         Map<String, String> headers = new HashMap<String, String>();
66         MultivaluedMap<String, Object> responseHeaders = response.getHeaders();
67
68         for (String key : responseHeaders.keySet()) {
69             headers.put(key, responseHeaders.getFirst(key).toString());
70         }
71
72         return headers;
73     }
74
75 }