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
7 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
17 package com.woorea.openstack.connector;
19 import java.util.List;
21 import java.util.Map.Entry;
23 import javax.ws.rs.core.UriBuilder;
24 import javax.ws.rs.ext.ContextResolver;
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;
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;
43 public class RESTEasyConnector implements OpenStackClientConnector {
45 public static ObjectMapper DEFAULT_MAPPER;
47 public static ObjectMapper WRAPPED_MAPPER;
49 static class OpenStackProviderFactory extends ResteasyProviderFactory {
51 private JacksonJsonProvider jsonProvider;
52 private InputStreamProvider streamProvider;
54 public OpenStackProviderFactory() {
57 addContextResolver(new ContextResolver<ObjectMapper>() {
58 public ObjectMapper getContext(Class<?> type) {
59 return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
63 jsonProvider = new JacksonJsonProvider();
64 addMessageBodyReader(jsonProvider);
65 addMessageBodyWriter(jsonProvider);
67 streamProvider = new InputStreamProvider();
68 addMessageBodyReader(streamProvider);
69 addMessageBodyWriter(streamProvider);
74 private static OpenStackProviderFactory providerFactory;
77 DEFAULT_MAPPER = new ObjectMapper();
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);
85 WRAPPED_MAPPER = new ObjectMapper();
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);
95 providerFactory = new OpenStackProviderFactory();
98 public <T> OpenStackResponse request(OpenStackRequest<T> request) {
99 ClientRequest client = new ClientRequest(UriBuilder.fromUri(request.endpoint() + "/" + request.path()),
100 ClientRequest.getDefaultExecutor(), providerFactory);
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));
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));
113 client.header(h.getKey(), sb);
116 if (request.entity() != null) {
117 client.body(request.entity().getContentType(), request.entity().getEntity());
120 ClientResponse<T> response;
123 response = client.httpMethod(request.method().name(), request.returnType());
124 } catch (Exception e) {
125 throw new RuntimeException("Unexpected client exception", e);
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);
135 response.releaseConnection();
137 throw new OpenStackResponseException(response.getResponseStatus()
138 .getReasonPhrase(), response.getStatus());