1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // mailto:mikehenderson@dunelm.org.uk
7    //
8    // This library is free software.
9    //
10   // You may redistribute it and/or modify it under the terms of the GNU
11   // Lesser General Public License as published by the Free Software Foundation.
12   //
13   // Version 2.1 of the license should be included with this distribution in
14   // the file LICENSE, as well as License.html. If the license is not
15   // included with this distribution, you may find a copy at the FSF web
16   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
17   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
18   //
19   // This library is distributed in the hope that it will be useful,
20   // but WITHOUT ANY WARRANTY; without even the implied waranty of
21   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   // Lesser General Public License for more details.
23   //
24   // $Id: SmtpClientTransport.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
25   package com.mjh.switchrmi.transport.smtp;
26   
27   import com.mjh.switchrmi.*;
28   import com.mjh.util.*;
29   
30   import java.io.*;
31   
32   import java.net.MalformedURLException;
33   
34   import java.util.*;
35   
36   import javax.activation.*;
37   
38   import javax.mail.*;
39   import javax.mail.internet.*;
40   
41   import org.apache.log4j.Logger;
42   
43   /**
44    * @author michaelh
45    *
46    * To change this generated comment edit the template variable "typecomment":
47    * Window>Preferences>Java>Templates.
48    * To enable and disable the creation of type comments go to
49    * Window>Preferences>Java>Code Generation.
50    */
51   public class SmtpClientTransport
52       extends RmiTransportBase
53   {
54       private static final Logger log = 
55               Logger.getLogger(SmtpClientTransport.class.getName());
56       private ByteArrayOutputStream out;
57       private String mimeType;
58       private UrlInfo urlInfo;
59   
60       /**
61        * Constructor for SmtpClientTransport.
62        */
63       public SmtpClientTransport()
64       {
65           super.setType(RmiTransport.ASYNCHRONOUS);
66           super.setName("smtp-client");
67       }
68   
69       /**
70        * @see com.mjh.switchrmi.RmiTransport#getOutputStream()
71        */
72       public OutputStream getOutputStream(RmiContext context)
73           throws IOException
74       {
75           System.out.println("getOutputStream()");
76   
77           if (out == null)
78           {
79               out = new ByteArrayOutputStream();
80           }
81   
82           return out;
83       }
84   
85       byte[] getBytes()
86       {
87           return out.toByteArray();
88       }
89   
90       public String getMimeType()
91       {
92           return mimeType;
93       }
94   
95       private String getCleanUrl()
96       {
97           String url = "smtp:" + urlInfo.getHost()
98                        + ((urlInfo.getPort() != 0) ? (":" + urlInfo.getPort()) : "")
99                        + urlInfo.getPath();
100  
101          return url;
102      }
103  
104      protected String getUserName()
105      {
106          String user = (String) urlInfo.getParameterValue("user");
107  
108          System.out.println("user = " + user);
109  
110          return user;
111      }
112  
113      protected String getPassword()
114      {
115          String password = (String) urlInfo.getParameterValue("password");
116  
117          System.out.println("password = " + password);
118  
119          return password;
120      }
121  
122      public UrlInfo getUrlInfo(RmiContext context)
123                         throws MalformedURLException
124      {
125          return new UrlInfo(context.getUrl(), new String[] { "from", "to" });
126      }
127  
128      private Properties makeSessionProperties(UrlInfo urlInfo)
129      {
130          Properties p = new Properties();
131  
132          p.setProperty("mail.transport.protocol", urlInfo.getProtocol());
133          p.setProperty("mail.host", urlInfo.getHost());
134  
135          String user = urlInfo.getParameterValue("user");
136  
137          if (user != null)
138          {
139              p.setProperty("mail.user", user);
140              p.setProperty("mail.smtp.auth", "true");
141          }
142  
143          return p;
144      }
145  
146      /**
147       * @see com.mjh.switchrmi.RmiTransport#send()
148       */
149      public void send(RmiContext context)
150                throws Exception
151      {
152          urlInfo = context.getUrlInfo();
153  
154          // looks for mail.smtp.host set in system properties
155          Properties sessionProps = makeSessionProperties(urlInfo);
156  
157          System.out.println("sessionProps = " + sessionProps);
158  
159          Authenticator auth = null;
160  
161          if (urlInfo.getParameterValue("user") != null)
162          {
163              auth = new SmtpAuthenticator();
164          }
165  
166          System.out.println("auth = " + auth);
167  
168          Session session = Session.getInstance(sessionProps, auth);
169  
170          if (log.isDebugEnabled())
171          {
172              session.setDebug(true);
173          }
174  
175          session.setDebug(true);
176  
177          mimeType = context.getProtocol().getMimeType();
178  
179          InternetAddress from = 
180                  new InternetAddress(urlInfo.getParameterValue("from"));
181          InternetAddress[] to = 
182                  InternetAddress.parse(urlInfo.getParameterValue("to"));
183  
184          Message message = new MimeMessage(session);
185  
186          message.setFrom(from);
187          message.setRecipients(Message.RecipientType.TO, to);
188          message.setSubject(
189                  "SwitchRMI Remote Method Invocation Request Attached To This Message");
190  
191          Multipart multipart = new MimeMultipart();
192          MimeBodyPart body = new MimeBodyPart();
193  
194          body.setContent(getCleanUrl(), "text/plain");
195  
196          MimeBodyPart attachment = new MimeBodyPart();
197  
198          attachment.setDataHandler(new DataHandler(new SmtpDataSource()));
199  
200          multipart.addBodyPart(body);
201          multipart.addBodyPart(attachment);
202  
203          message.setContent(multipart);
204  
205          Transport.send(message);
206      }
207  
208      private String getObjectName()
209      {
210          return "SwitchRMI-over-SMTP";
211      }
212  
213      class SmtpAuthenticator extends Authenticator
214      {
215          protected PasswordAuthentication getPasswordAuthentication()
216          {
217              return new PasswordAuthentication(
218                             SmtpClientTransport.this.getUserName(), 
219                             SmtpClientTransport.this.getPassword());
220          }
221      }
222  
223      class SmtpDataSource implements DataSource
224      {
225          public SmtpDataSource()
226          {
227          }
228  
229          public String getContentType()
230          {
231              String type = SmtpClientTransport.this.getMimeType();
232  
233              System.out.println("SmtpDataSource.getContentType() = " + type);
234  
235              return type;
236          }
237  
238          public String getName()
239          {
240              String name = SmtpClientTransport.this.getObjectName();
241  
242              System.out.println("SmtpDataSource.getName() = " + name);
243  
244              return name;
245          }
246  
247          public InputStream getInputStream()
248              throws IOException
249          {
250              byte[] bytes = SmtpClientTransport.this.getBytes();
251  
252              System.out.println("SmtpDataSource.getInputStream() bytes = "
253                                 + new String(bytes));
254  
255              return new ByteArrayInputStream(bytes);
256          }
257  
258          public OutputStream getOutputStream()
259              throws IOException
260          {
261              System.out.println("SmtpDataSource.getInputStream() out = "
262                                 + "null");
263  
264              return null;
265          }
266      }
267  }