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