james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [james-project] 02/12: JAMES-2709 Contribute a LinShare Blob Export Mechanism
Date Fri, 19 Apr 2019 07:42:55 GMT
This is an automated email from the ASF dual-hosted git repository.

aduprat pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit c2c74cf9609227c8f21dd192f38bc1639aa92f16
Author: Benoit Tellier <btellier@linagora.com>
AuthorDate: Tue Apr 9 15:37:13 2019 +0700

    JAMES-2709 Contribute a LinShare Blob Export Mechanism
---
 third-party/linshare/pom.xml                       |  9 +++
 .../linshare/LinshareBlobExportMechanism.java      | 78 +++++++++++++++++++
 .../james/linshare/client/ReceivedShare.java       |  2 +-
 .../apache/james/linshare/client/ShareRequest.java | 12 +--
 .../linshare/LinshareBlobExportMechanismTest.java  | 91 ++++++++++++++++++++++
 5 files changed, 185 insertions(+), 7 deletions(-)

diff --git a/third-party/linshare/pom.xml b/third-party/linshare/pom.xml
index 576c3ee..4a41a92 100644
--- a/third-party/linshare/pom.xml
+++ b/third-party/linshare/pom.xml
@@ -34,6 +34,15 @@
             <artifactId>james-core</artifactId>
         </dependency>
         <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>blob-export-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>blob-memory</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-classic</artifactId>
             <scope>test</scope>
diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java
b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java
new file mode 100644
index 0000000..b5b7db4
--- /dev/null
+++ b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java
@@ -0,0 +1,78 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.linshare;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Optional;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.james.blob.api.BlobId;
+import org.apache.james.blob.api.BlobStore;
+import org.apache.james.blob.export.api.BlobExportMechanism;
+import org.apache.james.blob.export.api.FileExtension;
+import org.apache.james.core.MailAddress;
+import org.apache.james.linshare.client.Document;
+import org.apache.james.linshare.client.LinshareAPI;
+import org.apache.james.linshare.client.ShareRequest;
+
+public class LinshareBlobExportMechanism implements BlobExportMechanism {
+    private final LinshareAPI linshareAPI;
+    private final BlobStore blobStore;
+
+    public LinshareBlobExportMechanism(LinshareAPI linshareAPI, BlobStore blobStore) {
+        this.linshareAPI = linshareAPI;
+        this.blobStore = blobStore;
+    }
+
+    @Override
+    public ShareeStage blobId(BlobId blobId) {
+        return mailAddress -> explanation -> fileExtension -> () ->  {
+            try {
+                exportBlob(blobId, mailAddress, fileExtension);
+            } catch (Exception e) {
+                throw new BlobExportException("Error while exporting blob " + blobId.asString()
+ " to " + mailAddress.asString(), e);
+            }
+        };
+    }
+
+    private void exportBlob(BlobId blobId, MailAddress mailAddress, Optional<FileExtension>
fileExtension) throws IOException {
+        File tempFile = createTempFile(blobId, fileExtension);
+        try {
+            FileUtils.copyInputStreamToFile(blobStore.read(blobId), tempFile);
+            uploadAndShare(mailAddress, tempFile);
+        } finally {
+            FileUtils.forceDelete(tempFile);
+        }
+    }
+
+    private void uploadAndShare(MailAddress mailAddress, File tempFile) {
+        Document document = linshareAPI.uploadDocument(tempFile);
+        linshareAPI.share(ShareRequest.builder()
+            .addDocumentId(document.getId())
+            .addRecipient(mailAddress)
+            .build());
+    }
+
+    private File createTempFile(BlobId blobId, Optional<FileExtension> fileExtension)
throws IOException {
+        String suffix = fileExtension.map(FileExtension::asFileSuffix).orElse("");
+        return File.createTempFile(blobId.asString() + "_", suffix);
+    }
+}
diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/client/ReceivedShare.java
b/third-party/linshare/src/main/java/org/apache/james/linshare/client/ReceivedShare.java
index 7d21962..cf11766 100644
--- a/third-party/linshare/src/main/java/org/apache/james/linshare/client/ReceivedShare.java
+++ b/third-party/linshare/src/main/java/org/apache/james/linshare/client/ReceivedShare.java
@@ -24,7 +24,7 @@ import java.util.Objects;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.google.common.annotations.VisibleForTesting;
 
-class ReceivedShare {
+public class ReceivedShare {
 
     private final User sender;
     private final Document document;
diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareRequest.java
b/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareRequest.java
index c0a95bb..61a2d8b 100644
--- a/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareRequest.java
+++ b/third-party/linshare/src/main/java/org/apache/james/linshare/client/ShareRequest.java
@@ -31,7 +31,7 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 
-class ShareRequest {
+public class ShareRequest {
 
     static class Recipient {
         private final MailAddress mail;
@@ -64,7 +64,7 @@ class ShareRequest {
         }
     }
 
-    static class Builder {
+    public static class Builder {
         private final ImmutableList.Builder<Recipient> recipientsBuilder;
         private final ImmutableList.Builder<DocumentId> documentIdsBuilder;
 
@@ -73,22 +73,22 @@ class ShareRequest {
             this.documentIdsBuilder = new ImmutableList.Builder<>();
         }
 
-        Builder addRecipient(MailAddress recipient) {
+        public Builder addRecipient(MailAddress recipient) {
             recipientsBuilder.add(new Recipient(recipient));
             return this;
         }
 
-        Builder addDocumentId(DocumentId documentId) {
+        public Builder addDocumentId(DocumentId documentId) {
             documentIdsBuilder.add(documentId);
             return this;
         }
 
-        ShareRequest build() {
+        public ShareRequest build() {
             return new ShareRequest(recipientsBuilder.build(), documentIdsBuilder.build());
         }
     }
 
-    static Builder builder() {
+    public static Builder builder() {
         return new Builder();
     }
 
diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareBlobExportMechanismTest.java
b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareBlobExportMechanismTest.java
new file mode 100644
index 0000000..e693cd5
--- /dev/null
+++ b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareBlobExportMechanismTest.java
@@ -0,0 +1,91 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.linshare;
+
+import static org.apache.james.linshare.LinshareFixture.USER_1;
+import static org.apache.james.linshare.LinshareFixture.USER_2;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.nio.charset.StandardCharsets;
+
+import org.apache.james.blob.api.BlobId;
+import org.apache.james.blob.api.HashBlobId;
+import org.apache.james.blob.export.api.BlobExportMechanism;
+import org.apache.james.blob.export.api.FileExtension;
+import org.apache.james.blob.memory.MemoryBlobStore;
+import org.apache.james.core.MailAddress;
+import org.apache.james.linshare.client.LinshareAPI;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+class LinshareBlobExportMechanismTest {
+    private static final String EXPLANATION = "Explanation about the file being shared";
+
+    @RegisterExtension
+    static LinshareExtension linshareExtension = new LinshareExtension();
+
+    private MemoryBlobStore blobStore;
+    private LinshareBlobExportMechanism testee;
+    private HashBlobId.Factory blobIdFactory;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        blobIdFactory = new HashBlobId.Factory();
+        blobStore = new MemoryBlobStore(blobIdFactory);
+
+        testee = new LinshareBlobExportMechanism(
+            linshareExtension.getAPIFor(USER_1),
+            blobStore);
+    }
+
+    @Test
+    void exportShouldShareTheDocumentViaLinShare() throws Exception {
+        BlobId blobId = blobStore.save("content".getBytes(StandardCharsets.UTF_8)).block();
+
+        testee.blobId(blobId)
+            .with(new MailAddress(USER_2.getUsername()))
+            .explanation(EXPLANATION)
+            .fileExtension(FileExtension.of("txt"))
+            .export();
+
+        LinshareAPI user2API = linshareExtension.getAPIFor(USER_2);
+
+        assertThat(user2API.receivedShares())
+            .hasSize(1)
+            .allSatisfy(receivedShare -> assertThat(receivedShare.getDocument().getName()).endsWith(".txt"))
+            .allSatisfy(receivedShare -> assertThat(receivedShare.getDocument().getName()).startsWith(blobId.asString()))
+            .allSatisfy(receivedShare -> assertThat(receivedShare.getSender().getMail()).isEqualTo(USER_1.getUsername()));
+    }
+
+    @Test
+    void exportShouldFailWhenBlobDoesNotExist() {
+        BlobId blobId = blobIdFactory.randomId();
+
+        assertThatThrownBy(
+            () -> testee.blobId(blobId)
+                .with(new MailAddress(USER_2.getUsername()))
+                .explanation(EXPLANATION)
+                .fileExtension(FileExtension.of("txt"))
+                .export())
+            .isInstanceOf(BlobExportMechanism.BlobExportException.class);
+    }
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message