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