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-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new a2f0e05  Adding error handling to the CMS
a2f0e05 is described below

commit a2f0e05d0824f570facef3cf58a87e01a326a056
Author: Dan Klco <daniel.k...@gmail.com>
AuthorDate: Tue Mar 6 16:33:35 2018 -0500

    Adding error handling to the CMS
---
 .../apache/sling/cms/core/models/ErrorHandler.java | 95 ++++++++++++++++++++++
 cms/ui/pom.xml                                     |  1 +
 .../libs/sling-cms/components/pages/error.json     |  5 ++
 .../libs/sling-cms/components/pages/error/nav.jsp  | 30 +++++++
 .../libs/sling-cms/content/errorhandling/404.json  | 17 ++++
 .../sling-cms/content/errorhandling/default.json   | 17 ++++
 .../libs/sling/servlet/errorhandler/Throwable.jsp  | 22 +++++
 .../libs/sling/servlet/errorhandler/default.jsp    | 22 +++++
 8 files changed, 209 insertions(+)

diff --git 
a/cms/core/src/main/java/org/apache/sling/cms/core/models/ErrorHandler.java 
b/cms/core/src/main/java/org/apache/sling/cms/core/models/ErrorHandler.java
new file mode 100644
index 0000000..eb2333c
--- /dev/null
+++ b/cms/core/src/main/java/org/apache/sling/cms/core/models/ErrorHandler.java
@@ -0,0 +1,95 @@
+/*
+ * 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.models;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Named;
+
+import org.apache.sling.api.SlingConstants;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.models.annotations.Default;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Optional;
+import org.apache.sling.models.annotations.injectorspecific.RequestAttribute;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Sling Model for retrieving an error handler based on the specified Sling
+ * Request. Checks if the specified resource is contained within a Sling site
+ * and if so, will display the error page found at
+ * [site-root]/errors/[error-code] or [site-root]/errors/default
+ */
+@Model(adaptables = SlingHttpServletRequest.class)
+public class ErrorHandler {
+
+       private static final Logger log = 
LoggerFactory.getLogger(ErrorHandler.class);
+
+       @RequestAttribute
+       @Named(SlingConstants.ERROR_STATUS)
+       @Optional
+       @Default(intValues = 500)
+       private Integer errorCode;
+
+       private SlingHttpServletRequest slingRequest;
+
+       private Resource handler;
+
+       public ErrorHandler(SlingHttpServletRequest slingRequest) {
+               this.slingRequest = slingRequest;
+       }
+
+       @PostConstruct
+       public void init() {
+
+               Resource resource = slingRequest.getResource();
+               ResourceResolver resolver = slingRequest.getResourceResolver();
+
+               log.debug("Calculating error handling scripts for resource {} 
and error code ", resource);
+               SiteManager siteMgr = resource.adaptTo(SiteManager.class);
+               if (siteMgr != null && siteMgr.getSite() != null) {
+                       Site site = siteMgr.getSite();
+                       log.debug("Checking for error pages in the site {}", 
site.getPath());
+
+                       handler = site.getResource().getChild("errors/" + 
errorCode.toString());
+                       if (handler == null) {
+                               handler = 
site.getResource().getChild("errors/default");
+                       }
+
+                       if (handler != null) {
+                               log.debug("Using error handler {}", handler);
+                       } else {
+                               log.debug("No error page defined for site {}", 
site.getPath());
+                       }
+               }
+
+               if (handler == null) {
+                       log.debug("Using Sling CMS default error pages");
+                       handler = 
resolver.getResource("/libs/sling-cms/content/errorhandling/" + 
errorCode.toString());
+                       if (handler == null) {
+                               handler = 
resolver.getResource("/libs/sling-cms/content/errorhandling/default");
+                       }
+                       log.debug("Using Sling CMS error handler {}", handler);
+               }
+       }
+
+       public Resource getHandler() {
+               return handler;
+       }
+}
diff --git a/cms/ui/pom.xml b/cms/ui/pom.xml
index e38f24e..63e8022 100644
--- a/cms/ui/pom.xml
+++ b/cms/ui/pom.xml
@@ -98,6 +98,7 @@
                                                        
jcr_root/etc/clientlibs;overwrite=true;ignoreImportProviders:=xml;path:=/etc/clientlibs,
                                                        
jcr_root/etc/fileeditors;overwrite:=false;uninstall:=true;path:=/etc/fileeditors,
                                                        
jcr_root/etc/taxonomy;overwrite:=false;uninstall:=true;path:=/etc/taxonomy,
+                                                       
jcr_root/libs/sling/servlet/errorhandler;overwriteProperties:=true;overwrite:=true;uninstall:=true;path:=/libs/sling/servlet/errorhandler,
                                                        
jcr_root/libs/sling-cms;overwrite:=true;uninstall:=true;path:=/libs/sling-cms,
                                                        
jcr_root/etc/clientlibs/sling-cms;overwrite:=true;uninstall:=true;path:=/etc/clientlibs/sling-cms
                                                </Sling-Initial-Content>
diff --git 
a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/pages/error.json 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/pages/error.json
new file mode 100644
index 0000000..473f1ee
--- /dev/null
+++ 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/pages/error.json
@@ -0,0 +1,5 @@
+{
+    "jcr:primaryType" : "sling:Component",
+    "sling:resourceSuperType" : "sling-cms/components/pages/base",
+    "jcr:title": "CMS - Error Page"
+}
\ No newline at end of file
diff --git 
a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/pages/error/nav.jsp
 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/pages/error/nav.jsp
new file mode 100644
index 0000000..b2c6686
--- /dev/null
+++ 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/components/pages/error/nav.jsp
@@ -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.
+ */ --%>
+ <%@include file="/libs/sling-cms/global.jsp"%>
+<a class="Cell-Pad" href="https://sling.apache.org"; target="_blank" 
title="Visit the Apache Sling website">
+       <img src="/content/starter/img/sling-logo.svg" alt="Apache Sling Logo"/>
+</a>
+<h1>
+       <a href="/cms/start.html">
+               CMS
+       </a>
+</h1>
+<a class="Cell-Pad"href="https://apache.org"; target="_blank" id="asf-logo" 
title="Visit the Apache Software Foundation website">
+    <img src="/content/starter/img/asf-logo.svg" alt="Apache Software 
Foundation Logo">
+</a>
\ No newline at end of file
diff --git 
a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/content/errorhandling/404.json
 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/content/errorhandling/404.json
new file mode 100644
index 0000000..0518c56
--- /dev/null
+++ 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/content/errorhandling/404.json
@@ -0,0 +1,17 @@
+{
+       "jcr:primaryType": "sling:Page",
+       "jcr:content": {
+               "sling:resourceType": "sling-cms/components/pages/error",
+               "jcr:title": "Not Found",
+               "jcr:primaryType": "nt:unstructured",
+               "container": {
+                       "jcr:primaryType": "nt:unstructured",
+                       "sling:resourceType": 
"sling-cms/components/general/container",
+                       "richtext": {
+                               "jcr:primaryType": "nt:unstructured",
+                               "sling:resourceType": 
"sling-cms/components/general/richtext",
+                               "text": "<h3>Not Found</h3><p>The requested 
content was not found.</p>"
+                       }
+               }
+       }
+}
\ No newline at end of file
diff --git 
a/cms/ui/src/main/resources/jcr_root/libs/sling-cms/content/errorhandling/default.json
 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/content/errorhandling/default.json
new file mode 100644
index 0000000..d7054e8
--- /dev/null
+++ 
b/cms/ui/src/main/resources/jcr_root/libs/sling-cms/content/errorhandling/default.json
@@ -0,0 +1,17 @@
+{
+       "jcr:primaryType": "sling:Page",
+       "jcr:content": {
+               "sling:resourceType": "sling-cms/components/pages/error",
+               "jcr:title": "Exception",
+               "jcr:primaryType": "nt:unstructured",
+               "container": {
+                       "jcr:primaryType": "nt:unstructured",
+                       "sling:resourceType": 
"sling-cms/components/general/container",
+                       "richtext": {
+                               "jcr:primaryType": "nt:unstructured",
+                               "sling:resourceType": 
"sling-cms/components/general/richtext",
+                               "text": "<h3>Unexpected Exception</h3><p>An 
unexpected exception occurred rendering the response.</p>"
+                       }
+               }
+       }
+}
\ No newline at end of file
diff --git 
a/cms/ui/src/main/resources/jcr_root/libs/sling/servlet/errorhandler/Throwable.jsp
 
b/cms/ui/src/main/resources/jcr_root/libs/sling/servlet/errorhandler/Throwable.jsp
new file mode 100644
index 0000000..317c9a4
--- /dev/null
+++ 
b/cms/ui/src/main/resources/jcr_root/libs/sling/servlet/errorhandler/Throwable.jsp
@@ -0,0 +1,22 @@
+<%-- /*
+ * 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.
+ */ --%>
+<%@ 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
diff --git 
a/cms/ui/src/main/resources/jcr_root/libs/sling/servlet/errorhandler/default.jsp
 
b/cms/ui/src/main/resources/jcr_root/libs/sling/servlet/errorhandler/default.jsp
new file mode 100644
index 0000000..92e0d12
--- /dev/null
+++ 
b/cms/ui/src/main/resources/jcr_root/libs/sling/servlet/errorhandler/default.jsp
@@ -0,0 +1,22 @@
+<%-- /*
+ * 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.
+ */ --%>
+ <%@ 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

-- 
To stop receiving notification emails like this one, please contact
dk...@apache.org.

Reply via email to