You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by de...@apache.org on 2005/07/15 13:03:24 UTC

svn commit: r219177 - in /webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server: MailServer.java MailSorter.java SMTPServer.java SMTPWorker.java

Author: deepal
Date: Fri Jul 15 04:03:20 2005
New Revision: 219177

URL: http://svn.apache.org/viewcvs?rev=219177&view=rev
Log:
applying Chamils patch

Modified:
    webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/MailServer.java
    webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/MailSorter.java
    webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/SMTPServer.java
    webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/SMTPWorker.java

Modified: webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/MailServer.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/MailServer.java?rev=219177&r1=219176&r2=219177&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/MailServer.java (original)
+++ webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/MailServer.java Fri Jul 15 04:03:20 2005
@@ -12,9 +12,11 @@
  */
 public class MailServer {
     Storage st = null;
+
     public ConfigurationContext configurationContext = null;
-    protected static Log log = LogFactory.getLog(
-            SimpleMailListener.class.getName());
+
+    protected static Log log = LogFactory.getLog(SimpleMailListener.class
+            .getName());
 
     public MailServer(String dir, int popPort, int smtpPort) throws AxisFault {
         try {
@@ -24,16 +26,15 @@
             log.error(e);
         }
         try {
-            System.out.println(
-                    "Sleeping for a bit to let the engine start up.");
+            System.out
+                    .println("Sleeping for a bit to let the engine start up.");
             Thread.sleep(2000);
         } catch (InterruptedException e1) {
             log.error(e1);
         }
         st = new Storage();
         // Start up the two servers and lets have some fun. - CT
-        SMTPServer smtpServer = new SMTPServer(st,
-                configurationContext,
+        SMTPServer smtpServer = new SMTPServer(st, configurationContext,
                 smtpPort);
         smtpServer.start();
         POP3Server pop3Server = new POP3Server(st, popPort);
@@ -41,13 +42,12 @@
 
     }
 
-    public MailServer(ConfigurationContext configurationContext,
-                      int popPort,
-                      int smtpPort) throws AxisFault {
+    public MailServer(ConfigurationContext configurationContext, int popPort,
+            int smtpPort) throws AxisFault {
         this.configurationContext = configurationContext;
         try {
-            System.out.println(
-                    "Sleeping for a bit to let the engine start up.");
+            System.out
+                    .println("Sleeping for a bit to let the engine start up.");
             Thread.sleep(2000);
         } catch (InterruptedException e1) {
             log.error(e1);
@@ -55,11 +55,41 @@
 
         st = new Storage();
         // Start up the two servers and lets have some fun. - CT
-        SMTPServer smtpServer = new SMTPServer(st,
-                configurationContext,
+        SMTPServer smtpServer = new SMTPServer(st, configurationContext,
                 smtpPort);
         smtpServer.start();
         POP3Server pop3Server = new POP3Server(st, popPort);
         pop3Server.start();
+    }
+
+    public MailServer(int popPort, int smtpPort) throws AxisFault {
+        st = new Storage();
+        // Start up the two servers and lets have some fun. - CT
+        SMTPServer smtpServer = new SMTPServer(st, smtpPort);
+        smtpServer.start();
+        POP3Server pop3Server = new POP3Server(st, popPort);
+        pop3Server.start();
+    }
+
+    public static void main(String args[]){
+        int smtpPost = MailConstants.SMTP_SERVER_PORT;
+        int popPort = MailConstants.POP_SERVER_PORT;
+        if (args.length == 2) {
+            try {
+                smtpPost = Integer.parseInt(args[0]);
+                popPort = Integer.parseInt(args[1]);
+            } catch (NumberFormatException e1) {
+                System.out.println("Error in parsing the custom ports.");
+            }
+        } else {
+            System.out.println("Usage MailServer <SMTP_PORT> <POP_PORT>");
+            System.out.println("Using 1134 as the SMTP port and 1049 as the POP port");
+        }
+
+        try {
+            MailServer ms = new MailServer(popPort, smtpPost);
+        } catch (AxisFault e) {
+            e.printStackTrace();
+        }
     }
 }

Modified: webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/MailSorter.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/MailSorter.java?rev=219177&r1=219176&r2=219177&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/MailSorter.java (original)
+++ webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/MailSorter.java Fri Jul 15 04:03:20 2005
@@ -36,20 +36,30 @@
     private ArrayList sUsers = new ArrayList(); // Special users. They are hard coded for the time being to axis2-server@localhost and axis2-server@127.0.0.1
     private ConfigurationContext configurationContext = null;
     protected static Log log = LogFactory.getLog(MailSorter.class.getName());
-
+    private boolean actAsMailet = false;
     public MailSorter(Storage st, ConfigurationContext configurationContext) {
         this.st = st;
         sUsers.add("axis2-server@localhost");
         sUsers.add("axis2-server@127.0.0.1");
-        this.configurationContext = configurationContext;
+        if (configurationContext == null){
+            actAsMailet = false;
+        } else {
+            this.configurationContext = configurationContext;
+            actAsMailet = true;
+        }
     }
 
     public void sort(String user, MimeMessage msg) {
-        if (sUsers.contains(user)) {
-            processMail(configurationContext, msg);
+        if (actAsMailet) {
+            if (sUsers.contains(user)) {
+                processMail(configurationContext, msg);
+            } else {
+                st.addMail(user, msg);
+            }            
         } else {
             st.addMail(user, msg);
         }
+
     }
 
     public void processMail(ConfigurationContext confContext,
@@ -131,4 +141,4 @@
         }
 
     }
-}
\ No newline at end of file
+}

Modified: webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/SMTPServer.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/SMTPServer.java?rev=219177&r1=219176&r2=219177&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/SMTPServer.java (original)
+++ webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/SMTPServer.java Fri Jul 15 04:03:20 2005
@@ -6,7 +6,6 @@
 import java.net.ServerSocket;
 import java.net.Socket;
 
-
 /**
  * @author Chamil Thanthrimudalige
  * @author Chamikara Jayalath
@@ -14,15 +13,25 @@
 
 public class SMTPServer extends Thread {
     private Storage st;
+
     private ConfigurationContext configurationContext;
+
     private int port;
 
-    public SMTPServer(Storage st,
-                      ConfigurationContext configurationContext,
-                      int port) {
+    private boolean actAsMailet = false;
+
+    public SMTPServer(Storage st, ConfigurationContext configurationContext,
+            int port) {
         this.st = st;
         this.configurationContext = configurationContext;
         this.port = port;
+        actAsMailet = true;
+    }
+
+    public SMTPServer(Storage st, int port) {
+        this.st = st;
+        this.port = port;
+        actAsMailet = false;
     }
 
     public void run() {
@@ -42,10 +51,12 @@
             try {
                 //wait for a client
                 Socket socket = ss.accept();
-
-                SMTPWorker thread = new SMTPWorker(socket,
-                        st,
-                        configurationContext);
+                SMTPWorker thread = null;
+                if (actAsMailet)
+                    thread = new SMTPWorker(socket, st, configurationContext);
+                else {
+                    thread = new SMTPWorker(socket, st);
+                }
                 thread.start();
 
             } catch (IOException ex) {
@@ -54,4 +65,4 @@
         }
     }
 
-}
\ No newline at end of file
+}

Modified: webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/SMTPWorker.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/SMTPWorker.java?rev=219177&r1=219176&r2=219177&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/SMTPWorker.java (original)
+++ webservices/axis/trunk/java/modules/core/src/org/apache/axis2/transport/mail/server/SMTPWorker.java Fri Jul 15 04:03:20 2005
@@ -17,7 +17,6 @@
 import java.util.ArrayList;
 import java.util.Properties;
 
-
 /**
  * @author Chamil Thanthrimudalige
  * @author Chamikara Jayalath
@@ -25,40 +24,53 @@
 
 public class SMTPWorker extends Thread {
 
-    BufferedReader reader = null;
+    private BufferedReader reader = null;
+
+    private BufferedWriter writer = null;
+
+    private boolean actAsMailet = false;
 
-    BufferedWriter writer = null;
+    private ArrayList receivers = new ArrayList();
 
-    ArrayList receivers = new ArrayList();
+    private Storage st = null;
 
-    Storage st = null;
     boolean runThread = true;
 
     private MimeMessage mail = null;
+
     private ConfigurationContext configurationContext = null;
 
-    String temp = "";
-    boolean dataWriting = false;
-    boolean transmitionEnd = false;
-
-    boolean bodyData = false;
-
-    public SMTPWorker(Socket socket,
-                      Storage st,
-                      ConfigurationContext configurationContext) {
+    private String temp = "";
+
+    private boolean dataWriting = false;
+
+    private boolean transmitionEnd = false;
+
+    private boolean bodyData = false;
+
+    public SMTPWorker(Socket socket, Storage st,
+            ConfigurationContext configurationContext) {
+        doWork(socket, st, configurationContext);
+    }
+
+    public SMTPWorker(Socket socket, Storage st) {
+        doWork(socket, st, null);
+    }
+
+    private void doWork(Socket socket, Storage st,
+            ConfigurationContext configurationContext) {
         try {
             this.st = st;
-            this.configurationContext = configurationContext;
+            if (configurationContext == null) {
+                actAsMailet = false;
+            } else {
+                this.configurationContext = configurationContext;
+                actAsMailet = true;
+            }
             //get the streams from the socket and save in instance variables.
-            reader =
-                    new BufferedReader(
-                            new InputStreamReader(
-                                    socket
+            reader = new BufferedReader(new InputStreamReader(socket
                     .getInputStream()));
-            writer =
-                    new BufferedWriter(
-                            new OutputStreamWriter(
-                                    socket
+            writer = new BufferedWriter(new OutputStreamWriter(socket
                     .getOutputStream()));
         } catch (IOException ex) {
             ex.printStackTrace();
@@ -70,7 +82,7 @@
         try {
             //do initial transmission.
             initializeClient();
-            
+
             //analyze all the inputs from client and work accordingly.
             while (runThread) {
                 String input = null;
@@ -93,10 +105,15 @@
             }
             for (int idx = 0; idx < receivers.size(); idx++) {
                 try {
-                    MailSorter mSort = new MailSorter(this.st,
-                            this.configurationContext);
-                    mSort.sort((String) receivers.get(idx),
-                            new MimeMessage(mail));
+                    MailSorter mSort = null;
+                    if (actAsMailet) {
+                        mSort = new MailSorter(this.st,
+                                this.configurationContext);
+                    } else {
+                        mSort = new MailSorter(this.st, null);
+                    }
+                    mSort.sort((String) receivers.get(idx), new MimeMessage(
+                            mail));
                 } catch (MessagingException e1) {
                     e1.printStackTrace();
                 }
@@ -115,16 +132,19 @@
     }
 
     private String processInput(String input) {
-        byte[] CR_LF = new byte[]{0x0D, 0x0A};
-        if (input == null) return MailConstants.COMMAND_UNKNOWN;
-        if (mail != null && transmitionEnd) return MailConstants.COMMAND_TRANSMISSION_END;
+        byte[] CR_LF = new byte[] { 0x0D, 0x0A };
+        if (input == null)
+            return MailConstants.COMMAND_UNKNOWN;
+        if (mail != null && transmitionEnd)
+            return MailConstants.COMMAND_TRANSMISSION_END;
 
         if (input.startsWith("MAIL")) {
-            mail = new MimeMessage(Session.getInstance(new Properties(), new Authenticator() {
-                protected PasswordAuthentication getPasswordAuthentication() {
-                    return null;
-                }
-            }));
+            mail = new MimeMessage(Session.getInstance(new Properties(),
+                    new Authenticator() {
+                        protected PasswordAuthentication getPasswordAuthentication() {
+                            return null;
+                        }
+                    }));
 
             int start = input.indexOf("<") + 1;
             int end;
@@ -139,7 +159,8 @@
             String from = input.substring(start, end);
 
             if (from != null && !from.trim().equals("")) {
-                //TODO this is an ugly hack to get the from address in. There should be a better way to do this.
+                //TODO this is an ugly hack to get the from address in. There
+                // should be a better way to do this.
                 MailAddress mailFrom[] = new MailAddress[1];
                 mailFrom[0] = new MailAddress(from);
                 try {
@@ -160,29 +181,29 @@
 
             String domain = MailConstants.SERVER_DOMAIN;
             //System.out.println("RCPT:" + input);
-            //temp += input + "\n";  TODO Check this
+            //temp += input + "\n"; TODO Check this
             int start = input.indexOf("<") + 1;
             int end;
 
             if (start <= 0) {
                 start = input.indexOf("TO:") + 3;
-/*                if(!input.endsWith(domain)){
-                    System.out.println("ERROR: wrong donmain name");
-                    return MailConstants.RCPT_ERROR;
-                }*/
+                /*
+                 * if(!input.endsWith(domain)){ System.out.println("ERROR: wrong
+                 * donmain name"); return MailConstants.RCPT_ERROR; }
+                 */
             } else {
-/*                if(!input.endsWith(domain + ">")){
-                    System.out.println("ERROR: wrong donmain name");
-                    return MailConstants.RCPT_ERROR;
-                }*/
+                /*
+                 * if(!input.endsWith(domain + ">")){ System.out.println("ERROR:
+                 * wrong donmain name"); return MailConstants.RCPT_ERROR; }
+                 */
             }
 
             end = input.indexOf(">");
             String toStr = input.substring(start, end);
 
             try {
-                mail.addRecipient(Message.RecipientType.TO,
-                        new MailAddress(toStr));
+                mail.addRecipient(Message.RecipientType.TO, new MailAddress(
+                        toStr));
                 receivers.add(toStr);
             } catch (MessagingException e) {
                 // TODO Auto-generated catch block
@@ -210,9 +231,8 @@
                 if (bodyData) {
                     temp += input;
                     mail.setContent(temp, "text/plain");
-                    System.out.println(
-                            "\n\n\n---------------" + temp +
-                            "---------------\n\n\n");
+                    System.out.println("\n\n\n---------------" + temp
+                            + "---------------\n\n\n");
                 } else {
                     mail.addHeaderLine(input);
                 }
@@ -241,4 +261,4 @@
             send("220 SMTP Server IS UP");
         }
     }
-}
\ No newline at end of file
+}