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 2018/10/03 14:01:38 UTC

[sling-org-apache-sling-app-cms] branch master updated: Fixing SLING-7963 and a number of small errors I ran into while investigating it

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


The following commit(s) were added to refs/heads/master by this push:
     new ae114f7  Fixing SLING-7963 and a number of small errors I ran into while investigating it
ae114f7 is described below

commit ae114f70acb54fb33023e85d7fbbb5cb5678daa9
Author: Dan Klco <dk...@apache.org>
AuthorDate: Wed Oct 3 10:01:28 2018 -0400

    Fixing SLING-7963 and a number of small errors I ran into while
    investigating it
---
 core/pom.xml                                       |  23 +--
 .../apache/sling/cms/core/models/ErrorHandler.java | 195 ++++++++++++++++-----
 .../apache/sling/cms/core/models/package-info.java |   4 +-
 pom.xml                                            |  25 +--
 .../components/cms/editconfig/editconfig.jsp       |   4 +-
 .../libs/sling/servlet/errorhandler/default.jsp    |   3 +-
 6 files changed, 182 insertions(+), 72 deletions(-)

diff --git a/core/pom.xml b/core/pom.xml
index f1a2568..640e9f2 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -1,15 +1,12 @@
 <?xml version="1.0" encoding="ISO-8859-1"?>
-<!-- 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. -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<!-- 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. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
+>
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <artifactId>org.apache.sling.cms</artifactId>
@@ -185,5 +182,9 @@
             <artifactId>org.apache.sling.cms.api</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.jackrabbit</groupId>
+            <artifactId>oak-core</artifactId>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
diff --git a/core/src/main/java/org/apache/sling/cms/core/models/ErrorHandler.java b/core/src/main/java/org/apache/sling/cms/core/models/ErrorHandler.java
index ac0ba8d..9c738b0 100644
--- a/core/src/main/java/org/apache/sling/cms/core/models/ErrorHandler.java
+++ b/core/src/main/java/org/apache/sling/cms/core/models/ErrorHandler.java
@@ -20,14 +20,21 @@ import java.util.HashMap;
 
 import javax.annotation.PostConstruct;
 import javax.inject.Named;
+import javax.servlet.RequestDispatcher;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
 import org.apache.sling.api.SlingConstants;
 import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.request.RequestDispatcherOptions;
+import org.apache.sling.api.request.RequestPathInfo;
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
 import org.apache.sling.cms.CMSUtils;
 import org.apache.sling.cms.Site;
 import org.apache.sling.cms.SiteManager;
@@ -51,30 +58,159 @@ import org.slf4j.LoggerFactory;
 @Model(adaptables = SlingHttpServletRequest.class)
 public class ErrorHandler {
 
-    public static final String SERVICE_USER_NAME = "sling-cms-error";
+    private static class GetRequest extends SlingHttpServletRequestWrapper {
+
+        public GetRequest(SlingHttpServletRequest wrappedRequest) {
+            super(wrappedRequest);
+        }
+
+        @Override
+        public String getMethod() {
+            return "GET";
+        }
+
+        @Override
+        public RequestPathInfo getRequestPathInfo() {
+            return new HTMLRequestPathInfo(super.getRequestPathInfo());
+        }
+    }
+
+    private static class HTMLRequestPathInfo implements RequestPathInfo {
+
+        private RequestPathInfo info;
+
+        public HTMLRequestPathInfo(RequestPathInfo info) {
+            this.info = info;
+        }
+
+        @Override
+        public String getResourcePath() {
+            return info.getResourcePath();
+        }
+
+        @Override
+        public String getExtension() {
+            return "html";
+        }
+
+        @Override
+        public String getSelectorString() {
+            return "";
+        }
+
+        @Override
+        public String[] getSelectors() {
+            return new String[0];
+        }
+
+        @Override
+        public String getSuffix() {
+            return "";
+        }
+
+        @Override
+        public Resource getSuffixResource() {
+            return null;
+        }
+
+    }
+
+    /**
+     * The page to fall back to if there is not an error page for the specific code
+     */
+    public static final String DEFAULT_ERROR_PAGE = "default";
 
     private static final Logger log = LoggerFactory.getLogger(ErrorHandler.class);
 
+    /**
+     * Service User Name for the Error Handler
+     */
+    public static final String SERVICE_USER_NAME = "sling-cms-error";
+
+    /**
+     * The subpath under which to find the error pages
+     */
+    public static final String SITE_ERRORS_SUBPATH = "errors/";
+
+    /**
+     * Path under which the error pages for Sling CMS can be found.
+     */
+    public static final String SLING_CMS_ERROR_PATH = "/content/sling-cms/errorhandling/";
+
     @RequestAttribute
     @Named(SlingConstants.ERROR_STATUS)
     @Optional
-    @Default(intValues = 500)
+    @Default(intValues = HttpServletResponse.SC_INTERNAL_SERVER_ERROR)
     private Integer errorCode;
 
-    private SlingHttpServletRequest slingRequest;
+    @OSGiService
+    private ResourceResolverFactory factory;
 
     private Resource handler;
 
     @SlingObject
-    private HttpServletResponse response;
+    private SlingHttpServletResponse slingResponse;
 
-    @OSGiService
-    private ResourceResolverFactory factory;
+    private SlingHttpServletRequest slingRequest;
 
     public ErrorHandler(SlingHttpServletRequest slingRequest) {
         this.slingRequest = slingRequest;
     }
 
+    private void calculateErrorCode(ResourceResolver resolver) {
+        if (errorCode == HttpServletResponse.SC_NOT_FOUND) {
+            log.debug("Validating the resource does not exist for all users");
+            ResourceResolver adminResolver = null;
+            try {
+                adminResolver = factory.getServiceResourceResolver(new HashMap<String, Object>() {
+                    private static final long serialVersionUID = 1L;
+                    {
+                        put(ResourceResolverFactory.SUBSERVICE, SERVICE_USER_NAME);
+                    }
+                });
+                Resource pResource = adminResolver.resolve(slingRequest, slingRequest.getResource().getPath());
+                if (!CMSUtils.isPublished(pResource) || pResource.isResourceType(Resource.RESOURCE_TYPE_NON_EXISTING)) {
+                    errorCode = HttpServletResponse.SC_NOT_FOUND;
+                } else if (UserConstants.DEFAULT_ANONYMOUS_ID.equals(resolver.getUserID())) {
+                    errorCode = HttpServletResponse.SC_UNAUTHORIZED;
+                } else {
+                    errorCode = HttpServletResponse.SC_FORBIDDEN;
+                }
+            } catch (LoginException e) {
+                log.error("Exception retrieving service user", e);
+            } finally {
+                if (adminResolver != null) {
+                    adminResolver.close();
+                }
+            }
+        }
+    }
+
+    private void doInclude() {
+
+        Resource handlerContent = handler.getChild(JcrConstants.JCR_CONTENT);
+        if (handlerContent != null) {
+            log.debug("Including handler {}", handlerContent);
+
+            RequestDispatcherOptions rdo = new RequestDispatcherOptions();
+            rdo.setReplaceSelectors("");
+            rdo.setReplaceSuffix("");
+            rdo.setForceResourceType(handlerContent.getResourceType());
+            final RequestDispatcher dispatcher = slingRequest.getRequestDispatcher(handlerContent, rdo);
+            if (dispatcher != null) {
+                try {
+                    dispatcher.include(new GetRequest(slingRequest), slingResponse);
+                } catch (Exception e) {
+                    log.debug("Exception swallowed while including error page", e);
+                }
+            } else {
+                log.warn("Failed to get request dispatcher for handler {}", handler.getPath());
+            }
+        } else {
+            log.warn("Error hander {} content is null", handler);
+        }
+    }
+
     @PostConstruct
     public void init() {
 
@@ -96,11 +232,10 @@ public class ErrorHandler {
                 Site site = siteMgr.getSite();
                 log.debug("Checking for error pages in the site {}", site.getPath());
 
-                handler = site.getResource().getChild("errors/" + errorCode.toString());
+                handler = site.getResource().getChild(SITE_ERRORS_SUBPATH + errorCode.toString());
                 if (handler == null) {
-                    handler = site.getResource().getChild("errors/default");
+                    handler = site.getResource().getChild(SITE_ERRORS_SUBPATH + DEFAULT_ERROR_PAGE);
                 }
-
                 if (handler != null) {
                     log.debug("Using error handler {}", handler);
                 } else {
@@ -113,49 +248,21 @@ public class ErrorHandler {
 
         if (handler == null) {
             log.debug("Using Sling CMS default error pages");
-            handler = resolver.getResource("/content/sling-cms/errorhandling/" + errorCode.toString());
+            handler = resolver.getResource(SLING_CMS_ERROR_PATH + errorCode.toString());
             if (handler == null) {
-                handler = resolver.getResource("/content/sling-cms/errorhandling/default");
+                handler = resolver.getResource(SLING_CMS_ERROR_PATH + DEFAULT_ERROR_PAGE);
             }
             log.debug("Using Sling CMS error handler {}", handler);
         }
 
         log.debug("Sending error {}", errorCode);
-        response.setStatus(errorCode);
+        slingResponse.reset();
+        slingResponse.setContentType("text/html");
+        slingResponse.setStatus(errorCode);
 
-        log.debug("Error handler initialized successfully!");
-    }
+        doInclude();
 
-    private void calculateErrorCode(ResourceResolver resolver) {
-        if (errorCode == 404) {
-            log.debug("Validating the resource does not exist for all users");
-            ResourceResolver adminResolver;
-            try {
-                adminResolver = factory.getServiceResourceResolver(new HashMap<String, Object>() {
-                    private static final long serialVersionUID = 1L;
-                    {
-                        put(ResourceResolverFactory.SUBSERVICE, SERVICE_USER_NAME);
-                    }
-                });
-                Resource pResource = adminResolver.resolve(slingRequest, slingRequest.getResource().getPath());
-                if (!CMSUtils.isPublished(pResource)) {
-                    errorCode = 404;
-                } else if ("anonymous".equals(resolver.getUserID())) {
-                    errorCode = 401;
-                } else {
-                    errorCode = 403;
-                }
-            } catch (LoginException e) {
-                log.error("Exception retrieving service user", e);
-            }
-        }
-    }
-
-    public Resource getHandler() {
-        return handler;
+        log.debug("Error handler initialized successfully!");
     }
 
-    public int getErrorCode() {
-        return errorCode;
-    }
 }
diff --git a/core/src/main/java/org/apache/sling/cms/core/models/package-info.java b/core/src/main/java/org/apache/sling/cms/core/models/package-info.java
index 6579c9c..a8c656b 100644
--- a/core/src/main/java/org/apache/sling/cms/core/models/package-info.java
+++ b/core/src/main/java/org/apache/sling/cms/core/models/package-info.java
@@ -19,10 +19,8 @@
 
 /**
  * Package with all of the core models used to support the Sling reference CMS
- *
- * @version 0.9.0
  */
-@Version("1.0.0")
+@Version("2.0.0")
 package org.apache.sling.cms.core.models;
 
 import org.osgi.annotation.versioning.Version;
diff --git a/pom.xml b/pom.xml
index 34540a6..f9e2671 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,15 +1,12 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!-- 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. -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<!-- 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. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+>
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.apache.sling</groupId>
@@ -167,6 +164,12 @@
                 <version>16.0.2</version>
                 <scope>provided</scope>
             </dependency>
+            <dependency>
+                <groupId>org.apache.jackrabbit</groupId>
+                <artifactId>oak-core</artifactId>
+                <version>1.6.8</version>
+                <scope>provided</scope>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 
diff --git a/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/editconfig/editconfig.jsp b/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/editconfig/editconfig.jsp
index b688572..69e44fa 100644
--- a/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/editconfig/editconfig.jsp
+++ b/ui/src/main/resources/jcr_root/libs/sling-cms/components/cms/editconfig/editconfig.jsp
@@ -17,4 +17,6 @@
  * under the License.
  */ --%>
 <%@include file="/libs/sling-cms/global.jsp"%>
-<sling:include resource="${slingRequest.requestPathInfo.suffixResource}" />
\ No newline at end of file
+<c:if test="${slingRequest.requestPathInfo.suffixResource != null}">
+    <sling:include resource="${slingRequest.requestPathInfo.suffixResource}" />
+</c:if>
\ No newline at end of file
diff --git a/ui/src/main/resources/jcr_root/libs/sling/servlet/errorhandler/default.jsp b/ui/src/main/resources/jcr_root/libs/sling/servlet/errorhandler/default.jsp
index e2b9f80..55f605a 100644
--- a/ui/src/main/resources/jcr_root/libs/sling/servlet/errorhandler/default.jsp
+++ b/ui/src/main/resources/jcr_root/libs/sling/servlet/errorhandler/default.jsp
@@ -15,5 +15,4 @@
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
- */ --%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@include file="/libs/sling-cms/global.jsp"%><sling:adaptTo var="errorHandler" adaptable="${slingRequest}" adaptTo="org.apache.sling.cms.core.models.ErrorHandler" />
-<sling:include path="${sling:getRelativeResource(errorHandler.handler,'jcr:content').path}.html" resourceType="${sling:getRelativeResource(errorHandler.handler,'jcr:content').resourceType}" />
\ No newline at end of file
+ */ --%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@include file="/libs/sling-cms/global.jsp"%><sling:adaptTo var="errorHandler" adaptable="${slingRequest}" adaptTo="org.apache.sling.cms.core.models.ErrorHandler" />
\ No newline at end of file