1 57 58 package org.apache.soap.server; 59 60 import java.io.*; 61 import java.net.*; 62 import java.util.*; 63 64 import com.ibm.network.mail.base.*; 65 import com.ibm.network.mail.smtp.event.*; 66 import org.apache.soap.util.net.*; 67 import org.apache.soap.util.*; 68 import org.apache.soap.rpc.SOAPContext; 69 import org.apache.soap.Constants; 70 import org.apache.soap.transport.*; 71 import org.apache.soap.transport.smtp.*; 72 import javax.mail.MessagingException ; 73 74 83 public class SMTP2HTTPBridge implements Runnable { 84 Object waitObject = new Object (); 85 long pollDelayMillis; 86 com.ibm.network.mail.pop3.protocol.CoreProtocolBean pop3 = 87 new com.ibm.network.mail.pop3.protocol.CoreProtocolBean (); 88 com.ibm.network.mail.smtp.protocol.CoreProtocolBean smtp = 89 new com.ibm.network.mail.smtp.protocol.CoreProtocolBean (); 90 URL httpURL; 91 92 103 public SMTP2HTTPBridge ( 104 long pollDelayMillis, 105 String popServer, String popLoginName, 106 String password, 107 108 URL httpURL, 109 110 String smtpServer) { 111 112 this.pollDelayMillis = pollDelayMillis; 113 pop3.setPOP3ServerHost (popServer); 114 pop3.setUserOptions( popLoginName, 115 password, 116 false, 117 false); 118 pop3.addMessageListener ( 119 new com.ibm.network.mail.pop3.event.MessageListener () { 120 public void messageReceived ( 121 com.ibm.network.mail.pop3.event.MessageEvent me) { 122 receiveMessage (me.getMessage ()); 123 } 124 } 125 ); 126 pop3.addStatusListener (new POP3StatusListener ()); 127 128 129 this.httpURL = httpURL; 130 131 132 smtp.setSmtpServerHost (smtpServer); 133 smtp.addStatusListener (new StatusListener () { 134 public void operationComplete (StatusEvent e) { 135 System.err.println ("DONE: " + e.getStatusString ()); 136 synchronized (waitObject) { 137 waitObject.notify (); 138 } 139 } 140 141 public void processStatus (StatusEvent e) { 142 System.err.println ("Status update: " + e.getStatusString ()); 143 } 144 }); 145 } 146 147 150 public void run () { 151 while (true) { 152 if (pop3.isReady ()) { 153 System.err.println ("SMTP2HTTPBridge: Polling for messages .."); 154 pop3.receiveMessage (); 155 } 156 try { 157 Thread.sleep (pollDelayMillis); 158 } catch (Exception e) { 159 e.printStackTrace (); 160 } 161 } 162 } 163 164 170 void receiveMessage (MimeMessage msg) { 171 172 String subject = msg.getHeader (SMTPConstants.SMTP_HEADER_SUBJECT); 173 String actionURI = msg.getHeader (Constants.HEADER_SOAP_ACTION); 174 String toAddr = msg.getHeader (SMTPConstants.SMTP_HEADER_TO); 175 String fromAddr = msg.getHeader (SMTPConstants.SMTP_HEADER_FROM); 176 MimeBodyPart mbp = (MimeBodyPart) msg.getContent (); 177 byte[] ba = (byte[]) mbp.getContent (); 178 179 180 Hashtable headers = new Hashtable (); 181 headers.put (Constants.HEADER_SOAP_ACTION, actionURI); 182 TransportMessage response; 183 SOAPContext ctx; 185 try 186 { 187 TransportMessage tmsg = new TransportMessage(new String (ba), 189 new SOAPContext(), 190 headers); 191 tmsg.save(); 192 193 response = HTTPUtils.post (httpURL, tmsg, 194 30000, null, 0); 195 ctx = response.getSOAPContext(); 196 } catch (Exception e) { 197 e.printStackTrace(); 198 return; 199 } 200 System.err.println ("HTTP RESPONSE IS: "); 201 String payload = null; 202 try { 203 payload = IOUtils.getStringFromReader (response.getEnvelopeReader()); 205 } catch (Exception e) { 206 e.printStackTrace (); 207 } 208 System.err.println ("'" + payload + "'"); 209 210 211 MimeBodyPart mbpResponse = new MimeBodyPart (); 212 mbpResponse.setContent (new String (payload), response.getContentType()); 213 mbpResponse.setEncoding (MimePart.SEVENBIT); 214 mbpResponse.setDisposition (MimePart.INLINE); 215 216 MimeMessage msgResponse = new MimeMessage(); 217 msgResponse.setContent (mbpResponse, ""); 218 msgResponse.addHeader (SMTPConstants.SMTP_HEADER_SUBJECT, 219 "Re: " + subject); 220 msgResponse.addHeader (Constants.HEADER_SOAP_ACTION, actionURI); 221 msgResponse.addHeader (SMTPConstants.SMTP_HEADER_FROM, 222 SMTPUtils.getAddressFromAddressHeader (toAddr)); 223 String sendTo = SMTPUtils.getAddressFromAddressHeader (fromAddr); 224 msgResponse.addRecipients (MimeMessage.TO, 225 new InternetAddress[] { 226 new InternetAddress (sendTo) 227 }); 228 smtp.sendMessage (msgResponse); 229 } 230 231 public static void main (String [] args) throws Exception { 232 if (args.length != 6) { 233 System.err.println ("Usage: java " + SMTP2HTTPBridge.class.getName () + 234 " polldelay \\"); 235 System.err.println ("\t\t pop3host pop3login pop3passwd httpurl " + 236 "smtphostname"); 237 System.err.println (" where:"); 238 System.err.println (" polldelay number of millisec to " + 239 "sleep between polls"); 240 System.err.println (" pop3host hostname of the POP3 server"); 241 System.err.println (" pop3login login ID of POP3 account"); 242 System.err.println (" pop3passwd POP3 account password"); 243 System.err.println (" routerURL http URL of SOAP router to " + 244 "bridge to"); 245 System.err.println (" smtphostname SMTP server host name"); 246 System.exit (1); 247 } 248 SMTP2HTTPBridge sb = new SMTP2HTTPBridge ( 249 Integer.parseInt (args[0]), 250 args[1], 251 args[2], 252 args[3], 253 254 new URL (args[4]), 255 256 args[5]); 257 new Thread (sb).start (); 258 } 259 } 260 | Popular Tags |