OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 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
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.utils.sync;

import org.openconcerto.utils.CollectionUtils;
import org.openconcerto.utils.MessageDigestUtils;
import org.openconcerto.utils.StreamUtils;

import java.io.BufferedOutputStream;
import java.io.File;
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;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileTime;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Set;
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 {

    private static final int MIN_GZIP_SIZE = 64;

    static protected final long getLong(final Object o) {
        return ((Number) o).longValue();
    }

    static protected final Instant getInstant(final Object o) {
        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;
        private final Instant lastModified;

        protected BaseAttrs(final String path, final String name, final Instant lastModified) {
            super();
            this.path = path;
            this.name = name;
            this.lastModified = lastModified;
        }

        public final String getPath() {
            return this.path;
        }

        public final String getName() {
            return this.name;
        }

        public final Instant getLastModified() {
            return this.lastModified;
        }

        @Override
        public String toString() {
            return this.getClass().getSimpleName() + " '" + this.getName() + "'";
        }
    }

    static public final class DirAttrs extends BaseAttrs {
        static protected DirAttrs fromJSON(final String path, final JSONArray array) {
            return new DirAttrs(path, (String) array.get(0), getInstant(array.get(1)));
        }

        protected DirAttrs(final String path, String name, Instant lastModified) {
            super(path, name, lastModified);
        }
    }

    static public final class FileAttrs extends BaseAttrs {

        static protected 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));
        }

        private final long size;
        private final String sha256;

        protected FileAttrs(final String path, String name, Instant lastModified, long size, String sha256) {
            super(path, name, lastModified);
            this.size = size;
            this.sha256 = sha256;
        }

        public final long getSize() {
            return this.size;
        }

        public final String getSHA256() {
            return this.sha256;
        }

        public final void saveFile(final InputStream in, final Path localFile) throws IOException {
            // Save to temporary file to avoid overwriting old file with a new invalid one. In
            // same folder for the move.
            final Path tmpFile = Files.createTempFile(localFile.getParent(), "partial", null);
            try {
                final MessageDigest md = this.getSHA256() == null ? null : MessageDigestUtils.getSHA256();
                try (final BufferedOutputStream fileStream = new BufferedOutputStream(Files.newOutputStream(tmpFile));
                        //
                        final OutputStream out = md == null ? fileStream : new DigestOutputStream(fileStream, md)) {
                    StreamUtils.copy(in, out);
                }
                if (this.getSize() >= 0) {
                    final long savedSize = Files.size(tmpFile);
                    if (savedSize != this.getSize())
                        throw new IOException("Expected " + this.getSize() + " bytes but saved " + savedSize);
                }
                if (md != null) {
                    final String savedHash = MessageDigestUtils.getHashString(md);
                    if (!savedHash.equalsIgnoreCase(this.getSHA256()))
                        throw new IOException("Expected hash was " + this.getSHA256() + " but saved " + savedHash);
                }
                Files.move(tmpFile, localFile, StandardCopyOption.REPLACE_EXISTING);
            } finally {
                Files.deleteIfExists(tmpFile);
            }
            if (this.getLastModified().compareTo(Instant.EPOCH) > 0)
                Files.setLastModifiedTime(localFile, FileTime.from(this.getLastModified()));
        }

        @Override
        public String toString() {
            return super.toString() + " of size " + getSize();
        }
    }

    static public final class DirContent {
        private final String path;
        private final JSONObject json;

        protected DirContent(final String path, JSONObject json) {
            super();
            this.path = path;
            this.json = json;
        }

        public final List<FileAttrs> getFiles() {
            return this.getFiles(null);
        }

        public final List<FileAttrs> getFiles(final Predicate<String> namePredicate) {
            return this.getContent("files", namePredicate, FileAttrs::fromJSON);
        }

        public final List<DirAttrs> getDirs() {
            return this.getDirs(null);
        }

        public final List<DirAttrs> getDirs(final Predicate<String> namePredicate) {
            return this.getContent("dirs", namePredicate, DirAttrs::fromJSON);
        }

        protected final <T extends BaseAttrs> List<T> getContent(final String key, final Predicate<String> namePredicate, final BiFunction<String, JSONArray, T> create) {
            final JSONArray files = (JSONArray) this.json.get(key);
            if (files == null)
                return Collections.emptyList();
            final List<T> res = new ArrayList<>();
            for (final Object f : files) {
                final JSONArray array = (JSONArray) f;
                if (namePredicate == null || namePredicate.test((String) array.get(0))) {
                    res.add(create.apply(this.path, array));
                }
            }
            return res;
        }
    }

    private final String url;
    private SSLSocketFactory socketFactory;
    private String token;
    private boolean throwException = true;

    public SimpleSyncClient(final String url) {
        this.url = 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"));
        if (!res.isSuccess())
            return null;
        final JSONParser p = new JSONParser(JSONParser.MODE_STRICTEST);
        try (final InputStream in = getInputStream(con)) {
            return new DirContent(path, (JSONObject) p.parse(in));
        }
    }

    @FunctionalInterface
    static public interface FileConsumer {
        public void accept(FileAttrs attrs, InputStream fileStream) throws IOException;
    }

    static private final Set<Integer> GETFILE_OK_CODES = CollectionUtils.createSet(200, 404);

    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);
        final Response res = checkResponseCode(con, GETFILE_OK_CODES);
        if (res.getCode() == 404) {
            fileConsumer.accept(null, null);
        } else if (res.getCode() == 200) {
            final FileAttrs fileAttrs = new FileAttrs(path, fileName, Instant.ofEpochMilli(con.getLastModified()), -1, con.getHeaderField("X-SHA256"));
            try (final InputStream in = getInputStream(con)) {
                fileConsumer.accept(fileAttrs, in);
            }
        }
        return res;
    }

    // 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.
    public boolean saveFile(final String path, final String fileName, final Path localFile) throws IOException {
        final AtomicBoolean missing = new AtomicBoolean(true);
        final Response res = this.getFile(path, fileName, (fileAttrs, in) -> {
            missing.set(fileAttrs == null);
            if (!missing.get()) {
                fileAttrs.saveFile(in, localFile);
            }
        });
        if (!res.isSuccess())
            throw new IOException("Couldn't retrieve file " + fileName);
        return !missing.get();
    }

    public Response deleteFile(final String path, final String fileName) throws MalformedURLException, IOException, ProtocolException, UnsupportedEncodingException {
        final HttpsURLConnection con = openConnection("/delete");
        return checkResponseCode(send(con, "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();

        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_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));
    }
}