axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject cvs commit: xml-axis/java/xmls properties.xml targets.xml
Date Fri, 13 Dec 2002 20:46:08 GMT
dims        2002/12/13 12:46:08

  Modified:    java     build.xml
               java/src/org/apache/axis/utils Options.java
               java/xmls properties.xml targets.xml
  Added:       java/src/org/apache/axis/transport/mail Handler.java
                        MailConstants.java MailSender.java MailServer.java
                        MailTransport.java MailWorker.java
  Log:
  Checking in an Initial Impl of a SMTP/POP3 transport.
  
  Notes:
  - You will need a daily build of Jakarta Commons/Net from (http://jakarta.apache.org/commons/sandbox/net/)
  - Checked this in to be able to try Async API (When the Async Implementations start, as i don't have access to a  JMS environment)
  - One major problem is that Commons/Net uses Reader/Writer and JavaMail API uses InputStream/OutputStream so we need to store messages in an intermediate byte array
  
  Revision  Changes    Path
  1.219     +1 -0      xml-axis/java/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/build.xml,v
  retrieving revision 1.218
  retrieving revision 1.219
  diff -u -r1.218 -r1.219
  --- build.xml	6 Dec 2002 18:21:40 -0000	1.218
  +++ build.xml	13 Dec 2002 20:46:08 -0000	1.219
  @@ -126,6 +126,7 @@
         <exclude name="**/org/apache/axis/transport/http/ServletEndpointContextImpl.java" unless="servlet.present"/>
         <exclude name="**/org/apache/axis/transport/configuration/EngineConfigurationFactoryServlet.java" unless="servlet.present"/>
         <exclude name="**/org/apache/axis/transport/http/CommonsHTTPSender.java" unless="commons-httpclient.present"/>
  +      <exclude name="**/org/apache/axis/transport/mail/*.java" unless="commons-httpclient.present"/>
         <exclude name="**/org/apache/axis/transport/jms/*" unless="jms.present"/>
         <exclude name="**/org/apache/axis/components/jms/BeanVendorAdapter.java" unless="jms.present"/>
         <exclude name="**/org/apache/axis/components/jms/JMSVendorAdapter.java" unless="jms.present"/>
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/transport/mail/Handler.java
  
  Index: Handler.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Axis" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.axis.transport.mail;
  
  import java.net.URL;
  import java.net.URLConnection;
  import java.net.URLStreamHandler;
  
  /**
   * A stub URLStreamHandler, so the system will recognize our
   * custom URLs as valid.
   * 
   * @author Davanum Srinivas <dims@yahoo.com>
   */
  public class Handler extends URLStreamHandler {
      protected URLConnection openConnection(URL u) {
          return null;
      }
  }
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/transport/mail/MailConstants.java
  
  Index: MailConstants.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Axis" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.axis.transport.mail;
  
  public class MailConstants {
      public final static String FROM_ADDRESS = "transport.mail.from";
      public final static String TO_ADDRESS = "transport.mail.to";
      public final static String SMTP_HOST = "transport.mail.smtp.host";
      public final static String POP3_HOST = "transport.mail.pop3.host";
      public final static String POP3_USERID = "transport.mail.pop3.userid";
      public final static String POP3_PASSWORD = "transport.mail.pop3.password";
  }
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/transport/mail/MailSender.java
  
  Index: MailSender.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Axis" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.axis.transport.mail;
  
  import org.apache.axis.AxisFault;
  import org.apache.axis.Message;
  import org.apache.axis.MessageContext;
  import org.apache.axis.components.logger.LogFactory;
  import org.apache.axis.components.uuid.UUIDGen;
  import org.apache.axis.components.uuid.UUIDGenFactory;
  import org.apache.axis.handlers.BasicHandler;
  import org.apache.axis.transport.http.HTTPConstants;
  import org.apache.axis.utils.Messages;
  import org.apache.commons.logging.Log;
  import org.apache.commons.net.pop3.POP3Client;
  import org.apache.commons.net.pop3.POP3MessageInfo;
  import org.apache.commons.net.smtp.SMTPClient;
  import org.apache.commons.net.smtp.SMTPReply;
  
  import javax.mail.Session;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  import javax.mail.internet.MimePart;
  import java.io.BufferedReader;
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.Reader;
  import java.io.Writer;
  import java.util.Properties;
  
  /**
   * This is meant to be used on a SOAP Client to call a SOAP server via SMTP/POP3
   *
   * @author Davanum Srinivas (dims@yahoo.com)
   */
  public class MailSender extends BasicHandler {
  
      protected static Log log = LogFactory.getLog(MailSender.class.getName());
      private UUIDGen uuidGen = UUIDGenFactory.getUUIDGen(null);
  
      Properties prop = new Properties();
      Session session = Session.getDefaultInstance(prop, null);
  
      /**
       * invoke creates a socket connection, sends the request SOAP message and then
       * reads the response SOAP message back from the SOAP server
       *
       * @param msgContext the messsage context
       *
       * @throws AxisFault
       */
      public void invoke(MessageContext msgContext) throws AxisFault {
  
          if (log.isDebugEnabled()) {
              log.debug(Messages.getMessage("enter00", "MailSender::invoke"));
          }
          try {
              // Send the SOAP request to the SMTP server
              String id = writeUsingSMTP(msgContext);
              
              // Read SOAP response from the POP3 Server
              readUsingPOP3(id, msgContext);
          } catch (Exception e) {
              log.debug(e);
              throw AxisFault.makeFault(e);
          }
          if (log.isDebugEnabled()) {
              log.debug(Messages.getMessage("exit00",
                      "HTTPDispatchHandler::invoke"));
          }
      }
  
  
      /**
       * Send the soap request message to the server
       *
       * @param msgContext message context
       *
       * @return id for the current message
       * @throws Exception
       */
      private String writeUsingSMTP(MessageContext msgContext)
              throws Exception {
          String id = (new java.rmi.server.UID()).toString();
          String smtpHost = msgContext.getStrProp(MailConstants.SMTP_HOST);
  
          SMTPClient client = new SMTPClient();
          client.connect(smtpHost);
          
          // After connection attempt, you should check the reply code to verify
          // success.
          System.out.print(client.getReplyString());
          int reply = client.getReplyCode();
          if (!SMTPReply.isPositiveCompletion(reply)) {
              client.disconnect();
              AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null);
              throw fault;
          }
  
          client.login(smtpHost);
          System.out.print(client.getReplyString());
          reply = client.getReplyCode();
          if (!SMTPReply.isPositiveCompletion(reply)) {
              client.disconnect();
              AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null);
              throw fault;
          }
  
          String fromAddress = msgContext.getStrProp(MailConstants.FROM_ADDRESS);
          String toAddress = msgContext.getStrProp(MailConstants.TO_ADDRESS);
  
          MimeMessage msg = new MimeMessage(session);
          msg.setFrom(new InternetAddress(fromAddress));
          msg.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toAddress));
  
          // Get SOAPAction, default to ""
          String action = msgContext.useSOAPAction()
                  ? msgContext.getSOAPActionURI()
                  : "";
  
          if (action == null) {
              action = "";
          }
  
          Message reqMessage = msgContext.getRequestMessage();
  
          msg.addHeader(HTTPConstants.HEADER_USER_AGENT, Messages.getMessage("axisUserAgent"));
          msg.addHeader(HTTPConstants.HEADER_SOAP_ACTION, action);
          msg.setDisposition(MimePart.INLINE);
          msg.setSubject(id);
  
          ByteArrayOutputStream out = new ByteArrayOutputStream(8 * 1024);
          reqMessage.writeTo(out);
          msg.setContent(out.toString(), reqMessage.getContentType(msgContext.getSOAPConstants()));
  
          ByteArrayOutputStream out2 = new ByteArrayOutputStream(8 * 1024);
          msg.writeTo(out2);
  
          client.setSender(fromAddress);
          System.out.print(client.getReplyString());
          client.addRecipient(toAddress);
          System.out.print(client.getReplyString());
  
          Writer writer = client.sendMessageData();
          System.out.print(client.getReplyString());
          writer.write(out2.toString());
          writer.flush();
          writer.close();
  
          System.out.print(client.getReplyString());
          if (!client.completePendingCommand()) {
              System.out.print(client.getReplyString());
              AxisFault fault = new AxisFault("SMTP", "( Failed to send email )", null, null);
              throw fault;
          }
          System.out.print(client.getReplyString());
          client.logout();
          client.disconnect();
          return id;
      }
  
      /**
       * Read from server using POP3
       * @param msgContext
       * @throws Exception
       */
      private void readUsingPOP3(String id, MessageContext msgContext) throws Exception {
          // Read the response back from the server
          String pop3Host = msgContext.getStrProp(MailConstants.POP3_HOST);
          String pop3User = msgContext.getStrProp(MailConstants.POP3_USERID);
          String pop3passwd = msgContext.getStrProp(MailConstants.POP3_PASSWORD);
  
          Reader reader;
          POP3MessageInfo[] messages = null;
  
          MimeMessage mimeMsg = null;
          POP3Client pop3 = new POP3Client();
          // We want to timeout if a response takes longer than 60 seconds
          pop3.setDefaultTimeout(60000);
  
          for (int i = 0; i < 12; i++) {
              pop3.connect(pop3Host);
  
              if (!pop3.login(pop3User, pop3passwd)) {
                  pop3.disconnect();
                  AxisFault fault = new AxisFault("POP3", "( Could not login to server.  Check password. )", null, null);
                  throw fault;
              }
  
              messages = pop3.listMessages();
              if (messages != null && messages.length > 0) {
                  StringBuffer buffer = null;
                  for (int j = 0; j < messages.length; j++) {
                      reader = pop3.retrieveMessage(messages[j].number);
                      if (reader == null) {
                          AxisFault fault = new AxisFault("POP3", "( Could not retrieve message header. )", null, null);
                          throw fault;
                      }
  
                      buffer = new StringBuffer();
                      BufferedReader bufferedReader = new BufferedReader(reader);
                      int ch;
                      while ((ch = bufferedReader.read()) != -1) {
                          buffer.append((char) ch);
                      }
                      bufferedReader.close();
                      if (buffer.toString().indexOf(id) != -1) {
                          ByteArrayInputStream bais = new ByteArrayInputStream(buffer.toString().getBytes());
                          Properties prop = new Properties();
                          Session session = Session.getDefaultInstance(prop, null);
  
                          mimeMsg = new MimeMessage(session, bais);
                          pop3.deleteMessage(messages[j].number);
                          break;
                      }
                      buffer = null;
                  }
              }
              pop3.logout();
              pop3.disconnect();
              if (mimeMsg == null) {
                  Thread.sleep(5000);
              } else {
                  break;
              }
          }
  
          if (mimeMsg == null) {
              pop3.logout();
              pop3.disconnect();
              AxisFault fault = new AxisFault("POP3", "( Could not retrieve message list. )", null, null);
              throw fault;
          }
  
          String contentType = mimeMsg.getContentType();
          String contentLocation = mimeMsg.getContentID();
          Message outMsg = new Message(mimeMsg.getInputStream(), false,
                  contentType, contentLocation);
  
          outMsg.setMessageType(Message.RESPONSE);
          msgContext.setResponseMessage(outMsg);
          if (log.isDebugEnabled()) {
              log.debug("\n" + Messages.getMessage("xmlRecd00"));
              log.debug("-----------------------------------------------");
              log.debug(outMsg.getSOAPPartAsString());
          }
      }
  }
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/transport/mail/MailServer.java
  
  Index: MailServer.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Axis" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.axis.transport.mail;
  
  import org.apache.axis.components.logger.LogFactory;
  import org.apache.axis.i18n.Messages;
  import org.apache.axis.server.AxisServer;
  import org.apache.axis.utils.Options;
  import org.apache.commons.logging.Log;
  import org.apache.commons.net.pop3.POP3Client;
  import org.apache.commons.net.pop3.POP3MessageInfo;
  
  import javax.mail.Session;
  import javax.mail.internet.MimeMessage;
  import java.io.BufferedReader;
  import java.io.ByteArrayInputStream;
  import java.io.Reader;
  import java.net.MalformedURLException;
  import java.util.Properties;
  
  /**
   * This is a simple implementation of an SMTP/POP3 server for processing
   * SOAP requests via Apache's xml-axis.  This is not intended for production
   * use.  Its intended uses are for demos, debugging, and performance
   * profiling.
   *
   * @author Davanum Srinivas <dims@yahoo.com>
   * @author Rob Jellinghaus (robj@unrealities.com)
   */
  
  public class MailServer implements Runnable {
      protected static Log log =
              LogFactory.getLog(MailServer.class.getName());
  
      private String host;
      private int port;
      private String userid;
      private String password;
  
      public MailServer(String host, int port, String userid, String password) {
          this.host = host;
          this.port = port;
          this.userid = userid;
          this.password = password;
      }
  
      // Are we doing threads?
      private static boolean doThreads = true;
  
      public void setDoThreads(boolean value) {
          doThreads = value;
      }
  
      public boolean getDoThreads() {
          return doThreads;
      }
  
      public String getHost() {
          return host;
      }
  
      // Axis server (shared between instances)
      private static AxisServer myAxisServer = null;
  
      protected static synchronized AxisServer getAxisServer() {
          if (myAxisServer == null) {
              myAxisServer = new AxisServer();
          }
          return myAxisServer;
      }
  
      // are we stopped?
      // latch to true if stop() is called
      private boolean stopped = false;
  
      /**
       * Accept requests from a given TCP port and send them through the
       * Axis engine for processing.
       */
      public void run() {
          log.info(Messages.getMessage("start00", "MailServer", host + ":" + port));
  
          // Accept and process requests from the socket
          while (!stopped) {
              try {
                  pop3.connect(host, port);
                  pop3.login(userid, password);
  
                  POP3MessageInfo[] messages = pop3.listMessages();
                  if (messages != null && messages.length > 0) {
                      for (int i = 0; i < messages.length; i++) {
                          Reader reader = pop3.retrieveMessage(messages[i].number);
                          if (reader == null) {
                              continue;
                          }
  
                          StringBuffer buffer = new StringBuffer();
                          BufferedReader bufferedReader =
                                  new BufferedReader(reader);
                          int ch;
                          while ((ch = bufferedReader.read()) != -1) {
                              buffer.append((char) ch);
                          }
                          bufferedReader.close();
                          ByteArrayInputStream bais = new ByteArrayInputStream(buffer.toString().getBytes());
                          Properties prop = new Properties();
                          Session session = Session.getDefaultInstance(prop, null);
  
                          MimeMessage mimeMsg = new MimeMessage(session, bais);
                          pop3.deleteMessage(messages[i].number);
                          if (mimeMsg != null) {
                              MailWorker worker = new MailWorker(this, mimeMsg);
                              if (doThreads) {
                                  Thread thread = new Thread(worker);
                                  thread.setDaemon(true);
                                  thread.start();
                              } else {
                                  worker.run();
                              }
                          }
                      }
                  }
              } catch (java.io.InterruptedIOException iie) {
              } catch (Exception e) {
                  log.debug(Messages.getMessage("exception00"), e);
                  break;
              } finally {
                  try {
                      pop3.logout();
                      pop3.disconnect();
                      Thread.sleep(3000);
                  } catch (Exception e) {
                      log.error(Messages.getMessage("exception00"), e);
                  }
              }
          }
          log.info(Messages.getMessage("quit00", "MailServer"));
      }
  
      /**
       * POP3 connection
       */
      private POP3Client pop3;
  
      /**
       * Obtain the serverSocket that that MailServer is listening on.
       */
      public POP3Client getPOP3() {
          return pop3;
      }
  
      /**
       * Set the serverSocket this server should listen on.
       * (note : changing this will not affect a running server, but if you
       *  stop() and then start() the server, the new socket will be used).
       */
      public void setPOP3(POP3Client pop3) {
          this.pop3 = pop3;
      }
  
      /**
       * Start this server.
       *
       * Spawns a worker thread to listen for HTTP requests.
       *
       * @param daemon a boolean indicating if the thread should be a daemon.
       */
      public void start(boolean daemon) throws Exception {
          if (doThreads) {
              Thread thread = new Thread(this);
              thread.setDaemon(daemon);
              thread.start();
          } else {
              run();
          }
      }
  
      /**
       * Start this server as a NON-daemon.
       */
      public void start() throws Exception {
          start(false);
      }
  
      /**
       * Stop this server.
       *
       * This will interrupt any pending accept().
       */
      public void stop() throws Exception {
          /* 
           * Close the server socket cleanly, but avoid fresh accepts while
           * the socket is closing.
           */
          stopped = true;
          log.info(Messages.getMessage("quit00", "MailServer"));
  
          // Kill the JVM, which will interrupt pending accepts even on linux.
          System.exit(0);
      }
  
      /**
       * Server process.
       */
      public static void main(String args[]) {
          Options opts = null;
          try {
              opts = new Options(args);
          } catch (MalformedURLException e) {
              log.error(Messages.getMessage("malformedURLException00"), e);
              return;
          }
  
          try {
              doThreads = (opts.isFlagSet('t') > 0);
              String host = opts.getHost();
              int port = ((opts.isFlagSet('p') > 0) ? opts.getPort() : 110);
              POP3Client pop3 = new POP3Client();
              MailServer sas = new MailServer(host, port, opts.getUser(), opts.getPassword());
  
              sas.setPOP3(pop3);
              sas.start();
          } catch (Exception e) {
              log.error(Messages.getMessage("exception00"), e);
              return;
          }
  
      }
  }
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/transport/mail/MailTransport.java
  
  Index: MailTransport.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *    Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Axis" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.axis.transport.mail;
  
  import org.apache.axis.AxisEngine;
  import org.apache.axis.MessageContext;
  import org.apache.axis.client.Call;
  import org.apache.axis.client.Transport;
  
  /**
   * A Transport which will cause an invocation via "mail" 
   * 
   * @author Davanum Srinivas <dims@yahoo.com>
   */
  public class MailTransport extends Transport {
      public MailTransport() {
          transportName = "mail";
      }
  
      /**
       * Set up any transport-specific derived properties in the message context.
       * @param mc the context to set up
       * @param call the Call object
       * @param engine the engine containing the registries
       */
      public void setupMessageContextImpl(MessageContext mc,
                                          Call call,
                                          AxisEngine engine) {
      }
  }
  
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/transport/mail/MailWorker.java
  
  Index: MailWorker.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Axis" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.axis.transport.mail;
  
  import org.apache.axis.AxisFault;
  import org.apache.axis.Constants;
  import org.apache.axis.Message;
  import org.apache.axis.MessageContext;
  import org.apache.axis.components.logger.LogFactory;
  import org.apache.axis.message.SOAPEnvelope;
  import org.apache.axis.message.SOAPFault;
  import org.apache.axis.server.AxisServer;
  import org.apache.axis.transport.http.HTTPConstants;
  import org.apache.axis.utils.Messages;
  import org.apache.commons.logging.Log;
  import org.apache.commons.net.smtp.SMTPClient;
  import org.apache.commons.net.smtp.SMTPReply;
  
  import javax.mail.Session;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  import javax.mail.internet.MimePart;
  import java.io.ByteArrayOutputStream;
  import java.io.Writer;
  import java.util.Properties;
  
  
  public class MailWorker implements Runnable {
      protected static Log log =
              LogFactory.getLog(MailWorker.class.getName());
  
      // Server 
      private MailServer server;
  
      // Current message
      private MimeMessage mimeMessage;
  
      // Axis specific constants
      private static String transportName = "Mail";
  
      private Properties prop = new Properties();
      private Session session = Session.getDefaultInstance(prop, null);
  
      /**
       * Constructor for MailWorker
       * @param server
       * @param mimeMessage
       */
      public MailWorker(MailServer server, MimeMessage mimeMessage) {
          this.server = server;
          this.mimeMessage = mimeMessage;
      }
  
      /**
       * The main workhorse method.
       */
      public void run() {
          // create an Axis server
          AxisServer engine = server.getAxisServer();
  
          // create and initialize a message context
          MessageContext msgContext = new MessageContext(engine);
          Message requestMsg;
  
          // buffers for the headers we care about
          StringBuffer soapAction = new StringBuffer();
          StringBuffer fileName = new StringBuffer();
          StringBuffer contentType = new StringBuffer();
          StringBuffer contentLocation = new StringBuffer();
  
          Message responseMsg = null;
  
          // prepare request (do as much as possible while waiting for the
          // next connection).  
          try {
              msgContext.setTargetService(null);
          } catch (AxisFault fault) {
          }
          msgContext.setResponseMessage(null);
          msgContext.reset();
          msgContext.setTransportName(transportName);
  
          responseMsg = null;
  
          try {
              try {
                  // parse all headers into hashtable
                  parseHeaders(mimeMessage, contentType,
                          contentLocation, soapAction);
  
                  // Real and relative paths are the same for the
                  // MailServer
                  msgContext.setProperty(Constants.MC_REALPATH,
                          fileName.toString());
                  msgContext.setProperty(Constants.MC_RELATIVE_PATH,
                          fileName.toString());
                  msgContext.setProperty(Constants.MC_JWS_CLASSDIR,
                          "jwsClasses");
  
                  // this may be "" if either SOAPAction: "" or if no SOAPAction at all.
                  // for now, do not complain if no SOAPAction at all
                  String soapActionString = soapAction.toString();
                  if (soapActionString != null) {
                      msgContext.setUseSOAPAction(true);
                      msgContext.setSOAPActionURI(soapActionString);
                  }
                  requestMsg = new Message(mimeMessage.getInputStream(), false,
                          contentType.toString(), contentLocation.toString());
                  msgContext.setRequestMessage(requestMsg);
  
                  // invoke the Axis engine
                  engine.invoke(msgContext);
  
                  // Retrieve the response from Axis
                  responseMsg = msgContext.getResponseMessage();
                  if (responseMsg == null) {
                      throw new AxisFault(Messages.getMessage("nullResponse00"));
                  }
              } catch (Exception e) {
                  e.printStackTrace();
                  AxisFault af;
                  if (e instanceof AxisFault) {
                      af = (AxisFault) e;
                      log.debug(Messages.getMessage("serverFault00"), af);
                  } else {
                      af = AxisFault.makeFault(e);
                  }
  
                  // There may be headers we want to preserve in the
                  // response message - so if it's there, just add the
                  // FaultElement to it.  Otherwise, make a new one.
                  responseMsg = msgContext.getResponseMessage();
                  if (responseMsg == null) {
                      responseMsg = new Message(af);
                  } else {
                      try {
                          SOAPEnvelope env = responseMsg.getSOAPEnvelope();
                          env.clearBody();
                          env.addBodyElement(new SOAPFault((AxisFault) e));
                      } catch (AxisFault fault) {
                          // Should never reach here!
                      }
                  }
              }
  
              String replyTo = ((InternetAddress) mimeMessage.getReplyTo()[0]).getAddress();
              String sendFrom = ((InternetAddress) mimeMessage.getAllRecipients()[0]).getAddress();
              String subject = "Re: " + mimeMessage.getSubject();
              writeUsingSMTP(msgContext, server.getHost(), sendFrom, replyTo, subject, responseMsg);
          } catch (Exception e) {
              e.printStackTrace();
              log.debug(Messages.getMessage("exception00"), e);
          }
          if (msgContext.getProperty(msgContext.QUIT_REQUESTED) != null) {
              // why then, quit!
              try {
                  server.stop();
              } catch (Exception e) {
              }
          }
  
      }
  
      /**
       * Send the soap request message to the server
       * 
       * @param msgContext
       * @param smtpHost
       * @param sendFrom
       * @param replyTo
       * @param output
       * @throws Exception
       */
      private void writeUsingSMTP(MessageContext msgContext,
                                  String smtpHost,
                                  String sendFrom,
                                  String replyTo,
                                  String subject,
                                  Message output)
              throws Exception {
          SMTPClient client = new SMTPClient();
          client.connect(smtpHost);
          
          // After connection attempt, you should check the reply code to verify
          // success.
          System.out.print(client.getReplyString());
          int reply = client.getReplyCode();
          if (!SMTPReply.isPositiveCompletion(reply)) {
              client.disconnect();
              AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null);
              throw fault;
          }
  
          client.login(smtpHost);
          System.out.print(client.getReplyString());
          reply = client.getReplyCode();
          if (!SMTPReply.isPositiveCompletion(reply)) {
              client.disconnect();
              AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null);
              throw fault;
          }
  
          MimeMessage msg = new MimeMessage(session);
          msg.setFrom(new InternetAddress(sendFrom));
          msg.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(replyTo));
          msg.setDisposition(MimePart.INLINE);
          msg.setSubject(subject);
  
          ByteArrayOutputStream out = new ByteArrayOutputStream(8 * 1024);
          output.writeTo(out);
          msg.setContent(out.toString(), output.getContentType(msgContext.getSOAPConstants()));
  
          ByteArrayOutputStream out2 = new ByteArrayOutputStream(8 * 1024);
          msg.writeTo(out2);
  
          client.setSender(sendFrom);
          System.out.print(client.getReplyString());
          client.addRecipient(replyTo);
          System.out.print(client.getReplyString());
  
          Writer writer = client.sendMessageData();
          System.out.print(client.getReplyString());
          writer.write(out2.toString());
          writer.flush();
          writer.close();
  
          System.out.print(client.getReplyString());
          if (!client.completePendingCommand()) {
              System.out.print(client.getReplyString());
              AxisFault fault = new AxisFault("SMTP", "( Failed to send email )", null, null);
              throw fault;
          }
          System.out.print(client.getReplyString());
          client.logout();
          client.disconnect();
      }
  
      /**
       * Read all mime headers, returning the value of Content-Length and
       * SOAPAction.
       * @param mimeMessage         InputStream to read from
       * @param contentType The content type.
       * @param contentLocation The content location
       * @param soapAction StringBuffer to return the soapAction into
       */
      private void parseHeaders(MimeMessage mimeMessage,
                                StringBuffer contentType,
                                StringBuffer contentLocation,
                                StringBuffer soapAction)
              throws Exception {
          contentType.append(mimeMessage.getContentType());
          contentLocation.append(mimeMessage.getContentID());
          String values[] = mimeMessage.getHeader(HTTPConstants.HEADER_SOAP_ACTION);
          if (values != null)
              soapAction.append(values[0]);
      }
  }
  
  
  
  1.35      +10 -0     xml-axis/java/src/org/apache/axis/utils/Options.java
  
  Index: Options.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/utils/Options.java,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- Options.java	18 Sep 2002 16:10:41 -0000	1.34
  +++ Options.java	13 Dec 2002 20:46:08 -0000	1.35
  @@ -308,6 +308,16 @@
           return( tmp );
       }
       
  +    public String getHost() {
  +        try {
  +            URL url = new URL(getURL());
  +            return( url.getHost() );
  +        }
  +        catch( Exception exp ) {
  +            return( "localhost" );
  +        }
  +    }
  +
       public int getPort() {
           try {
               URL url = new URL(getURL());
  
  
  
  1.34      +1 -0      xml-axis/java/xmls/properties.xml
  
  Index: properties.xml
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/xmls/properties.xml,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- properties.xml	3 Dec 2002 15:42:54 -0000	1.33
  +++ properties.xml	13 Dec 2002 20:46:08 -0000	1.34
  @@ -43,6 +43,7 @@
   <property name="commons-logging.jar" location="${lib.dir}/commons-logging.jar"/>
   <property name="commons-discovery.jar" location="${lib.dir}/commons-discovery.jar"/>
   <property name="commons-httpclient.jar" location="${lib.dir}/commons-httpclient.jar"/>
  +<property name="commons-net.jar" location="${lib.dir}/commons-net-1.0.0-dev.jar"/>
   <property name="log4j-core.jar" location="${lib.dir}/log4j-1.2.4.jar"/>
   <property name="junit.jar" location="${lib.dir}/junit.jar"/>
   <property name="jimi.jar" location="${lib.dir}/JimiProClasses.zip"/>
  
  
  
  1.51      +4 -0      xml-axis/java/xmls/targets.xml
  
  Index: targets.xml
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/xmls/targets.xml,v
  retrieving revision 1.50
  retrieving revision 1.51
  diff -u -r1.50 -r1.51
  --- targets.xml	6 Dec 2002 18:21:41 -0000	1.50
  +++ targets.xml	13 Dec 2002 20:46:08 -0000	1.51
  @@ -95,6 +95,10 @@
         classname="org.apache.commons.httpclient.HttpConnection"
         classpathref="classpath"/>
   
  +    <available property="commons-net.present"
  +      classname="org.apache.commons.net.smtp.SMTPClient"
  +      classpathref="classpath"/>
  +
       <available property="log4j.present"
         classname="org.apache.log4j.Category"
         classpathref="classpath"/>
  
  
  

Mime
View raw message