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 // bwj: changed the HttpStatus package
27 //import org.apache.commons.httpclient.HttpStatus;
28 import org.apache.http.HttpStatus;
29 import org.jboss.resteasy.client.ClientRequest;
30 import org.jboss.resteasy.client.ClientResponse;
31 import org.jboss.resteasy.plugins.providers.InputStreamProvider;
32 import org.jboss.resteasy.spi.ResteasyProviderFactory;
34 import com.fasterxml.jackson.annotation.JsonRootName;
35 import com.fasterxml.jackson.annotation.JsonInclude.Include;
36 import com.fasterxml.jackson.databind.DeserializationFeature;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.fasterxml.jackson.databind.SerializationFeature;
39 import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
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;
45 public class RESTEasyConnector implements OpenStackClientConnector {
47 public static ObjectMapper DEFAULT_MAPPER;
49 public static ObjectMapper WRAPPED_MAPPER;
51 static class OpenStackProviderFactory extends ResteasyProviderFactory {
53 private JacksonJsonProvider jsonProvider;
54 private InputStreamProvider streamProvider;
56 public OpenStackProviderFactory() {
59 addContextResolver(new ContextResolver<ObjectMapper>() {
61 public ObjectMapper getContext(Class<?> type) {
62 return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
66 jsonProvider = new JacksonJsonProvider();
67 addMessageBodyReader(jsonProvider);
68 addMessageBodyWriter(jsonProvider);
70 streamProvider = new InputStreamProvider();
71 addMessageBodyReader(streamProvider);
72 addMessageBodyWriter(streamProvider);
77 private static OpenStackProviderFactory providerFactory;
80 DEFAULT_MAPPER = new ObjectMapper();
82 DEFAULT_MAPPER.setSerializationInclusion(Include.NON_NULL);
83 DEFAULT_MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
84 DEFAULT_MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
85 DEFAULT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
86 DEFAULT_MAPPER.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
88 WRAPPED_MAPPER = new ObjectMapper();
90 WRAPPED_MAPPER.setSerializationInclusion(Include.NON_NULL);
91 WRAPPED_MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
92 WRAPPED_MAPPER.enable(SerializationFeature.WRAP_ROOT_VALUE);
93 WRAPPED_MAPPER.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
94 WRAPPED_MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
95 WRAPPED_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
96 WRAPPED_MAPPER.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
98 providerFactory = new OpenStackProviderFactory();
102 public <T> OpenStackResponse request(OpenStackRequest<T> request) {
103 ClientRequest client = new ClientRequest(UriBuilder.fromUri(request.endpoint() + "/" + request.path()),
104 ClientRequest.getDefaultExecutor(), providerFactory);
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));
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));
117 client.header(h.getKey(), sb);
120 if (request.entity() != null) {
121 client.body(request.entity().getContentType(), request.entity().getEntity());
124 ClientResponse<T> response;
127 response = client.httpMethod(request.method().name(), request.returnType());
128 } catch (Exception e) {
129 throw new RuntimeException("Unexpected client exception", e);
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);
139 response.releaseConnection();
141 throw new OpenStackResponseException(response.getResponseStatus()
142 .getReasonPhrase(), response.getStatus());