OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Rev 180 | Go to most recent revision | Blame | Compare with Previous | 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.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.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 javax.net.ssl.HttpsURLConnection;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;

public final class SimpleSyncClient extends HTTPClient {

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

    public SimpleSyncClient(final String url) {
        super(url);
    }

    public DirContent getDir(final String path) throws Exception {
        final HttpsURLConnection con = openConnection("/getDir");
        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);
        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, NetUtils.urlEncode("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 IOException {
        final HttpsURLConnection con = openConnection("/delete");
        return checkResponseCode(send(con, NetUtils.urlEncode("rn", fileName, "rp", path)));
    }

    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, ba.toByteArray(), true));
    }
}