View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.http;
8   
9   import org.mule.api.endpoint.OutboundEndpoint;
10  
11  import java.net.URI;
12  import java.security.GeneralSecurityException;
13  import java.util.Collections;
14  import java.util.HashMap;
15  import java.util.Map;
16  
17  import javax.net.ssl.SSLSocketFactory;
18  
19  import org.apache.commons.httpclient.HostConfiguration;
20  import org.apache.commons.httpclient.protocol.Protocol;
21  import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
22  
23  public class HttpsClientMessageDispatcher extends HttpClientMessageDispatcher
24  {
25  
26      private final Map<String, Protocol> PROTOCOL = Collections.synchronizedMap(new HashMap<String, Protocol>());
27  
28      public HttpsClientMessageDispatcher(OutboundEndpoint endpoint)
29      {
30          super(endpoint);
31      }
32  
33      @Override
34      protected HostConfiguration getHostConfig(URI uri) throws Exception
35      {        
36          HostConfiguration hostConfig = new MuleHostConfiguration(super.getHostConfig(uri));
37          String host = uri.getHost();
38          int port = uri.getPort();
39          Protocol protocol = getProtocol(uri.getScheme().toLowerCase());
40          hostConfig.setHost(host, port, protocol);            
41          
42          return hostConfig;
43      }
44  
45      private Protocol getProtocol(String scheme) throws GeneralSecurityException
46      {
47          Protocol protocol = PROTOCOL.get(scheme);
48          if (protocol == null)
49          {
50              HttpsConnector httpsConnector = (HttpsConnector) httpConnector;
51              SSLSocketFactory factory = httpsConnector.getSslSocketFactory();
52              ProtocolSocketFactory protocolSocketFactory = new MuleSecureProtocolSocketFactory(factory);
53              protocol = new Protocol(scheme, protocolSocketFactory, 443);
54              PROTOCOL.put(scheme, protocol);
55          }
56          return protocol;
57      }
58  
59  }
60  
61