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 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.
|
10 |
*
|
10 |
*
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
12 |
*/
|
12 |
*/
|
13 |
|
13 |
|
14 |
package org.openconcerto.utils.sync;
|
14 |
package org.openconcerto.utils.sync;
|
15 |
|
15 |
|
16 |
import org.openconcerto.utils.CollectionUtils;
|
16 |
import org.openconcerto.utils.CollectionUtils;
|
17 |
import org.openconcerto.utils.MessageDigestUtils;
|
17 |
import org.openconcerto.utils.MessageDigestUtils;
|
18 |
import org.openconcerto.utils.NetUtils;
|
18 |
import org.openconcerto.utils.NetUtils;
|
19 |
import org.openconcerto.utils.StreamUtils;
|
19 |
import org.openconcerto.utils.StreamUtils;
|
20 |
import org.openconcerto.utils.net.HTTPClient;
|
20 |
import org.openconcerto.utils.net.HTTPClient;
|
21 |
|
21 |
|
22 |
import java.io.BufferedInputStream;
|
22 |
import java.io.BufferedInputStream;
|
23 |
import java.io.BufferedOutputStream;
|
23 |
import java.io.BufferedOutputStream;
|
24 |
import java.io.ByteArrayOutputStream;
|
24 |
import java.io.ByteArrayOutputStream;
|
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.nio.charset.StandardCharsets;
|
30 |
import java.nio.charset.StandardCharsets;
|
31 |
import java.nio.file.Files;
|
31 |
import java.nio.file.Files;
|
32 |
import java.nio.file.Path;
|
32 |
import java.nio.file.Path;
|
33 |
import java.nio.file.StandardCopyOption;
|
33 |
import java.nio.file.StandardCopyOption;
|
34 |
import java.nio.file.attribute.FileTime;
|
34 |
import java.nio.file.attribute.FileTime;
|
35 |
import java.security.DigestOutputStream;
|
35 |
import java.security.DigestOutputStream;
|
36 |
import java.security.MessageDigest;
|
36 |
import java.security.MessageDigest;
|
37 |
import java.time.Instant;
|
37 |
import java.time.Instant;
|
38 |
import java.util.ArrayList;
|
38 |
import java.util.ArrayList;
|
39 |
import java.util.Base64;
|
39 |
import java.util.Base64;
|
40 |
import java.util.Collections;
|
40 |
import java.util.Collections;
|
41 |
import java.util.List;
|
41 |
import java.util.List;
|
42 |
import java.util.Set;
|
42 |
import java.util.Set;
|
43 |
import java.util.concurrent.atomic.AtomicBoolean;
|
43 |
import java.util.concurrent.atomic.AtomicBoolean;
|
44 |
import java.util.function.BiFunction;
|
44 |
import java.util.function.BiFunction;
|
45 |
import java.util.function.Predicate;
|
45 |
import java.util.function.Predicate;
|
46 |
|
46 |
|
47 |
import javax.net.ssl.HttpsURLConnection;
|
47 |
import javax.net.ssl.HttpsURLConnection;
|
48 |
|
48 |
|
49 |
import net.minidev.json.JSONArray;
|
49 |
import net.minidev.json.JSONArray;
|
50 |
import net.minidev.json.JSONObject;
|
50 |
import net.minidev.json.JSONObject;
|
51 |
import net.minidev.json.parser.JSONParser;
|
51 |
import net.minidev.json.parser.JSONParser;
|
52 |
|
52 |
|
53 |
public final class SimpleSyncClient extends HTTPClient {
|
53 |
public final class SimpleSyncClient extends HTTPClient {
|
54 |
|
54 |
|
55 |
static protected final long getLong(final Object o) {
|
55 |
static protected final long getLong(final Object o) {
|
56 |
return ((Number) o).longValue();
|
56 |
return ((Number) o).longValue();
|
57 |
}
|
57 |
}
|
58 |
|
58 |
|
59 |
static protected final Instant getInstant(final Object o) {
|
59 |
static protected final Instant getInstant(final Object o) {
|
60 |
return Instant.ofEpochMilli(getLong(o));
|
60 |
return Instant.ofEpochMilli(getLong(o));
|
61 |
}
|
61 |
}
|
62 |
|
62 |
|
63 |
static protected abstract class BaseAttrs {
|
63 |
static protected abstract class BaseAttrs {
|
64 |
private final String path;
|
64 |
private final String path;
|
65 |
private final String name;
|
65 |
private final String name;
|
66 |
private final Instant lastModified;
|
66 |
private final Instant lastModified;
|
67 |
|
67 |
|
68 |
protected BaseAttrs(final String path, final String name, final Instant lastModified) {
|
68 |
protected BaseAttrs(final String path, final String name, final Instant lastModified) {
|
69 |
super();
|
69 |
super();
|
70 |
this.path = path;
|
70 |
this.path = path;
|
71 |
this.name = name;
|
71 |
this.name = name;
|
72 |
this.lastModified = lastModified;
|
72 |
this.lastModified = lastModified;
|
73 |
}
|
73 |
}
|
74 |
|
74 |
|
75 |
public final String getPath() {
|
75 |
public final String getPath() {
|
76 |
return this.path;
|
76 |
return this.path;
|
77 |
}
|
77 |
}
|
78 |
|
78 |
|
79 |
public final String getName() {
|
79 |
public final String getName() {
|
80 |
return this.name;
|
80 |
return this.name;
|
81 |
}
|
81 |
}
|
82 |
|
82 |
|
83 |
public final Instant getLastModified() {
|
83 |
public final Instant getLastModified() {
|
84 |
return this.lastModified;
|
84 |
return this.lastModified;
|
85 |
}
|
85 |
}
|
86 |
|
86 |
|
87 |
@Override
|
87 |
@Override
|
88 |
public String toString() {
|
88 |
public String toString() {
|
89 |
return this.getClass().getSimpleName() + " '" + this.getName() + "'";
|
89 |
return this.getClass().getSimpleName() + " '" + this.getName() + "'";
|
90 |
}
|
90 |
}
|
91 |
}
|
91 |
}
|
92 |
|
92 |
|
93 |
static public final class DirAttrs extends BaseAttrs {
|
93 |
static public final class DirAttrs extends BaseAttrs {
|
94 |
static protected DirAttrs fromJSON(final String path, final JSONArray array) {
|
94 |
static protected DirAttrs fromJSON(final String path, final JSONArray array) {
|
95 |
return new DirAttrs(path, (String) array.get(0), getInstant(array.get(1)));
|
95 |
return new DirAttrs(path, (String) array.get(0), getInstant(array.get(1)));
|
96 |
}
|
96 |
}
|
97 |
|
97 |
|
98 |
protected DirAttrs(final String path, String name, Instant lastModified) {
|
98 |
protected DirAttrs(final String path, String name, Instant lastModified) {
|
99 |
super(path, name, lastModified);
|
99 |
super(path, name, lastModified);
|
100 |
}
|
100 |
}
|
101 |
}
|
101 |
}
|
102 |
|
102 |
|
103 |
static public final class FileAttrs extends BaseAttrs {
|
103 |
static public final class FileAttrs extends BaseAttrs {
|
104 |
|
104 |
|
105 |
static protected FileAttrs fromJSON(final String path, final JSONArray array) {
|
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));
|
106 |
return new FileAttrs(path, (String) array.get(0), getInstant(array.get(2)), getLong(array.get(1)), (String) array.get(3));
|
107 |
}
|
107 |
}
|
108 |
|
108 |
|
109 |
private final long size;
|
109 |
private final long size;
|
110 |
private final String sha256;
|
110 |
private final String sha256;
|
111 |
|
111 |
|
112 |
protected FileAttrs(final String path, String name, Instant lastModified, long size, String sha256) {
|
112 |
protected FileAttrs(final String path, String name, Instant lastModified, long size, String sha256) {
|
113 |
super(path, name, lastModified);
|
113 |
super(path, name, lastModified);
|
114 |
this.size = size;
|
114 |
this.size = size;
|
115 |
this.sha256 = sha256;
|
115 |
this.sha256 = sha256;
|
116 |
}
|
116 |
}
|
117 |
|
117 |
|
118 |
public final long getSize() {
|
118 |
public final long getSize() {
|
119 |
return this.size;
|
119 |
return this.size;
|
120 |
}
|
120 |
}
|
121 |
|
121 |
|
122 |
public final String getSHA256() {
|
122 |
public final String getSHA256() {
|
123 |
return this.sha256;
|
123 |
return this.sha256;
|
124 |
}
|
124 |
}
|
125 |
|
125 |
|
126 |
public final void saveFile(final InputStream in, final Path localFile) throws IOException {
|
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
|
127 |
// Save to temporary file to avoid overwriting old file with a new invalid one. In
|
128 |
// same folder for the move.
|
128 |
// same folder for the move.
|
129 |
final Path tmpFile = Files.createTempFile(localFile.getParent(), "partial", null);
|
129 |
final Path tmpFile = Files.createTempFile(localFile.getParent(), "partial", null);
|
130 |
try {
|
130 |
try {
|
131 |
final MessageDigest md = this.getSHA256() == null ? null : MessageDigestUtils.getSHA256();
|
131 |
final MessageDigest md = this.getSHA256() == null ? null : MessageDigestUtils.getSHA256();
|
132 |
try (final BufferedOutputStream fileStream = new BufferedOutputStream(Files.newOutputStream(tmpFile));
|
132 |
try (final BufferedOutputStream fileStream = new BufferedOutputStream(Files.newOutputStream(tmpFile));
|
133 |
//
|
133 |
//
|
134 |
final OutputStream out = md == null ? fileStream : new DigestOutputStream(fileStream, md)) {
|
134 |
final OutputStream out = md == null ? fileStream : new DigestOutputStream(fileStream, md)) {
|
135 |
StreamUtils.copy(in, out);
|
135 |
StreamUtils.copy(in, out);
|
136 |
}
|
136 |
}
|
137 |
if (this.getSize() >= 0) {
|
137 |
if (this.getSize() >= 0) {
|
138 |
final long savedSize = Files.size(tmpFile);
|
138 |
final long savedSize = Files.size(tmpFile);
|
139 |
if (savedSize != this.getSize())
|
139 |
if (savedSize != this.getSize())
|
140 |
throw new IOException("Expected " + this.getSize() + " bytes but saved " + savedSize);
|
140 |
throw new IOException("Expected " + this.getSize() + " bytes but saved " + savedSize);
|
141 |
}
|
141 |
}
|
142 |
if (md != null) {
|
142 |
if (md != null) {
|
143 |
final String savedHash = MessageDigestUtils.getHashString(md);
|
143 |
final String savedHash = MessageDigestUtils.getHashString(md);
|
144 |
if (!savedHash.equalsIgnoreCase(this.getSHA256()))
|
144 |
if (!savedHash.equalsIgnoreCase(this.getSHA256()))
|
145 |
throw new IOException("Expected hash was " + this.getSHA256() + " but saved " + savedHash);
|
145 |
throw new IOException("Expected hash was " + this.getSHA256() + " but saved " + savedHash);
|
146 |
}
|
146 |
}
|
147 |
Files.move(tmpFile, localFile, StandardCopyOption.REPLACE_EXISTING);
|
147 |
Files.move(tmpFile, localFile, StandardCopyOption.REPLACE_EXISTING);
|
148 |
} finally {
|
148 |
} finally {
|
149 |
Files.deleteIfExists(tmpFile);
|
149 |
Files.deleteIfExists(tmpFile);
|
150 |
}
|
150 |
}
|
151 |
if (this.getLastModified().compareTo(Instant.EPOCH) > 0)
|
151 |
if (this.getLastModified().compareTo(Instant.EPOCH) > 0)
|
152 |
Files.setLastModifiedTime(localFile, FileTime.from(this.getLastModified()));
|
152 |
Files.setLastModifiedTime(localFile, FileTime.from(this.getLastModified()));
|
153 |
}
|
153 |
}
|
154 |
|
154 |
|
155 |
@Override
|
155 |
@Override
|
156 |
public String toString() {
|
156 |
public String toString() {
|
157 |
return super.toString() + " of size " + getSize();
|
157 |
return super.toString() + " of size " + getSize();
|
158 |
}
|
158 |
}
|
159 |
}
|
159 |
}
|
160 |
|
160 |
|
161 |
static public final class DirContent {
|
161 |
static public final class DirContent {
|
162 |
private final String path;
|
162 |
private final String path;
|
163 |
private final JSONObject json;
|
163 |
private final JSONObject json;
|
164 |
|
164 |
|
165 |
protected DirContent(final String path, JSONObject json) {
|
165 |
protected DirContent(final String path, JSONObject json) {
|
166 |
super();
|
166 |
super();
|
167 |
this.path = path;
|
167 |
this.path = path;
|
168 |
this.json = json;
|
168 |
this.json = json;
|
169 |
}
|
169 |
}
|
170 |
|
170 |
|
171 |
public final List<FileAttrs> getFiles() {
|
171 |
public final List<FileAttrs> getFiles() {
|
172 |
return this.getFiles(null);
|
172 |
return this.getFiles(null);
|
173 |
}
|
173 |
}
|
174 |
|
174 |
|
175 |
public final List<FileAttrs> getFiles(final Predicate<String> namePredicate) {
|
175 |
public final List<FileAttrs> getFiles(final Predicate<String> namePredicate) {
|
176 |
return this.getContent("files", namePredicate, FileAttrs::fromJSON);
|
176 |
return this.getContent("files", namePredicate, FileAttrs::fromJSON);
|
177 |
}
|
177 |
}
|
178 |
|
178 |
|
179 |
public final List<DirAttrs> getDirs() {
|
179 |
public final List<DirAttrs> getDirs() {
|
180 |
return this.getDirs(null);
|
180 |
return this.getDirs(null);
|
181 |
}
|
181 |
}
|
182 |
|
182 |
|
183 |
public final List<DirAttrs> getDirs(final Predicate<String> namePredicate) {
|
183 |
public final List<DirAttrs> getDirs(final Predicate<String> namePredicate) {
|
184 |
return this.getContent("dirs", namePredicate, DirAttrs::fromJSON);
|
184 |
return this.getContent("dirs", namePredicate, DirAttrs::fromJSON);
|
185 |
}
|
185 |
}
|
186 |
|
186 |
|
187 |
protected final <T extends BaseAttrs> List<T> getContent(final String key, final Predicate<String> namePredicate, final BiFunction<String, JSONArray, T> create) {
|
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);
|
188 |
final JSONArray files = (JSONArray) this.json.get(key);
|
189 |
if (files == null)
|
189 |
if (files == null)
|
190 |
return Collections.emptyList();
|
190 |
return Collections.emptyList();
|
191 |
final List<T> res = new ArrayList<>();
|
191 |
final List<T> res = new ArrayList<>();
|
192 |
for (final Object f : files) {
|
192 |
for (final Object f : files) {
|
193 |
final JSONArray array = (JSONArray) f;
|
193 |
final JSONArray array = (JSONArray) f;
|
194 |
if (namePredicate == null || namePredicate.test((String) array.get(0))) {
|
194 |
if (namePredicate == null || namePredicate.test((String) array.get(0))) {
|
195 |
res.add(create.apply(this.path, array));
|
195 |
res.add(create.apply(this.path, array));
|
196 |
}
|
196 |
}
|
197 |
}
|
197 |
}
|
198 |
return res;
|
198 |
return res;
|
199 |
}
|
199 |
}
|
200 |
}
|
200 |
}
|
201 |
|
201 |
|
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));
|
214 |
}
|
218 |
}
|
215 |
}
|
219 |
}
|
216 |
|
220 |
|
217 |
@FunctionalInterface
|
221 |
@FunctionalInterface
|
218 |
static public interface FileConsumer {
|
222 |
static public interface FileConsumer {
|
219 |
public void accept(FileAttrs attrs, InputStream fileStream) throws IOException;
|
223 |
public void accept(FileAttrs attrs, InputStream fileStream) throws IOException;
|
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"));
|
232 |
try (final InputStream in = getInputStream(con)) {
|
242 |
try (final InputStream in = getInputStream(con)) {
|
233 |
fileConsumer.accept(fileAttrs, in);
|
243 |
fileConsumer.accept(fileAttrs, in);
|
234 |
}
|
244 |
}
|
235 |
}
|
245 |
}
|
236 |
return res;
|
246 |
return res;
|
237 |
}
|
247 |
}
|
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);
|
248 |
}
|
265 |
}
|
249 |
});
|
266 |
});
|
250 |
if (!res.isSuccess())
|
267 |
if (!res.isSuccess())
|
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;
|
279 |
// compute digest at the same time to protect against race conditions
|
318 |
// compute digest at the same time to protect against race conditions
|
280 |
try (final InputStream ins = new BufferedInputStream(new FileInputStream(localFile))) {
|
319 |
try (final InputStream ins = new BufferedInputStream(new FileInputStream(localFile))) {
|
281 |
newsha256 = MessageDigestUtils.getHash(MessageDigestUtils.getSHA256(), ins, ba);
|
320 |
newsha256 = MessageDigestUtils.getHash(MessageDigestUtils.getSHA256(), ins, ba);
|
282 |
}
|
321 |
}
|
283 |
|
322 |
|
284 |
final HttpsURLConnection con = openConnection("/put");
|
323 |
final HttpsURLConnection con = openConnection("/put");
|
285 |
// We use Base64 because headers are not supporting UTF8
|
324 |
// We use Base64 because headers are not supporting UTF8
|
286 |
con.setRequestProperty("X_FILENAME_B64", Base64.getEncoder().encodeToString(localFile.getName().getBytes(StandardCharsets.UTF_8)));
|
325 |
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)));
|
326 |
con.setRequestProperty("X_PATH_B64", Base64.getEncoder().encodeToString(path.getBytes(StandardCharsets.UTF_8)));
|
288 |
con.setRequestProperty("X-OVERWRITE", Boolean.toString(overwrite));
|
327 |
con.setRequestProperty("X-OVERWRITE", Boolean.toString(overwrite));
|
289 |
con.setRequestProperty("X_FILESIZE", String.valueOf(size));
|
328 |
con.setRequestProperty("X_FILESIZE", String.valueOf(size));
|
290 |
con.setRequestProperty("X_SHA256", HashWriter.bytesToHex(newsha256));
|
329 |
con.setRequestProperty("X_SHA256", HashWriter.bytesToHex(newsha256));
|
291 |
con.setRequestProperty("X-Last-Modified-ms", String.valueOf(localFile.lastModified()));
|
330 |
con.setRequestProperty("X-Last-Modified-ms", String.valueOf(localFile.lastModified()));
|
292 |
|
331 |
|
293 |
return checkResponseCode(send(con, ba.toByteArray(), true));
|
332 |
return checkResponseCode(send(con, ba.toByteArray(), true));
|
294 |
}
|
333 |
}
|
295 |
}
|
334 |
}
|