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 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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 {
180 ilm 207
        if (path == null) {
208
            throw new IllegalArgumentException("null path");
209
        }
210
 
174 ilm 211
        final HttpsURLConnection con = openConnection("/getDir");
180 ilm 212
        final Response res = checkResponseCode(send(con, NetUtils.urlEncode("rp", path, "type", "json"), false));
174 ilm 213
        if (!res.isSuccess())
214
            return null;
215
        final JSONParser p = new JSONParser(JSONParser.MODE_STRICTEST);
216
        try (final InputStream in = getInputStream(con)) {
217
            return new DirContent(path, (JSONObject) p.parse(in));
218
        }
219
    }
220
 
221
    @FunctionalInterface
222
    static public interface FileConsumer {
223
        public void accept(FileAttrs attrs, InputStream fileStream) throws IOException;
224
    }
225
 
226
    static private final Set<Integer> GETFILE_OK_CODES = CollectionUtils.createSet(200, 404);
227
 
228
    public Response getFile(final String path, final String fileName, final FileConsumer fileConsumer) throws IOException {
180 ilm 229
        if (path == null) {
230
            throw new IllegalArgumentException("null path");
231
        }
232
        if (fileName == null) {
233
            throw new IllegalArgumentException("null fileName");
234
        }
174 ilm 235
        final HttpsURLConnection con = openConnection("/get");
180 ilm 236
        send(con, NetUtils.urlEncode("rn", fileName, "rp", path), false);
174 ilm 237
        final Response res = checkResponseCode(con, GETFILE_OK_CODES);
238
        if (res.getCode() == 404) {
239
            fileConsumer.accept(null, null);
240
        } else if (res.getCode() == 200) {
241
            final FileAttrs fileAttrs = new FileAttrs(path, fileName, Instant.ofEpochMilli(con.getLastModified()), -1, con.getHeaderField("X-SHA256"));
242
            try (final InputStream in = getInputStream(con)) {
243
                fileConsumer.accept(fileAttrs, in);
244
            }
245
        }
246
        return res;
247
    }
248
 
249
    // 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
251
    // saved.
252
    public boolean saveFile(final String path, final String fileName, final Path localFile) throws IOException {
180 ilm 253
        if (path == null) {
254
            throw new IllegalArgumentException("null path");
255
        }
256
        if (fileName == null) {
257
            throw new IllegalArgumentException("null fileName");
258
        }
259
 
174 ilm 260
        final AtomicBoolean missing = new AtomicBoolean(true);
261
        final Response res = this.getFile(path, fileName, (fileAttrs, in) -> {
262
            missing.set(fileAttrs == null);
263
            if (!missing.get()) {
264
                fileAttrs.saveFile(in, localFile);
265
            }
266
        });
267
        if (!res.isSuccess())
268
            throw new IOException("Couldn't retrieve file " + fileName);
269
        return !missing.get();
270
    }
271
 
177 ilm 272
    public Response deleteFile(final String path, final String fileName) throws IOException {
180 ilm 273
        if (path == null) {
274
            throw new IllegalArgumentException("null path");
275
        }
276
        if (fileName == null) {
277
            throw new IllegalArgumentException("null fileName");
278
        }
174 ilm 279
        final HttpsURLConnection con = openConnection("/delete");
180 ilm 280
        return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path), false));
174 ilm 281
    }
282
 
177 ilm 283
    public final Response renameFile(final String path, final String fileName, final String newFileName) throws IOException {
284
        return this.renameFile(path, fileName, null, newFileName);
285
    }
174 ilm 286
 
177 ilm 287
    public final Response renameFile(final String path, final String fileName, final String newPath, final String newFileName) throws IOException {
180 ilm 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
        }
177 ilm 300
        final HttpsURLConnection con = openConnection("/rename");
180 ilm 301
        return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path, "newPath", newPath, "newName", newFileName), false));
177 ilm 302
    }
303
 
304
    public Response sendFile(String path, File localFile) throws IOException {
305
        return this.sendFile(path, localFile, false);
306
    }
307
 
308
    public Response sendFile(String path, File localFile, final boolean overwrite) throws IOException {
180 ilm 309
        if (path == null) {
310
            throw new IllegalArgumentException("null path");
311
        }
312
 
177 ilm 313
        final long size = localFile.length();
314
        if (size >= Integer.MAX_VALUE)
315
            throw new OutOfMemoryError("Required array size too large : " + size);
316
        final ByteArrayOutputStream ba = new ByteArrayOutputStream((int) size);
317
        final byte[] newsha256;
318
        // compute digest at the same time to protect against race conditions
319
        try (final InputStream ins = new BufferedInputStream(new FileInputStream(localFile))) {
320
            newsha256 = MessageDigestUtils.getHash(MessageDigestUtils.getSHA256(), ins, ba);
321
        }
322
 
174 ilm 323
        final HttpsURLConnection con = openConnection("/put");
324
        // We use Base64 because headers are not supporting UTF8
325
        con.setRequestProperty("X_FILENAME_B64", Base64.getEncoder().encodeToString(localFile.getName().getBytes(StandardCharsets.UTF_8)));
326
        con.setRequestProperty("X_PATH_B64", Base64.getEncoder().encodeToString(path.getBytes(StandardCharsets.UTF_8)));
177 ilm 327
        con.setRequestProperty("X-OVERWRITE", Boolean.toString(overwrite));
174 ilm 328
        con.setRequestProperty("X_FILESIZE", String.valueOf(size));
329
        con.setRequestProperty("X_SHA256", HashWriter.bytesToHex(newsha256));
330
        con.setRequestProperty("X-Last-Modified-ms", String.valueOf(localFile.lastModified()));
331
 
177 ilm 332
        return checkResponseCode(send(con, ba.toByteArray(), true));
174 ilm 333
    }
334
}