1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: SmtpServiceHandler.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   package com.mjh.switchrmi.transport.smtp;
27   
28   import com.mjh.switchrmi.*;
29   
30   import java.io.*;
31   
32   import java.lang.reflect.Method;
33   
34   import javax.activation.*;
35   
36   import javax.mail.*;
37   import javax.mail.internet.*;
38   
39   import javax.naming.*;
40   
41   import org.apache.log4j.*;
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 SmtpServiceHandler
52   {
53       private static final Logger log = 
54               Logger.getLogger(SmtpServiceHandler.class.getName());
55       private Message message;
56       private Context jndiContext;
57   
58       /**
59        * Constructor for SmtpServiceHandler.
60        */
61       public SmtpServiceHandler(Context ctx)
62       {
63           jndiContext = ctx;
64       }
65   
66       public void setMessage(Message msg)
67       {
68           message = msg;
69       }
70   
71       public void invoke() throws Exception
72       {
73           RmiHandler handler = new RmiHandler();
74           Multipart multipart = (Multipart) message.getContent();
75           MimeBodyPart text = (MimeBodyPart) multipart.getBodyPart(0);
76           MimeBodyPart attachment = (MimeBodyPart) multipart.getBodyPart(1);
77           String objectUrl = (String) text.getContent();
78   
79           System.out.println("objectUrl = " + objectUrl);
80   
81           RmiContext context = new RmiContextImpl(true, objectUrl, jndiContext);
82   
83           context.setTransport(new SmtpHandlerTransport(attachment));
84           handler.serviceInvoke(context, null);
85       }
86   
87       private void debug(String msg)
88       {
89           System.out.println(msg);
90           log.debug(msg);
91       }
92   
93       private void debug(Exception ex)
94       {
95           log.debug("Exception", ex);
96       }
97   
98       class SmtpHandlerTransport
99           extends RmiTransportBase
100      {
101          private MimeBodyPart methodInfo;
102  
103          public SmtpHandlerTransport(MimeBodyPart part)
104          {
105              methodInfo = part;
106              super.setType(RmiTransport.ASYNCHRONOUS);
107              super.setName("smtp-service");
108          }
109  
110          public InputStream getInputStream(RmiContext context)
111          {
112              InputStream in = null;
113  
114              try
115              {
116                  in = methodInfo.getInputStream();
117              }
118              catch (Exception ex)
119              {
120                  log.debug(ex);
121              }
122  
123              return in;
124          }
125  
126          public OutputStream getOutputStream(RmiContext context)
127          {
128              return new ByteArrayOutputStream();
129          }
130      }
131  }