1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.context.task;
21
22 import org.apache.commons.exec.CommandLine;
23 import org.waarp.common.logging.WaarpLogger;
24 import org.waarp.common.logging.WaarpLoggerFactory;
25 import org.waarp.icap.IcapScanFile;
26 import org.waarp.openr66.context.R66Session;
27 import org.waarp.openr66.context.task.exception.OpenR66RunnerErrorException;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public class IcapTask extends AbstractTask {
86
87
88
89 private static final WaarpLogger logger =
90 WaarpLoggerFactory.getLogger(IcapTask.class);
91
92
93
94
95
96
97
98 public IcapTask(final String argRule, final int delay,
99 final String argTransfer, final R66Session session) {
100 super(TaskType.ICAP, delay, argRule, argTransfer, session);
101 }
102
103 @Override
104 public final void run() {
105 logger.info("ICAP with " + argRule + ':' + argTransfer + " and {}",
106 session);
107 String finalname = applyTransferSubstitutions(argRule);
108 finalname = finalname.replaceAll("#([A-Z]+)#", "\\${$1}");
109 final CommandLine commandLine = new CommandLine("dummy");
110 commandLine.setSubstitutionMap(getSubstitutionMap());
111 commandLine.addArguments(finalname, false);
112 final String[] args = commandLine.getArguments();
113
114 if (args.length < 6) {
115 futureCompletion.setFailure(
116 new OpenR66RunnerErrorException("Not enough argument in ICAP"));
117 return;
118 }
119 final int status = IcapScanFile.scanFile(args);
120 switch (status) {
121 case IcapScanFile.STATUS_OK:
122 futureCompletion.setSuccess();
123 return;
124 case IcapScanFile.STATUS_BAD_ARGUMENT:
125 logger.error(
126 "ICAP Bad argument error with " + argRule + ':' + argTransfer +
127 ':' + delay + " and " + session);
128 futureCompletion.setFailure(
129 new OpenR66RunnerErrorException("ICAP Bad argument error"));
130 return;
131 case IcapScanFile.STATUS_ICAP_ISSUE:
132 logger.error(
133 "ICAP Bad protocol error with " + argRule + ':' + argTransfer +
134 ':' + delay + " and " + session);
135 futureCompletion.setFailure(
136 new OpenR66RunnerErrorException("ICAP Bad protocol error"));
137 return;
138 case IcapScanFile.STATUS_NETWORK_ISSUE:
139 logger.error(
140 "ICAP Network error with " + argRule + ':' + argTransfer + ':' +
141 delay + " and " + session);
142 futureCompletion.setFailure(
143 new OpenR66RunnerErrorException("ICAP Network error"));
144 return;
145 case IcapScanFile.STATUS_KO_SCAN:
146 if (finalizeIcapOnError(args)) {
147 return;
148 }
149
150 logger.error(
151 "ICAP KO error with " + argRule + ':' + argTransfer + ':' + delay +
152 " and " + session);
153 futureCompletion.setFailure(
154 new OpenR66RunnerErrorException("ICAP KO error"));
155 return;
156 case IcapScanFile.STATUS_KO_SCAN_POST_ACTION_ERROR:
157 logger.error(
158 "ICAP KO post error with " + argRule + ':' + argTransfer + ':' +
159 delay + " and " + session);
160 futureCompletion.setFailure(
161 new OpenR66RunnerErrorException("ICAP KO post error"));
162 return;
163 default:
164 logger.error(
165 "ICAP Unknown error with " + argRule + ':' + argTransfer + ':' +
166 delay + " and " + session);
167 futureCompletion.setFailure(
168 new OpenR66RunnerErrorException("ICAP Unknown error"));
169 }
170 }
171
172
173
174
175
176
177
178
179 private boolean finalizeIcapOnError(final String[] args) {
180 for (int i = 0; i < args.length; i++) {
181 if (IcapScanFile.ERROR_SEND_ARG.equalsIgnoreCase(args[i])) {
182 for (i++; i < args.length; i++) {
183 if (IcapScanFile.SEPARATOR_SEND.equals(args[i])) {
184 final StringBuilder newArgs = new StringBuilder();
185 for (i++; i < args.length; i++) {
186 newArgs.append(args[i]).append(' ');
187 }
188
189 final TransferTask transferTask =
190 new TransferTask(newArgs.toString(), 0, argTransfer, session);
191 transferTask.run();
192 transferTask.futureCompletion.awaitOrInterruptible();
193 if (transferTask.futureCompletion.isSuccess()) {
194 futureCompletion.setResult(
195 transferTask.futureCompletion.getResult());
196 logger.info(
197 "ICAP ended in KO on file but resend as requested is OK for {}",
198 session.getFile().getTrueFile().getAbsolutePath());
199 futureCompletion.setFailure(new OpenR66RunnerErrorException(
200 "ICAP ended in KO on file but resend as requested is OK"));
201 } else {
202 logger.error(
203 "ICAP KO with Resend in error with " + argRule + ':' +
204 argTransfer + ':' + delay + " and " + session,
205 transferTask.futureCompletion.getCause());
206 if (transferTask.futureCompletion.getCause() == null) {
207 futureCompletion.setFailure(new OpenR66RunnerErrorException(
208 "ICAP ended in KO on file and Resend is KO too"));
209 } else {
210 futureCompletion.setFailure(new OpenR66RunnerErrorException(
211 "ICAP ended in KO on file and Resend is KO too",
212 transferTask.futureCompletion.getCause()));
213 }
214 }
215 return true;
216 }
217 }
218 }
219 }
220 return false;
221 }
222
223 }