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  /** Creates a secure SMTP connection */
23  public class SmtpsConnector extends SmtpConnector implements TlsIndirectTrustStore, TlsIndirectKeyStore
24  {
25  
26      public static final String SMTPS = "smtps";
27      public static final String DEFAULT_SOCKET_FACTORY = SmtpsSocketFactory.class.getName();
28  
29      private String socketFactory = DEFAULT_SOCKET_FACTORY;
30      private String socketFactoryFallback = "false";
31      private TlsConfiguration tls = new TlsConfiguration(TlsConfiguration.DEFAULT_KEYSTORE);
32  
33      public static final int DEFAULT_SMTPS_PORT = 465;
34  
35  
36      public SmtpsConnector(MuleContext context)
37      {
38          super(DEFAULT_SMTPS_PORT, context);
39      }
40  
41      public String getProtocol()
42      {
43          return "smtps";
44      }
45  
46      public String getBaseProtocol()
47      {
48          return "smtp";
49      }
50  
51      protected void doInitialise() throws InitialisationException
52      {
53          try
54          {
55              tls.initialise(true, null);
56          }
57          catch (CreateException e)
58          {
59              throw new InitialisationException(e, this);
60          }
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(SmtpsSocketFactory.MULE_SMTPS_NAMESPACE).writeToProperties(global, tls);
73      }
74  
75      public String getSocketFactory()
76      {
77          return socketFactory;
78      }
79  
80      public void setSocketFactory(String sslSocketFactory)
81      {
82          this.socketFactory = sslSocketFactory;
83      }
84  
85      public String getSocketFactoryFallback()
86      {
87          return socketFactoryFallback;
88      }
89  
90      public void setSocketFactoryFallback(String socketFactoryFallback)
91      {
92          this.socketFactoryFallback = socketFactoryFallback;
93      }
94  
95      public String getTrustStore()
96      {
97          return tls.getTrustStore();
98      }
99  
100     public String getTrustStorePassword()
101     {
102         return tls.getTrustStorePassword();
103     }
104 
105     public void setTrustStore(String trustStore) throws IOException
106     {
107         tls.setTrustStore(trustStore);
108     }
109 
110     public void setTrustStorePassword(String trustStorePassword)
111     {
112         tls.setTrustStorePassword(trustStorePassword);
113     }
114 
115     // these were not present before, but could be set implicitly via global proeprties
116     // that is no longer true, so i have added them in here
117 
118     public String getClientKeyStore()
119     {
120         return this.tls.getClientKeyStore();
121     }
122 
123     public String getClientKeyStorePassword()
124     {
125         return this.tls.getClientKeyStorePassword();
126     }
127 
128     public String getClientKeyStoreType()
129     {
130         return this.tls.getClientKeyStoreType();
131     }
132 
133     public void setClientKeyStore(String name) throws IOException
134     {
135         this.tls.setClientKeyStore(name);
136     }
137 
138     public void setClientKeyStorePassword(String clientKeyStorePassword)
139     {
140         this.tls.setClientKeyStorePassword(clientKeyStorePassword);
141     }
142 
143     public void setClientKeyStoreType(String clientKeyStoreType)
144     {
145         this.tls.setClientKeyStoreType(clientKeyStoreType);
146     }
147 
148 }