You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2013/10/15 17:14:34 UTC

svn commit: r1532380 - in /myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main: java/org/apache/myfaces/tobago/example/test/ webapp/ webapp/WEB-INF/ webapp/test/error/

Author: lofwyr
Date: Tue Oct 15 15:14:34 2013
New Revision: 1532380

URL: http://svn.apache.org/r1532380
Log:
error handling example

Added:
    myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/ErrorTest.java
    myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/ErrorTestException.java
    myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/TestExceptionHandler.java
    myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/TestExceptionHandlerFactory.java
    myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/500.xhtml
      - copied, changed from r1523927, myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/404.xhtml
    myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/test/error/
    myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/test/error/display-exception.xhtml
    myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/test/error/throw-exception.xhtml
Modified:
    myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/404.xhtml
    myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/WEB-INF/faces-config.xml
    myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/WEB-INF/web.xml

Added: myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/ErrorTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/ErrorTest.java?rev=1532380&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/ErrorTest.java (added)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/ErrorTest.java Tue Oct 15 15:14:34 2013
@@ -0,0 +1,28 @@
+/*
+ * 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.myfaces.tobago.example.test;
+
+public class ErrorTest {
+
+  public String error() {
+    throw new ErrorTestException("This exception is thrown to test error pages");
+  }
+
+}

Added: myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/ErrorTestException.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/ErrorTestException.java?rev=1532380&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/ErrorTestException.java (added)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/ErrorTestException.java Tue Oct 15 15:14:34 2013
@@ -0,0 +1,27 @@
+/*
+ * 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.myfaces.tobago.example.test;
+
+public class ErrorTestException extends RuntimeException {
+
+  public ErrorTestException(String message) {
+    super(message);
+  }
+}

Added: myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/TestExceptionHandler.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/TestExceptionHandler.java?rev=1532380&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/TestExceptionHandler.java (added)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/TestExceptionHandler.java Tue Oct 15 15:14:34 2013
@@ -0,0 +1,69 @@
+/*
+ * 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.myfaces.tobago.example.test;
+
+import org.apache.commons.lang.exception.ExceptionUtils;
+
+import javax.faces.FacesException;
+import javax.faces.application.FacesMessage;
+import javax.faces.application.NavigationHandler;
+import javax.faces.context.ExceptionHandler;
+import javax.faces.context.ExceptionHandlerWrapper;
+import javax.faces.context.FacesContext;
+import javax.faces.event.ExceptionQueuedEvent;
+import javax.faces.event.ExceptionQueuedEventContext;
+import java.util.Iterator;
+
+public class TestExceptionHandler extends ExceptionHandlerWrapper {
+
+  private ExceptionHandler wrapped;
+
+  public TestExceptionHandler(ExceptionHandler wrapped) {
+    this.wrapped = wrapped;
+  }
+
+  @Override
+  public ExceptionHandler getWrapped() {
+    return wrapped;
+  }
+
+  @Override
+  public void handle() throws FacesException {
+
+    Iterator<ExceptionQueuedEvent> iterator = getUnhandledExceptionQueuedEvents().iterator();
+    while (iterator.hasNext()) {
+      ExceptionQueuedEvent event = iterator.next();
+      ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
+      Throwable cause = ExceptionUtils.getRootCause(context.getException());
+      if (cause instanceof ErrorTestException) {
+        FacesContext facesContext = FacesContext.getCurrentInstance();
+        NavigationHandler nav = facesContext.getApplication().getNavigationHandler();
+        try {
+          facesContext.addMessage(null, new FacesMessage("The expected exception was thrown!"));
+          nav.handleNavigation(facesContext, null, "/test/error/display-exception.xhtml");
+          facesContext.renderResponse();
+        } finally {
+          iterator.remove();
+        }
+      }
+    }
+    getWrapped().handle();
+  }
+}

Added: myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/TestExceptionHandlerFactory.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/TestExceptionHandlerFactory.java?rev=1532380&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/TestExceptionHandlerFactory.java (added)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/java/org/apache/myfaces/tobago/example/test/TestExceptionHandlerFactory.java Tue Oct 15 15:14:34 2013
@@ -0,0 +1,40 @@
+/*
+ * 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.myfaces.tobago.example.test;
+
+import javax.faces.context.ExceptionHandler;
+import javax.faces.context.ExceptionHandlerFactory;
+
+public class TestExceptionHandlerFactory extends ExceptionHandlerFactory {
+
+   private ExceptionHandlerFactory parent;
+
+   public TestExceptionHandlerFactory( ExceptionHandlerFactory parent ) {
+      this.parent = parent;
+   }
+
+   @Override
+   public ExceptionHandler getExceptionHandler() {
+      ExceptionHandler result = parent.getExceptionHandler();
+      result = new TestExceptionHandler(result);
+      return result;
+   }
+
+}

Modified: myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/404.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/404.xhtml?rev=1532380&r1=1532379&r2=1532380&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/404.xhtml (original)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/404.xhtml Tue Oct 15 15:14:34 2013
@@ -19,7 +19,7 @@
 <f:view xmlns:f="http://java.sun.com/jsf/core"
         xmlns:tc="http://myfaces.apache.org/tobago/component">
   <tc:loadBundle basename="overview" var="overviewBundle"/>
-  <tc:page label="Test" id="page" width="1000px" height="750px">
+  <tc:page label="404 - page not found" id="page" width="1000px" height="750px">
     <f:facet name="layout">
       <tc:gridLayout/>
     </f:facet>
@@ -31,9 +31,10 @@
 
       <tc:messages/>
       <tc:out value="The page was not found!"/>
+      <tc:out escape="false" value="&lt;h1>404 - page not found&lt;/h1>"/>
     </tc:panel>
 
-    <tc:script onload="LOG.error('404 - page not found.')"/>
+    <tc:script onload="LOG.error('404 - page not found')"/>
 
   </tc:page>
 </f:view>

Copied: myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/500.xhtml (from r1523927, myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/404.xhtml)
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/500.xhtml?p2=myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/500.xhtml&p1=myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/404.xhtml&r1=1523927&r2=1532380&rev=1532380&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/404.xhtml (original)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/500.xhtml Tue Oct 15 15:14:34 2013
@@ -19,7 +19,7 @@
 <f:view xmlns:f="http://java.sun.com/jsf/core"
         xmlns:tc="http://myfaces.apache.org/tobago/component">
   <tc:loadBundle basename="overview" var="overviewBundle"/>
-  <tc:page label="Test" id="page" width="1000px" height="750px">
+  <tc:page label="500 - server error" id="page" width="1000px" height="750px">
     <f:facet name="layout">
       <tc:gridLayout/>
     </f:facet>
@@ -30,10 +30,10 @@
       </f:facet>
 
       <tc:messages/>
-      <tc:out value="The page was not found!"/>
+      <tc:out escape="false" value="&lt;h1>500 - server error&lt;/h1>"/>
     </tc:panel>
 
-    <tc:script onload="LOG.error('404 - page not found.')"/>
+    <tc:script onload="LOG.error('500 - server error')"/>
 
   </tc:page>
 </f:view>

Modified: myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/WEB-INF/faces-config.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/WEB-INF/faces-config.xml?rev=1532380&r1=1532379&r2=1532380&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/WEB-INF/faces-config.xml (original)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/WEB-INF/faces-config.xml Tue Oct 15 15:14:34 2013
@@ -23,6 +23,18 @@
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
     version="2.0">
 
+  <lifecycle>
+    <phase-listener>
+      org.apache.myfaces.tobago.example.test.DebugModePhaseListener
+    </phase-listener>
+  </lifecycle>
+
+  <factory>
+    <exception-handler-factory>
+      org.apache.myfaces.tobago.example.test.TestExceptionHandlerFactory
+    </exception-handler-factory>
+  </factory>
+
   <managed-bean>
     <managed-bean-name>browser</managed-bean-name>
     <managed-bean-class>org.apache.myfaces.tobago.example.test.DirectoryBrowser</managed-bean-class>
@@ -192,6 +204,12 @@
   </managed-bean>
 
   <managed-bean>
+    <managed-bean-name>errorTest</managed-bean-name>
+    <managed-bean-class>org.apache.myfaces.tobago.example.test.ErrorTest</managed-bean-class>
+    <managed-bean-scope>none</managed-bean-scope>
+  </managed-bean>
+
+  <managed-bean>
     <managed-bean-name>version</managed-bean-name>
     <managed-bean-class>org.apache.myfaces.tobago.example.test.Version</managed-bean-class>
     <managed-bean-scope>none</managed-bean-scope>
@@ -232,12 +250,6 @@
     </navigation-case>
   </navigation-rule>
 
-  <lifecycle>
-    <phase-listener>
-      org.apache.myfaces.tobago.example.test.DebugModePhaseListener
-    </phase-listener>
-  </lifecycle>
-
   <!-- Wizard -->
 
   <managed-bean>

Modified: myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/WEB-INF/web.xml?rev=1532380&r1=1532379&r2=1532380&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/WEB-INF/web.xml (original)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/WEB-INF/web.xml Tue Oct 15 15:14:34 2013
@@ -27,10 +27,8 @@
 
   <context-param>
     <param-name>javax.faces.PROJECT_STAGE</param-name>
-<!--
-    <param-value>Development</param-value>
--->
     <param-value>Production</param-value>
+    <!--<param-value>Development</param-value>-->
   </context-param>
 
   <context-param>
@@ -119,6 +117,11 @@
     <location>/faces/404.xhtml</location>
   </error-page>
 
+  <error-page>
+    <error-code>500</error-code>
+    <location>/faces/500.xhtml</location>
+  </error-page>
+
   <jsp-config>
     <jsp-property-group>
       <url-pattern>*.jsp</url-pattern>
@@ -128,11 +131,13 @@
     </jsp-property-group>
   </jsp-config>
 
+<!-- This is an alternative way to set the project stage.
   <env-entry>
     <env-entry-name>jsf/ProjectStage</env-entry-name>
     <env-entry-type>java.lang.String</env-entry-type>
     <env-entry-value>Development</env-entry-value>
   </env-entry>
+-->
 
   <filter>
     <filter-name>WaitFilter</filter-name>

Added: myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/test/error/display-exception.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/test/error/display-exception.xhtml?rev=1532380&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/test/error/display-exception.xhtml (added)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/test/error/display-exception.xhtml Tue Oct 15 15:14:34 2013
@@ -0,0 +1,40 @@
+<?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.
+-->
+
+<f:view
+    xmlns:tc="http://myfaces.apache.org/tobago/component"
+    xmlns:tx="http://myfaces.apache.org/tobago/extension"
+    xmlns:ui="http://java.sun.com/jsf/facelets"
+    xmlns:f="http://java.sun.com/jsf/core">
+
+  <tc:page>
+    <tc:gridLayoutConstraint width="600px" height="300px"/>
+
+    <tc:box label="An Exception has occured">
+      <f:facet name="layout">
+        <tc:gridLayout rows="auto;*"/>
+      </f:facet>
+
+      <tc:messages/>
+      <tc:out
+          value="This page will be shown, when an org.apache.myfaces.tobago.example.test.ErrorTestException occures!"/>
+
+    </tc:box>
+
+  </tc:page>
+</f:view>

Added: myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/test/error/throw-exception.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/test/error/throw-exception.xhtml?rev=1532380&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/test/error/throw-exception.xhtml (added)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-test/src/main/webapp/test/error/throw-exception.xhtml Tue Oct 15 15:14:34 2013
@@ -0,0 +1,52 @@
+<?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.
+-->
+
+<f:view
+    xmlns:tc="http://myfaces.apache.org/tobago/component"
+    xmlns:tx="http://myfaces.apache.org/tobago/extension"
+    xmlns:ui="http://java.sun.com/jsf/facelets"
+    xmlns:f="http://java.sun.com/jsf/core">
+
+  <tc:page>
+    <tc:gridLayoutConstraint width="600px" height="300px"/>
+
+    <tc:box label="Error Test">
+      <f:facet name="layout">
+        <tc:gridLayout rows="100px;auto"/>
+      </f:facet>
+
+      <tc:panel>
+        Please click the button, then an error occures!
+        <br/>
+        <b>Note:</b> This example seems not to work. Todo: Check why!
+        <br/>
+        <b>Note:</b> Can only work in production mode.
+      </tc:panel>
+
+      <tc:panel>
+        <f:facet name="layout">
+          <tc:gridLayout columns="*;auto"/>
+        </f:facet>
+
+        <tc:panel/>
+        <tc:button label="Error" action="#{errorTest.error}"/>
+      </tc:panel>
+
+    </tc:box>
+  </tc:page>
+</f:view>