1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.http.protocol;
22
23 import org.waarp.common.command.exception.CommandAbstractException;
24 import org.waarp.common.database.data.AbstractDbData.UpdatedInfo;
25 import org.waarp.common.database.exception.WaarpDatabaseException;
26 import org.waarp.common.database.exception.WaarpDatabaseNoDataException;
27 import org.waarp.common.logging.WaarpLogger;
28 import org.waarp.common.logging.WaarpLoggerFactory;
29 import org.waarp.http.protocol.servlet.HttpAuthent;
30 import org.waarp.openr66.context.ErrorCode;
31 import org.waarp.openr66.context.R66BusinessInterface;
32 import org.waarp.openr66.context.R66FiniteDualStates;
33 import org.waarp.openr66.context.R66Session;
34 import org.waarp.openr66.context.task.exception.OpenR66RunnerErrorException;
35 import org.waarp.openr66.database.data.DbRule;
36 import org.waarp.openr66.database.data.DbTaskRunner;
37 import org.waarp.openr66.database.data.DbTaskRunner.TASKSTEP;
38 import org.waarp.openr66.pojo.Transfer;
39 import org.waarp.openr66.protocol.configuration.Configuration;
40
41 import static org.waarp.openr66.context.R66FiniteDualStates.*;
42
43
44
45
46 public class HttpSessionAbstract implements HttpSession {
47 private static final WaarpLogger logger =
48 WaarpLoggerFactory.getLogger(HttpSessionAbstract.class);
49
50 protected final R66Session session;
51 protected final HttpAuthent authent;
52
53 public HttpSessionAbstract(final HttpAuthent authent) {
54 this.session = new R66Session(false);
55 this.authent = authent;
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70 protected R66BusinessInterface checkAuthentR66Business(
71 final HttpSession httpSession, final R66Session session,
72 final HttpAuthent authent) throws IllegalArgumentException {
73 session.setBusinessObject(
74 Configuration.configuration.getR66BusinessFactory()
75 .getBusinessInterface(session));
76 final R66BusinessInterface business = session.getBusinessObject();
77 session.newState(R66FiniteDualStates.STARTUP);
78 try {
79 if (business != null) {
80 business.checkAtStartup(session);
81 }
82 } catch (final OpenR66RunnerErrorException e) {
83 httpSession.error(e, business);
84 }
85 authent.checkAuthent(this, session);
86 try {
87 if (business != null) {
88 business.checkAtAuthentication(session);
89 }
90 } catch (final OpenR66RunnerErrorException e) {
91 httpSession.error(e, business);
92 }
93 session.newState(R66FiniteDualStates.AUTHENTR);
94 session.newState(R66FiniteDualStates.AUTHENTD);
95 return business;
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 protected final DbTaskRunner getDbTaskRunner(final String user,
115 final String filename,
116 final String rulename,
117 final long identifier,
118 final String comment,
119 final int chunkSize,
120 final R66BusinessInterface business,
121 final boolean uploadMode)
122 throws IllegalArgumentException {
123 session.newState(REQUESTR);
124 session.setBlockSize(chunkSize);
125
126 DbRule rule = null;
127 try {
128 rule = new DbRule(rulename);
129 } catch (final WaarpDatabaseException e) {
130 error(e, business);
131 throw new IllegalArgumentException(e);
132 }
133 final String requested = Configuration.configuration.getHostId();
134 DbTaskRunner runner = null;
135 try {
136
137 runner = new DbTaskRunner(session, rule, identifier, user, requested);
138 runner.setSender(!uploadMode);
139 logger.debug("{} {} {}", identifier, user, requested);
140 if (runner.isAllDone()) {
141 error(new IllegalArgumentException("Already finished"), business);
142 }
143 } catch (final WaarpDatabaseNoDataException e) {
144
145 try {
146 final Transfer transfer =
147 new Transfer(user, rulename, rule.getMode(), !uploadMode, filename,
148 comment, chunkSize);
149 transfer.setRequested(requested);
150 transfer.setRequester(user);
151 transfer.setId(identifier);
152 runner = new DbTaskRunner(transfer);
153 runner.insert();
154 runner.select();
155 runner.setSender(!uploadMode);
156 logger.debug("{} {} {}", identifier, user, requested);
157 } catch (final WaarpDatabaseException ex) {
158 error(ex, business);
159 throw new IllegalArgumentException(e);
160 }
161 } catch (final WaarpDatabaseException e) {
162 error(e, business);
163 throw new IllegalArgumentException(e);
164 }
165 runner.setOriginalFilename(filename);
166 runner.setFilename(filename);
167 runner.restart(false);
168 try {
169 session.setRunner(runner);
170 session.setFileBeforePreRunner();
171 } catch (final OpenR66RunnerErrorException e) {
172 if (runner.getErrorInfo() == ErrorCode.InitOk ||
173 runner.getErrorInfo() == ErrorCode.PreProcessingOk ||
174 runner.getErrorInfo() == ErrorCode.TransferOk) {
175 runner.setErrorExecutionStatus(ErrorCode.ExternalOp);
176 }
177 try {
178 runner.saveStatus();
179 } catch (final OpenR66RunnerErrorException e1) {
180 logger.error("Cannot save Status: " + runner + ": {}", e1.getMessage());
181 }
182 runner.clean();
183 error(e, business);
184 }
185 session.setReady(true);
186 session.newState(REQUESTD);
187 try {
188 runner.saveStatus();
189 } catch (final OpenR66RunnerErrorException e1) {
190 logger.error("Cannot save Status: " + runner + ": {}", e1.getMessage());
191 }
192 return runner;
193 }
194
195 @Override
196 public void error(final Exception e, final R66BusinessInterface business)
197 throws IllegalArgumentException {
198 logger.error(e.getMessage());
199 if (business != null) {
200 business.checkAtError(session);
201 }
202 session.newState(R66FiniteDualStates.ERROR);
203 if (session.getRunner() != null) {
204 final DbTaskRunner runner = session.getRunner();
205 runner.setErrorExecutionStatus(ErrorCode.TransferError);
206 runner.setErrorTask();
207 try {
208 runner.run();
209 runner.saveStatus();
210 } catch (final OpenR66RunnerErrorException e1) {
211 logger.debug(e1);
212 }
213 }
214 throw new IllegalArgumentException(e);
215 }
216
217
218
219
220
221
222
223
224
225 protected final void preTasks(final R66BusinessInterface business,
226 final DbTaskRunner runner)
227 throws IllegalArgumentException {
228 runner.reset();
229 runner.changeUpdatedInfo(UpdatedInfo.RUNNING);
230 try {
231 runner.saveStatus();
232 } catch (final OpenR66RunnerErrorException e) {
233 error(e, business);
234 }
235
236 try {
237 session.setFileBeforePreRunner();
238 if (runner.getStep() <= TASKSTEP.PRETASK.ordinal()) {
239 runner.setPreTask();
240 runner.saveStatus();
241 runner.run();
242 session.setFileAfterPreRunner(true);
243 }
244 } catch (final OpenR66RunnerErrorException e) {
245 error(e, business);
246 } catch (final CommandAbstractException e) {
247 error(e, business);
248 }
249 session.newState(DATAR);
250 try {
251 runner.saveStatus();
252 } catch (final OpenR66RunnerErrorException e1) {
253 logger.error("Cannot save Status: " + runner + ": {}", e1.getMessage());
254 }
255 }
256
257
258
259
260
261
262 protected final void startTransfer(final DbTaskRunner runner) {
263 runner.setTransferTask(0);
264 try {
265 runner.saveStatus();
266 } catch (final OpenR66RunnerErrorException e1) {
267 logger.error("Cannot save Status: " + runner + ": {}", e1.getMessage());
268 }
269 }
270
271
272
273
274 protected final void runPostTask() {
275 final DbTaskRunner runner = session.getRunner();
276 runner.setPostTask();
277 try {
278 session.newState(ENDTRANSFERR);
279 logger.debug("Post actions {}", session);
280 runner.run();
281 runner.saveStatus();
282 session.newState(ENDREQUESTS);
283 } catch (final OpenR66RunnerErrorException e) {
284 error(e, session.getBusinessObject());
285 }
286 session.newState(CLOSEDCHANNEL);
287 session.partialClear();
288 runner.setAllDone();
289 try {
290 runner.saveStatus();
291 } catch (final OpenR66RunnerErrorException ignored) {
292
293 }
294 runner.clean();
295 }
296
297
298
299
300 public final DbTaskRunner getDbTaskRunner() {
301 return session.getRunner();
302 }
303 }