View Javadoc

1   /**
2    * This file is part of Waarp Project.
3    * 
4    * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
5    * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
6    * 
7    * All Waarp Project is free software: you can redistribute it and/or modify it under the terms of
8    * the GNU General Public License as published by the Free Software Foundation, either version 3 of
9    * the License, or (at your option) any later version.
10   * 
11   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
12   * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along with Waarp . If not, see
16   * <http://www.gnu.org/licenses/>.
17   */
18  package org.waarp.ftp.core.command;
19  
20  import org.waarp.common.command.CommandInterface;
21  import org.waarp.common.exception.InvalidArgumentException;
22  import org.waarp.common.file.SessionInterface;
23  import org.waarp.ftp.core.config.FtpConfiguration;
24  import org.waarp.ftp.core.session.FtpSession;
25  
26  /**
27   * Abstract definition of an FTP Command
28   * 
29   * @author Frederic Bregier
30   * 
31   */
32  public abstract class AbstractCommand implements CommandInterface {
33      /**
34       * Code of Command
35       */
36      private FtpCommandCode code;
37  
38      /**
39       * String attached to the command
40       */
41      private String command;
42  
43      /**
44       * Argument attached to this command
45       */
46      private String arg;
47  
48      /**
49       * The Ftp SessionInterface
50       */
51      private FtpSession session;
52  
53      /**
54       * Internal Object (whatever the used). This has to be clean by Business Handler cleanSession.
55       */
56      private Object object;
57  
58      /**
59       * Extra allowed nextCommand
60       */
61      private FtpCommandCode extraNextCommand = null;
62  
63      @Override
64      public void setArgs(SessionInterface session, String command, String arg,
65              @SuppressWarnings("rawtypes") Enum code) {
66          this.session = (FtpSession) session;
67          this.command = command;
68          this.arg = arg;
69          this.code = (FtpCommandCode) code;
70      }
71  
72      @Override
73      public void setExtraNextCommand(@SuppressWarnings("rawtypes") Enum extraNextCommand) {
74          if (extraNextCommand != FtpCommandCode.NOOP) {
75              this.extraNextCommand = (FtpCommandCode) extraNextCommand;
76          } else {
77              this.extraNextCommand = null;
78          }
79      }
80  
81      @Override
82      public boolean isNextCommandValid(CommandInterface newCommandArg) {
83          AbstractCommand newCommand = (AbstractCommand) newCommandArg;
84          Class<? extends AbstractCommand> newClass = newCommand.getClass();
85          // Special commands: QUIT ABORT STAT NOP
86          if (FtpCommandCode.isSpecialCommand(newCommand.getCode())) {
87              return true;
88          }
89          if (code == null) {
90              return false;
91          }
92          if (extraNextCommand != null) {
93              if (extraNextCommand.command == newClass) {
94                  return true;
95              }
96              if (code.nextValids != null && code.nextValids.length > 0) {
97                  for (Class<?> nextValid : code.nextValids) {
98                      if (nextValid == newClass) {
99                          return true;
100                     }
101                 }
102             }
103             return false;
104         }
105         if (code.nextValids == null || code.nextValids.length == 0) {
106             // Any command is allowed
107             return true;
108         }
109         for (Class<?> nextValid : code.nextValids) {
110             if (nextValid == newClass) {
111                 return true;
112             }
113         }
114         return false;
115     }
116 
117     public Object getObject() {
118         return object;
119     }
120 
121     public void setObject(Object object) {
122         this.object = object;
123     }
124 
125     public String getArg() {
126         return arg;
127     }
128 
129     public String[] getArgs() {
130         return arg.split(" ");
131     }
132 
133     public int getValue(String argx) throws InvalidArgumentException {
134         int i = 0;
135         try {
136             i = Integer.parseInt(argx);
137         } catch (NumberFormatException e) {
138             throw new InvalidArgumentException("Not an integer", e);
139         }
140         return i;
141     }
142 
143     public String getCommand() {
144         return command;
145     }
146 
147     public boolean hasArg() {
148         return arg != null && arg.length() != 0;
149     }
150 
151     /**
152      * 
153      * @return the current FtpSession
154      */
155     public FtpSession getSession() {
156         return session;
157     }
158 
159     // some helpful functions
160     /**
161      * 
162      * @return The current configuration object
163      */
164     public FtpConfiguration getConfiguration() {
165         return session.getConfiguration();
166     }
167 
168     public void invalidCurrentCommand() {
169         session.getRestart().setSet(false);
170         session.setPreviousAsCurrentCommand();
171     }
172 
173     /**
174      * 
175      * @return The FtpCommandCode associated with this command
176      */
177     public FtpCommandCode getCode() {
178         return code;
179     }
180 }