View Javadoc

1   /*
2    * $Id: AbstractTlsRetrieveMailConnector.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.email;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.lifecycle.CreateException;
15  import org.mule.api.lifecycle.InitialisationException;
16  import org.mule.api.security.TlsIndirectKeyStore;
17  import org.mule.api.security.TlsIndirectTrustStore;
18  import org.mule.api.security.tls.TlsConfiguration;
19  import org.mule.api.security.tls.TlsPropertiesMapper;
20  
21  import java.io.IOException;
22  import java.util.Properties;
23  
24  import javax.mail.URLName;
25  
26  /**
27   * Support for connecting to and receiving email from a secure mailbox (the exact protocol depends on
28   * the subclass).
29   */
30  public abstract class AbstractTlsRetrieveMailConnector
31          extends AbstractRetrieveMailConnector implements TlsIndirectTrustStore, TlsIndirectKeyStore
32  {
33  
34      private String namespace;
35      private String socketFactory;
36      private String socketFactoryFallback = "false";
37      private TlsConfiguration tls = new TlsConfiguration(TlsConfiguration.DEFAULT_KEYSTORE);
38  
39      protected AbstractTlsRetrieveMailConnector(int defaultPort, String namespace, Class defaultSocketFactory, MuleContext context)
40      {
41          super(defaultPort, context);
42          this.namespace = namespace;
43          socketFactory = defaultSocketFactory.getName();
44  
45          // see comment below
46  //        this.namespace = TlsConfiguration.JSSE_NAMESPACE;
47  //        socketFactory = SSLServerSocketFactory.class.getName();
48      }
49  
50      protected void doInitialise() throws InitialisationException
51      {
52          try
53          {
54              tls.initialise(true, null);
55          }
56          catch (CreateException e)
57          {
58              throw new InitialisationException(e, this);
59          }
60          super.doInitialise();
61      }
62  
63      @Override
64      protected void extendPropertiesForSession(Properties global, Properties local, URLName url)
65      {
66          super.extendPropertiesForSession(global, local, url);
67  
68          local.setProperty("mail." + getProtocol() + ".ssl", "true");
69          local.setProperty("mail." + getProtocol() + ".socketFactory.class", getSocketFactory());
70          local.setProperty("mail." + getProtocol() + ".socketFactory.fallback", getSocketFactoryFallback());
71  
72          new TlsPropertiesMapper(namespace).writeToProperties(global, tls);
73  
74          // this, instead of the line above, and with the constructor changes,
75          // would have changed to local SSL configuration, if that was possible
76          // (it didn't work)
77  //        new TlsPropertiesMapper(namespace).writeToProperties(local, tls);
78      }
79  
80      public String getSocketFactory()
81      {
82          return socketFactory;
83      }
84  
85      public void setSocketFactory(String sslSocketFactory)
86      {
87          this.socketFactory = sslSocketFactory;
88      }
89  
90      public String getSocketFactoryFallback()
91      {
92          return socketFactoryFallback;
93      }
94  
95      public void setSocketFactoryFallback(String socketFactoryFallback)
96      {
97          this.socketFactoryFallback = socketFactoryFallback;
98      }
99  
100     public String getTrustStore()
101     {
102         return tls.getTrustStore();
103     }
104 
105     public String getTrustStorePassword()
106     {
107         return tls.getTrustStorePassword();
108     }
109 
110     public void setTrustStore(String trustStore) throws IOException
111     {
112         tls.setTrustStore(trustStore);
113     }
114 
115     public void setTrustStorePassword(String trustStorePassword)
116     {
117         tls.setTrustStorePassword(trustStorePassword);
118     }
119 
120     // these were not present before, but could be set implicitly via global properties
121     // that is no longer true, so i have added them in here
122 
123     public String getClientKeyStore()
124     {
125         return this.tls.getClientKeyStore();
126     }
127 
128     public String getClientKeyStorePassword()
129     {
130         return this.tls.getClientKeyStorePassword();
131     }
132 
133     public String getClientKeyStoreType()
134     {
135         return this.tls.getClientKeyStoreType();
136     }
137 
138     public void setClientKeyStore(String name) throws IOException
139     {
140         this.tls.setClientKeyStore(name);
141     }
142 
143     public void setClientKeyStorePassword(String clientKeyStorePassword)
144     {
145         this.tls.setClientKeyStorePassword(clientKeyStorePassword);
146     }
147 
148     public void setClientKeyStoreType(String clientKeyStoreType)
149     {
150         this.tls.setClientKeyStoreType(clientKeyStoreType);
151     }
152 
153 }