1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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
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 }