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 com.fasterxml.jackson.annotation.JsonRootName;
28 import com.fasterxml.jackson.annotation.JsonInclude.Include;
29 import com.fasterxml.jackson.databind.DeserializationFeature;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.databind.SerializationFeature;
32 import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
33 import com.sun.jersey.api.client.Client;
34 import com.sun.jersey.api.client.ClientResponse;
35 import com.sun.jersey.api.client.UniformInterfaceException;
36 import com.sun.jersey.api.client.WebResource;
37 import com.sun.jersey.api.client.config.ClientConfig;
38 import com.sun.jersey.api.client.config.DefaultClientConfig;
39 import com.sun.jersey.client.impl.ClientRequestImpl;
40 import com.sun.jersey.core.header.OutBoundHeaders;
41 import com.woorea.openstack.base.client.OpenStackClientConnector;
42 import com.woorea.openstack.base.client.OpenStackRequest;
43 import com.woorea.openstack.base.client.OpenStackResponse;
44 import com.woorea.openstack.base.client.OpenStackResponseException;
46 public class JerseyConnector implements OpenStackClientConnector {
48 protected Client client = null;
49 protected boolean logPassword;
50 private JerseyLoggingFilter logger = new JerseyLoggingFilter(Logger.getLogger("os"));
52 public JerseyConnector() {
53 ClientConfig clientConfig = new DefaultClientConfig();
54 clientConfig.getClasses().add(JacksonJaxbJsonProvider.class);
55 clientConfig.getClasses().add(OpenStackObjectMapper.class);
56 client = Client.create(clientConfig);
60 public <T> OpenStackResponse request(OpenStackRequest<T> request) {
61 WebResource target = client.resource(request.endpoint()).path(request.path());
62 for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) {
63 for (Object o : entry.getValue()) {
64 target = target.queryParam(entry.getKey(), String.valueOf(o));
67 target.addFilter(logger);
68 MultivaluedMap<String, Object> headers = new OutBoundHeaders();
69 for(Map.Entry<String, List<Object>> h : request.headers().entrySet()) {
70 for(Object v : h.getValue()) {
71 headers.add(h.getKey(), v);
74 if(request.entity() != null && request.entity().getContentType() != null) {
75 headers.add("Content-Type", request.entity().getContentType());
77 headers.add("Content-Type", "application/json");
80 ClientResponse response;
81 if (request.entity() != null && request.entity().getEntity() != null) {
82 response = target.getHeadHandler().handle(new ClientRequestImpl(target.getURI(), request.method().name(), request.entity().getEntity(), headers));
84 response = target.getHeadHandler().handle(new ClientRequestImpl(target.getURI(), request.method().name(), null, headers));
86 return new JerseyResponse(response);
87 } catch (UniformInterfaceException e) {
88 throw new OpenStackResponseException(e.getResponse().getClientResponseStatus().getReasonPhrase(), e.getResponse().getStatus());
93 public static class OpenStackObjectMapper implements ContextResolver<ObjectMapper> {
94 static ObjectMapper DEFAULT_MAPPER;
95 static ObjectMapper WRAPPED_MAPPER;
97 DEFAULT_MAPPER = new ObjectMapper();
98 DEFAULT_MAPPER.setSerializationInclusion(Include.NON_NULL);
99 DEFAULT_MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
100 DEFAULT_MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
101 DEFAULT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
102 DEFAULT_MAPPER.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
104 WRAPPED_MAPPER = new ObjectMapper();
105 WRAPPED_MAPPER.setSerializationInclusion(Include.NON_NULL);
106 WRAPPED_MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
107 WRAPPED_MAPPER.enable(SerializationFeature.WRAP_ROOT_VALUE);
108 WRAPPED_MAPPER.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
109 WRAPPED_MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
110 WRAPPED_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
111 WRAPPED_MAPPER.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
115 public ObjectMapper getContext(Class<?> type) {
116 return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;