You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by hb...@apache.org on 2001/04/02 09:51:34 UTC

cvs commit: jakarta-james/src/org/apache/james/pop3server POP3Handler.java

hbedi       01/04/02 00:51:33

  Modified:    src/org/apache/james/pop3server POP3Handler.java
  Log:
  The code for handling different protocol messages looks like this
  -------------------------------
  if method == 'foo':
     doFoo(..)
  else if method == 'bar':
    doBar(..)
  else if method == 'shoo':
    doShoo(..)
  else if method == 'boo':
    doBoo(..)
  ------------------------------
  
  Revision  Changes    Path
  1.38      +238 -207  jakarta-james/src/org/apache/james/pop3server/POP3Handler.java
  
  Index: POP3Handler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/org/apache/james/pop3server/POP3Handler.java,v
  retrieving revision 1.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- POP3Handler.java	2001/03/17 18:48:55	1.37
  +++ POP3Handler.java	2001/04/02 07:51:31	1.38
  @@ -95,7 +95,7 @@
               lookup( "org.apache.james.services.MailServer" );
           UsersStore usersStore = (UsersStore)componentManager.
               lookup( "org.apache.james.services.UsersStore" );
  -	users = usersStore.getRepository("LocalUsers");
  +        users = usersStore.getRepository("LocalUsers");
           scheduler = (TimeScheduler)componentManager.
               lookup( "org.apache.cornerstone.services.scheduler.TimeScheduler" );
       }
  @@ -160,6 +160,17 @@
           }
       }
   
  +    private void stat() {
  +        userMailbox = new Vector();
  +        userMailbox.addElement(DELETED);
  +        for (Iterator it = userInbox.list(); it.hasNext(); ) {
  +            String key = (String) it.next();
  +            MailImpl mc = userInbox.retrieve(key);
  +            userMailbox.addElement(mc);
  +        }
  +        backupUserMailbox = (Vector) userMailbox.clone();
  +    }
  +
       private boolean parseCommand(String commandRaw) {
           if (commandRaw == null) return false;
           getLogger().info("Command received: " + commandRaw);
  @@ -179,33 +190,84 @@
           if(arguments > 2) {
               argument1 = commandLine.nextToken();
           }
  -        if (command.equalsIgnoreCase("USER")) {
  -            if (state == AUTHENTICATION_READY && argument != null) {
  -                user = argument;
  -                state = AUTHENTICATION_USERSET;
  -                out.println(OK_RESPONSE);
  +
  +        if (command.equalsIgnoreCase("USER"))
  +            doUSER(command,argument,argument1);
  +        else if (command.equalsIgnoreCase("PASS"))
  +            doPASS(command,argument,argument1);
  +        else if (command.equalsIgnoreCase("STAT"))
  +            doSTAT(command,argument,argument1);
  +        else if (command.equalsIgnoreCase("LIST"))
  +            doLIST(command,argument,argument1);
  +        else if (command.equalsIgnoreCase("UIDL"))
  +            doUIDL(command,argument,argument1);
  +        else if (command.equalsIgnoreCase("RSET"))
  +            doRSET(command,argument,argument1);
  +        else if (command.equalsIgnoreCase("DELE"))
  +            doDELE(command,argument,argument1);
  +        else if (command.equalsIgnoreCase("NOOP"))
  +            doNOOP(command,argument,argument1);
  +        else if (command.equalsIgnoreCase("RETR"))
  +            doRETR(command,argument,argument1);
  +        else if (command.equalsIgnoreCase("TOP"))
  +            doTOP(command,argument,argument1);
  +        else if (command.equalsIgnoreCase("QUIT"))
  +            doQUIT(command,argument,argument1);
  +        else
  +            doUnknownCmd(command,argument,argument1);
  +        return (command.equalsIgnoreCase("QUIT") == false);
  +    }
  +
  +    private void doUSER(String command,String argument,String argument1) {
  +        if (state == AUTHENTICATION_READY && argument != null) {
  +            user = argument;
  +            state = AUTHENTICATION_USERSET;
  +            out.println(OK_RESPONSE);
  +        } else {
  +            out.println(ERR_RESPONSE);
  +        }
  +    }
  +
  +    private void doPASS(String command,String argument,String argument1) {
  +        if (state == AUTHENTICATION_USERSET && argument != null) {
  +            String passArg = argument;
  +            if (users.test(user, passArg)) {
  +                state = TRANSACTION;
  +                out.println(OK_RESPONSE + " Welcome " + user);
  +                userInbox = mailServer.getUserInbox(user);
  +                stat();
               } else {
  -                out.println(ERR_RESPONSE);
  +                state = AUTHENTICATION_READY;
  +                out.println(ERR_RESPONSE + " Authentication failed.");
               }
  -            return true;
  -        } else if (command.equalsIgnoreCase("PASS")) {
  -            if (state == AUTHENTICATION_USERSET && argument != null) {
  -                String passArg = commandRaw.substring(5);
  -                if (users.test(user, passArg)) {
  -                    state = TRANSACTION;
  -                    out.println(OK_RESPONSE + " Welcome " + user);
  -                    userInbox = mailServer.getUserInbox(user);
  -                    stat();
  -                } else {
  -                    state = AUTHENTICATION_READY;
  -                    out.println(ERR_RESPONSE + " Authentication failed.");
  +        } else {
  +            out.println(ERR_RESPONSE);
  +        }
  +    }
  +
  +    private void doSTAT(String command,String argument,String argument1) {
  +        if (state == TRANSACTION) {
  +            long size = 0;
  +            int count = 0;
  +            try {
  +                for (Enumeration e = userMailbox.elements(); e.hasMoreElements(); ) {
  +                    MailImpl mc = (MailImpl) e.nextElement();
  +                    if (mc != DELETED) {
  +                        size += mc.getSize();
  +                        count++;
  +                    }
                   }
  -            } else {
  +                out.println(OK_RESPONSE + " " + count + " " + size);
  +            } catch (MessagingException me) {
                   out.println(ERR_RESPONSE);
               }
  -            return true;
  -        } else if (command.equalsIgnoreCase("STAT")) {
  -            if (state == TRANSACTION) {
  +        } else {
  +            out.println(ERR_RESPONSE);
  +        }
  +    }
  +    private void doLIST(String command,String argument,String argument1) {
  +        if (state == TRANSACTION) {
  +            if (argument == null) {
                   long size = 0;
                   int count = 0;
                   try {
  @@ -217,229 +279,198 @@
                           }
                       }
                       out.println(OK_RESPONSE + " " + count + " " + size);
  -                } catch (MessagingException me) {
  -                    out.println(ERR_RESPONSE);
  -                }
  -            } else {
  -                out.println(ERR_RESPONSE);
  -            }
  -            return true;
  -        } else if (command.equalsIgnoreCase("LIST")) {
  -            if (state == TRANSACTION) {
  -                if (argument == null) {
  -                    long size = 0;
  -                    int count = 0;
  -                    try {
  -                        for (Enumeration e = userMailbox.elements(); e.hasMoreElements(); ) {
  -                            MailImpl mc = (MailImpl) e.nextElement();
  -                            if (mc != DELETED) {
  -                                size += mc.getSize();
  -                                count++;
  -                            }
  -                        }
  -                        out.println(OK_RESPONSE + " " + count + " " + size);
  -                        count = 0;
  -                        for (Enumeration e = userMailbox.elements(); e.hasMoreElements(); count++) {
  -                            MailImpl mc = (MailImpl) e.nextElement();
  -                            if (mc != DELETED) {
  -                                out.println(count + " " + mc.getSize());
  -                            }
  -                        }
  -                        out.println(".");
  -                    } catch (MessagingException me) {
  -                        out.println(ERR_RESPONSE);
  -                    }
  -                } else {
  -                    int num = 0;
  -                    try {
  -                        num = Integer.parseInt(argument);
  -                        MailImpl mc = (MailImpl) userMailbox.elementAt(num);
  -                        if (mc != DELETED) {
  -                            out.println(OK_RESPONSE + " " + num + " " + mc.getSize());
  -                        } else {
  -                            out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
  -                        }
  -                    } catch (ArrayIndexOutOfBoundsException npe) {
  -                        out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
  -                    } catch (NumberFormatException nfe) {
  -                        out.println(ERR_RESPONSE + " " + argument + " is not a valid number");
  -                    } catch (MessagingException me) {
  -                        out.println(ERR_RESPONSE);
  -                    }
  -                }
  -            } else {
  -                out.println(ERR_RESPONSE);
  -            }
  -            return true;
  -        } else if (command.equalsIgnoreCase("UIDL")) {
  -            if (state == TRANSACTION) {
  -                if (argument == null) {
  -                    out.println(OK_RESPONSE + " unique-id listing follows");
  -                    int count = 0;
  +                    count = 0;
                       for (Enumeration e = userMailbox.elements(); e.hasMoreElements(); count++) {
                           MailImpl mc = (MailImpl) e.nextElement();
                           if (mc != DELETED) {
  -                            out.println(count + " " + mc.getName());
  +                            out.println(count + " " + mc.getSize());
                           }
                       }
                       out.println(".");
  -                } else {
  -                    int num = 0;
  -                    try {
  -                        num = Integer.parseInt(argument);
  -                        MailImpl mc = (MailImpl) userMailbox.elementAt(num);
  -                        if (mc != DELETED) {
  -                            out.println(OK_RESPONSE + " " + num + " " + mc.getName());
  -                        } else {
  -                            out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
  -                        }
  -                    } catch (ArrayIndexOutOfBoundsException npe) {
  -                        out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
  -                    } catch (NumberFormatException nfe) {
  -                        out.println(ERR_RESPONSE + " " + argument + " is not a valid number");
  -                    }
  +                } catch (MessagingException me) {
  +                    out.println(ERR_RESPONSE);
                   }
               } else {
  -                out.println(ERR_RESPONSE);
  -            }
  -            return true;
  -        } else if (command.equalsIgnoreCase("RSET")) {
  -            if (state == TRANSACTION) {
  -                stat();
  -                out.println(OK_RESPONSE);
  -            } else {
  -                out.println(ERR_RESPONSE);
  -            }
  -            return true;
  -        } else if (command.equalsIgnoreCase("DELE")) {
  -            if (state == TRANSACTION) {
                   int num = 0;
                   try {
                       num = Integer.parseInt(argument);
  -                } catch (Exception e) {
  -                    out.println(ERR_RESPONSE + " Usage: DELE [mail number]");
  -                    return true;
  -                }
  -                try {
                       MailImpl mc = (MailImpl) userMailbox.elementAt(num);
  -                    if (mc == DELETED) {
  -                         out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
  +                    if (mc != DELETED) {
  +                        out.println(OK_RESPONSE + " " + num + " " + mc.getSize());
                       } else {
  -                         userMailbox.setElementAt(DELETED, num);
  -                         out.println(OK_RESPONSE + " Message removed");
  +                        out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
                       }
  -                } catch (ArrayIndexOutOfBoundsException iob) {
  +                } catch (ArrayIndexOutOfBoundsException npe) {
                       out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
  +                } catch (NumberFormatException nfe) {
  +                    out.println(ERR_RESPONSE + " " + argument + " is not a valid number");
  +                } catch (MessagingException me) {
  +                    out.println(ERR_RESPONSE);
                   }
  -            } else {
  -                out.println(ERR_RESPONSE);
  -            }
  -            return true;
  -        } else if (command.equalsIgnoreCase("NOOP")) {
  -            if (state == TRANSACTION) {
  -                out.println(OK_RESPONSE);
  -            } else {
  -                out.println(ERR_RESPONSE);
               }
  -            return true;
  -        } else if (command.equalsIgnoreCase("RETR")) {
  -            if (state == TRANSACTION) {
  -                int num = 0;
  -                try {
  -                    num = Integer.parseInt(argument.trim());
  -                } catch (Exception e) {
  -                    out.println(ERR_RESPONSE + " Usage: RETR [mail number]");
  -                    return true;
  -                }
  -                //?May be written as 
  -                //return parseCommand("TOP " + num + " " + Integer.MAX_VALUE);?
  -                try {
  -                    MailImpl mc = (MailImpl) userMailbox.elementAt(num);
  +        } else {
  +            out.println(ERR_RESPONSE);
  +        }
  +    }
  +
  +    private void doUIDL(String command,String argument,String argument1) {
  +        if (state == TRANSACTION) {
  +            if (argument == null) {
  +                out.println(OK_RESPONSE + " unique-id listing follows");
  +                int count = 0;
  +                for (Enumeration e = userMailbox.elements(); e.hasMoreElements(); count++) {
  +                    MailImpl mc = (MailImpl) e.nextElement();
                       if (mc != DELETED) {
  -                        out.println(OK_RESPONSE + " Message follows");
  -                        mc.writeMessageTo(outs);
  -                        out.println();
  -                        out.println(".");
  -                    } else {
  -                        out.println(ERR_RESPONSE + " Message (" + num + ") deleted.");
  +                        out.println(count + " " + mc.getName());
                       }
  -                } catch (IOException ioe) {
  -                    out.println(ERR_RESPONSE + " Error while retrieving message.");
  -                } catch (MessagingException me) {
  -                    out.println(ERR_RESPONSE + " Error while retrieving message.");
  -                } catch (ArrayIndexOutOfBoundsException iob) {
  -                    out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
                   }
  -                // -------------------------------------------?
  +                out.println(".");
               } else {
  -                out.println(ERR_RESPONSE);
  -            }
  -            return true;
  -        } else if (command.equalsIgnoreCase("TOP")) {
  -            if (state == TRANSACTION) {
                   int num = 0;
  -                int lines = 0;
                   try {
                       num = Integer.parseInt(argument);
  -                    lines = Integer.parseInt(argument1);
  -                } catch (NumberFormatException nfe) {
  -                    out.println(ERR_RESPONSE + " Usage: TOP [mail number] [Line number]");
  -                    return true;
  -                }
  -                try {
                       MailImpl mc = (MailImpl) userMailbox.elementAt(num);
                       if (mc != DELETED) {
  -                        out.println(OK_RESPONSE + " Message follows");
  -                        for (Enumeration e = mc.getMessage().getAllHeaderLines(); e.hasMoreElements(); ) {
  -                            out.println(e.nextElement());
  -                        }
  -                        out.println("");
  -                        mc.writeContentTo(outs, lines);
  -                        out.println(".");
  +                        out.println(OK_RESPONSE + " " + num + " " + mc.getName());
                       } else {
  -                        out.println(ERR_RESPONSE + " Message (" + num + ") already deleted.");
  +                        out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
                       }
  -                } catch (IOException ioe) {
  -                    out.println(ERR_RESPONSE + " Error while retrieving message.");
  -                } catch (MessagingException me) {
  -                    out.println(ERR_RESPONSE + " Error while retrieving message.");
  -                } catch (ArrayIndexOutOfBoundsException iob) {
  +                } catch (ArrayIndexOutOfBoundsException npe) {
                       out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
  +                } catch (NumberFormatException nfe) {
  +                    out.println(ERR_RESPONSE + " " + argument + " is not a valid number");
                   }
  -            } else {
  -                out.println(ERR_RESPONSE);
               }
  -            return true;
  -        } else if (command.equalsIgnoreCase("QUIT")) {
  -            if (state == AUTHENTICATION_READY ||  state == AUTHENTICATION_USERSET) {
  -                return false;
  +        } else {
  +            out.println(ERR_RESPONSE);
  +        }
  +    }
  +    private void doRSET(String command,String argument,String argument1) {
  +        if (state == TRANSACTION) {
  +            stat();
  +            out.println(OK_RESPONSE);
  +        } else {
  +            out.println(ERR_RESPONSE);
  +        }
  +    }
  +
  +    private void doDELE(String command,String argument,String argument1) {
  +        if (state == TRANSACTION) {
  +            int num = 0;
  +            try {
  +                num = Integer.parseInt(argument);
  +            } catch (Exception e) {
  +                out.println(ERR_RESPONSE + " Usage: DELE [mail number]");
  +                return;
               }
  -            List toBeRemoved =  ListUtils.subtract(backupUserMailbox, userMailbox);
               try {
  -                for (Iterator it = toBeRemoved.iterator(); it.hasNext(); ) {
  -                    MailImpl mc = (MailImpl) it.next();
  -                    userInbox.remove(mc.getName());
  +                MailImpl mc = (MailImpl) userMailbox.elementAt(num);
  +                if (mc == DELETED) {
  +                    out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
  +                } else {
  +                    userMailbox.setElementAt(DELETED, num);
  +                    out.println(OK_RESPONSE + " Message removed");
                   }
  -                out.println(OK_RESPONSE + " Apache James POP3 Server signing off.");
  -            } catch (Exception ex) {
  -                out.println(ERR_RESPONSE + " Some deleted messages were not removed");
  -                getLogger().error("Some deleted messages were not removed: " + ex.getMessage());
  +            } catch (ArrayIndexOutOfBoundsException iob) {
  +                out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
               }
  -            return false;
           } else {
               out.println(ERR_RESPONSE);
  -            return true;
           }
       }
  -
  -    private void stat() {
  -        userMailbox = new Vector();
  -        userMailbox.addElement(DELETED);
  -        for (Iterator it = userInbox.list(); it.hasNext(); ) {
  -            String key = (String) it.next();
  -            MailImpl mc = userInbox.retrieve(key);
  -            userMailbox.addElement(mc);
  +    private void doNOOP(String command,String argument,String argument1) {
  +        if (state == TRANSACTION) {
  +            out.println(OK_RESPONSE);
  +        } else {
  +            out.println(ERR_RESPONSE);
           }
  -        backupUserMailbox = (Vector) userMailbox.clone();
       }
  +    private void doRETR(String command,String argument,String argument1) {
  +        if (state == TRANSACTION) {
  +            int num = 0;
  +            try {
  +                num = Integer.parseInt(argument.trim());
  +            } catch (Exception e) {
  +                out.println(ERR_RESPONSE + " Usage: RETR [mail number]");
  +                return;
  +            }
  +            //?May be written as 
  +            //return parseCommand("TOP " + num + " " + Integer.MAX_VALUE);?
  +            try {
  +                MailImpl mc = (MailImpl) userMailbox.elementAt(num);
  +                if (mc != DELETED) {
  +                    out.println(OK_RESPONSE + " Message follows");
  +                    mc.writeMessageTo(outs);
  +                    out.println();
  +                    out.println(".");
  +                } else {
  +                    out.println(ERR_RESPONSE + " Message (" + num + ") deleted.");
  +                }
  +            } catch (IOException ioe) {
  +                out.println(ERR_RESPONSE + " Error while retrieving message.");
  +            } catch (MessagingException me) {
  +                out.println(ERR_RESPONSE + " Error while retrieving message.");
  +            } catch (ArrayIndexOutOfBoundsException iob) {
  +                out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
  +            }
  +            // -------------------------------------------?
  +        } else {
  +            out.println(ERR_RESPONSE);
  +        }
  +    }
  +    private void doTOP(String command,String argument,String argument1) {
  +        if (state == TRANSACTION) {
  +            int num = 0;
  +            int lines = 0;
  +            try {
  +                num = Integer.parseInt(argument);
  +                lines = Integer.parseInt(argument1);
  +            } catch (NumberFormatException nfe) {
  +                out.println(ERR_RESPONSE + " Usage: TOP [mail number] [Line number]");
  +                return;
  +            }
  +            try {
  +                MailImpl mc = (MailImpl) userMailbox.elementAt(num);
  +                if (mc != DELETED) {
  +                    out.println(OK_RESPONSE + " Message follows");
  +                    for (Enumeration e = mc.getMessage().getAllHeaderLines(); e.hasMoreElements(); ) {
  +                        out.println(e.nextElement());
  +                    }
  +                    out.println("");
  +                    mc.writeContentTo(outs, lines);
  +                    out.println(".");
  +                } else {
  +                    out.println(ERR_RESPONSE + " Message (" + num + ") already deleted.");
  +                }
  +            } catch (IOException ioe) {
  +                out.println(ERR_RESPONSE + " Error while retrieving message.");
  +            } catch (MessagingException me) {
  +                out.println(ERR_RESPONSE + " Error while retrieving message.");
  +            } catch (ArrayIndexOutOfBoundsException iob) {
  +                out.println(ERR_RESPONSE + " Message (" + num + ") does not exist.");
  +            }
  +        } else {
  +            out.println(ERR_RESPONSE);
  +        }
  +    }
  +    private void doQUIT(String command,String argument,String argument1) {
  +        if (state == AUTHENTICATION_READY ||  state == AUTHENTICATION_USERSET) {
  +            return;
  +        }
  +        List toBeRemoved =  ListUtils.subtract(backupUserMailbox, userMailbox);
  +        try {
  +            for (Iterator it = toBeRemoved.iterator(); it.hasNext(); ) {
  +                MailImpl mc = (MailImpl) it.next();
  +                userInbox.remove(mc.getName());
  +            }
  +            out.println(OK_RESPONSE + " Apache James POP3 Server signing off.");
  +        } catch (Exception ex) {
  +            out.println(ERR_RESPONSE + " Some deleted messages were not removed");
  +            getLogger().error("Some deleted messages were not removed: " + ex.getMessage());
  +        }
  +    }
  +    private void doUnknownCmd(String command,String argument,String argument1) {
  +        out.println(ERR_RESPONSE);
  +    }
   }
  +
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: james-dev-help@jakarta.apache.org