You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/05/10 11:30:14 UTC

[james-project] 01/17: JAMES-2712 Add the download file method to Linshare API

This is an automated email from the ASF dual-hosted git repository.

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

commit df652ebc1359a1b6707ff4245020838fcd98ad37
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Tue May 7 17:02:45 2019 +0700

    JAMES-2712 Add the download file method to Linshare API
---
 .../org/apache/james/linshare/client/Document.java |  2 +-
 .../apache/james/linshare/ByteArrayDecoder.java    | 45 ++++++++++++
 .../org/apache/james/linshare/CombinedDecoder.java | 83 ++++++++++++++++++++++
 .../apache/james/linshare/LinshareExtension.java   | 19 ++++-
 4 files changed, 147 insertions(+), 2 deletions(-)

diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/client/Document.java b/third-party/linshare/src/main/java/org/apache/james/linshare/client/Document.java
index ef87ed6..229de5b 100644
--- a/third-party/linshare/src/main/java/org/apache/james/linshare/client/Document.java
+++ b/third-party/linshare/src/main/java/org/apache/james/linshare/client/Document.java
@@ -28,7 +28,7 @@ import com.google.common.base.Preconditions;
 
 public class Document {
 
-    static class DocumentId {
+    public static class DocumentId {
         private final UUID id;
 
         DocumentId(UUID id) {
diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/ByteArrayDecoder.java b/third-party/linshare/src/test/java/org/apache/james/linshare/ByteArrayDecoder.java
new file mode 100644
index 0000000..f5ce658
--- /dev/null
+++ b/third-party/linshare/src/test/java/org/apache/james/linshare/ByteArrayDecoder.java
@@ -0,0 +1,45 @@
+/****************************************************************
+ * 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.IOException;
+import java.lang.reflect.Type;
+
+import com.google.common.base.Preconditions;
+
+import feign.FeignException;
+import feign.Response;
+import feign.Util;
+
+public class ByteArrayDecoder implements CombinedDecoder.SingleTypeDecoder {
+    private static final Type BYTE_TYPE = byte[].class;
+
+    @Override
+    public Type handledType() {
+        return BYTE_TYPE;
+    }
+
+    @Override
+    public Object decode(Response response, Type type) throws IOException, FeignException {
+        Preconditions.checkArgument(type.equals(BYTE_TYPE));
+
+        return Util.toByteArray(response.body().asInputStream());
+    }
+}
diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/CombinedDecoder.java b/third-party/linshare/src/test/java/org/apache/james/linshare/CombinedDecoder.java
new file mode 100644
index 0000000..d6c96e6
--- /dev/null
+++ b/third-party/linshare/src/test/java/org/apache/james/linshare/CombinedDecoder.java
@@ -0,0 +1,83 @@
+/****************************************************************
+ * 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.IOException;
+import java.lang.reflect.Type;
+
+import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;
+
+import com.google.common.base.Preconditions;
+
+import feign.FeignException;
+import feign.Response;
+import feign.codec.Decoder;
+
+public class CombinedDecoder implements Decoder {
+    interface SingleTypeDecoder extends Decoder {
+        Type handledType();
+    }
+
+    static class Builder {
+        @FunctionalInterface
+        public interface DefaultDecoder {
+            ReadyToBuild defaultDecoder(Decoder decoder);
+        }
+
+        static class ReadyToBuild {
+            private final Decoder defaultDecoder;
+            private final ImmutableMap.Builder<Type, Decoder> decoders;
+
+            ReadyToBuild(Decoder decoder) {
+                this.defaultDecoder = decoder;
+                this.decoders = ImmutableMap.builder();
+            }
+
+            ReadyToBuild registerSingleTypeDecoder(SingleTypeDecoder decoder) {
+                Preconditions.checkNotNull(decoder);
+                decoders.put(decoder.handledType(), decoder);
+                return this;
+            }
+
+            CombinedDecoder build() {
+                Preconditions.checkNotNull(defaultDecoder);
+                return new CombinedDecoder(defaultDecoder, decoders.build());
+            }
+        }
+    }
+
+    static Builder.DefaultDecoder builder() {
+        return Builder.ReadyToBuild::new;
+    }
+
+    private final Decoder defaultDecoder;
+    private final ImmutableMap<Type, Decoder> decoders;
+
+    private CombinedDecoder(Decoder defaultDecoder, ImmutableMap<Type, Decoder> decoders) {
+        this.defaultDecoder = defaultDecoder;
+        this.decoders = decoders;
+    }
+
+    @Override
+    public Object decode(Response response, Type type) throws IOException, FeignException {
+        return decoders.getOrDefault(type, defaultDecoder)
+            .decode(response, type);
+    }
+}
diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareExtension.java b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareExtension.java
index 620d8ee..488083b 100644
--- a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareExtension.java
+++ b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareExtension.java
@@ -25,6 +25,7 @@ import static org.apache.james.linshare.client.LinshareAPI.Headers.ACCEPT_APPLIC
 import java.util.List;
 import java.util.Optional;
 
+import org.apache.james.linshare.client.Document;
 import org.apache.james.linshare.client.LinshareAPI;
 import org.apache.james.linshare.client.User;
 import org.apache.james.utils.FakeSmtp;
@@ -36,6 +37,7 @@ import com.github.fge.lambdas.Throwing;
 import feign.Feign;
 import feign.Headers;
 import feign.Logger;
+import feign.Param;
 import feign.RequestLine;
 import feign.auth.BasicAuthRequestInterceptor;
 import feign.form.FormEncoder;
@@ -47,6 +49,9 @@ public class LinshareExtension implements BeforeEachCallback {
 
     private interface LinshareAPIForTesting {
 
+        String CONTENT_DISPOSITION_ATTACHMENT = "Content-Disposition: attachment; filename=\"{filename}\"";
+        String CONTENT_TYPE_APPLICATION_OCTET_STREAM = "Content-Type: application/octet-stream";
+
         static LinshareAPIForTesting from(LinshareFixture.Credential credential, Linshare linshare) {
 
             return Feign.builder()
@@ -54,7 +59,10 @@ public class LinshareExtension implements BeforeEachCallback {
                 .logger(new Slf4jLogger(LinshareAPIForTesting.class))
                 .logLevel(Logger.Level.FULL)
                 .encoder(new FormEncoder(new JacksonEncoder()))
-                .decoder(new JacksonDecoder())
+                .decoder(CombinedDecoder.builder()
+                    .defaultDecoder(new JacksonDecoder())
+                    .registerSingleTypeDecoder(new ByteArrayDecoder())
+                    .build())
                 .target(LinshareAPIForTesting.class, linshare.getUrl());
         }
 
@@ -65,6 +73,10 @@ public class LinshareExtension implements BeforeEachCallback {
         @RequestLine("GET /linshare/webservice/rest/user/v2/users")
         @Headers(ACCEPT_APPLICATION_JSON)
         List<User> allUsers();
+
+        @RequestLine("GET /linshare/webservice/rest/user/v2/received_shares/{documentId}/download")
+        @Headers({ CONTENT_TYPE_APPLICATION_OCTET_STREAM, CONTENT_DISPOSITION_ATTACHMENT })
+        byte[] downloadShare(@Param("documentId") String documentId, @Param("filename") String filename);
     }
 
     private final Linshare linshare = LinshareSingleton.singleton;
@@ -92,6 +104,11 @@ public class LinshareExtension implements BeforeEachCallback {
             .build();
     }
 
+    public byte[] downloadSharedFile(LinshareFixture.Credential credential, Document.DocumentId document, String filename) {
+        return LinshareAPIForTesting.from(credential, linshare)
+            .downloadShare(document.asString(), filename);
+    }
+
     private void deleteAllUsersDocuments() {
         LinshareAPIForTesting.from(USER_1, linshare)
             .allUsers()


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