174 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils.sync;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.utils.CollectionUtils;
|
|
|
17 |
import org.openconcerto.utils.MessageDigestUtils;
|
177 |
ilm |
18 |
import org.openconcerto.utils.NetUtils;
|
174 |
ilm |
19 |
import org.openconcerto.utils.StreamUtils;
|
177 |
ilm |
20 |
import org.openconcerto.utils.net.HTTPClient;
|
174 |
ilm |
21 |
|
177 |
ilm |
22 |
import java.io.BufferedInputStream;
|
174 |
ilm |
23 |
import java.io.BufferedOutputStream;
|
177 |
ilm |
24 |
import java.io.ByteArrayOutputStream;
|
174 |
ilm |
25 |
import java.io.File;
|
177 |
ilm |
26 |
import java.io.FileInputStream;
|
174 |
ilm |
27 |
import java.io.IOException;
|
|
|
28 |
import java.io.InputStream;
|
|
|
29 |
import java.io.OutputStream;
|
|
|
30 |
import java.nio.charset.StandardCharsets;
|
|
|
31 |
import java.nio.file.Files;
|
|
|
32 |
import java.nio.file.Path;
|
|
|
33 |
import java.nio.file.StandardCopyOption;
|
|
|
34 |
import java.nio.file.attribute.FileTime;
|
|
|
35 |
import java.security.DigestOutputStream;
|
|
|
36 |
import java.security.MessageDigest;
|
|
|
37 |
import java.time.Instant;
|
|
|
38 |
import java.util.ArrayList;
|
|
|
39 |
import java.util.Base64;
|
|
|
40 |
import java.util.Collections;
|
|
|
41 |
import java.util.List;
|
|
|
42 |
import java.util.Set;
|
|
|
43 |
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
44 |
import java.util.function.BiFunction;
|
|
|
45 |
import java.util.function.Predicate;
|
|
|
46 |
|
|
|
47 |
import javax.net.ssl.HttpsURLConnection;
|
|
|
48 |
|
|
|
49 |
import net.minidev.json.JSONArray;
|
|
|
50 |
import net.minidev.json.JSONObject;
|
|
|
51 |
import net.minidev.json.parser.JSONParser;
|
|
|
52 |
|
177 |
ilm |
53 |
public final class SimpleSyncClient extends HTTPClient {
|
174 |
ilm |
54 |
|
|
|
55 |
static protected final long getLong(final Object o) {
|
|
|
56 |
return ((Number) o).longValue();
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
static protected final Instant getInstant(final Object o) {
|
|
|
60 |
return Instant.ofEpochMilli(getLong(o));
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
static protected abstract class BaseAttrs {
|
|
|
64 |
private final String path;
|
|
|
65 |
private final String name;
|
|
|
66 |
private final Instant lastModified;
|
|
|
67 |
|
|
|
68 |
protected BaseAttrs(final String path, final String name, final Instant lastModified) {
|
|
|
69 |
super();
|
|
|
70 |
this.path = path;
|
|
|
71 |
this.name = name;
|
|
|
72 |
this.lastModified = lastModified;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public final String getPath() {
|
|
|
76 |
return this.path;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public final String getName() {
|
|
|
80 |
return this.name;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public final Instant getLastModified() {
|
|
|
84 |
return this.lastModified;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
@Override
|
|
|
88 |
public String toString() {
|
|
|
89 |
return this.getClass().getSimpleName() + " '" + this.getName() + "'";
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
static public final class DirAttrs extends BaseAttrs {
|
|
|
94 |
static protected DirAttrs fromJSON(final String path, final JSONArray array) {
|
|
|
95 |
return new DirAttrs(path, (String) array.get(0), getInstant(array.get(1)));
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
protected DirAttrs(final String path, String name, Instant lastModified) {
|
|
|
99 |
super(path, name, lastModified);
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
static public final class FileAttrs extends BaseAttrs {
|
|
|
104 |
|
|
|
105 |
static protected FileAttrs fromJSON(final String path, final JSONArray array) {
|
|
|
106 |
return new FileAttrs(path, (String) array.get(0), getInstant(array.get(2)), getLong(array.get(1)), (String) array.get(3));
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
private final long size;
|
|
|
110 |
private final String sha256;
|
|
|
111 |
|
|
|
112 |
protected FileAttrs(final String path, String name, Instant lastModified, long size, String sha256) {
|
|
|
113 |
super(path, name, lastModified);
|
|
|
114 |
this.size = size;
|
|
|
115 |
this.sha256 = sha256;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
public final long getSize() {
|
|
|
119 |
return this.size;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
public final String getSHA256() {
|
|
|
123 |
return this.sha256;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
public final void saveFile(final InputStream in, final Path localFile) throws IOException {
|
|
|
127 |
// Save to temporary file to avoid overwriting old file with a new invalid one. In
|
|
|
128 |
// same folder for the move.
|
|
|
129 |
final Path tmpFile = Files.createTempFile(localFile.getParent(), "partial", null);
|
|
|
130 |
try {
|
|
|
131 |
final MessageDigest md = this.getSHA256() == null ? null : MessageDigestUtils.getSHA256();
|
|
|
132 |
try (final BufferedOutputStream fileStream = new BufferedOutputStream(Files.newOutputStream(tmpFile));
|
|
|
133 |
//
|
|
|
134 |
final OutputStream out = md == null ? fileStream : new DigestOutputStream(fileStream, md)) {
|
|
|
135 |
StreamUtils.copy(in, out);
|
|
|
136 |
}
|
|
|
137 |
if (this.getSize() >= 0) {
|
|
|
138 |
final long savedSize = Files.size(tmpFile);
|
|
|
139 |
if (savedSize != this.getSize())
|
|
|
140 |
throw new IOException("Expected " + this.getSize() + " bytes but saved " + savedSize);
|
|
|
141 |
}
|
|
|
142 |
if (md != null) {
|
|
|
143 |
final String savedHash = MessageDigestUtils.getHashString(md);
|
|
|
144 |
if (!savedHash.equalsIgnoreCase(this.getSHA256()))
|
|
|
145 |
throw new IOException("Expected hash was " + this.getSHA256() + " but saved " + savedHash);
|
|
|
146 |
}
|
|
|
147 |
Files.move(tmpFile, localFile, StandardCopyOption.REPLACE_EXISTING);
|
|
|
148 |
} finally {
|
|
|
149 |
Files.deleteIfExists(tmpFile);
|
|
|
150 |
}
|
|
|
151 |
if (this.getLastModified().compareTo(Instant.EPOCH) > 0)
|
|
|
152 |
Files.setLastModifiedTime(localFile, FileTime.from(this.getLastModified()));
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
@Override
|
|
|
156 |
public String toString() {
|
|
|
157 |
return super.toString() + " of size " + getSize();
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
static public final class DirContent {
|
|
|
162 |
private final String path;
|
|
|
163 |
private final JSONObject json;
|
|
|
164 |
|
|
|
165 |
protected DirContent(final String path, JSONObject json) {
|
|
|
166 |
super();
|
|
|
167 |
this.path = path;
|
|
|
168 |
this.json = json;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
public final List<FileAttrs> getFiles() {
|
|
|
172 |
return this.getFiles(null);
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
public final List<FileAttrs> getFiles(final Predicate<String> namePredicate) {
|
|
|
176 |
return this.getContent("files", namePredicate, FileAttrs::fromJSON);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
public final List<DirAttrs> getDirs() {
|
|
|
180 |
return this.getDirs(null);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
public final List<DirAttrs> getDirs(final Predicate<String> namePredicate) {
|
|
|
184 |
return this.getContent("dirs", namePredicate, DirAttrs::fromJSON);
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
protected final <T extends BaseAttrs> List<T> getContent(final String key, final Predicate<String> namePredicate, final BiFunction<String, JSONArray, T> create) {
|
|
|
188 |
final JSONArray files = (JSONArray) this.json.get(key);
|
|
|
189 |
if (files == null)
|
|
|
190 |
return Collections.emptyList();
|
|
|
191 |
final List<T> res = new ArrayList<>();
|
|
|
192 |
for (final Object f : files) {
|
|
|
193 |
final JSONArray array = (JSONArray) f;
|
|
|
194 |
if (namePredicate == null || namePredicate.test((String) array.get(0))) {
|
|
|
195 |
res.add(create.apply(this.path, array));
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
return res;
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
public SimpleSyncClient(final String url) {
|
177 |
ilm |
203 |
super(url);
|
174 |
ilm |
204 |
}
|
|
|
205 |
|
|
|
206 |
public DirContent getDir(final String path) throws Exception {
|
|
|
207 |
final HttpsURLConnection con = openConnection("/getDir");
|
177 |
ilm |
208 |
final Response res = checkResponseCode(send(con, NetUtils.urlEncode("rp", path, "type", "json")));
|
174 |
ilm |
209 |
if (!res.isSuccess())
|
|
|
210 |
return null;
|
|
|
211 |
final JSONParser p = new JSONParser(JSONParser.MODE_STRICTEST);
|
|
|
212 |
try (final InputStream in = getInputStream(con)) {
|
|
|
213 |
return new DirContent(path, (JSONObject) p.parse(in));
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
@FunctionalInterface
|
|
|
218 |
static public interface FileConsumer {
|
|
|
219 |
public void accept(FileAttrs attrs, InputStream fileStream) throws IOException;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
static private final Set<Integer> GETFILE_OK_CODES = CollectionUtils.createSet(200, 404);
|
|
|
223 |
|
|
|
224 |
public Response getFile(final String path, final String fileName, final FileConsumer fileConsumer) throws IOException {
|
|
|
225 |
final HttpsURLConnection con = openConnection("/get");
|
177 |
ilm |
226 |
send(con, NetUtils.urlEncode("rn", fileName, "rp", path));
|
174 |
ilm |
227 |
final Response res = checkResponseCode(con, GETFILE_OK_CODES);
|
|
|
228 |
if (res.getCode() == 404) {
|
|
|
229 |
fileConsumer.accept(null, null);
|
|
|
230 |
} else if (res.getCode() == 200) {
|
|
|
231 |
final FileAttrs fileAttrs = new FileAttrs(path, fileName, Instant.ofEpochMilli(con.getLastModified()), -1, con.getHeaderField("X-SHA256"));
|
|
|
232 |
try (final InputStream in = getInputStream(con)) {
|
|
|
233 |
fileConsumer.accept(fileAttrs, in);
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
return res;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
// ATTN contrary to other methods, the result isn't if the request was OK : it ignores
|
|
|
240 |
// throwsException() and always throws. The return value is true if the file existed and was
|
|
|
241 |
// saved.
|
|
|
242 |
public boolean saveFile(final String path, final String fileName, final Path localFile) throws IOException {
|
|
|
243 |
final AtomicBoolean missing = new AtomicBoolean(true);
|
|
|
244 |
final Response res = this.getFile(path, fileName, (fileAttrs, in) -> {
|
|
|
245 |
missing.set(fileAttrs == null);
|
|
|
246 |
if (!missing.get()) {
|
|
|
247 |
fileAttrs.saveFile(in, localFile);
|
|
|
248 |
}
|
|
|
249 |
});
|
|
|
250 |
if (!res.isSuccess())
|
|
|
251 |
throw new IOException("Couldn't retrieve file " + fileName);
|
|
|
252 |
return !missing.get();
|
|
|
253 |
}
|
|
|
254 |
|
177 |
ilm |
255 |
public Response deleteFile(final String path, final String fileName) throws IOException {
|
174 |
ilm |
256 |
final HttpsURLConnection con = openConnection("/delete");
|
177 |
ilm |
257 |
return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path)));
|
174 |
ilm |
258 |
}
|
|
|
259 |
|
177 |
ilm |
260 |
public final Response renameFile(final String path, final String fileName, final String newFileName) throws IOException {
|
|
|
261 |
return this.renameFile(path, fileName, null, newFileName);
|
|
|
262 |
}
|
174 |
ilm |
263 |
|
177 |
ilm |
264 |
public final Response renameFile(final String path, final String fileName, final String newPath, final String newFileName) throws IOException {
|
|
|
265 |
final HttpsURLConnection con = openConnection("/rename");
|
|
|
266 |
return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path, "newPath", newPath, "newName", newFileName)));
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
public Response sendFile(String path, File localFile) throws IOException {
|
|
|
270 |
return this.sendFile(path, localFile, false);
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
public Response sendFile(String path, File localFile, final boolean overwrite) throws IOException {
|
|
|
274 |
final long size = localFile.length();
|
|
|
275 |
if (size >= Integer.MAX_VALUE)
|
|
|
276 |
throw new OutOfMemoryError("Required array size too large : " + size);
|
|
|
277 |
final ByteArrayOutputStream ba = new ByteArrayOutputStream((int) size);
|
|
|
278 |
final byte[] newsha256;
|
|
|
279 |
// compute digest at the same time to protect against race conditions
|
|
|
280 |
try (final InputStream ins = new BufferedInputStream(new FileInputStream(localFile))) {
|
|
|
281 |
newsha256 = MessageDigestUtils.getHash(MessageDigestUtils.getSHA256(), ins, ba);
|
|
|
282 |
}
|
|
|
283 |
|
174 |
ilm |
284 |
final HttpsURLConnection con = openConnection("/put");
|
|
|
285 |
// We use Base64 because headers are not supporting UTF8
|
|
|
286 |
con.setRequestProperty("X_FILENAME_B64", Base64.getEncoder().encodeToString(localFile.getName().getBytes(StandardCharsets.UTF_8)));
|
|
|
287 |
con.setRequestProperty("X_PATH_B64", Base64.getEncoder().encodeToString(path.getBytes(StandardCharsets.UTF_8)));
|
177 |
ilm |
288 |
con.setRequestProperty("X-OVERWRITE", Boolean.toString(overwrite));
|
174 |
ilm |
289 |
con.setRequestProperty("X_FILESIZE", String.valueOf(size));
|
|
|
290 |
con.setRequestProperty("X_SHA256", HashWriter.bytesToHex(newsha256));
|
|
|
291 |
con.setRequestProperty("X-Last-Modified-ms", String.valueOf(localFile.lastModified()));
|
|
|
292 |
|
177 |
ilm |
293 |
return checkResponseCode(send(con, ba.toByteArray(), true));
|
174 |
ilm |
294 |
}
|
|
|
295 |
}
|