View Javadoc

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