View Javadoc

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