📄 pop3handler.java
字号:
writeLoggedFlushedResponse(responseString); } else { StringBuffer responseBuffer = new StringBuffer(64) .append(ERR_RESPONSE) .append(" Message (") .append(num) .append(") already deleted."); responseString = responseBuffer.toString(); writeLoggedFlushedResponse(responseString); } } catch (IndexOutOfBoundsException npe) { StringBuffer responseBuffer = new StringBuffer(64) .append(ERR_RESPONSE) .append(" Message (") .append(num) .append(") does not exist."); responseString = responseBuffer.toString(); writeLoggedFlushedResponse(responseString); } catch (NumberFormatException nfe) { StringBuffer responseBuffer = new StringBuffer(64) .append(ERR_RESPONSE) .append(" ") .append(argument) .append(" is not a valid number"); responseString = responseBuffer.toString(); writeLoggedFlushedResponse(responseString); } } } else { writeLoggedFlushedResponse(ERR_RESPONSE); } } /** * Handler method called upon receipt of a RSET command. * Calls stat() to reset the mailbox. * * @param command the command parsed by the parseCommand method * @param argument the first argument parsed by the parseCommand method * @param argument1 the second argument parsed by the parseCommand method */ private void doRSET(String command,String argument,String argument1) { String responseString = null; if (state == TRANSACTION) { stat(); responseString = OK_RESPONSE; } else { responseString = ERR_RESPONSE; } writeLoggedFlushedResponse(responseString); } /** * Handler method called upon receipt of a DELE command. * This command deletes a particular mail message from the * mailbox. * * @param command the command parsed by the parseCommand method * @param argument the first argument parsed by the parseCommand method * @param argument1 the second argument parsed by the parseCommand method */ private void doDELE(String command,String argument,String argument1) { String responseString = null; if (state == TRANSACTION) { int num = 0; try { num = Integer.parseInt(argument); } catch (Exception e) { responseString = ERR_RESPONSE + " Usage: DELE [mail number]"; writeLoggedFlushedResponse(responseString); return; } try { MailImpl mc = (MailImpl) userMailbox.get(num); if (mc == DELETED) { StringBuffer responseBuffer = new StringBuffer(64) .append(ERR_RESPONSE) .append(" Message (") .append(num) .append(") already deleted."); responseString = responseBuffer.toString(); writeLoggedFlushedResponse(responseString); } else { userMailbox.set(num, DELETED); writeLoggedFlushedResponse(OK_RESPONSE + " Message deleted"); } } catch (IndexOutOfBoundsException iob) { StringBuffer responseBuffer = new StringBuffer(64) .append(ERR_RESPONSE) .append(" Message (") .append(num) .append(") does not exist."); responseString = responseBuffer.toString(); writeLoggedFlushedResponse(responseString); } } else { responseString = ERR_RESPONSE; writeLoggedFlushedResponse(responseString); } } /** * Handler method called upon receipt of a NOOP command. * Like all good NOOPs, does nothing much. * * @param command the command parsed by the parseCommand method * @param argument the first argument parsed by the parseCommand method * @param argument1 the second argument parsed by the parseCommand method */ private void doNOOP(String command,String argument,String argument1) { String responseString = null; if (state == TRANSACTION) { responseString = OK_RESPONSE; writeLoggedFlushedResponse(responseString); } else { responseString = ERR_RESPONSE; writeLoggedFlushedResponse(responseString); } } /** * Handler method called upon receipt of a RETR command. * This command retrieves a particular mail message from the * mailbox. * * @param command the command parsed by the parseCommand method * @param argument the first argument parsed by the parseCommand method * @param argument1 the second argument parsed by the parseCommand method */ private void doRETR(String command,String argument,String argument1) { String responseString = null; if (state == TRANSACTION) { int num = 0; try { num = Integer.parseInt(argument.trim()); } catch (Exception e) { responseString = ERR_RESPONSE + " Usage: RETR [mail number]"; writeLoggedFlushedResponse(responseString); return; } try { MailImpl mc = (MailImpl) userMailbox.get(num); if (mc != DELETED) { responseString = OK_RESPONSE + " Message follows"; writeLoggedFlushedResponse(responseString); OutputStream nouts = new ExtraDotOutputStream(outs); nouts = new BytesWrittenResetOutputStream(nouts, theWatchdog, theConfigData.getResetLength()); mc.writeMessageTo(nouts); nouts.flush(); // TODO: Is this an extra CRLF? out.println(); out.println("."); out.flush(); } else { StringBuffer responseBuffer = new StringBuffer(64) .append(ERR_RESPONSE) .append(" Message (") .append(num) .append(") already deleted."); responseString = responseBuffer.toString(); writeLoggedFlushedResponse(responseString); } } catch (IOException ioe) { responseString = ERR_RESPONSE + " Error while retrieving message."; writeLoggedFlushedResponse(responseString); } catch (MessagingException me) { responseString = ERR_RESPONSE + " Error while retrieving message."; writeLoggedFlushedResponse(responseString); } catch (IndexOutOfBoundsException iob) { StringBuffer responseBuffer = new StringBuffer(64) .append(ERR_RESPONSE) .append(" Message (") .append(num) .append(") does not exist."); responseString = responseBuffer.toString(); writeLoggedFlushedResponse(responseString); } } else { responseString = ERR_RESPONSE; writeLoggedFlushedResponse(responseString); } } /** * Handler method called upon receipt of a TOP command. * This command retrieves the top N lines of a specified * message in the mailbox. * * The expected command format is * TOP [mail message number] [number of lines to return] * * @param command the command parsed by the parseCommand method * @param argument the first argument parsed by the parseCommand method * @param argument1 the second argument parsed by the parseCommand method */ private void doTOP(String command,String argument,String argument1) { String responseString = null; if (state == TRANSACTION) { int num = 0; int lines = 0; try { num = Integer.parseInt(argument); lines = Integer.parseInt(argument1); } catch (NumberFormatException nfe) { responseString = ERR_RESPONSE + " Usage: TOP [mail number] [Line number]"; writeLoggedFlushedResponse(responseString); return; } try { MailImpl mc = (MailImpl) userMailbox.get(num); if (mc != DELETED) { responseString = OK_RESPONSE + " Message follows"; writeLoggedFlushedResponse(responseString); for (Enumeration e = mc.getMessage().getAllHeaderLines(); e.hasMoreElements(); ) { out.println(e.nextElement()); } out.println(); OutputStream nouts = new ExtraDotOutputStream(outs); nouts = new BytesWrittenResetOutputStream(nouts, theWatchdog, theConfigData.getResetLength()); mc.writeContentTo(nouts, lines); nouts.flush(); out.println("."); out.flush(); } else { StringBuffer responseBuffer = new StringBuffer(64) .append(ERR_RESPONSE) .append(" Message (") .append(num) .append(") already deleted."); responseString = responseBuffer.toString(); writeLoggedFlushedResponse(responseString); } } catch (IOException ioe) { responseString = ERR_RESPONSE + " Error while retrieving message."; writeLoggedFlushedResponse(responseString); } catch (MessagingException me) { responseString = ERR_RESPONSE + " Error while retrieving message."; writeLoggedFlushedResponse(responseString); } catch (IndexOutOfBoundsException iob) { StringBuffer exceptionBuffer = new StringBuffer(64) .append(ERR_RESPONSE) .append(" Message (") .append(num) .append(") does not exist."); responseString = exceptionBuffer.toString(); writeLoggedFlushedResponse(responseString); } } else { responseString = ERR_RESPONSE; writeLoggedFlushedResponse(responseString); } } /** * Handler method called upon receipt of a QUIT command. * This method handles cleanup of the POP3Handler state. * * @param command the command parsed by the parseCommand method * @param argument the first argument parsed by the parseCommand method * @param argument1 the second argument parsed by the parseCommand method */ private void doQUIT(String command,String argument,String argument1) { String responseString = null; if (state == AUTHENTICATION_READY || state == AUTHENTICATION_USERSET) { responseString = OK_RESPONSE + " Apache James POP3 Server signing off."; writeLoggedFlushedResponse(responseString); return; } List toBeRemoved = ListUtils.subtract(backupUserMailbox, userMailbox); try { userInbox.remove(toBeRemoved); // for (Iterator it = toBeRemoved.iterator(); it.hasNext(); ) { // MailImpl mc = (MailImpl) it.next(); // userInbox.remove(mc.getName()); //} responseString = OK_RESPONSE + " Apache James POP3 Server signing off."; writeLoggedFlushedResponse(responseString); } catch (Exception ex) { responseString = ERR_RESPONSE + " Some deleted messages were not removed"; writeLoggedFlushedResponse(responseString); getLogger().error("Some deleted messages were not removed: " + ex.getMessage()); } } /** * Handler method called upon receipt of an unrecognized command. * Returns an error response and logs the command. * * @param command the command parsed by the parseCommand method * @param argument the first argument parsed by the parseCommand method * @param argument1 the second argument parsed by the parseCommand method */ private void doUnknownCmd(String command,String argument,String argument1) { writeLoggedFlushedResponse(ERR_RESPONSE); } /** * This method logs at a "DEBUG" level the response string that * was sent to the POP3 client. The method is provided largely * as syntactic sugar to neaten up the code base. It is declared * private and final to encourage compiler inlining. * * @param responseString the response string sent to the client */ private final void logResponseString(String responseString) { if (getLogger().isDebugEnabled()) { getLogger().debug("Sent: " + responseString); } } /** * Write and flush a response string. The response is also logged. * Should be used for the last line of a multi-line response or * for a single line response. * * @param responseString the response string sent to the client */ final void writeLoggedFlushedResponse(String responseString) { out.println(responseString); out.flush(); logResponseString(responseString); } /** * Write a response string. The response is also logged. * Used for multi-line responses. * * @param responseString the response string sent to the client */ final void writeLoggedResponse(String responseString) { out.println(responseString); logResponseString(responseString); } /** * A private inner class which serves as an adaptor * between the WatchdogTarget interface and this * handler class. */ private class POP3WatchdogTarget implements WatchdogTarget { /** * @see org.apache.james.util.watchdog.WatchdogTarget#execute() */ public void execute() { POP3Handler.this.idleClose(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -