1f9c0de68ceb8b8fb6a50c45c2baccd106ce40a1
[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 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22
23 import javax.ws.rs.core.UriBuilder;
24 import javax.ws.rs.ext.ContextResolver;
25
26 // bwj: changed the HttpStatus package
27 //import org.apache.commons.httpclient.HttpStatus;
28 import org.apache.http.HttpStatus;
29 import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
30 import org.codehaus.jackson.map.DeserializationConfig;
31 import org.codehaus.jackson.map.ObjectMapper;
32 import org.codehaus.jackson.map.SerializationConfig;
33 import org.codehaus.jackson.map.annotate.JsonRootName;
34 import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
35 import org.jboss.resteasy.client.ClientRequest;
36 import org.jboss.resteasy.client.ClientResponse;
37 import org.jboss.resteasy.plugins.providers.InputStreamProvider;
38 import org.jboss.resteasy.spi.ResteasyProviderFactory;
39
40 import com.woorea.openstack.base.client.OpenStackClientConnector;
41 import com.woorea.openstack.base.client.OpenStackRequest;
42 import com.woorea.openstack.base.client.OpenStackResponse;
43 import com.woorea.openstack.base.client.OpenStackResponseException;
44
45 public class RESTEasyConnector implements OpenStackClientConnector {
46
47     public static ObjectMapper DEFAULT_MAPPER;
48
49     public static ObjectMapper WRAPPED_MAPPER;
50
51     static class OpenStackProviderFactory extends ResteasyProviderFactory {
52
53         private JacksonJsonProvider jsonProvider;
54         private InputStreamProvider streamProvider;
55
56         public OpenStackProviderFactory() {
57             super();
58
59             addContextResolver(new ContextResolver<ObjectMapper>() {
60                 @Override
61                 public ObjectMapper getContext(Class<?> type) {
62                     return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
63                 }
64             });
65
66             jsonProvider = new JacksonJsonProvider();
67             addMessageBodyReader(jsonProvider);
68             addMessageBodyWriter(jsonProvider);
69
70             streamProvider = new InputStreamProvider();
71             addMessageBodyReader(streamProvider);
72             addMessageBodyWriter(streamProvider);
73         }
74
75     }
76
77     private static OpenStackProviderFactory providerFactory;
78
79     static {
80         DEFAULT_MAPPER = new ObjectMapper();
81
82         DEFAULT_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
83         DEFAULT_MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
84         DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
85         DEFAULT_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
86         DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
87
88         WRAPPED_MAPPER = new ObjectMapper();
89
90         WRAPPED_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
91         WRAPPED_MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
92         WRAPPED_MAPPER.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
93         WRAPPED_MAPPER.enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
94         WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
95         WRAPPED_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
96         WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
97
98         providerFactory = new OpenStackProviderFactory();
99     }
100
101     @Override
102     public <T> OpenStackResponse request(OpenStackRequest<T> request) {
103         ClientRequest client = new ClientRequest(UriBuilder.fromUri(request.endpoint() + "/" + request.path()),
104                 ClientRequest.getDefaultExecutor(), providerFactory);
105
106         for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) {
107             for (Object o : entry.getValue()) {
108                 client = client.queryParameter(entry.getKey(), String.valueOf(o));
109             }
110         }
111
112         for (Entry<String, List<Object>> h : request.headers().entrySet()) {
113             StringBuilder sb = new StringBuilder();
114             for (Object v : h.getValue()) {
115                 sb.append(String.valueOf(v));
116             }
117             client.header(h.getKey(), sb);
118         }
119
120         if (request.entity() != null) {
121             client.body(request.entity().getContentType(), request.entity().getEntity());
122         }
123
124         ClientResponse<T> response;
125
126         try {
127             response = client.httpMethod(request.method().name(), request.returnType());
128         } catch (Exception e) {
129             throw new RuntimeException("Unexpected client exception", e);
130         }
131
132         if (response.getStatus() == HttpStatus.SC_OK
133                 || response.getStatus() == HttpStatus.SC_CREATED
134                 || response.getStatus() == HttpStatus.SC_NO_CONTENT
135                 || response.getStatus() == HttpStatus.SC_ACCEPTED) {
136             return new RESTEasyResponse(client, response);
137         }
138
139         response.releaseConnection();
140
141         throw new OpenStackResponseException(response.getResponseStatus()
142                 .getReasonPhrase(), response.getStatus());
143     }
144
145 }