View Javadoc
1   /**
2    * This file Copyright (c) 2014-2016 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  
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   * Factory that provides SSL-aware rest client.
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 }