OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 177 Rev 180
Line 202... Line 202...
202
    public SimpleSyncClient(final String url) {
202
    public SimpleSyncClient(final String url) {
203
        super(url);
203
        super(url);
204
    }
204
    }
205
 
205
 
206
    public DirContent getDir(final String path) throws Exception {
206
    public DirContent getDir(final String path) throws Exception {
-
 
207
        if (path == null) {
-
 
208
            throw new IllegalArgumentException("null path");
-
 
209
        }
-
 
210
 
207
        final HttpsURLConnection con = openConnection("/getDir");
211
        final HttpsURLConnection con = openConnection("/getDir");
208
        final Response res = checkResponseCode(send(con, NetUtils.urlEncode("rp", path, "type", "json")));
212
        final Response res = checkResponseCode(send(con, NetUtils.urlEncode("rp", path, "type", "json"), false));
209
        if (!res.isSuccess())
213
        if (!res.isSuccess())
210
            return null;
214
            return null;
211
        final JSONParser p = new JSONParser(JSONParser.MODE_STRICTEST);
215
        final JSONParser p = new JSONParser(JSONParser.MODE_STRICTEST);
212
        try (final InputStream in = getInputStream(con)) {
216
        try (final InputStream in = getInputStream(con)) {
213
            return new DirContent(path, (JSONObject) p.parse(in));
217
            return new DirContent(path, (JSONObject) p.parse(in));
Line 220... Line 224...
220
    }
224
    }
221
 
225
 
222
    static private final Set<Integer> GETFILE_OK_CODES = CollectionUtils.createSet(200, 404);
226
    static private final Set<Integer> GETFILE_OK_CODES = CollectionUtils.createSet(200, 404);
223
 
227
 
224
    public Response getFile(final String path, final String fileName, final FileConsumer fileConsumer) throws IOException {
228
    public Response getFile(final String path, final String fileName, final FileConsumer fileConsumer) throws IOException {
-
 
229
        if (path == null) {
-
 
230
            throw new IllegalArgumentException("null path");
-
 
231
        }
-
 
232
        if (fileName == null) {
-
 
233
            throw new IllegalArgumentException("null fileName");
-
 
234
        }
225
        final HttpsURLConnection con = openConnection("/get");
235
        final HttpsURLConnection con = openConnection("/get");
226
        send(con, NetUtils.urlEncode("rn", fileName, "rp", path));
236
        send(con, NetUtils.urlEncode("rn", fileName, "rp", path), false);
227
        final Response res = checkResponseCode(con, GETFILE_OK_CODES);
237
        final Response res = checkResponseCode(con, GETFILE_OK_CODES);
228
        if (res.getCode() == 404) {
238
        if (res.getCode() == 404) {
229
            fileConsumer.accept(null, null);
239
            fileConsumer.accept(null, null);
230
        } else if (res.getCode() == 200) {
240
        } else if (res.getCode() == 200) {
231
            final FileAttrs fileAttrs = new FileAttrs(path, fileName, Instant.ofEpochMilli(con.getLastModified()), -1, con.getHeaderField("X-SHA256"));
241
            final FileAttrs fileAttrs = new FileAttrs(path, fileName, Instant.ofEpochMilli(con.getLastModified()), -1, con.getHeaderField("X-SHA256"));
Line 238... Line 248...
238
 
248
 
239
    // ATTN contrary to other methods, the result isn't if the request was OK : it ignores
249
    // 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
250
    // throwsException() and always throws. The return value is true if the file existed and was
241
    // saved.
251
    // saved.
242
    public boolean saveFile(final String path, final String fileName, final Path localFile) throws IOException {
252
    public boolean saveFile(final String path, final String fileName, final Path localFile) throws IOException {
-
 
253
        if (path == null) {
-
 
254
            throw new IllegalArgumentException("null path");
-
 
255
        }
-
 
256
        if (fileName == null) {
-
 
257
            throw new IllegalArgumentException("null fileName");
-
 
258
        }
-
 
259
 
243
        final AtomicBoolean missing = new AtomicBoolean(true);
260
        final AtomicBoolean missing = new AtomicBoolean(true);
244
        final Response res = this.getFile(path, fileName, (fileAttrs, in) -> {
261
        final Response res = this.getFile(path, fileName, (fileAttrs, in) -> {
245
            missing.set(fileAttrs == null);
262
            missing.set(fileAttrs == null);
246
            if (!missing.get()) {
263
            if (!missing.get()) {
247
                fileAttrs.saveFile(in, localFile);
264
                fileAttrs.saveFile(in, localFile);
Line 251... Line 268...
251
            throw new IOException("Couldn't retrieve file " + fileName);
268
            throw new IOException("Couldn't retrieve file " + fileName);
252
        return !missing.get();
269
        return !missing.get();
253
    }
270
    }
254
 
271
 
255
    public Response deleteFile(final String path, final String fileName) throws IOException {
272
    public Response deleteFile(final String path, final String fileName) throws IOException {
-
 
273
        if (path == null) {
-
 
274
            throw new IllegalArgumentException("null path");
-
 
275
        }
-
 
276
        if (fileName == null) {
-
 
277
            throw new IllegalArgumentException("null fileName");
-
 
278
        }
256
        final HttpsURLConnection con = openConnection("/delete");
279
        final HttpsURLConnection con = openConnection("/delete");
257
        return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path)));
280
        return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path), false));
258
    }
281
    }
259
 
282
 
260
    public final Response renameFile(final String path, final String fileName, final String newFileName) throws IOException {
283
    public final Response renameFile(final String path, final String fileName, final String newFileName) throws IOException {
261
        return this.renameFile(path, fileName, null, newFileName);
284
        return this.renameFile(path, fileName, null, newFileName);
262
    }
285
    }
263
 
286
 
264
    public final Response renameFile(final String path, final String fileName, final String newPath, final String newFileName) throws IOException {
287
    public final Response renameFile(final String path, final String fileName, final String newPath, final String newFileName) throws IOException {
-
 
288
        if (path == null) {
-
 
289
            throw new IllegalArgumentException("null path");
-
 
290
        }
-
 
291
        if (fileName == null) {
-
 
292
            throw new IllegalArgumentException("null fileName");
-
 
293
        }
-
 
294
        if (newPath == null) {
-
 
295
            throw new IllegalArgumentException("null newPath");
-
 
296
        }
-
 
297
        if (newFileName == null) {
-
 
298
            throw new IllegalArgumentException("null newFileName");
-
 
299
        }
265
        final HttpsURLConnection con = openConnection("/rename");
300
        final HttpsURLConnection con = openConnection("/rename");
266
        return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path, "newPath", newPath, "newName", newFileName)));
301
        return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path, "newPath", newPath, "newName", newFileName), false));
267
    }
302
    }
268
 
303
 
269
    public Response sendFile(String path, File localFile) throws IOException {
304
    public Response sendFile(String path, File localFile) throws IOException {
270
        return this.sendFile(path, localFile, false);
305
        return this.sendFile(path, localFile, false);
271
    }
306
    }
272
 
307
 
273
    public Response sendFile(String path, File localFile, final boolean overwrite) throws IOException {
308
    public Response sendFile(String path, File localFile, final boolean overwrite) throws IOException {
-
 
309
        if (path == null) {
-
 
310
            throw new IllegalArgumentException("null path");
-
 
311
        }
-
 
312
 
274
        final long size = localFile.length();
313
        final long size = localFile.length();
275
        if (size >= Integer.MAX_VALUE)
314
        if (size >= Integer.MAX_VALUE)
276
            throw new OutOfMemoryError("Required array size too large : " + size);
315
            throw new OutOfMemoryError("Required array size too large : " + size);
277
        final ByteArrayOutputStream ba = new ByteArrayOutputStream((int) size);
316
        final ByteArrayOutputStream ba = new ByteArrayOutputStream((int) size);
278
        final byte[] newsha256;
317
        final byte[] newsha256;