Dépôt officiel du code source de l'ERP OpenConcerto
/trunk/OpenConcerto/src/org/openconcerto/utils/sync/SimpleSyncClient.java |
---|
1,7 → 1,7 |
/* |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
* |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved. |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved. |
* |
* The contents of this file are subject to the terms of the GNU General Public License Version 3 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a |
27,6 → 27,7 |
import java.io.IOException; |
import java.io.InputStream; |
import java.io.OutputStream; |
import java.io.Serializable; |
import java.nio.charset.StandardCharsets; |
import java.nio.file.Files; |
import java.nio.file.Path; |
52,15 → 53,15 |
public final class SimpleSyncClient extends HTTPClient { |
static protected final long getLong(final Object o) { |
protected static final long getLong(final Object o) { |
return ((Number) o).longValue(); |
} |
static protected final Instant getInstant(final Object o) { |
protected static final Instant getInstant(final Object o) { |
return Instant.ofEpochMilli(getLong(o)); |
} |
static protected abstract class BaseAttrs { |
public static abstract class BaseAttrs implements Serializable { |
private final String path; |
private final String name; |
private final Instant lastModified; |
90,8 → 91,8 |
} |
} |
static public final class DirAttrs extends BaseAttrs { |
static protected DirAttrs fromJSON(final String path, final JSONArray array) { |
public static final class DirAttrs extends BaseAttrs { |
protected static DirAttrs fromJSON(final String path, final JSONArray array) { |
return new DirAttrs(path, (String) array.get(0), getInstant(array.get(1))); |
} |
100,9 → 101,9 |
} |
} |
static public final class FileAttrs extends BaseAttrs { |
public static final class FileAttrs extends BaseAttrs { |
static protected FileAttrs fromJSON(final String path, final JSONArray array) { |
protected static FileAttrs fromJSON(final String path, final JSONArray array) { |
return new FileAttrs(path, (String) array.get(0), getInstant(array.get(2)), getLong(array.get(1)), (String) array.get(3)); |
} |
158,7 → 159,7 |
} |
} |
static public final class DirContent { |
public static final class DirContent { |
private final String path; |
private final JSONObject json; |
219,11 → 220,11 |
} |
@FunctionalInterface |
static public interface FileConsumer { |
public static interface FileConsumer { |
public void accept(FileAttrs attrs, InputStream fileStream) throws IOException; |
} |
static private final Set<Integer> GETFILE_OK_CODES = CollectionUtils.createSet(200, 404); |
private static final Set<Integer> GETFILE_OK_CODES = CollectionUtils.createSet(200, 404); |
public Response getFile(final String path, final String fileName, final FileConsumer fileConsumer) throws IOException { |
if (path == null) { |
246,6 → 247,26 |
return res; |
} |
public Integer getCounter(final String key) throws IOException { |
if (key == null) { |
throw new IllegalArgumentException("null key"); |
} |
final HttpsURLConnection con = openConnection("/getCounter"); |
send(con, NetUtils.urlEncode("key", key), false); |
final Response res = checkResponseCode(con, GETFILE_OK_CODES); |
if (res.getCode() == 200) { |
byte[] bytes = new byte[20]; |
try (final InputStream in = getInputStream(con)) { |
int r = in.read(bytes); |
String str = new String(bytes, 0, r); |
return Integer.parseInt(str); |
} |
} |
return null; |
} |
// ATTN contrary to other methods, the result isn't if the request was OK : it ignores |
// throwsException() and always throws. The return value is true if the file existed and was |
// saved. |
281,7 → 302,7 |
} |
public final Response renameFile(final String path, final String fileName, final String newFileName) throws IOException { |
return this.renameFile(path, fileName, null, newFileName); |
return this.renameFile(path, fileName, path, newFileName); |
} |
public final Response renameFile(final String path, final String fileName, final String newPath, final String newFileName) throws IOException { |
331,4 → 352,15 |
return checkResponseCode(send(con, ba.toByteArray(), true)); |
} |
public Response createDir(String path, String fileName) throws IOException { |
if (path == null) { |
throw new IllegalArgumentException("null path"); |
} |
if (fileName == null) { |
throw new IllegalArgumentException("null fileName"); |
} |
final HttpsURLConnection con = openConnection("/mkdir"); |
return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path), false)); |
} |
} |