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.io.IOException;
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;
28 import org.glassfish.jersey.SslConfigurator;
29 import org.glassfish.jersey.jackson.JacksonFeature;
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;
37 public class OpenStack {
39 public static Client CLIENT;
41 public static ObjectMapper DEFAULT_MAPPER;
43 public static ObjectMapper WRAPPED_MAPPER;
49 private static void initialize() {
52 //class MyX509TrustManager implements X509TrustManager
53 TrustManager mytm[] = null;
54 KeyManager mykm[] = null;
57 mytm = new TrustManager[]{new MyX509TrustManager("./truststore_client", "asdfgh".toCharArray())};
58 mykm = new KeyManager[]{new MyX509KeyManager("./keystore_client", "asdfgh".toCharArray())};
59 } catch (Exception ex) {
63 SSLContext context = null;
64 context = SSLContext.getInstance("SSL");
65 context.init(mykm, mytm, null);
72 context = SSLContext.getInstance("SSL");
73 context.init(null, null, null);
75 SslConfigurator sslConfig = SslConfigurator.newInstance();
77 .trustStoreFile("./truststore_client")
78 .trustStorePassword("asdfgh")
80 .keyStoreFile("./keystore_client")
81 .keyPassword("asdfgh");
83 //old: CLIENT.property(ClientProperties.SSL_CONFIG, new SslConfig(context));
85 CLIENT = ClientBuilder.newBuilder().sslContext(sslConfig.createSSLContext()).build();
87 DEFAULT_MAPPER = new ObjectMapper();
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);
95 WRAPPED_MAPPER = new ObjectMapper();
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);
105 CLIENT.register(new JacksonFeature()).register(new ContextResolver<ObjectMapper>() {
108 public ObjectMapper getContext(Class<?> type) {
109 return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
114 CLIENT.register(new ClientRequestFilter() {
117 public void filter(ClientRequestContext requestContext) throws IOException {
118 requestContext.getHeaders().remove("Content-Language");
119 requestContext.getHeaders().remove("Content-Encoding");
123 } catch(Exception e) {
124 throw new RuntimeException(e.getMessage(), e);