-
Notifications
You must be signed in to change notification settings - Fork 89
/
Main.java
380 lines (307 loc) · 14.6 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright 2010-2018 Norwegian Agency for Public Management and eGovernment (Difi)
*
* Licensed under the EUPL, Version 1.1 or – as soon they
* will be approved by the European Commission - subsequent
* versions of the EUPL (the "Licence");
*
* You may not use this work except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
*
* https://joinup.ec.europa.eu/community/eupl/og_page/eupl
*
* Unless required by applicable law or agreed to in
* writing, software distributed under the Licence is
* distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the Licence for the specific language governing
* permissions and limitations under the Licence.
*/
package eu.sendregning.oxalis;
import com.google.common.io.ByteStreams;
import com.google.inject.Key;
import com.google.inject.name.Names;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import lombok.extern.slf4j.Slf4j;
import network.oxalis.commons.certvalidator.Validator;
import network.oxalis.api.model.TransmissionIdentifier;
import network.oxalis.outbound.OxalisOutboundComponent;
import network.oxalis.vefa.peppol.common.model.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.*;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.OptionalDouble;
import java.util.concurrent.*;
import java.util.stream.Collectors;
/**
* @author Steinar O. Cook
* @author Nigel Parker
* @author Thore Johnsen
* @author erlend
*
* @author aaron-kumar
* @since 5.0.0
*/
@Slf4j
public class Main {
private static OptionSpec<String> fileSpec;
private static OptionSpec<String> sender;
private static OptionSpec<String> recipient;
private static OptionSpec<String> docType; // The PEPPOL document type (very long string)
private static OptionSpec<String> profileType; // The PEPPOL document profile
private static OptionSpec<File> evidencePath; // Path to persistent storage of evidence data
private static OptionSpec<Boolean> useRequestFactory;
private static OptionSpec<Integer> repeatCount;
private static OptionSpec<String> destinationUrl;
private static OptionSpec<File> destinationCertificate;
private static OptionSpec<String> protocol;
private static OptionSpec<Integer> maxTransmissions; // Maximum number of transmissions no matter what
private static OptionSpec<Boolean> probe;
private static OptionSpec<String> tag;
private static OptionSpec<Integer> sleep;
public static void main(String[] args) throws Exception {
OptionParser optionParser = getOptionParser();
if (args.length == 0) {
System.out.println();
optionParser.printHelpOn(System.out);
System.out.println();
System.out.println("Configure logging: java -Dlogback.configurationFile=/path/to/config.xml -jar <this_jar_file> options");
return;
}
OptionSet optionSet;
try {
optionSet = optionParser.parse(args);
} catch (Exception e) {
printErrorMessage(e.getMessage());
return;
}
// bootstraps the Oxalis outbound module
OxalisOutboundComponent oxalisOutboundComponent = new OxalisOutboundComponent();
TransmissionParameters params = new TransmissionParameters(oxalisOutboundComponent);
// Verifies the existence of a directory in which transmission evidence is stored.
File evidencePath = Main.evidencePath.value(optionSet);
if (evidencePath == null) {
evidencePath = new File("."); // Default is current directory
}
if (!evidencePath.exists() || !evidencePath.isDirectory()) {
printErrorMessage(evidencePath + " does not exist or is not a directory");
}
params.setEvidencePath(evidencePath);
// Tag
params.setTag(tag.value(optionSet));
// --- Use Factory
params.setUseFactory(useRequestFactory.value(optionSet));
// --- Recipient
String recipientId = recipient.value(optionSet);
if (recipientId != null) {
params.setReceiver(ParticipantIdentifier.of(recipientId));
}
// --- Sender
String senderId = sender.value(optionSet);
if (senderId != null) {
params.setSender(ParticipantIdentifier.of(senderId));
}
// --- Document type
if (docType != null && docType.value(optionSet) != null) {
String value = docType.value(optionSet);
params.setDocType(DocumentTypeIdentifier.of(value));
}
// --- Process type
if (profileType != null && profileType.value(optionSet) != null) {
String value = profileType.value(optionSet);
params.setProcessIdentifier(ProcessIdentifier.of(value));
}
if (probe.value(optionSet)) {
CloseableHttpClient httpClient = oxalisOutboundComponent.getInjector()
.getInstance(CloseableHttpClient.class);
try (CloseableHttpResponse response = httpClient.execute(new HttpGet(destinationUrl.value(optionSet)))) {
ByteStreams.copy(response.getEntity().getContent(), System.out);
}
} else {
// --- Destination URL, protocl and system identifier
if (optionSet.has(destinationUrl) && !probe.value(optionSet)) {
String destinationString = destinationUrl.value(optionSet);
X509Certificate certificate;
try (InputStream inputStream = new FileInputStream(destinationCertificate.value(optionSet))) {
certificate = Validator.getCertificate(inputStream);
}
params.setEndpoint(Endpoint.of(
TransportProfile.of(protocol.value(optionSet)),
URI.create(destinationString),
certificate));
}
// Retrieves the name of the file to be transmitted
String payloadFileSpec = fileSpec.value(optionSet);
List<File> files = locateFiles(payloadFileSpec);
System.out.println();
System.out.println();
int repeats = optionSet.valueOf(repeatCount);
int maximumTransmissions = optionSet.valueOf(maxTransmissions);
ExecutorService exec = oxalisOutboundComponent.getInjector()
.getInstance(Key.get(ExecutorService.class, Names.named("default")));
ExecutorCompletionService<TransmissionResult> ecs = new ExecutorCompletionService<>(exec);
long start = System.nanoTime();
int submittedTaskCount = 0;
for (File file : files) {
if (!file.isFile() || !file.canRead()) {
log.error("File " + file + " is not a file or can not be read, skipping...");
continue;
}
for (int i = 0; i < repeats; i++) {
TransmissionTask transmissionTask = new TransmissionTask(params, file);
Future<TransmissionResult> submit = ecs.submit(transmissionTask);
submittedTaskCount++;
if (submittedTaskCount > maximumTransmissions) {
log.info("Stopped submitting tasks at {} " + submittedTaskCount);
break;
}
}
if (submittedTaskCount > maximumTransmissions) {
break;
}
}
// Wait for the results to be available
List<TransmissionResult> results = new ArrayList<>();
int failed = 0;
for (int i = 0; i < submittedTaskCount; i++) {
try {
Future<TransmissionResult> future = ecs.take();
TransmissionResult transmissionResult = future.get();
results.add(transmissionResult);
} catch (InterruptedException e) {
System.err.println(e.getMessage());
} catch (ExecutionException e) {
log.error("Execution failed: {}", e.getMessage(), e);
failed++;
}
}
long elapsed = System.nanoTime() - start;
exec.shutdownNow(); // Shuts down the executor service
for (TransmissionResult transmissionResult : results) {
TransmissionIdentifier transmissionIdentifier = transmissionResult.getTransmissionIdentifier();
System.out.println(String.format("%s transmission took %s ms",
transmissionIdentifier, transmissionResult.getDuration()));
}
OptionalDouble average = results.stream().mapToLong(TransmissionResult::getDuration).average();
if (average.isPresent()) {
System.out.println("Average transmission time was " + average.getAsDouble() + "ms");
}
long elapsedInSeconds = TimeUnit.SECONDS.convert(elapsed, TimeUnit.NANOSECONDS);
System.out.println("Total time spent: " + elapsedInSeconds + "s");
System.out.println("Attempted to send " + results.size() + " files");
System.out.println("Failed transmissions: " + failed);
if (results.size() > 0 && elapsedInSeconds > 0) {
System.out.println("Transmission speed " + results.size() / elapsedInSeconds + " documents per second");
}
// Sleep if set to do so.
Integer sleepSecs = sleep.value(optionSet);
if (sleepSecs > 0) {
Thread.sleep(sleepSecs * 1000);
}
System.exit(failed == 0 ? 0 : 1);
}
}
/**
* Locates the files to load and transmit.
*
* @param payloadFileSpec specifies the name of the file or the directory holding files to be transmitted.
* @return list of eligible files.
*/
static List<File> locateFiles(String payloadFileSpec) {
List<File> files = new ArrayList<>();
if ("-".equals(payloadFileSpec)) {
// Reads list of files from stdin
try (Reader reader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(reader)) {
files = bufferedReader.lines()
.map(File::new)
.collect(Collectors.toList());
} catch (IOException e) {
log.warn(e.getMessage(), e);
}
} else {
File fileSpec = new File(payloadFileSpec);
if (!fileSpec.exists()) {
log.warn(String.format("'%s' does not exists.", fileSpec.getAbsolutePath()));
} else if (fileSpec.isDirectory()) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(payloadFileSpec), "*.{XML,xml}")) {
for (Path path : stream) {
files.add(path.toFile());
}
} catch (IOException e) {
throw new IllegalStateException(
String.format("Unable to list files %s; %s", payloadFileSpec, e.getMessage()), e);
}
} else if (fileSpec.isFile()) {
files.add(fileSpec);
} else {
log.warn(String.format("'%s' is neither file nor directory.", fileSpec.getAbsoluteFile()));
}
}
return files;
}
private static void printErrorMessage(String message) {
System.out.println();
System.out.println("*** " + message);
System.out.println();
}
static OptionParser getOptionParser() {
OptionParser optionParser = new OptionParser();
fileSpec = optionParser.accepts("f", "File(s) to be transmitted")
.withRequiredArg().ofType(String.class);
docType = optionParser.accepts("d", "Document type")
.withRequiredArg();
profileType = optionParser.accepts("p", "Profile type")
.withRequiredArg();
sender = optionParser.accepts("s", "sender [e.g. 9908:976098897]")
.withRequiredArg();
recipient = optionParser.accepts("r", "recipient [e.g. 9908:976098897]")
.withRequiredArg();
evidencePath = optionParser.accepts("e", "Evidence storage dir")
.withRequiredArg().ofType(File.class);
useRequestFactory = optionParser.accepts("factory", "Use TransmissionRequestFactory (no overrides!)")
.withOptionalArg().ofType(Boolean.class).defaultsTo(false);
repeatCount = optionParser.accepts("repeat", "Number of repeats to use ")
.withRequiredArg().ofType(Integer.class).defaultsTo(1);
probe = optionParser.accepts("probe", "Perform probing of endpoint.")
.withRequiredArg().ofType(Boolean.class).defaultsTo(false);
destinationUrl = optionParser.accepts("u", "destination URL")
.requiredIf(probe, protocol).withRequiredArg();
destinationCertificate = optionParser.accepts("cert", "Receiving AP's certificate (when overriding endpoint)")
.requiredIf(destinationUrl).withRequiredArg().ofType(File.class);
protocol = optionParser.accepts("protocol", "Protocol to be used")
.withRequiredArg().ofType(String.class).defaultsTo(TransportProfile.AS2_1_0.getIdentifier());
maxTransmissions = optionParser.accepts("m", "Max number of transmissions")
.withRequiredArg().ofType(Integer.class).defaultsTo(Integer.MAX_VALUE);
tag = optionParser.accepts("tag", "User defined tag")
.withRequiredArg();
sleep = optionParser.accepts("sleep", "Sleep standalone for x seconds after transmission.")
.withRequiredArg().ofType(Integer.class).defaultsTo(0);
return optionParser;
}
@SuppressWarnings("unused")
private static String enterPassword() {
System.out.print("Keystore password: ");
String password = null;
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))) {
password = bufferedReader.readLine();
} catch (Exception e) {
log.error(e.getMessage(), e);
System.exit(1);
}
return password;
}
}