OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Compare Revisions

Regard whitespace Rev 176 → Rev 177

/trunk/OpenConcerto/src/org/openconcerto/utils/sync/SimpleSyncClient.java
15,17 → 15,18
 
import org.openconcerto.utils.CollectionUtils;
import org.openconcerto.utils.MessageDigestUtils;
import org.openconcerto.utils.NetUtils;
import org.openconcerto.utils.StreamUtils;
import org.openconcerto.utils.net.HTTPClient;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
42,20 → 43,15
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
 
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
 
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
 
public final class SimpleSyncClient {
public final class SimpleSyncClient extends HTTPClient {
 
private static final int MIN_GZIP_SIZE = 64;
 
static protected final long getLong(final Object o) {
return ((Number) o).longValue();
}
64,67 → 60,6
return Instant.ofEpochMilli(getLong(o));
}
 
static public class ServerException extends RuntimeException {
private final int responseCode;
private final boolean authenticateError;
 
protected ServerException(int responseCode, boolean authenticateError) {
super("Response code was " + responseCode);
this.responseCode = responseCode;
this.authenticateError = authenticateError;
}
 
public final int getResponseCode() {
return this.responseCode;
}
 
public final boolean isAuthenticateError() {
return this.authenticateError;
}
}
 
static public final class Response {
 
protected final static Response create(HttpsURLConnection con, final Set<Integer> okCodes) throws IOException {
final boolean success = okCodes == null ? con.getResponseCode() == 200 : okCodes.contains(con.getResponseCode());
return new Response(success, con.getResponseCode(), con.getResponseMessage(), con.getContentEncoding(), con.getContentType());
}
 
private final boolean success;
private final int code;
private final String message;
private final String contentEncoding, contentType;
 
protected Response(boolean success, int code, String message, String contentEncoding, String contentType) {
super();
this.success = success;
this.code = code;
this.message = message;
this.contentEncoding = contentEncoding;
this.contentType = contentType;
}
 
public final int getCode() {
return this.code;
}
 
public final boolean isSuccess() {
return this.success;
}
 
public final String getMessage() {
return this.message;
}
 
public final String getContentEncoding() {
return this.contentEncoding;
}
 
public final String getContentType() {
return this.contentType;
}
}
 
static protected abstract class BaseAttrs {
private final String path;
private final String name;
264,87 → 199,13
}
}
 
private final String url;
private SSLSocketFactory socketFactory;
private String token;
private boolean throwException = true;
 
public SimpleSyncClient(final String url) {
this.url = url;
super(url);
}
 
public final SSLSocketFactory getSocketFactory() {
return this.socketFactory;
}
 
public final void setSocketFactory(SSLSocketFactory socketFactory) {
this.socketFactory = socketFactory;
}
 
public final void setToken(String token) {
this.token = token;
}
 
public final boolean hasToken() {
return this.getToken() != null;
}
 
protected final String getToken() {
return this.token;
}
 
public final void setThrowException(boolean throwException) {
this.throwException = throwException;
}
 
public final boolean throwsException() {
return this.throwException;
}
 
protected Response checkResponseCode(final HttpsURLConnection con) throws IOException {
return checkResponseCode(con, null);
}
 
protected Response checkResponseCode(final HttpsURLConnection con, final Set<Integer> okCodes) throws IOException {
final Response res = Response.create(con, okCodes);
if (this.throwsException() && !res.isSuccess())
throw new ServerException(res.getCode(), con.getHeaderField("WWW-Authenticate") != null);
return res;
}
 
private final HttpsURLConnection openConnection(final String path) throws IOException {
final HttpsURLConnection con = (HttpsURLConnection) new URL(this.url + path).openConnection();
con.setRequestProperty("Accept-Encoding", "gzip");
if (this.getSocketFactory() != null)
con.setSSLSocketFactory(this.getSocketFactory());
if (getToken() != null)
con.setRequestProperty("Authorization", "Bearer " + Base64.getEncoder().encodeToString(getToken().getBytes(StandardCharsets.UTF_8)));
return con;
}
 
private final InputStream getInputStream(final HttpsURLConnection con) throws IOException {
return "gzip".equals(con.getContentEncoding()) ? new GZIPInputStream(con.getInputStream()) : con.getInputStream();
}
 
private final HttpsURLConnection send(final HttpsURLConnection con, final String params) throws IOException {
return this.send(con, params.getBytes(StandardCharsets.UTF_8), false);
}
 
private final HttpsURLConnection send(final HttpsURLConnection con, final byte[] toSend, final boolean allowGzip) throws IOException {
final boolean useGzip = allowGzip && toSend.length >= MIN_GZIP_SIZE;
if (useGzip)
con.setRequestProperty("Content-Encoding", "gzip");
con.setRequestMethod("POST");
con.setDoOutput(true);
try (final OutputStream o = useGzip ? new GZIPOutputStream(con.getOutputStream()) : con.getOutputStream()) {
o.write(toSend);
}
return con;
}
 
public DirContent getDir(final String path) throws Exception {
final HttpsURLConnection con = openConnection("/getDir");
final Response res = checkResponseCode(send(con, "rp=" + path + "&type=json"));
final Response res = checkResponseCode(send(con, NetUtils.urlEncode("rp", path, "type", "json")));
if (!res.isSuccess())
return null;
final JSONParser p = new JSONParser(JSONParser.MODE_STRICTEST);
362,7 → 223,7
 
public Response getFile(final String path, final String fileName, final FileConsumer fileConsumer) throws IOException {
final HttpsURLConnection con = openConnection("/get");
send(con, "rn=" + fileName + "&rp=" + path);
send(con, NetUtils.urlEncode("rn", fileName, "rp", path));
final Response res = checkResponseCode(con, GETFILE_OK_CODES);
if (res.getCode() == 404) {
fileConsumer.accept(null, null);
391,23 → 252,44
return !missing.get();
}
 
public Response deleteFile(final String path, final String fileName) throws MalformedURLException, IOException, ProtocolException, UnsupportedEncodingException {
public Response deleteFile(final String path, final String fileName) throws IOException {
final HttpsURLConnection con = openConnection("/delete");
return checkResponseCode(send(con, "rn=" + fileName + "&rp=" + path));
return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path)));
}
 
public Response sendFile(String path, File localFile) throws Exception, MalformedURLException, IOException, ProtocolException {
byte[] newsha256 = HashWriter.getHash(localFile);
long size = localFile.length();
public final Response renameFile(final String path, final String fileName, final String newFileName) throws IOException {
return this.renameFile(path, fileName, null, newFileName);
}
 
public final Response renameFile(final String path, final String fileName, final String newPath, final String newFileName) throws IOException {
final HttpsURLConnection con = openConnection("/rename");
return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path, "newPath", newPath, "newName", newFileName)));
}
 
public Response sendFile(String path, File localFile) throws IOException {
return this.sendFile(path, localFile, false);
}
 
public Response sendFile(String path, File localFile, final boolean overwrite) throws IOException {
final long size = localFile.length();
if (size >= Integer.MAX_VALUE)
throw new OutOfMemoryError("Required array size too large : " + size);
final ByteArrayOutputStream ba = new ByteArrayOutputStream((int) size);
final byte[] newsha256;
// compute digest at the same time to protect against race conditions
try (final InputStream ins = new BufferedInputStream(new FileInputStream(localFile))) {
newsha256 = MessageDigestUtils.getHash(MessageDigestUtils.getSHA256(), ins, ba);
}
 
final HttpsURLConnection con = openConnection("/put");
// We use Base64 because headers are not supporting UTF8
con.setRequestProperty("X_FILENAME_B64", Base64.getEncoder().encodeToString(localFile.getName().getBytes(StandardCharsets.UTF_8)));
con.setRequestProperty("X_PATH_B64", Base64.getEncoder().encodeToString(path.getBytes(StandardCharsets.UTF_8)));
con.setRequestProperty("X-OVERWRITE", Boolean.toString(overwrite));
con.setRequestProperty("X_FILESIZE", String.valueOf(size));
con.setRequestProperty("X_SHA256", HashWriter.bytesToHex(newsha256));
con.setRequestProperty("X-Last-Modified-ms", String.valueOf(localFile.lastModified()));
 
return checkResponseCode(send(con, Files.readAllBytes(localFile.toPath()), true));
return checkResponseCode(send(con, ba.toByteArray(), true));
}
}