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.logging.Logger;
23 import javax.ws.rs.core.MultivaluedMap;
24 import javax.ws.rs.ext.ContextResolver;
25 import javax.ws.rs.ext.Provider;
27 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
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;
34 import com.sun.jersey.api.client.Client;
35 import com.sun.jersey.api.client.ClientResponse;
36 import com.sun.jersey.api.client.UniformInterfaceException;
37 import com.sun.jersey.api.client.WebResource;
38 import com.sun.jersey.api.client.config.ClientConfig;
39 import com.sun.jersey.api.client.config.DefaultClientConfig;
40 import com.sun.jersey.client.impl.ClientRequestImpl;
41 import com.sun.jersey.core.header.OutBoundHeaders;
42 import com.woorea.openstack.base.client.OpenStackClientConnector;
43 import com.woorea.openstack.base.client.OpenStackRequest;
44 import com.woorea.openstack.base.client.OpenStackResponse;
45 import com.woorea.openstack.base.client.OpenStackResponseException;
47 public class JerseyConnector implements OpenStackClientConnector {
49 protected Client client = null;
50 protected boolean logPassword;
51 private JerseyLoggingFilter logger = new JerseyLoggingFilter(Logger.getLogger("os"));
53 public JerseyConnector() {
54 ClientConfig clientConfig = new DefaultClientConfig();
55 clientConfig.getClasses().add(JacksonJaxbJsonProvider.class);
56 clientConfig.getClasses().add(OpenStackObjectMapper.class);
57 client = Client.create(clientConfig);
61 public <T> OpenStackResponse request(OpenStackRequest<T> request) {
62 WebResource target = client.resource(request.endpoint()).path(request.path());
63 for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) {
64 for (Object o : entry.getValue()) {
65 target = target.queryParam(entry.getKey(), String.valueOf(o));
68 target.addFilter(logger);
69 MultivaluedMap<String, Object> headers = new OutBoundHeaders();
70 for(Map.Entry<String, List<Object>> h : request.headers().entrySet()) {
71 for(Object v : h.getValue()) {
72 headers.add(h.getKey(), v);
75 if(request.entity() != null && request.entity().getContentType() != null) {
76 headers.add("Content-Type", request.entity().getContentType());
78 headers.add("Content-Type", "application/json");
81 ClientResponse response = null;
82 if (request.entity() != null && request.entity().getEntity() != null) {
83 response = target.getHeadHandler().handle(new ClientRequestImpl(target.getURI(), request.method().name(), request.entity().getEntity(), headers));
85 response = target.getHeadHandler().handle(new ClientRequestImpl(target.getURI(), request.method().name(), null, headers));
87 return new JerseyResponse(response);
88 } catch (UniformInterfaceException e) {
89 throw new OpenStackResponseException(e.getResponse().getClientResponseStatus().getReasonPhrase(), e.getResponse().getStatus());
94 public static class OpenStackObjectMapper implements ContextResolver<ObjectMapper> {
95 static ObjectMapper DEFAULT_MAPPER;
96 static ObjectMapper WRAPPED_MAPPER;
98 DEFAULT_MAPPER = new ObjectMapper();
99 DEFAULT_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
100 DEFAULT_MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
101 DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
102 DEFAULT_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
103 DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
105 WRAPPED_MAPPER = new ObjectMapper();
106 WRAPPED_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
107 WRAPPED_MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
108 WRAPPED_MAPPER.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
109 WRAPPED_MAPPER.enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
110 WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
111 WRAPPED_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
112 WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
116 public ObjectMapper getContext(Class<?> type) {
117 return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;