afd7a2b656be9d0f23fe9fe1162570b87d5a3bfa
[so/libs.git] /
1 package com.woorea.openstack.connector;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.logging.Logger;
6
7 import javax.ws.rs.core.MultivaluedMap;
8 import javax.ws.rs.ext.ContextResolver;
9 import javax.ws.rs.ext.Provider;
10
11 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
12 import org.codehaus.jackson.map.DeserializationConfig;
13 import org.codehaus.jackson.map.ObjectMapper;
14 import org.codehaus.jackson.map.SerializationConfig;
15 import org.codehaus.jackson.map.annotate.JsonRootName;
16 import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
17
18 import com.sun.jersey.api.client.Client;
19 import com.sun.jersey.api.client.ClientResponse;
20 import com.sun.jersey.api.client.UniformInterfaceException;
21 import com.sun.jersey.api.client.WebResource;
22 import com.sun.jersey.api.client.config.ClientConfig;
23 import com.sun.jersey.api.client.config.DefaultClientConfig;
24 import com.sun.jersey.client.impl.ClientRequestImpl;
25 import com.sun.jersey.core.header.OutBoundHeaders;
26 import com.woorea.openstack.base.client.OpenStackClientConnector;
27 import com.woorea.openstack.base.client.OpenStackRequest;
28 import com.woorea.openstack.base.client.OpenStackResponse;
29 import com.woorea.openstack.base.client.OpenStackResponseException;
30
31 public class JerseyConnector implements OpenStackClientConnector {
32         
33         protected Client client = null;
34     protected boolean logPassword;
35     private JerseyLoggingFilter logger = new JerseyLoggingFilter(Logger.getLogger("os"));
36
37         public JerseyConnector() {
38                 ClientConfig clientConfig = new DefaultClientConfig();
39                 clientConfig.getClasses().add(JacksonJaxbJsonProvider.class);
40                 clientConfig.getClasses().add(OpenStackObjectMapper.class);
41                 client = Client.create(clientConfig);
42         }
43
44         @Override
45         public <T> OpenStackResponse request(OpenStackRequest<T> request) {
46                 WebResource target = client.resource(request.endpoint()).path(request.path());
47                 for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) {
48                         for (Object o : entry.getValue()) {
49                                 target = target.queryParam(entry.getKey(), String.valueOf(o));
50                         }
51                 }
52                 target.addFilter(logger);
53                 MultivaluedMap<String, Object> headers = new OutBoundHeaders();
54                 for(Map.Entry<String, List<Object>> h : request.headers().entrySet()) {
55                         for(Object v : h.getValue()) {
56                                 headers.add(h.getKey(), v);
57                         }
58                 }
59                 if(request.entity() != null && request.entity().getContentType() != null) {
60                         headers.add("Content-Type", request.entity().getContentType());
61                 } else {
62                         headers.add("Content-Type", "application/json");
63                 }
64                 try {
65                         ClientResponse response = null;
66                         if (request.entity() != null && request.entity().getEntity() != null) {
67                                 response = target.getHeadHandler().handle(new ClientRequestImpl(target.getURI(), request.method().name(), request.entity().getEntity(), headers));
68                         } else {
69                                 response = target.getHeadHandler().handle(new ClientRequestImpl(target.getURI(), request.method().name(), null, headers));
70                         }
71                         return new JerseyResponse(response);
72                 } catch (UniformInterfaceException e) {
73                         throw new OpenStackResponseException(e.getResponse().getClientResponseStatus().getReasonPhrase(), e.getResponse().getStatus());
74                 }
75         }
76
77         @Provider
78         public static class OpenStackObjectMapper implements ContextResolver<ObjectMapper> {
79                 static ObjectMapper DEFAULT_MAPPER;
80                 static ObjectMapper WRAPPED_MAPPER;
81                 static {
82                         DEFAULT_MAPPER = new ObjectMapper();
83                         DEFAULT_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
84                         DEFAULT_MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
85                         DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
86                         DEFAULT_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
87                         DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
88
89                         WRAPPED_MAPPER = new ObjectMapper();
90                         WRAPPED_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
91                         WRAPPED_MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
92                         WRAPPED_MAPPER.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
93                         WRAPPED_MAPPER.enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
94                         WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
95                         WRAPPED_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
96                         WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
97                 }
98                 
99                 @Override
100                 public ObjectMapper getContext(Class<?> type) {
101                         return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
102                 }
103         }
104 }