1 57 58 package org.apache.soap.transport.smtp; 59 60 import java.io.*; 61 import java.net.*; 62 import java.util.*; 63 import org.w3c.dom.*; 64 import org.apache.soap.util.*; 65 import org.apache.soap.util.xml.*; 66 import org.apache.soap.Envelope; 67 import org.apache.soap.SOAPException; 68 import org.apache.soap.encoding.*; 69 import org.apache.soap.transport.*; 70 import org.apache.soap.rpc.*; 71 72 import com.ibm.network.mail.base.*; 73 74 80 public class SOAPSMTPConnection implements SOAPTransport { 81 private String fromAddr; 82 private String subject; 83 private Object waitObject = new Object (); 84 private long popPollDelay; 85 private boolean responseReceived = false; 86 private Hashtable responseHeaders = new Hashtable (); 87 private BufferedReader responseReader = null; 88 com.ibm.network.mail.smtp.protocol.CoreProtocolBean smtpBean = 89 new com.ibm.network.mail.smtp.protocol.CoreProtocolBean (); 90 com.ibm.network.mail.pop3.protocol.CoreProtocolBean pop3Bean = 91 new com.ibm.network.mail.pop3.protocol.CoreProtocolBean (); 92 private SOAPContext responseSOAPContext; 93 94 106 public SOAPSMTPConnection (String fromAddr, String subject, 107 String smtpServer, long popPollDelay, 108 String popServer, String popLogin, 109 String popPassword) { 110 this.fromAddr = fromAddr; 111 this.subject = subject; 112 this.popPollDelay = popPollDelay; 113 114 smtpBean.setSmtpServerHost (smtpServer); 116 smtpBean.addStatusListener ( 117 new com.ibm.network.mail.smtp.event.StatusListener () { 118 public void operationComplete ( 119 com.ibm.network.mail.smtp.event.StatusEvent e) { 120 System.err.println ("DONE: " + e.getStatusString ()); 121 synchronized (waitObject) { 122 waitObject.notify (); 123 } 124 } 125 126 public void processStatus ( 127 com.ibm.network.mail.smtp.event.StatusEvent e) { 128 System.err.println ("Status update: " + e.getStatusString ()); 129 } 130 } 131 ); 132 133 responseReader = null; 135 pop3Bean.setPOP3ServerHost (popServer); 136 pop3Bean.setUserOptions( popLogin, 137 popPassword, 138 false, 139 false); 140 pop3Bean.addMessageListener ( 141 new com.ibm.network.mail.pop3.event.MessageListener () { 142 public void messageReceived ( 143 com.ibm.network.mail.pop3.event.MessageEvent me) { 144 145 MimeMessage msg = me.getMessage (); 146 String subj = msg.getHeader (SMTPConstants.SMTP_HEADER_SUBJECT); 147 if (subj != null) { 148 responseHeaders.put (SMTPConstants.SMTP_HEADER_SUBJECT, subj); 149 } 150 String soapAction = 151 msg.getHeader (org.apache.soap.Constants.HEADER_SOAP_ACTION); 152 if (soapAction != null) { 153 responseHeaders.put (org.apache.soap.Constants.HEADER_SOAP_ACTION, 154 soapAction); 155 } 156 String to = msg.getHeader (SMTPConstants.SMTP_HEADER_TO); 157 if (to != null) { 158 responseHeaders.put (SMTPConstants.SMTP_HEADER_TO, to); 159 } 160 String from = msg.getHeader (SMTPConstants.SMTP_HEADER_FROM); 161 if (from != null) { 162 responseHeaders.put (SMTPConstants.SMTP_HEADER_FROM, from); 163 } 164 MimeBodyPart mbp = (MimeBodyPart) msg.getContent (); 165 byte[] ba = (byte[]) mbp.getContent (); 166 String baStr = new String (ba); 167 try { 168 responseSOAPContext = new SOAPContext(); 169 responseSOAPContext.setRootPart(baStr, "text/xml"); 170 } catch(Exception ioe) { 171 ioe.printStackTrace(); 172 } 173 responseReader = new BufferedReader (new StringReader (baStr)); 174 responseReceived = true; 175 } 176 } 177 ); 178 pop3Bean.addStatusListener (new POP3StatusListener ()); 179 } 180 181 195 public void send (URL toAddr, String actionURI, Hashtable headers, 196 Envelope env, 197 SOAPMappingRegistry smr, SOAPContext ctx) 198 throws SOAPException { 199 MimeMessage msg = new MimeMessage(); 200 201 MimeBodyPart mbp = new MimeBodyPart (); 202 StringWriter sw = new StringWriter(); 203 try { 204 env.marshall (sw, smr); 205 } catch (IOException e) { 206 e.printStackTrace (); 207 } 208 mbp.setContent (sw.toString (), "text/xml"); 209 mbp.setEncoding (MimePart.SEVENBIT); 210 mbp.setDisposition (MimePart.INLINE); 211 msg.setContent (mbp, ""); 212 213 msg.addHeader ("Subject", subject); 214 msg.addHeader ("SOAPAction", actionURI); 215 msg.addHeader ("From", fromAddr); 216 msg.addRecipients (MimeMessage.TO, 217 new InternetAddress[] { 218 new InternetAddress (toAddr.getFile ()) 219 }); 220 221 smtpBean.sendMessage (msg); 222 223 try { 224 synchronized (waitObject) { 225 waitObject.wait (); 226 } 227 } catch (Exception e) { 228 e.printStackTrace (); 229 } 230 } 231 232 241 public synchronized BufferedReader receive () { 242 if (responseReader == null) { 243 responseReceived = false; 244 while (!responseReceived) { 245 try { 246 if (pop3Bean.isReady ()) { 247 System.err.println ("SOAPSMTPConnection: Polling for response .."); 248 pop3Bean.receiveMessage (); 249 if (responseReceived) { 250 break; 251 } 252 } 253 Thread.sleep (popPollDelay); 254 } catch (Exception e) { 255 e.printStackTrace (); 256 } 257 } 258 } 259 return responseReader; 260 } 261 262 267 public synchronized Hashtable getHeaders () { 268 if (responseReader == null) 269 receive(); 270 return responseHeaders; 271 } 272 273 279 public synchronized SOAPContext getResponseSOAPContext () { 280 if (responseReader == null) 281 receive(); 282 return responseSOAPContext; 283 } 284 } 285 | Popular Tags |