Package org.apache.james.protocols.pop3.netty

Examples of org.apache.james.protocols.pop3.netty.NettyStartTlsPOP3ServerTest


     * Handler method called upon receipt of a STAT command. Returns the number
     * of messages in the mailbox and its aggregate size.
     */
    @SuppressWarnings("unchecked")
    public Response onCommand(POP3Session session, Request request) {
        POP3Response response = null;
        if (session.getHandlerState() == POP3Session.TRANSACTION) {

            List<MessageMetaData> uidList = (List<MessageMetaData>) 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) {
                List<MessageMetaData> validResults = new ArrayList<MessageMetaData>();
                for (int i = 0; i < uidList.size(); i++) {
                    MessageMetaData data = uidList.get(i);
                    if (deletedUidList.contains(data.getUid()) == false) {
                        size += data.getSize();
                        count++;
                        validResults.add(data);
                    }
                }
            }
            StringBuilder responseBuffer = new StringBuilder(32).append(count).append(" ").append(size);
            response = new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString());

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


     * to return]
     */
    @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<MessageMetaData> uidList = (List<MessageMetaData>) session.getState().get(POP3Session.UID_LIST);
                List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);

                Long uid = uidList.get(num - 1).getUid();
                if (deletedUidList.contains(uid) == false) {

                    InputStream body = new CountingBodyInputStream(new ExtraDotInputStream(new CRLFTerminatedInputStream(session.getUserMailbox().getMessageBody(uid))), lines);
                    InputStream headers = session.getUserMailbox().getMessageHeaders(uid);
                    if (body != null && headers != null) {
                        response = new POP3StreamResponse(POP3Response.OK_RESPONSE, "Message follows", new SequenceInputStream(headers, body));
                        return response;

                    } else {
                        StringBuilder exceptionBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist.");
                        response = new POP3Response(POP3Response.ERR_RESPONSE, exceptionBuffer.toString());
                    }

                } 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 (IndexOutOfBoundsException iob) {
                StringBuilder exceptionBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist.");
                response = new POP3Response(POP3Response.ERR_RESPONSE, exceptionBuffer.toString());
            } catch (NoSuchElementException iob) {
                StringBuilder exceptionBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist.");
                response = new POP3Response(POP3Response.ERR_RESPONSE, exceptionBuffer.toString());
            }
        } 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.handler.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.
     */
    @SuppressWarnings("unchecked")
    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<MessageMetaData> uidList = (List<MessageMetaData>) session.getState().get(POP3Session.UID_LIST);
                List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);

                Long uid = uidList.get(num - 1).getUid();
                if (deletedUidList.contains(uid) == false) {
                    InputStream content = session.getUserMailbox().getMessage(uid);

                    if (content != null) {
                        InputStream in = new CRLFTerminatedInputStream(new ExtraDotInputStream(content));
                        response = new POP3StreamResponse(POP3Response.OK_RESPONSE, "Message follows", in);
                        return response;
                    } else {
                        StringBuilder responseBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist.");
                        response = new POP3Response(POP3Response.ERR_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 (IOException ioe) {
                response = new POP3Response(POP3Response.ERR_RESPONSE, "Error while retrieving message.");
            }
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
        }
        return response;
    }
View Full Code Here

     * Handler method called upon receipt of a PASS command. 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;
            try {
                Mailbox mailbox = mailboxManager.getMailbox(session, passArg);
                if (mailbox != null) {
                  session.setUserMailbox(mailbox);
                  stat(session);
               
                  StringBuilder responseBuffer = new StringBuilder(64).append("Welcome ").append(session.getUser());
                  response = new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString());
                  session.setHandlerState(POP3Session.TRANSACTION);
                } else {
                  response = new POP3Response(POP3Response.ERR_RESPONSE, "Authentication failed.");
                  session.setHandlerState(POP3Session.AUTHENTICATION_READY);
                }
            } catch (IOException 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

    /**
     * @see CommandHandler#onCommand(org.apache.james.protocols.api.ProtocolSession, 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 StartTlsPop3Response(POP3Response.OK_RESPONSE, "Begin TLS negotiation");
            return response;

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

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

                Long uid = uidList.get(num - 1).getUid();

                if (deletedUidList.contains(uid)) {
                    StringBuilder responseBuffer = new StringBuilder(64).append("Message (").append(num).append(") already deleted.");
                    response = new POP3Response(POP3Response.ERR_RESPONSE, responseBuffer.toString());
                } else {
                    deletedUidList.add(uid);
                    // we are replacing our reference with "DELETED", so we have
                    // to dispose the no-more-referenced mail object.
                    response = new POP3Response(POP3Response.OK_RESPONSE, "Message deleted");
                }
            } 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

TOP

Related Classes of org.apache.james.protocols.pop3.netty.NettyStartTlsPOP3ServerTest

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.