-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecurefsFileServiceClient.java
160 lines (137 loc) · 6.2 KB
/
SecurefsFileServiceClient.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
/*
* Copyright 2015 Thomas Frühbeck, fruehbeck(at)aon(dot)at.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package at.tfr.securefs.client;
import at.tfr.securefs.client.ws.FileService;
import jakarta.xml.ws.BindingProvider;
import org.apache.commons.cli.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.joda.time.DateTime;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
*
* @author Thomas Frühbeck
*/
public class SecurefsFileServiceClient implements Runnable {
private Options options = new Options();
private String fileServiceUrl = "http://localhost:8080/securefs/FileService?wsdl";
private List<Path> files = new ArrayList<>();
private boolean asyncTest = false;
private int threads = 1;
private String username = "user";
private String password = "User08154711!";
private boolean read = true, write = true, delete = true;
{
options.addOption("u", true, "Service URL, default: "+fileServiceUrl);
options.addOption("f", true, "Files to run to/from Server, comma separated list, mandatory");
options.addOption("a", true, "Asynchronous tests, default: "+asyncTest);
options.addOption("t", true, "Number of concurrent Threads, default: "+threads);
options.addOption("i", true, "Indentiy, default: "+username);
options.addOption("p", true, "Password, default: "+password);
options.addOption("r", false, "read file");
options.addOption("w", false, "write file");
options.addOption("d", false, "delete file");
}
public static void main(String[] args) throws Exception {
SecurefsFileServiceClient client = new SecurefsFileServiceClient();
try {
client.parse(args);
if (client.asyncTest) {
ExecutorService executor = Executors.newFixedThreadPool(client.threads);
for (int i = 0; i < client.threads; i++) {
executor.submit(client);
}
executor.shutdown();
executor.awaitTermination(10, TimeUnit.MINUTES);
} else {
client.run();
}
} catch (Throwable e) {
HelpFormatter hf = new HelpFormatter();
hf.printHelp(SecurefsFileServiceClient.class.getSimpleName(), client.options);
e.printStackTrace();
}
}
public void run() {
DateTime start = new DateTime();
try {
FileService svc = new FileService(new URL(fileServiceUrl));
BindingProvider binding = (BindingProvider)svc.getFileServicePort();
binding.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
binding.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
for (Path path : files) {
if (write) {
if (!path.toFile().exists()) {
System.err.println(Thread.currentThread()+": NoSuchFile: "+path+ " currentWorkdir="+Paths.get("./").toAbsolutePath());
continue;
}
System.out.println(Thread.currentThread()+": Sending file: "+ start + " : " + path);
svc.getFileServicePort().write(path.toString(), IOUtils.toByteArray(Files.newInputStream(path)));
}
Path out = path.resolveSibling(path.getFileName()
+ (asyncTest ? "." + Thread.currentThread().getId() : "") + ".out");
if (read) {
System.out.println(Thread.currentThread()+": Reading file: "+ new DateTime() + " : " + out);
if (out.getParent() != null) {
Files.createDirectories(out.getParent());
}
byte[] arr = svc.getFileServicePort().read(path.toString());
IOUtils.write(arr, Files.newOutputStream(out));
}
if (write && read) {
long inputChk = FileUtils.checksumCRC32(path.toFile());
long outputChk = FileUtils.checksumCRC32(out.toFile());
if (inputChk != outputChk) {
throw new IOException(Thread.currentThread()+": Checksum Failed: failure to write/read: in=" + path + ", out=" + out);
}
System.out.println(Thread.currentThread()+": Checked Checksums: "+ new DateTime() + " : " + inputChk + " / " + outputChk);
}
if (delete) {
boolean deleted = svc.getFileServicePort().delete(path.toString());
if (!deleted) {
throw new IOException(Thread.currentThread()+": Delete Failed: failure to delete: in=" + path);
} else {
System.out.println(Thread.currentThread()+": Deleted File: in=" + path);
}
}
}
} catch (Throwable t) {
t.printStackTrace();
throw new RuntimeException(t);
}
}
public void parse(String[] args) throws ParseException {
CommandLineParser clp = new DefaultParser();
CommandLine cmd = clp.parse(options, args);
// validate:
if (!cmd.hasOption("f")) {
throw new ParseException("file name is mandatory");
}
fileServiceUrl = cmd.getOptionValue("u", fileServiceUrl);
if (cmd.hasOption("f")) {
Arrays.stream(cmd.getOptionValue("f").split(",")).forEach(f -> files.add(Paths.get(f)));
}
asyncTest = Boolean.parseBoolean(cmd.getOptionValue("a", "false"));
threads = Integer.parseInt(cmd.getOptionValue("t", "1"));
if (cmd.hasOption("r") || cmd.hasOption("w") || cmd.hasOption("d")) {
read = write = delete = false;
read = cmd.hasOption("r");
write = cmd.hasOption("w");
delete = cmd.hasOption("d");
}
}
}