1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package goldengate.ftp.core.command;
22
23 import goldengate.common.command.CommandInterface;
24 import goldengate.common.exception.InvalidArgumentException;
25 import goldengate.common.file.SessionInterface;
26 import goldengate.ftp.core.config.FtpConfiguration;
27 import goldengate.ftp.core.session.FtpSession;
28
29
30
31
32
33
34
35 public abstract class AbstractCommand implements CommandInterface {
36
37
38
39 private FtpCommandCode code;
40
41
42
43
44 private String command;
45
46
47
48
49 private String arg;
50
51
52
53
54 private FtpSession session;
55
56
57
58
59
60 private Object object;
61
62
63
64
65 private FtpCommandCode extraNextCommand = null;
66
67
68
69
70
71
72
73
74 public void setArgs(SessionInterface session, String command, String arg,
75 @SuppressWarnings("rawtypes") Enum code) {
76 this.session = (FtpSession) session;
77 this.command = command;
78 this.arg = arg;
79 this.code = (FtpCommandCode) code;
80 }
81
82
83
84
85
86
87
88
89
90 public void setExtraNextCommand(@SuppressWarnings("rawtypes") Enum extraNextCommand) {
91 if (extraNextCommand != FtpCommandCode.NOOP) {
92 this.extraNextCommand = (FtpCommandCode) extraNextCommand;
93 } else {
94 this.extraNextCommand = null;
95 }
96 }
97
98
99
100
101
102
103
104
105 public boolean isNextCommandValid(CommandInterface newCommandArg) {
106 AbstractCommand newCommand = (AbstractCommand) newCommandArg;
107 Class<? extends AbstractCommand> newClass = newCommand.getClass();
108
109 if (FtpCommandCode.isSpecialCommand(newCommand.getCode())) {
110 return true;
111 }
112 if (extraNextCommand != null) {
113 if (extraNextCommand.command == newClass) {
114 return true;
115 }
116 if (code.nextValids != null && code.nextValids.length > 0) {
117 for (Class<?> nextValid: code.nextValids) {
118 if (nextValid == newClass) {
119 return true;
120 }
121 }
122 }
123 return false;
124 }
125 if (code.nextValids == null || code.nextValids.length == 0) {
126
127 return true;
128 }
129 for (Class<?> nextValid: code.nextValids) {
130 if (nextValid == newClass) {
131 return true;
132 }
133 }
134 return false;
135 }
136
137 public Object getObject() {
138 return object;
139 }
140
141 public void setObject(Object object) {
142 this.object = object;
143 }
144
145 public String getArg() {
146 return arg;
147 }
148
149 public String[] getArgs() {
150 return arg.split(" ");
151 }
152
153 public int getValue(String argx) throws InvalidArgumentException {
154 int i = 0;
155 try {
156 i = Integer.parseInt(argx);
157 } catch (NumberFormatException e) {
158 throw new InvalidArgumentException("Not an integer", e);
159 }
160 return i;
161 }
162
163 public String getCommand() {
164 return command;
165 }
166
167 public boolean hasArg() {
168 return arg != null && arg.length() != 0;
169 }
170
171
172
173
174
175 public FtpSession getSession() {
176 return session;
177 }
178
179
180
181
182
183
184 public FtpConfiguration getConfiguration() {
185 return session.getConfiguration();
186 }
187
188 public void invalidCurrentCommand() {
189 session.getRestart().setSet(false);
190 session.setPreviousAsCurrentCommand();
191 }
192
193
194
195
196
197 public FtpCommandCode getCode() {
198 return code;
199 }
200 }