Line 1... |
Line 1... |
1 |
/*
|
1 |
/*
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
3 |
*
|
3 |
*
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
5 |
*
|
5 |
*
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
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
|
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
|
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.
|
9 |
* language governing permissions and limitations under the License.
|
Line 25... |
Line 25... |
25 |
import java.io.File;
|
25 |
import java.io.File;
|
26 |
import java.io.FileInputStream;
|
26 |
import java.io.FileInputStream;
|
27 |
import java.io.IOException;
|
27 |
import java.io.IOException;
|
28 |
import java.io.InputStream;
|
28 |
import java.io.InputStream;
|
29 |
import java.io.OutputStream;
|
29 |
import java.io.OutputStream;
|
- |
|
30 |
import java.io.Serializable;
|
30 |
import java.nio.charset.StandardCharsets;
|
31 |
import java.nio.charset.StandardCharsets;
|
31 |
import java.nio.file.Files;
|
32 |
import java.nio.file.Files;
|
32 |
import java.nio.file.Path;
|
33 |
import java.nio.file.Path;
|
33 |
import java.nio.file.StandardCopyOption;
|
34 |
import java.nio.file.StandardCopyOption;
|
34 |
import java.nio.file.attribute.FileTime;
|
35 |
import java.nio.file.attribute.FileTime;
|
Line 50... |
Line 51... |
50 |
import net.minidev.json.JSONObject;
|
51 |
import net.minidev.json.JSONObject;
|
51 |
import net.minidev.json.parser.JSONParser;
|
52 |
import net.minidev.json.parser.JSONParser;
|
52 |
|
53 |
|
53 |
public final class SimpleSyncClient extends HTTPClient {
|
54 |
public final class SimpleSyncClient extends HTTPClient {
|
54 |
|
55 |
|
55 |
static protected final long getLong(final Object o) {
|
56 |
protected static final long getLong(final Object o) {
|
56 |
return ((Number) o).longValue();
|
57 |
return ((Number) o).longValue();
|
57 |
}
|
58 |
}
|
58 |
|
59 |
|
59 |
static protected final Instant getInstant(final Object o) {
|
60 |
protected static final Instant getInstant(final Object o) {
|
60 |
return Instant.ofEpochMilli(getLong(o));
|
61 |
return Instant.ofEpochMilli(getLong(o));
|
61 |
}
|
62 |
}
|
62 |
|
63 |
|
63 |
static protected abstract class BaseAttrs {
|
64 |
public static abstract class BaseAttrs implements Serializable {
|
64 |
private final String path;
|
65 |
private final String path;
|
65 |
private final String name;
|
66 |
private final String name;
|
66 |
private final Instant lastModified;
|
67 |
private final Instant lastModified;
|
67 |
|
68 |
|
68 |
protected BaseAttrs(final String path, final String name, final Instant lastModified) {
|
69 |
protected BaseAttrs(final String path, final String name, final Instant lastModified) {
|
Line 88... |
Line 89... |
88 |
public String toString() {
|
89 |
public String toString() {
|
89 |
return this.getClass().getSimpleName() + " '" + this.getName() + "'";
|
90 |
return this.getClass().getSimpleName() + " '" + this.getName() + "'";
|
90 |
}
|
91 |
}
|
91 |
}
|
92 |
}
|
92 |
|
93 |
|
93 |
static public final class DirAttrs extends BaseAttrs {
|
94 |
public static final class DirAttrs extends BaseAttrs {
|
94 |
static protected DirAttrs fromJSON(final String path, final JSONArray array) {
|
95 |
protected static DirAttrs fromJSON(final String path, final JSONArray array) {
|
95 |
return new DirAttrs(path, (String) array.get(0), getInstant(array.get(1)));
|
96 |
return new DirAttrs(path, (String) array.get(0), getInstant(array.get(1)));
|
96 |
}
|
97 |
}
|
97 |
|
98 |
|
98 |
protected DirAttrs(final String path, String name, Instant lastModified) {
|
99 |
protected DirAttrs(final String path, String name, Instant lastModified) {
|
99 |
super(path, name, lastModified);
|
100 |
super(path, name, lastModified);
|
100 |
}
|
101 |
}
|
101 |
}
|
102 |
}
|
102 |
|
103 |
|
103 |
static public final class FileAttrs extends BaseAttrs {
|
104 |
public static final class FileAttrs extends BaseAttrs {
|
104 |
|
105 |
|
105 |
static protected FileAttrs fromJSON(final String path, final JSONArray array) {
|
106 |
protected static 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 |
return new FileAttrs(path, (String) array.get(0), getInstant(array.get(2)), getLong(array.get(1)), (String) array.get(3));
|
107 |
}
|
108 |
}
|
108 |
|
109 |
|
109 |
private final long size;
|
110 |
private final long size;
|
110 |
private final String sha256;
|
111 |
private final String sha256;
|
Line 156... |
Line 157... |
156 |
public String toString() {
|
157 |
public String toString() {
|
157 |
return super.toString() + " of size " + getSize();
|
158 |
return super.toString() + " of size " + getSize();
|
158 |
}
|
159 |
}
|
159 |
}
|
160 |
}
|
160 |
|
161 |
|
161 |
static public final class DirContent {
|
162 |
public static final class DirContent {
|
162 |
private final String path;
|
163 |
private final String path;
|
163 |
private final JSONObject json;
|
164 |
private final JSONObject json;
|
164 |
|
165 |
|
165 |
protected DirContent(final String path, JSONObject json) {
|
166 |
protected DirContent(final String path, JSONObject json) {
|
166 |
super();
|
167 |
super();
|
Line 217... |
Line 218... |
217 |
return new DirContent(path, (JSONObject) p.parse(in));
|
218 |
return new DirContent(path, (JSONObject) p.parse(in));
|
218 |
}
|
219 |
}
|
219 |
}
|
220 |
}
|
220 |
|
221 |
|
221 |
@FunctionalInterface
|
222 |
@FunctionalInterface
|
222 |
static public interface FileConsumer {
|
223 |
public static interface FileConsumer {
|
223 |
public void accept(FileAttrs attrs, InputStream fileStream) throws IOException;
|
224 |
public void accept(FileAttrs attrs, InputStream fileStream) throws IOException;
|
224 |
}
|
225 |
}
|
225 |
|
226 |
|
226 |
static private final Set<Integer> GETFILE_OK_CODES = CollectionUtils.createSet(200, 404);
|
227 |
private static final Set<Integer> GETFILE_OK_CODES = CollectionUtils.createSet(200, 404);
|
227 |
|
228 |
|
228 |
public Response getFile(final String path, final String fileName, final FileConsumer fileConsumer) throws IOException {
|
229 |
public Response getFile(final String path, final String fileName, final FileConsumer fileConsumer) throws IOException {
|
229 |
if (path == null) {
|
230 |
if (path == null) {
|
230 |
throw new IllegalArgumentException("null path");
|
231 |
throw new IllegalArgumentException("null path");
|
231 |
}
|
232 |
}
|
Line 244... |
Line 245... |
244 |
}
|
245 |
}
|
245 |
}
|
246 |
}
|
246 |
return res;
|
247 |
return res;
|
247 |
}
|
248 |
}
|
248 |
|
249 |
|
- |
|
250 |
public Integer getCounter(final String key) throws IOException {
|
- |
|
251 |
if (key == null) {
|
- |
|
252 |
throw new IllegalArgumentException("null key");
|
- |
|
253 |
}
|
- |
|
254 |
final HttpsURLConnection con = openConnection("/getCounter");
|
- |
|
255 |
send(con, NetUtils.urlEncode("key", key), false);
|
- |
|
256 |
final Response res = checkResponseCode(con, GETFILE_OK_CODES);
|
- |
|
257 |
if (res.getCode() == 200) {
|
- |
|
258 |
byte[] bytes = new byte[20];
|
- |
|
259 |
try (final InputStream in = getInputStream(con)) {
|
- |
|
260 |
int r = in.read(bytes);
|
- |
|
261 |
String str = new String(bytes, 0, r);
|
- |
|
262 |
return Integer.parseInt(str);
|
- |
|
263 |
}
|
- |
|
264 |
|
- |
|
265 |
}
|
- |
|
266 |
return null;
|
- |
|
267 |
|
- |
|
268 |
}
|
- |
|
269 |
|
249 |
// ATTN contrary to other methods, the result isn't if the request was OK : it ignores
|
270 |
// ATTN contrary to other methods, the result isn't if the request was OK : it ignores
|
250 |
// throwsException() and always throws. The return value is true if the file existed and was
|
271 |
// throwsException() and always throws. The return value is true if the file existed and was
|
251 |
// saved.
|
272 |
// saved.
|
252 |
public boolean saveFile(final String path, final String fileName, final Path localFile) throws IOException {
|
273 |
public boolean saveFile(final String path, final String fileName, final Path localFile) throws IOException {
|
253 |
if (path == null) {
|
274 |
if (path == null) {
|
Line 279... |
Line 300... |
279 |
final HttpsURLConnection con = openConnection("/delete");
|
300 |
final HttpsURLConnection con = openConnection("/delete");
|
280 |
return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path), false));
|
301 |
return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path), false));
|
281 |
}
|
302 |
}
|
282 |
|
303 |
|
283 |
public final Response renameFile(final String path, final String fileName, final String newFileName) throws IOException {
|
304 |
public final Response renameFile(final String path, final String fileName, final String newFileName) throws IOException {
|
284 |
return this.renameFile(path, fileName, null, newFileName);
|
305 |
return this.renameFile(path, fileName, path, newFileName);
|
285 |
}
|
306 |
}
|
286 |
|
307 |
|
287 |
public final Response renameFile(final String path, final String fileName, final String newPath, final String newFileName) throws IOException {
|
308 |
public final Response renameFile(final String path, final String fileName, final String newPath, final String newFileName) throws IOException {
|
288 |
if (path == null) {
|
309 |
if (path == null) {
|
289 |
throw new IllegalArgumentException("null path");
|
310 |
throw new IllegalArgumentException("null path");
|
Line 329... |
Line 350... |
329 |
con.setRequestProperty("X_SHA256", HashWriter.bytesToHex(newsha256));
|
350 |
con.setRequestProperty("X_SHA256", HashWriter.bytesToHex(newsha256));
|
330 |
con.setRequestProperty("X-Last-Modified-ms", String.valueOf(localFile.lastModified()));
|
351 |
con.setRequestProperty("X-Last-Modified-ms", String.valueOf(localFile.lastModified()));
|
331 |
|
352 |
|
332 |
return checkResponseCode(send(con, ba.toByteArray(), true));
|
353 |
return checkResponseCode(send(con, ba.toByteArray(), true));
|
333 |
}
|
354 |
}
|
- |
|
355 |
|
- |
|
356 |
public Response createDir(String path, String fileName) throws IOException {
|
- |
|
357 |
if (path == null) {
|
- |
|
358 |
throw new IllegalArgumentException("null path");
|
- |
|
359 |
}
|
- |
|
360 |
if (fileName == null) {
|
- |
|
361 |
throw new IllegalArgumentException("null fileName");
|
- |
|
362 |
}
|
- |
|
363 |
final HttpsURLConnection con = openConnection("/mkdir");
|
- |
|
364 |
return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path), false));
|
- |
|
365 |
}
|
334 |
}
|
366 |
}
|