1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
28
29
30
31
32 public abstract class AbstractCommand implements CommandInterface {
33
34
35
36 private FtpCommandCode code;
37
38
39
40
41 private String command;
42
43
44
45
46 private String arg;
47
48
49
50
51 private FtpSession session;
52
53
54
55
56 private Object object;
57
58
59
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
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
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
154
155 public FtpSession getSession() {
156 return session;
157 }
158
159
160
161
162
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
176
177 public FtpCommandCode getCode() {
178 return code;
179 }
180 }