View Javadoc

1   /*
2    * $Id: SmtpsConnector.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  /** Creates a secure SMTP connection */
27  public class SmtpsConnector extends SmtpConnector implements TlsIndirectTrustStore, TlsIndirectKeyStore
28  {
29  
30      public static final String SMTPS = "smtps";
31      public static final String DEFAULT_SOCKET_FACTORY = SmtpsSocketFactory.class.getName();
32  
33      private String socketFactory = DEFAULT_SOCKET_FACTORY;
34      private String socketFactoryFallback = "false";
35      private TlsConfiguration tls = new TlsConfiguration(TlsConfiguration.DEFAULT_KEYSTORE);
36  
37      public static final int DEFAULT_SMTPS_PORT = 465;
38  
39  
40      public SmtpsConnector(MuleContext context)
41      {
42          super(DEFAULT_SMTPS_PORT, context);
43      }
44  
45      public String getProtocol()
46      {
47          return "smtps";
48      }
49  
50      public String getBaseProtocol()
51      {
52          return "smtp";
53      }
54  
55      protected void doInitialise() throws InitialisationException
56      {
57          try
58          {
59              tls.initialise(true, null);
60          }
61          catch (CreateException e)
62          {
63              throw new InitialisationException(e, this);
64          }
65      }
66  
67      @Override
68      protected void extendPropertiesForSession(Properties global, Properties local, URLName url)
69      {
70          super.extendPropertiesForSession(global, local, url);
71  
72          local.setProperty("mail." + getProtocol() + ".ssl", "true");
73          local.setProperty("mail." + getProtocol() + ".socketFactory.class", getSocketFactory());
74          local.setProperty("mail." + getProtocol() + ".socketFactory.fallback", getSocketFactoryFallback());
75  
76          new TlsPropertiesMapper(SmtpsSocketFactory.MULE_SMTPS_NAMESPACE).writeToProperties(global, 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 proeprties
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 }