Package org.apache.james.pop3server

Examples of org.apache.james.pop3server.POP3Response


     * cleanup of the POP3Handler state.
     *
     */
    @SuppressWarnings("unchecked")
    public Response onCommand(POP3Session session, Request request) {
        POP3Response response = null;
        if (session.getHandlerState() == POP3Session.AUTHENTICATION_READY || session.getHandlerState() == POP3Session.AUTHENTICATION_USERSET) {
            response = new POP3Response(POP3Response.OK_RESPONSE, "Apache James POP3 Server signing off.");
            response.setEndSession(true);
            return response;
        }
        MailboxSession mailboxSession = (MailboxSession) session.getState().get(POP3Session.MAILBOX_SESSION);

        List<Long> toBeRemoved = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);
        try {
            MessageManager mailbox = session.getUserMailbox();

            for (int i = 0; i < toBeRemoved.size(); i++) {
                MessageRange range = MessageRange.one(toBeRemoved.get(i));
                mailbox.setFlags(new Flags(Flags.Flag.DELETED), true, false, range, mailboxSession);
                mailbox.expunge(range, mailboxSession);
            }
            response = new POP3Response(POP3Response.OK_RESPONSE, "Apache James POP3 Server signing off.");
        } catch (Exception ex) {
            response = new POP3Response(POP3Response.ERR_RESPONSE, "Some deleted messages were not removed");
            session.getLogger().error("Some deleted messages were not removed", ex);
        }
        response.setEndSession(true);
        try {
            mailboxManager.logout(mailboxSession, false);
        } catch (MailboxException e) {
            // nothing todo on logout
        }
View Full Code Here


     * Handler method called upon receipt of a STAT command. 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 {
                List<Long> uidList = (List<Long>) session.getState().get(POP3Session.UID_LIST);
                List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);
                long size = 0;
                int count = 0;
                if (uidList.isEmpty() == false) {
                    MailboxSession mailboxSession = (MailboxSession) session.getState().get(POP3Session.MAILBOX_SESSION);
                    Iterator<MessageResult> results = session.getUserMailbox().getMessages(MessageRange.range(uidList.get(0), uidList.get(uidList.size() - 1)), new FetchGroupImpl(FetchGroup.MINIMAL), mailboxSession);

                    List<MessageResult> validResults = new ArrayList<MessageResult>();
                    while (results.hasNext()) {
                        MessageResult result = results.next();
                        if (deletedUidList.contains(result.getUid()) == false) {
                            size += result.getSize();
                            count++;
                            validResults.add(result);
                        }
                    }
                }
                StringBuilder responseBuffer = new StringBuilder(32).append(count).append(" ").append(size);
                response = new POP3Response(POP3Response.OK_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

     *
     */
    @SuppressWarnings("unchecked")
    @Override
    public Response onCommand(POP3Session session, Request request) {
        POP3Response response = null;
        String parameters = request.getArgument();
        if (parameters == null) {
            response = new POP3Response(POP3Response.ERR_RESPONSE, "Usage: TOP [mail number] [Line number]");
            return response;
        }

        String argument = "";
        String argument1 = "";
        int pos = parameters.indexOf(" ");
        if (pos > 0) {
            argument = parameters.substring(0, pos);
            argument1 = parameters.substring(pos + 1);
        }

        if (session.getHandlerState() == POP3Session.TRANSACTION) {
            int num = 0;
            int lines = -1;
            try {
                num = Integer.parseInt(argument);
                lines = Integer.parseInt(argument1);
            } catch (NumberFormatException nfe) {
                response = new POP3Response(POP3Response.ERR_RESPONSE, "Usage: TOP [mail number] [Line number]");
                return response;
            }
            try {
                List<Long> uidList = (List<Long>) session.getState().get(POP3Session.UID_LIST);
                List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);

                MailboxSession mailboxSession = (MailboxSession) session.getState().get(POP3Session.MAILBOX_SESSION);
                Long uid = uidList.get(num - 1);
                if (deletedUidList.contains(uid) == false) {
                    FetchGroupImpl fetchGroup = new FetchGroupImpl(FetchGroup.BODY_CONTENT);
                    fetchGroup.or(FetchGroup.HEADERS);
                    Iterator<MessageResult> results = session.getUserMailbox().getMessages(MessageRange.one(uid), fetchGroup, mailboxSession);
                    OutputStream out = session.getOutputStream();
                    OutputStream extraDotOut = new ExtraDotOutputStream(out);
                   
                    out.write((POP3Response.OK_RESPONSE + " Message follows\r\n").getBytes());
                    try {
                        MessageResult result = results.next();

                        WritableByteChannel outChannel = Channels.newChannel(extraDotOut);

                        // write headers
                        Iterator<Header> headers = result.headers();
                        while (headers.hasNext()) {
                            headers.next().writeTo(outChannel);

                            // we need to write out the CRLF after each header
                            extraDotOut.write("\r\n".getBytes());

                        }
                        // headers and body are seperated by a CRLF
                        extraDotOut.write("\r\n".getBytes());

                        // write body
                        result.getBody().writeTo(Channels.newChannel(new CountingBodyOutputStream(extraDotOut, lines)));

                    } finally {
                        extraDotOut.flush();
                        // write a single dot to mark message as complete
                        out.write((".\r\n").getBytes());
                        out.flush();
                       
                        extraDotOut.close();
                        out.close();
                    }

                    return null;

                } else {
                    StringBuilder responseBuffer = new StringBuilder(64).append("Message (").append(num).append(") already deleted.");
                    response = new POP3Response(POP3Response.ERR_RESPONSE, responseBuffer.toString());
                }
            } catch (IOException ioe) {
                response = new POP3Response(POP3Response.ERR_RESPONSE, "Error while retrieving message.");
            } catch (MessagingException me) {
                response = new POP3Response(POP3Response.ERR_RESPONSE, "Error while retrieving message.");
            } catch (IndexOutOfBoundsException iob) {
                StringBuilder exceptionBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist.");
                response = new POP3Response(POP3Response.ERR_RESPONSE, exceptionBuffer.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } 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

     * @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

     * Handler method called upon receipt of a RETR command. This command
     * retrieves a particular mail message from the mailbox.
     *
     */
    public Response onCommand(POP3Session session, Request request) {
        POP3Response response = null;
        String parameters = request.getArgument();
        if (session.getHandlerState() == POP3Session.TRANSACTION) {
            int num = 0;
            try {
                num = Integer.parseInt(parameters.trim());
            } catch (Exception e) {
                response = new POP3Response(POP3Response.ERR_RESPONSE, "Usage: RETR [mail number]");
                return response;
            }
            try {
                List<Long> uidList = (List<Long>) session.getState().get(POP3Session.UID_LIST);
                List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);

                MailboxSession mailboxSession = (MailboxSession) session.getState().get(POP3Session.MAILBOX_SESSION);
                Long uid = uidList.get(num - 1);
                if (deletedUidList.contains(uid) == false) {
                    Iterator<MessageResult> results = session.getUserMailbox().getMessages(MessageRange.one(uid), new FetchGroupImpl(FetchGroup.FULL_CONTENT), mailboxSession);
                    OutputStream out = session.getOutputStream();
                    OutputStream extraDotOut = new ExtraDotOutputStream(out);
                   
                    out.write((POP3Response.OK_RESPONSE + " Message follows\r\n").getBytes());
                    out.flush();
                   
                    // response = new POP3Response(POP3Response.OK_RESPONSE,
                    // "Message follows");
                    try {
                        MessageResult result = results.next();
                        result.getFullContent().writeTo(Channels.newChannel(extraDotOut));

                    } finally {
                        extraDotOut.flush();
                       
                        // write a single dot to mark message as complete
                        out.write((".\r\n").getBytes());
                        out.flush();
                       
                        extraDotOut.close();
                        out.close();
                    }

                    return null;
                } else {

                    StringBuilder responseBuffer = new StringBuilder(64).append("Message (").append(num).append(") already deleted.");
                    response = new POP3Response(POP3Response.ERR_RESPONSE, responseBuffer.toString());
                }
            } catch (IOException ioe) {
                response = new POP3Response(POP3Response.ERR_RESPONSE, "Error while retrieving message.");
            } catch (MessagingException me) {
                response = new POP3Response(POP3Response.ERR_RESPONSE, "Error while retrieving message.");
            } catch (IndexOutOfBoundsException iob) {
                StringBuilder responseBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist.");
                response = new POP3Response(POP3Response.ERR_RESPONSE, responseBuffer.toString());
            }
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
        }
        return response;
    }
View Full Code Here

     * 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;
            try {
                MailboxSession mSession = mailboxManager.login(session.getUser(), passArg, session.getLogger());
               
                // explicit call start processing because it was not stored before in the session
                mailboxManager.startProcessingRequest(mSession);
               
                MailboxPath mailboxPath = MailboxPath.inbox(session.getUser());
               
                // check if mailbox exists.. if not just create it
                if (mailboxManager.mailboxExists(mailboxPath, mSession) == false) {
                    mailboxManager.createMailbox(mailboxPath, mSession);
                }
                MessageManager mailbox = mailboxManager.getMailbox(mailboxPath, mSession);

                session.getState().put(POP3Session.MAILBOX_SESSION, mSession);
                session.setUserMailbox(mailbox);
                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 (BadCredentialsException e) {

                response = new POP3Response(POP3Response.ERR_RESPONSE, "Authentication failed.");
                session.setHandlerState(POP3Session.AUTHENTICATION_READY);
            } catch (MailboxException 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);
        }

        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.pop3server.POP3Response

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.