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 java.io.IOException;
16  
17  import org.abstracthorizon.mercury.common.command.CommandException;
18  import org.abstracthorizon.mercury.smtp.SMTPResponse;
19  import org.abstracthorizon.mercury.smtp.SMTPScanner;
20  import org.abstracthorizon.mercury.smtp.SMTPSession;
21  import org.abstracthorizon.mercury.smtp.exception.ParserException;
22  
23  /**
24   * EHLO command
25   *
26   * @author Daniel Sendula
27   */
28  public class EhloCommand extends ResetCommand {
29  
30      /**
31       * Constructor
32       */
33      public EhloCommand() {
34      }
35  
36      /**
37       * Executed the command
38       * @param connection smtp session
39       * @throws CommandException
40       * @throws IOException
41       * @throws ParserException
42       */
43      protected void execute(SMTPSession connection) throws CommandException, IOException, ParserException {
44  //        if (connection.getState() != SMTPSession.STATE_READY) {
45  //            connection.sendResponse(SMTPResponses.BAD_SEQUENCE_OF_COMMANDS_RESPONSE);
46  //            return;
47  //        }
48  
49          SMTPScanner scanner = connection.getScanner();
50          if (!scanner.is_char(' ')) { throw new ParserException("space"); }
51  
52          
53          boolean bracked = scanner.is_char('[');
54          StringBuffer domain = new StringBuffer();
55          if (!scanner.domain(domain)) { throw new ParserException("domain"); }
56          if (bracked) {
57              if (!scanner.is_char(']')) { throw new ParserException("]"); }
58          }
59  
60          readExtraParameters(connection, scanner);
61  
62          String domainStr = domain.toString();
63          connection.getMailSessionData().setSourceDomain(domainStr);
64          processEHLO(connection, domainStr);
65      }
66  
67      /**
68       * Obtains extra parameters.
69       *
70       * @param session SMTP session
71       * @param scanner STMP scanner
72       * @throws IOException io exception
73       * @throws ParserException parsing exception
74       * @throws CommandException command exception
75       */
76      protected void readExtraParameters(SMTPSession connection, SMTPScanner scanner) throws IOException, ParserException, CommandException {
77          scanner.check_eol();
78      }
79  
80      /**
81       * Processes EHLO command
82       *
83       * @param connection SMTP session
84       * @throws IOException
85       */
86      protected void processEHLO(SMTPSession connection, String domain) throws IOException {
87          resetSession(connection);
88          connection.setState(SMTPSession.STATE_READY);
89          SMTPResponse ehloResponse = new SMTPResponse(250, connection.getConnectionHandler().getStorageManager().getMainDomain());
90          connection.sendResponse(ehloResponse);
91      }
92  
93  }