Package org.apache.james.protocols.pop3

Examples of org.apache.james.protocols.pop3.POP3Protocol


     *
     * @param argument the first argument parsed by the parseCommand method
     */

    public Response onCommand(POP3Session session, Request request) {
        POP3Response response = null;
        String parameters = request.getArgument();
       
        if (session.getHandlerState() == POP3Session.TRANSACTION) {
            Mail dm = (Mail) session.getState().get(POP3Session.DELETED);

            if (parameters == null) {

                long size = 0;
                int count = 0;

                try {

                    for (Mail mc:session.getUserMailbox()) {
                        if (mc != dm) {
                            size += mc.getMessageSize();
                            count++;
                        }
                    }
                    StringBuilder responseBuffer =
                        new StringBuilder(32)
                                .append(count)
                                .append(" ")
                                .append(size);
                    response = new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString());
                    count = 0;
                    for (Mail mc:session.getUserMailbox()) {
                        if (mc != dm) {
                            responseBuffer =
                                new StringBuilder(16)
                                        .append(count)
                                        .append(" ")
                                        .append(mc.getMessageSize());
                            response.appendLine(responseBuffer.toString());
                        }
                        count++;
                    }
                    response.appendLine(".");
                } catch (MessagingException me) {
                    response = new POP3Response(POP3Response.ERR_RESPONSE);
                }
            } else {
                int num = 0;
                try {
                    num = Integer.parseInt(parameters);
                    int size =getMessageSize(session, num);
                    if (mc != dm) {
                        StringBuilder responseBuffer =
                            new StringBuilder(64)
                                    .append(num)
                                    .append(" ")
                                    .append(mc.getMessageSize());
                        response = new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString());
                    } else {
                        StringBuilder responseBuffer =
                            new StringBuilder(64)
                                    .append("Message (")
                                    .append(num)
                                    .append(") already deleted.");
                        response = new POP3Response(POP3Response.ERR_RESPONSE, responseBuffer.toString());
                    }
                } catch (IndexOutOfBoundsException npe) {
                    StringBuilder responseBuffer =
                        new StringBuilder(64)
                                .append("Message (")
                                .append(num)
                                .append(") does not exist.");
                    response = new POP3Response(POP3Response.ERR_RESPONSE, responseBuffer.toString());
                } catch (NumberFormatException nfe) {
                    StringBuilder responseBuffer =
                        new StringBuilder(64)
                                .append(parameters)
                                .append(" is not a valid number");
                    response = new POP3Response(POP3Response.ERR_RESPONSE, responseBuffer.toString());
                } catch (MessagingException me) {
                    response = new POP3Response(POP3Response.ERR_RESPONSE);
                }
            }
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
        }
        return response;
    }
View Full Code Here


        // Format is:  Sat, 24 Jan 1998 13:16:09 -0500
        responseBuffer.append(session.getConfigurationData().getHelloName())
                    .append(" POP3 server (")
                    .append(softwaretype)
                    .append(") ready ");
        POP3Response response = new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString());
        session.writeResponse(response);
    }
View Full Code Here

     * Handler method called upon receipt of a RSET command.
     * Calls stat() to reset the mailbox.
     *
   */
    public Response onCommand(POP3Session session, Request request) {
        POP3Response response = null;
        if (session.getHandlerState() == POP3Session.TRANSACTION) {
            stat(session);
            response = new POP3Response(POP3Response.OK_RESPONSE);
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
        }
        return response;   
    }
View Full Code Here

     * Returns the number of messages in the mailbox and its
     * aggregate size.
     *
   */
    public Response onCommand(POP3Session session, Request request) {
        POP3Response response = null;
        if (session.getHandlerState() == POP3Session.TRANSACTION) {
           
            try {
              long size = getInboxSize(session);
                int count = getMessageCount(session);
               
                StringBuilder responseBuffer =
                    new StringBuilder(32)
                            .append(count)
                            .append(" ")
                            .append(size);
                response = new POP3Response(POP3Response.OK_RESPONSE,responseBuffer.toString());
            } catch (POP3Exception me) {
                response = new POP3Response(POP3Response.ERR_RESPONSE);
            }
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
        }
        return response;
    }
View Full Code Here

     * Handler method called upon receipt of an unrecognized command.
     * Returns an error response and logs the command.   
     *
     */
    public Response onCommand(POP3Session session, Request request) {
        return new POP3Response(POP3Response.ERR_RESPONSE);
    }
View Full Code Here

  /*
   * (non-Javadoc)
   * @see org.apache.james.protocols.api.CommandHandler#onCommand(org.apache.james.protocols.api.ProtocolSession, org.apache.james.protocols.api.Request)
   */
    public Response onCommand(POP3Session session, Request request) {
      POP3Response response = new POP3Response(POP3Response.OK_RESPONSE,"Capability list follows");
   
    for (int i = 0; i < caps.size(); i++) {
      List<String> cList = caps.get(i).getImplementedCapabilities(session);
      for (int a = 0; a < cList.size(); a++) {
        response.appendLine(cList.get(a));
      }
    }
    response.appendLine(".");
    return response;
  }
View Full Code Here

     * Handler method called upon receipt of a NOOP command.
     * Like all good NOOPs, does nothing much.
     *
   */
    public Response onCommand(POP3Session session, Request request) {
        POP3Response response = null;
        if (session.getHandlerState() == POP3Session.TRANSACTION) {
            response = new POP3Response(POP3Response.OK_RESPONSE);
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
        }
        return response;  
    }
View Full Code Here

     * Handler method called upon receipt of a USER command.
     * Reads in the user id.
     *
   */
    public Response onCommand(POP3Session session, Request request) {
        POP3Response response = null;
        String parameters = request.getArgument();
        if (session.getHandlerState() == POP3Session.AUTHENTICATION_READY && parameters != null) {
            session.setUser(parameters);
            session.setHandlerState(POP3Session.AUTHENTICATION_USERSET);
            response = new POP3Response(POP3Response.OK_RESPONSE);
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
        }
        return response;  
    }
View Full Code Here

     * Reads in and validates the password.
     *
   */
    public Response onCommand(POP3Session session, Request request) {
      String parameters = request.getArgument();
        POP3Response response = null;
        if (session.getHandlerState() == POP3Session.AUTHENTICATION_USERSET && parameters != null) {
            String passArg = parameters;
            if (authenticate(session.getUser(), passArg)) {
                try {
                    MailRepository inbox = mailServer.getUserInbox(session.getUser());
                    if (inbox == null) {
                        throw new IllegalStateException("MailServer returned a null inbox for "+session.getUser());
                    }
                    session.setUserInbox(inbox);
                    stat(session);
                   
                    // Store the ipAddress to use it later for pop before smtp
                    POP3BeforeSMTPHelper.addIPAddress(session.getRemoteIPAddress());
                   
                    StringBuilder responseBuffer =
                        new StringBuilder(64)
                                .append("Welcome ")
                                .append(session.getUser());
                    response = new POP3Response(POP3Response.OK_RESPONSE,responseBuffer.toString());
                    session.setHandlerState(POP3Session.TRANSACTION);
                } catch (RuntimeException e) {
                    session.getLogger().error("Unexpected error accessing mailbox for "+session.getUser(),e);
                    response = new POP3Response(POP3Response.ERR_RESPONSE,"Unexpected error accessing mailbox");
                    session.setHandlerState(POP3Session.AUTHENTICATION_READY);
                }
            } else {
                response = new POP3Response(POP3Response.ERR_RESPONSE, "Authentication failed.");

                session.setHandlerState(POP3Session.AUTHENTICATION_READY);
            }
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
        }
        return response;   
    }
View Full Code Here

    /*
     * (non-Javadoc)
     * @see org.apache.james.api.protocol.CommandHandler#onCommand(org.apache.james.api.protocol.LogEnabledSession, org.apache.james.api.protocol.Request)
     */
    public Response onCommand(POP3Session session, Request request) {
        POP3Response response;
        // check if starttls is supported, the state is the right one and it was
        // not started before
        if (session.isStartTLSSupported() && session.getHandlerState() == POP3Session.AUTHENTICATION_READY
                && session.isTLSStarted() == false) {
            response = new POP3Response(POP3Response.OK_RESPONSE,"Begin TLS negotiation");
            session.writeResponse(response);
            try {
                session.startTLS();
            } catch (IOException e) {
                session.getLogger().info("Error while trying to secure connection", e);

                // disconnect
                response = new POP3Response(POP3Response.ERR_RESPONSE);
                response.setEndSession(true);
                return response;
            }
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
            return response;
        }
        return null;
    }
View Full Code Here

TOP

Related Classes of org.apache.james.protocols.pop3.POP3Protocol

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.