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 2020/01/02 22:53:24 UTC

[sling-org-apache-sling-app-cms] 09/09: Fixed SLING-8958 - Adds default thumbnail on error

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 d69a3e14be1fccb90a369905c29887df7c6ce5df
Author: Dan Klco <dk...@apache.org>
AuthorDate: Thu Jan 2 17:53:02 2020 -0500

    Fixed SLING-8958 - Adds default thumbnail on error
---
 .../cms/transformer/internal/TransformServlet.java | 23 ++++++++++++--
 .../internal/TransformServletConfig.java           | 30 +++++++++++++++++++
 .../main/resources/OSGI-INF/l10n/bundle.properties | 35 ++++++++++++++++++++++
 transformer/src/test/resources/content.json        |  2 +-
 4 files changed, 86 insertions(+), 4 deletions(-)

diff --git a/transformer/src/main/java/org/apache/sling/cms/transformer/internal/TransformServlet.java b/transformer/src/main/java/org/apache/sling/cms/transformer/internal/TransformServlet.java
index 06a1715..5f6fcde 100644
--- a/transformer/src/main/java/org/apache/sling/cms/transformer/internal/TransformServlet.java
+++ b/transformer/src/main/java/org/apache/sling/cms/transformer/internal/TransformServlet.java
@@ -26,6 +26,7 @@ import java.util.Iterator;
 import java.util.Map;
 
 import javax.jcr.query.Query;
+import javax.servlet.RequestDispatcher;
 import javax.servlet.Servlet;
 import javax.servlet.ServletException;
 
@@ -34,6 +35,7 @@ import org.apache.jackrabbit.JcrConstants;
 import org.apache.poi.util.IOUtils;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.request.RequestDispatcherOptions;
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -43,8 +45,10 @@ import org.apache.sling.cms.CMSConstants;
 import org.apache.sling.cms.transformer.OutputFileFormat;
 import org.apache.sling.cms.transformer.Transformation;
 import org.apache.sling.cms.transformer.Transformer;
+import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.metatype.annotations.Designate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,10 +56,11 @@ import org.slf4j.LoggerFactory;
  * A servlet to transform images using the FileThumbnailTransformer API. Can be
  * invoked using the syntax:
  * 
- * /content/file/path.jpg.transform/command-param1-param2/command2-param1-param2.png
+ * /content/file/path.jpg.transform/transformation-name.png
  */
 @Component(service = { Servlet.class }, property = { "sling.servlet.extensions=transform",
         "sling.servlet.resourceTypes=sling:File", "sling.servlet.resourceTypes=nt:file" })
+@Designate(ocd = TransformServletConfig.class)
 public class TransformServlet extends SlingSafeMethodsServlet {
 
     private static final Logger log = LoggerFactory.getLogger(TransformServlet.class);
@@ -67,6 +72,13 @@ public class TransformServlet extends SlingSafeMethodsServlet {
     private transient TransformationServiceUser transformationServiceUser;
 
     private transient Transformer transformer;
+    
+    private TransformServletConfig config;
+    
+    @Activate
+    public void activate(TransformServletConfig config) {
+        this.config = config;
+    }
 
     @Override
     protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
@@ -74,6 +86,7 @@ public class TransformServlet extends SlingSafeMethodsServlet {
         log.trace("doGet");
 
         String name = StringUtils.substringBeforeLast(request.getRequestPathInfo().getSuffix(), ".");
+        response.setHeader("Content-Disposition", "filename=" + request.getResource().getName());
         String format = StringUtils.substringAfterLast(request.getRequestPathInfo().getSuffix(), ".");
         log.debug("Transforming resource: {} with transformation: {} to {}", request.getResource(), name, format);
         String original = response.getContentType();
@@ -113,8 +126,12 @@ public class TransformServlet extends SlingSafeMethodsServlet {
             }
         } catch (Exception e) {
             log.error("Exception rendering transformed resource", e);
-            response.setContentType(original);
-            response.sendError(500, "Could not transform image with transformation: " + name);
+            response.setStatus(500);
+            RequestDispatcherOptions op = new RequestDispatcherOptions();
+            op.setReplaceSuffix(config.errorSuffix());
+            op.setReplaceSelectors("transform");
+            RequestDispatcher disp = request.getRequestDispatcher(config.errorResourcePath(), op);
+            disp.forward(request, response);
 
         }
     }
diff --git a/transformer/src/main/java/org/apache/sling/cms/transformer/internal/TransformServletConfig.java b/transformer/src/main/java/org/apache/sling/cms/transformer/internal/TransformServletConfig.java
new file mode 100644
index 0000000..ee2d970
--- /dev/null
+++ b/transformer/src/main/java/org/apache/sling/cms/transformer/internal/TransformServletConfig.java
@@ -0,0 +1,30 @@
+/*
+ * 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.transformer.internal;
+
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+@ObjectClassDefinition(name = "%transformservlet.name", description = "%transformservlet.description", localization = "OSGI-INF/l10n/bundle")
+public @interface TransformServletConfig {
+
+    @AttributeDefinition(name = "%transformservlet.errorResourcePath.name", description = "%transformservlet.errorResourcePath.description")
+    String errorResourcePath() default "/static/sling-cms/thumbnails/file.png";
+    
+    @AttributeDefinition(name = "%transformservlet.errorSuffix.name", description = "%transformservlet.errorSuffix.description")
+    String errorSuffix() default "/sling-cms-thumbnail.png";
+}
diff --git a/transformer/src/main/resources/OSGI-INF/l10n/bundle.properties b/transformer/src/main/resources/OSGI-INF/l10n/bundle.properties
new file mode 100644
index 0000000..8e4e008
--- /dev/null
+++ b/transformer/src/main/resources/OSGI-INF/l10n/bundle.properties
@@ -0,0 +1,35 @@
+#
+#  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.
+#
+
+#
+# This file contains localization strings for configuration labels and
+# descriptions as used in the metatype.xml descriptor generated by the
+# the Sling SCR plugin
+
+## Transformation Servlet Entries
+transformservlet.name=Apache Sling CMS - Transform Servlet
+transformservlet.description=Servlet for transforming files into image renditions
+
+transformservlet.errorResourcePath.name=Error Resource Path
+transformservlet.errorResourcePath.name.description=The path of the resource to forward \
+if an error occurs transforming the provided resource
+
+transformservlet.errorSuffix.name=Error Suffix
+transformservlet.errorSuffix.name.description=The suffix to forward to \
+if an error occurs transforming the provided resource
diff --git a/transformer/src/test/resources/content.json b/transformer/src/test/resources/content.json
index b7c79ed..3591ef7 100644
--- a/transformer/src/test/resources/content.json
+++ b/transformer/src/test/resources/content.json
@@ -95,7 +95,7 @@
                     }
                 },
                 "apache.png": {
-                    "jcr:primaryType": "sling:File"
+                    "jcr:primaryType": "nt:file"
                 },
                 "sling.pdf": {
                     "jcr:primaryType": "sling:File"