View Javadoc
1   /**
2    * This file Copyright (c) 2014-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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  import javax.ws.rs.client.ClientRequestFilter;
45  import javax.ws.rs.client.ClientResponseFilter;
46  
47  import org.apache.commons.io.IOUtils;
48  import org.apache.commons.lang3.StringUtils;
49  import org.apache.http.client.HttpClient;
50  import org.apache.http.conn.ClientConnectionManager;
51  import org.apache.http.conn.scheme.PlainSocketFactory;
52  import org.apache.http.conn.scheme.Scheme;
53  import org.apache.http.conn.scheme.SchemeRegistry;
54  import org.apache.http.conn.ssl.SSLSocketFactory;
55  import org.apache.http.impl.client.DefaultHttpClient;
56  import org.apache.http.impl.conn.PoolingClientConnectionManager;
57  import org.jboss.resteasy.client.core.ClientErrorInterceptor;
58  import org.jboss.resteasy.client.jaxrs.ResteasyClient;
59  import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
60  import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
61  import org.jboss.resteasy.client.jaxrs.cache.BrowserCacheFeature;
62  import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine;
63  import org.jboss.resteasy.spi.ResteasyProviderFactory;
64  import org.slf4j.Logger;
65  import org.slf4j.LoggerFactory;
66  
67  /**
68   * Factory that provides SSL-aware rest client.
69   */
70  public class SSLRestEasyClientFactory extends RestEasyClientFactory {
71  
72      private static Logger log = LoggerFactory.getLogger(SSLRestEasyClientFactory.class);
73  
74      private SSLRestEasyClientDefinition definition;
75  
76      @Inject
77      public SSLRestEasyClientFactory(SSLRestEasyClientDefinition definition) {
78          super(definition);
79          this.definition = definition;
80      }
81  
82      @Override
83      public RestClient createClient() {
84          if (StringUtils.isBlank(definition.getTrustStorePath()) || StringUtils.isBlank(definition.getTrustStorePassword())) {
85              log.warn("Mandatory truststore arguments were not supplied, will create default resteasy client.");
86              return super.createClient();
87          }
88  
89          SchemeRegistry schemeRegistry;
90          FileInputStream keyStoreInputStream = null;
91          FileInputStream trustStoreInputStream = null;
92          try {
93              KeyStore trustStore;
94              KeyStore keyStore = null;
95  
96              if (StringUtils.isNotBlank(definition.getKeyStorePath()) && StringUtils.isNotBlank(definition.getKeyStorePassword())) {
97                  keyStore = KeyStore.getInstance(definition.getKeyStoreType());
98                  keyStoreInputStream = new FileInputStream(definition.getKeyStorePath());
99                  keyStore.load(keyStoreInputStream, definition.getKeyStorePassword().toCharArray());
100             }
101 
102             trustStore = KeyStore.getInstance(definition.getTrustStoreType());
103             trustStoreInputStream = new FileInputStream(definition.getTrustStorePath());
104             trustStore.load(trustStoreInputStream, definition.getTrustStorePassword().toCharArray());
105 
106             schemeRegistry = new SchemeRegistry();
107             SSLSocketFactory schemeSocketFactory;
108             if (keyStore == null) {
109                 schemeSocketFactory = new SSLSocketFactory(trustStore);
110             } else {
111                 schemeSocketFactory = new SSLSocketFactory(keyStore, definition.getKeyStorePassword(), trustStore);
112             }
113             schemeRegistry.register(new Scheme("https", definition.getHttpsPort(), schemeSocketFactory));
114             schemeRegistry.register(new Scheme("http", definition.getHttpPort(), PlainSocketFactory.getSocketFactory()));
115         } catch (Exception e) {
116             log.warn("Unable to load keystore/truststore, will create default resteasy client.", e);
117             return super.createClient();
118         } finally {
119             if (keyStoreInputStream != null) {
120                 IOUtils.closeQuietly(keyStoreInputStream);
121             }
122             if (trustStoreInputStream != null) {
123                 IOUtils.closeQuietly(trustStoreInputStream);
124             }
125         }
126 
127         ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
128         HttpClient httpClient = new DefaultHttpClient(cm);
129         ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
130         ResteasyClient client = ((ResteasyClientBuilder) ResteasyClientBuilder.newBuilder()).httpEngine(engine).build();
131         for (Object clientFilter : definition.getClientFilters()) {
132             if (clientFilter instanceof ClientRequestFilter || clientFilter instanceof ClientResponseFilter) {
133                 client.register(clientFilter);
134             }
135         }
136         ResteasyWebTarget target = client.target(definition.getBaseUrl());
137         ResteasyProviderFactory pf = ResteasyProviderFactory.getInstance();
138         for (ClientErrorInterceptor handler : definition.getClientErrorInterceptors()) {
139             pf.addClientErrorInterceptor(handler);
140         }
141         if (definition.isCacheable()) {
142             target.register(BrowserCacheFeature.class);
143         }
144         return new RestEasyClient(target, definition);
145     }
146 }