eeedfd0faa576424e3aed8192742cba7485ab903
[so/libs.git] /
1 /*-
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
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
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=========================================================
15  */
16
17 package com.woorea.openstack.connector;
18
19 import java.io.IOException;
20
21 import javax.net.ssl.SSLContext;
22 import javax.ws.rs.client.Client;
23 import javax.ws.rs.client.ClientBuilder;
24 import javax.ws.rs.client.ClientRequestContext;
25 import javax.ws.rs.client.ClientRequestFilter;
26 import javax.ws.rs.ext.ContextResolver;
27
28 import org.glassfish.jersey.SslConfigurator;
29 import org.glassfish.jersey.jackson.JacksonFeature;
30
31 import com.fasterxml.jackson.annotation.JsonRootName;
32 import com.fasterxml.jackson.annotation.JsonInclude.Include;
33 import com.fasterxml.jackson.databind.DeserializationFeature;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import com.fasterxml.jackson.databind.SerializationFeature;
36
37 public class OpenStack {
38
39     public static Client CLIENT;
40     
41     public static ObjectMapper DEFAULT_MAPPER;
42     
43     public static ObjectMapper WRAPPED_MAPPER;
44     
45     static {
46         initialize();
47     }
48     
49     private static void initialize() {
50         
51         /*
52         //class MyX509TrustManager implements X509TrustManager
53         TrustManager mytm[] = null;
54         KeyManager mykm[] = null;
55
56         try {
57             mytm = new TrustManager[]{new MyX509TrustManager("./truststore_client", "asdfgh".toCharArray())};
58             mykm = new KeyManager[]{new MyX509KeyManager("./keystore_client", "asdfgh".toCharArray())};
59         } catch (Exception ex) {
60
61         }
62         
63         SSLContext context = null;
64         context = SSLContext.getInstance("SSL");
65         context.init(mykm, mytm, null);
66         
67         */
68         
69         try {
70             
71             SSLContext context;
72             context = SSLContext.getInstance("SSL");
73             context.init(null, null, null);
74             
75             SslConfigurator sslConfig = SslConfigurator.newInstance();
76                     /*
77                     .trustStoreFile("./truststore_client")
78                     .trustStorePassword("asdfgh")
79
80                     .keyStoreFile("./keystore_client")
81                     .keyPassword("asdfgh");
82                     */
83                     //old: CLIENT.property(ClientProperties.SSL_CONFIG, new SslConfig(context));
84             
85             CLIENT = ClientBuilder.newBuilder().sslContext(sslConfig.createSSLContext()).build();
86             
87             DEFAULT_MAPPER = new ObjectMapper();
88
89             DEFAULT_MAPPER.setSerializationInclusion(Include.NON_NULL);
90             DEFAULT_MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
91             DEFAULT_MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
92             DEFAULT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
93             DEFAULT_MAPPER.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
94
95             WRAPPED_MAPPER = new ObjectMapper();
96
97             WRAPPED_MAPPER.setSerializationInclusion(Include.NON_NULL);
98             WRAPPED_MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
99             WRAPPED_MAPPER.enable(SerializationFeature.WRAP_ROOT_VALUE);
100             WRAPPED_MAPPER.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
101             WRAPPED_MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
102             WRAPPED_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
103             WRAPPED_MAPPER.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
104
105             CLIENT.register(new JacksonFeature()).register(new ContextResolver<ObjectMapper>() {
106
107                 @Override
108                 public ObjectMapper getContext(Class<?> type) {
109                     return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
110                 }
111                 
112             });
113             
114             CLIENT.register(new ClientRequestFilter() {
115                 
116                 @Override
117                 public void filter(ClientRequestContext requestContext) throws IOException {
118                     requestContext.getHeaders().remove("Content-Language");
119                     requestContext.getHeaders().remove("Content-Encoding");
120                 }
121             });
122             
123         } catch(Exception e) {
124             throw new RuntimeException(e.getMessage(), e);
125         }
126         
127     }
128
129 }