1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.resteasy.client.factory;
35
36 import info.magnolia.rest.client.RestClient;
37 import info.magnolia.resteasy.client.RestEasyClient;
38 import info.magnolia.resteasy.client.SSLRestEasyClientDefinition;
39
40 import java.io.FileInputStream;
41 import java.security.KeyStore;
42
43 import javax.inject.Inject;
44
45 import org.apache.commons.io.IOUtils;
46 import org.apache.commons.lang3.StringUtils;
47 import org.apache.http.client.HttpClient;
48 import org.apache.http.conn.ClientConnectionManager;
49 import org.apache.http.conn.scheme.PlainSocketFactory;
50 import org.apache.http.conn.scheme.Scheme;
51 import org.apache.http.conn.scheme.SchemeRegistry;
52 import org.apache.http.conn.ssl.SSLSocketFactory;
53 import org.apache.http.impl.client.DefaultHttpClient;
54 import org.apache.http.impl.conn.PoolingClientConnectionManager;
55 import org.jboss.resteasy.client.core.ClientErrorInterceptor;
56 import org.jboss.resteasy.client.jaxrs.ResteasyClient;
57 import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
58 import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
59 import org.jboss.resteasy.client.jaxrs.cache.BrowserCacheFeature;
60 import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine;
61 import org.jboss.resteasy.spi.ResteasyProviderFactory;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65
66
67
68 public class SSLRestEasyClientFactory extends RestEasyClientFactory {
69
70 private static Logger log = LoggerFactory.getLogger(SSLRestEasyClientFactory.class);
71
72 private SSLRestEasyClientDefinition definition;
73
74 @Inject
75 public SSLRestEasyClientFactory(SSLRestEasyClientDefinition definition) {
76 super(definition);
77 this.definition = definition;
78 }
79
80 @Override
81 public RestClient createClient() {
82 if (StringUtils.isBlank(definition.getTrustStorePath()) || StringUtils.isBlank(definition.getTrustStorePassword())) {
83 log.warn("Mandatory truststore arguments were not supplied, will create default resteasy client.");
84 return super.createClient();
85 }
86
87 SchemeRegistry schemeRegistry;
88 FileInputStream keyStoreInputStream = null;
89 FileInputStream trustStoreInputStream = null;
90 try {
91 KeyStore trustStore;
92 KeyStore keyStore = null;
93
94 if (StringUtils.isNotBlank(definition.getKeyStorePath()) && StringUtils.isNotBlank(definition.getKeyStorePassword())) {
95 keyStore = KeyStore.getInstance(definition.getKeyStoreType());
96 keyStoreInputStream = new FileInputStream(definition.getKeyStorePath());
97 keyStore.load(keyStoreInputStream, definition.getKeyStorePassword().toCharArray());
98 }
99
100 trustStore = KeyStore.getInstance(definition.getTrustStoreType());
101 trustStoreInputStream = new FileInputStream(definition.getTrustStorePath());
102 trustStore.load(trustStoreInputStream, definition.getTrustStorePassword().toCharArray());
103
104 schemeRegistry = new SchemeRegistry();
105 SSLSocketFactory schemeSocketFactory;
106 if (keyStore == null) {
107 schemeSocketFactory = new SSLSocketFactory(trustStore);
108 } else {
109 schemeSocketFactory = new SSLSocketFactory(keyStore, definition.getKeyStorePassword(), trustStore);
110 }
111 schemeRegistry.register(new Scheme("https", definition.getHttpsPort(), schemeSocketFactory));
112 schemeRegistry.register(new Scheme("http", definition.getHttpPort(), PlainSocketFactory.getSocketFactory()));
113 } catch (Exception e) {
114 log.warn("Unable to load keystore/truststore, will create default resteasy client.", e);
115 return super.createClient();
116 } finally {
117 if (keyStoreInputStream != null) {
118 IOUtils.closeQuietly(keyStoreInputStream);
119 }
120 if (trustStoreInputStream != null) {
121 IOUtils.closeQuietly(trustStoreInputStream);
122 }
123 }
124
125 ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
126 HttpClient httpClient = new DefaultHttpClient(cm);
127 ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
128 ResteasyClient client = ((ResteasyClientBuilder) ResteasyClientBuilder.newBuilder()).httpEngine(engine).build();
129 registerComponents(client, definition.getComponents());
130 ResteasyWebTarget target = client.target(definition.getBaseUrl());
131 ResteasyProviderFactory pf = ResteasyProviderFactory.getInstance();
132 for (ClientErrorInterceptor handler : definition.getClientErrorInterceptors()) {
133 pf.addClientErrorInterceptor(handler);
134 }
135 if (definition.isCacheable()) {
136 target.register(BrowserCacheFeature.class);
137 }
138 return new RestEasyClient(target, definition);
139 }
140 }