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>() {
59 public ObjectMapper getContext(Class<?> type) {
60 return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
64 jsonProvider = new JacksonJsonProvider();
65 addMessageBodyReader(jsonProvider);
66 addMessageBodyWriter(jsonProvider);
68 streamProvider = new InputStreamProvider();
69 addMessageBodyReader(streamProvider);
70 addMessageBodyWriter(streamProvider);
75 private static OpenStackProviderFactory providerFactory;
78 DEFAULT_MAPPER = new ObjectMapper();
80 DEFAULT_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
81 DEFAULT_MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
82 DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
83 DEFAULT_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
84 DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
86 WRAPPED_MAPPER = new ObjectMapper();
88 WRAPPED_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
89 WRAPPED_MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
90 WRAPPED_MAPPER.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
91 WRAPPED_MAPPER.enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
92 WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
93 WRAPPED_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
94 WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
96 providerFactory = new OpenStackProviderFactory();
100 public <T> OpenStackResponse request(OpenStackRequest<T> request) {
101 ClientRequest client = new ClientRequest(UriBuilder.fromUri(request.endpoint() + "/" + request.path()),
102 ClientRequest.getDefaultExecutor(), providerFactory);
104 for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) {
105 for (Object o : entry.getValue()) {
106 client = client.queryParameter(entry.getKey(), String.valueOf(o));
110 for (Entry<String, List<Object>> h : request.headers().entrySet()) {
111 StringBuilder sb = new StringBuilder();
112 for (Object v : h.getValue()) {
113 sb.append(String.valueOf(v));
115 client.header(h.getKey(), sb);
118 if (request.entity() != null) {
119 client.body(request.entity().getContentType(), request.entity().getEntity());
122 ClientResponse<T> response;
125 response = client.httpMethod(request.method().name(), request.returnType());
126 } catch (Exception e) {
127 throw new RuntimeException("Unexpected client exception", e);
130 if (response.getStatus() == HttpStatus.SC_OK
131 || response.getStatus() == HttpStatus.SC_CREATED
132 || response.getStatus() == HttpStatus.SC_NO_CONTENT
133 || response.getStatus() == HttpStatus.SC_ACCEPTED) {
134 return new RESTEasyResponse(client, response);
137 response.releaseConnection();
139 throw new OpenStackResponseException(response.getResponseStatus()
140 .getReasonPhrase(), response.getStatus());