View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.smtp.command;
14  
15  import org.abstracthorizon.mercury.common.command.CommandException;
16  import org.abstracthorizon.mercury.smtp.SMTPResponses;
17  import org.abstracthorizon.mercury.smtp.SMTPScanner;
18  import org.abstracthorizon.mercury.smtp.SMTPSession;
19  import org.abstracthorizon.mercury.smtp.exception.ParserException;
20  import org.abstracthorizon.mercury.smtp.util.Path;
21  
22  import java.io.IOException;
23  
24  /**
25   * MAIL TO command.
26   *
27   * @author Daniel Sendula
28   */
29  public class MailCommand extends SMTPCommand {
30  
31      /**
32       * Constructor
33       */
34      public MailCommand() {
35      }
36  
37      /**
38       * Executed the command
39       * @param connection smtp session
40       * @throws CommandException
41       * @throws IOException
42       * @throws ParserException
43       */
44      protected void execute(SMTPSession connection) throws CommandException, IOException, ParserException {
45          if (connection.getState() != SMTPSession.STATE_READY) {
46              connection.sendResponse(SMTPResponses.BAD_SEQUENCE_OF_COMMANDS_RESPONSE);
47              return;
48          }
49          connection.getMailSessionData().clear();
50  
51          SMTPScanner scanner = connection.getScanner();
52          if (!scanner.is_char(' ')) { throw new ParserException("space"); }
53          if (!scanner.keyword("FROM:")) { throw new ParserException("FROM:"); }
54          if (scanner.is_char(' ')) {
55              // Allow for incorrect clients that add space here..
56          }
57          Path path = new Path();
58          if (!scanner.keyword("<>")) {
59              if (!scanner.path(path)) { throw new ParserException("reverse path"); }
60          }
61  
62          readExtraParameters(connection, scanner);
63  
64          connection.getMailSessionData().setSourceMailbox(path);
65          processMailFrom(connection, path);
66  
67      }
68  
69      /**
70       * Obtains extra parameters.
71       *
72       * @param session SMTP session
73       * @param scanner STMP scanner
74       * @throws IOException io exception
75       * @throws ParserException parsing exception
76       * @throws CommandException command exception
77       */
78      protected void readExtraParameters(SMTPSession connection, SMTPScanner scanner) throws IOException, ParserException, CommandException {
79          scanner.skip_line();
80      }
81  
82      /**
83       * Sets from path to the session. This method can test if and make an
84       * different action (sending different response).
85       *
86       * @param path path to be stored in the session
87       * @param session SMTP session
88       * @throws IOException
89       */
90      protected void processMailFrom(SMTPSession connection, Path path) throws IOException {
91          connection.sendResponse(SMTPResponses.OK_RESPONSE);
92          connection.setState(SMTPSession.STATE_MAIL);
93      }
94  
95  }