Examples of POP3Response


Examples of org.apache.james.pop3server.POP3Response

     *
     */
    @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);
                    MessageStream stream = new MessageStream();
                    OutputStream out = stream.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();

                        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();
                    }
                    session.writeStream(stream.getInputStream());

                    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

Examples of org.apache.james.pop3server.POP3Response

     * 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);
                    MessageStream stream = new MessageStream();
                    OutputStream out = stream.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();
                    }

                    session.writeStream(stream.getInputStream());

                    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

Examples of org.apache.james.pop3server.POP3Response

     * This method handles 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;
        }
        List<Mail> toBeRemoved =  ListUtils.subtract(session.getBackupUserMailbox(), session.getUserMailbox());
        try {
            session.getUserInbox().remove(toBeRemoved);
            // for (Iterator it = toBeRemoved.iterator(); it.hasNext(); ) {
            //    Mail mc = (Mail) it.next();
            //    userInbox.remove(mc.getName());
            //}
            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.getMessage());
        }
        response.setEndSession(true);
        return response;   
    }
View Full Code Here

Examples of org.apache.james.pop3server.POP3Response

     *
   */
    @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 = 0;
            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 {
                Mail mc = session.getUserMailbox().get(num);
                Mail dm = (Mail) session.getState().get(POP3Session.DELETED);

                if (mc != dm) {
                    response = new POP3Response(POP3Response.OK_RESPONSE, "Message follows");
                    try {
                        for (Enumeration e = mc.getMessage().getAllHeaderLines(); e.hasMoreElements(); ) {
                          response.appendLine(e.nextElement().toString());
                        }

                         writeMessageContentTo(mc, response, lines);
                        
                    } finally {
                      response.appendLine(".");
                    }
                   
                  return response; 

                } 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());
            }
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
        }
        return response;    }
View Full Code Here

Examples of org.apache.james.pop3server.POP3Response

     * This command deletes a particular mail message from the
     * mailbox.  
     *
   */
    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 {
                Mail mc = session.getUserMailbox().get(num);
                Mail dm = (Mail) session.getState().get(POP3Session.DELETED);
                if (mc == dm) {
                    StringBuilder responseBuffer =
                        new StringBuilder(64)
                                .append("Message (")
                                .append(num)
                                .append(") already deleted.");
                    response = new POP3Response(POP3Response.ERR_RESPONSE,responseBuffer.toString());
                } else {
                    session.getUserMailbox().set(num, dm);
                    // we are replacing our reference with "DELETED", so we have
                    // to dispose the no-more-referenced mail object.
                    LifecycleUtil.dispose(mc);
                    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

Examples of org.apache.james.pop3server.POP3Response

     * Handler method called upon receipt of a UIDL command.
     * Returns a listing of message ids to the client.
     *
   */
    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) {
                response = new POP3Response(POP3Response.OK_RESPONSE,"unique-id listing follows");
                int count = 0;
                for (Mail mc:session.getUserMailbox()) {
                    if (mc != dm) {
                        StringBuilder responseBuffer =
                            new StringBuilder(64)
                                    .append(count)
                                    .append(" ")
                                    .append(mc.getName());
                        response.appendLine(responseBuffer.toString());
                    }
                    count++;
                }
                response.appendLine(".");
            } else {
                int num = 0;
                try {
                    num = Integer.parseInt(parameters);
                    Mail mc = (Mail) session.getUserMailbox().get(num);
                    if (mc != dm) {
                        StringBuilder responseBuffer =
                            new StringBuilder(64)
                                    .append(num)
                                    .append(" ")
                                    .append(mc.getName());
                        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());
                }
            }
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
        }
        return response;
    }
View Full Code Here

Examples of org.apache.james.pop3server.POP3Response

     * Handler method called upon receipt of a UIDL command. Returns a listing
     * of message ids to the client.
     *
     */
    public Response onCommand(POP3Session session, Request request) {
        POP3Response response = null;
        String parameters = request.getArgument();
        if (session.getHandlerState() == POP3Session.TRANSACTION) {
            List<Long> uidList = (List<Long>) session.getState().get(POP3Session.UID_LIST);
            List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);

            if (parameters == null) {
                response = new POP3Response(POP3Response.OK_RESPONSE, "unique-id listing follows");
                for (int i = 0; i < uidList.size(); i++) {
                    Long uid = uidList.get(i);
                    if (deletedUidList.contains(uid) == false) {
                        StringBuilder responseBuffer = new StringBuilder(64).append(i + 1).append(" ").append(uid);
                        response.appendLine(responseBuffer.toString());
                    }
                }

                response.appendLine(".");
            } else {
                int num = 0;
                try {
                    num = Integer.parseInt(parameters);
                    Long uid = uidList.get(num - 1);
                    if (deletedUidList.contains(uid) == false) {

                        StringBuilder responseBuffer = new StringBuilder(64).append(num).append(" ").append(uid.hashCode());
                        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());
                }
            }
        } else {
            response = new POP3Response(POP3Response.ERR_RESPONSE);
        }
        return response;
    }
View Full Code Here

Examples of org.apache.james.pop3server.POP3Response

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

    public Response onCommand(POP3Session session, Request request) {
        POP3Response response = null;
        String parameters = request.getArgument();
        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);
        if (session.getHandlerState() == POP3Session.TRANSACTION) {
            if (parameters == null) {

                try {
                    long size = 0;
                    int count = 0;
                    List<MessageResult> validResults = new ArrayList<MessageResult>();

                    if (uidList.isEmpty() == false) {
                        Iterator<MessageResult> results;
                        if (uidList.size() > 1) {
                            results = session.getUserMailbox().getMessages(MessageRange.range(uidList.get(0), uidList.get(uidList.size() - 1)), new FetchGroupImpl(FetchGroup.MINIMAL), mailboxSession);
                        } else {
                            results = session.getUserMailbox().getMessages(MessageRange.one(uidList.get(0)), new FetchGroupImpl(FetchGroup.MINIMAL), mailboxSession);
                        }

                        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());
                    count = 0;
                    for (int i = 0; i < validResults.size(); i++) {
                        responseBuffer = new StringBuilder(16).append(i + 1).append(" ").append(validResults.get(i).getSize());
                        response.appendLine(responseBuffer.toString());

                    }
                    response.appendLine(".");
                } catch (MailboxException me) {
                    response = new POP3Response(POP3Response.ERR_RESPONSE);
                }
            } else {
                int num = 0;
                try {
                    num = Integer.parseInt(parameters);
                    Long uid = uidList.get(num - 1);
                    if (deletedUidList.contains(uid) == false) {
                        Iterator<MessageResult> results = session.getUserMailbox().getMessages(MessageRange.one(uid), new FetchGroupImpl(FetchGroup.MINIMAL), mailboxSession);

                        StringBuilder responseBuffer = new StringBuilder(64).append(num).append(" ").append(results.next().getSize());
                        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

Examples of org.apache.james.pop3server.POP3Response

        StringBuilder responseBuffer = new StringBuilder();

        // Initially greet the connector
        // 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

Examples of org.apache.james.pop3server.POP3Response

     * 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
TOP
Copyright © 2018 www.massapi.com. 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.