View Javadoc

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