You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2019/07/02 18:43:52 UTC

[sling-org-apache-sling-app-cms] 01/02: Adding a servlet to download files rather than just linking to them

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

dklco pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-app-cms.git

commit 2167c18eb9b2f60c14d84679ee08e77237d04467
Author: Dan Klco <dk...@apache.org>
AuthorDate: Tue Jul 2 14:43:25 2019 -0400

    Adding a servlet to download files rather than just linking to them
---
 .../internal/servlets/DownloadFileServlet.java     | 28 +++++++++++++++++++++-
 .../libs/sling-cms/content/file/download.json      |  4 ++++
 .../libs/sling-cms/content/site/content.json       |  6 +++--
 .../libs/sling-cms/content/static/content.json     |  6 +++--
 4 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/core/src/main/java/org/apache/sling/cms/core/internal/servlets/DownloadFileServlet.java b/core/src/main/java/org/apache/sling/cms/core/internal/servlets/DownloadFileServlet.java
index 124060c..c0ffb28 100644
--- a/core/src/main/java/org/apache/sling/cms/core/internal/servlets/DownloadFileServlet.java
+++ b/core/src/main/java/org/apache/sling/cms/core/internal/servlets/DownloadFileServlet.java
@@ -1,12 +1,31 @@
+/*
+ * 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.sling.cms.core.internal.servlets;
 
 import java.io.IOException;
+import java.io.InputStream;
 
 import javax.servlet.Servlet;
 import javax.servlet.ServletException;
 
+import org.apache.poi.util.IOUtils;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
 import org.osgi.service.component.annotations.Component;
 
@@ -16,9 +35,16 @@ public class DownloadFileServlet extends SlingSafeMethodsServlet {
 
     private static final long serialVersionUID = 6234007100684499058L;
 
+    @Override
     protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
             throws ServletException, IOException {
-        handleMethodNotImplemented(request, response);
+        Resource suffixResource = request.getRequestPathInfo().getSuffixResource();
+        if (suffixResource != null) {
+            response.setHeader("Content-Disposition", "attachment; " + suffixResource.getName());
+            IOUtils.copy(suffixResource.adaptTo(InputStream.class), response.getOutputStream());
+        } else {
+            response.sendError(404);
+        }
     }
 
 }
diff --git a/ui/src/main/resources/jcr_root/libs/sling-cms/content/file/download.json b/ui/src/main/resources/jcr_root/libs/sling-cms/content/file/download.json
new file mode 100644
index 0000000..26a2209
--- /dev/null
+++ b/ui/src/main/resources/jcr_root/libs/sling-cms/content/file/download.json
@@ -0,0 +1,4 @@
+{
+    "jcr:primaryType": "nt:unstructured",
+    "sling:resourceType": "sling-cms/file/download"
+}
diff --git a/ui/src/main/resources/jcr_root/libs/sling-cms/content/site/content.json b/ui/src/main/resources/jcr_root/libs/sling-cms/content/site/content.json
index 1241863..672c375 100644
--- a/ui/src/main/resources/jcr_root/libs/sling-cms/content/site/content.json
+++ b/ui/src/main/resources/jcr_root/libs/sling-cms/content/site/content.json
@@ -189,7 +189,8 @@
                                     "jcr:primaryType": "nt:unstructured",
                                     "modal": false,
                                     "title": "Download file",
-                                    "icon": "download"
+                                    "icon": "download",
+                                    "prefix": "/cms/file/download.html"
                                 },
                                 "references": {
                                     "jcr:primaryType": "nt:unstructured",
@@ -265,7 +266,8 @@
                                     "jcr:primaryType": "nt:unstructured",
                                     "modal": false,
                                     "title": "Download file",
-                                    "icon": "download"
+                                    "icon": "download",
+                                    "prefix": "/cms/file/download.html"
                                 },
                                 "references": {
                                     "jcr:primaryType": "nt:unstructured",
diff --git a/ui/src/main/resources/jcr_root/libs/sling-cms/content/static/content.json b/ui/src/main/resources/jcr_root/libs/sling-cms/content/static/content.json
index 77a36e4..034d36e 100644
--- a/ui/src/main/resources/jcr_root/libs/sling-cms/content/static/content.json
+++ b/ui/src/main/resources/jcr_root/libs/sling-cms/content/static/content.json
@@ -111,7 +111,8 @@
                                     "jcr:primaryType": "nt:unstructured",
                                     "modal": false,
                                     "title": "Download file",
-                                    "icon": "download"
+                                    "icon": "download",
+                                    "prefix": "/cms/file/download.html"
                                 },
                                 "movecopy": {
                                     "jcr:primaryType": "nt:unstructured",
@@ -182,7 +183,8 @@
                                     "jcr:primaryType": "nt:unstructured",
                                     "modal": false,
                                     "title": "Download file",
-                                    "icon": "download"
+                                    "icon": "download",
+                                    "prefix": "/cms/file/download.html"
                                 },
                                 "references": {
                                     "jcr:primaryType": "nt:unstructured",