stephan 2003/05/08 03:13:02
Modified: src/samples/org/apache/cocoon/samples/errorhandling ExceptionGenerator.java src/java/org/apache/cocoon/selection XPathExceptionSelector.java src/webapp/samples/errorhandling main.htm sitemap.xmap src/webapp/samples/errorhandling/exception sitemap.xmap Log: Try to reproduce error handling for java.lang.Error . Revision Changes Path 1.2 +97 -48 cocoon-2.1/src/samples/org/apache/cocoon/samples/errorhandling/ExceptionGenerator.java Index: ExceptionGenerator.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/samples/org/apache/cocoon/samples/errorhandling/ExceptionGenerator.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ExceptionGenerator.java 29 Apr 2003 10:45:20 -0000 1.1 +++ ExceptionGenerator.java 8 May 2003 10:13:02 -0000 1.2 @@ -1,10 +1,54 @@ /* - ExceptionGenerator.java - Author: Björn Lütkemeier <[EMAIL PROTECTED]> - Date: April 23, 2003 + ============================================================================ + The Apache Software License, Version 1.1 + ============================================================================ + + Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. + + Redistribution and use in source and binary forms, with or without modifica- + tion, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + 3. The end-user documentation included with the redistribution, if any, must + include the following acknowledgment: "This product includes software + developed by the Apache Software Foundation (http://www.apache.org/)." + Alternately, this acknowledgment may appear in the software itself, if + and wherever such third-party acknowledgments normally appear. + + 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be + used to endorse or promote products derived from this software without + prior written permission. For written permission, please contact + [EMAIL PROTECTED] + + 5. Products derived from this software may not be called "Apache", nor may + "Apache" appear in their name, without prior written permission of the + Apache Software Foundation. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This software consists of voluntary contributions made by many individuals + on behalf of the Apache Software Foundation and was originally created by + Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache + Software Foundation, please see <http://www.apache.org/>. + +*/ - */ package org.apache.cocoon.samples.errorhandling; import java.io.IOException; @@ -18,49 +62,54 @@ import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; -public class ExceptionGenerator -extends AbstractGenerator { - - /* - * Name of request parameters. - */ - public static final String PAR_EXCEPTION = "exception"; - public static final String PAR_CODE = "code"; - - /** - * Overridden from superclass. - */ - public void generate() - throws IOException, SAXException, ProcessingException { - Request request = ObjectModelHelper.getRequest(this.objectModel); - String exception = request.getParameter(PAR_EXCEPTION); - String text = null; - - if (exception == null) { - text = "No exception occured."; - } else if (exception.equals("validation")) { - throw new ProcessingException(new ValidationException()); - } else if (exception.equals("application")) { - throw new ProcessingException(new ApplicationException( - Integer.parseInt(request.getParameter(PAR_CODE)))); - } else if (exception.equals("resourceNotFound")) { - throw new ProcessingException(new ResourceNotFoundException("")); - } else if (exception.equals("nullPointer")) { - throw new ProcessingException(new NullPointerException()); - } else { - text = "Unknown exception requested."; - } - - Attributes noAttrs = new AttributesImpl(); - this.contentHandler.startDocument(); - this.contentHandler.startElement(null, "html", "html", noAttrs); - this.contentHandler.startElement(null, "body", "body", noAttrs); - this.contentHandler.startElement(null, "p", "p", noAttrs); - this.contentHandler.characters(text.toCharArray(), 0, text.length()); - this.contentHandler.endElement(null, "p", "p"); - this.contentHandler.endElement(null, "body", "body"); - this.contentHandler.endElement(null, "html", "html"); - this.contentHandler.endDocument(); - } +/** + * Exception generator. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Björn Lütkemeier</a> + * @version CVS $Id$ + */ +public class ExceptionGenerator extends AbstractGenerator { + + /** Name of request parameters. */ + public static final String PAR_EXCEPTION = "exception"; + + public static final String PAR_CODE = "code"; + /** + * Overridden from superclass. + */ + public void generate() + throws IOException, SAXException, ProcessingException { + Request request = ObjectModelHelper.getRequest(this.objectModel); + String exception = request.getParameter(PAR_EXCEPTION); + String text = null; + + if (exception==null) { + text = "No exception occured."; + } else if (exception.equals("validation")) { + throw new ProcessingException(new ValidationException()); + } else if (exception.equals("application")) { + throw new ProcessingException(new ApplicationException(Integer.parseInt(request.getParameter(PAR_CODE)))); + } else if (exception.equals("resourceNotFound")) { + throw new ProcessingException(new ResourceNotFoundException("")); + } else if (exception.equals("nullPointer")) { + throw new NullPointerException(); + } else if (exception.equals("error")) { + throw new Error("Error"); + } else { + text = "Unknown exception requested."; + } + + Attributes noAttrs = new AttributesImpl(); + + this.contentHandler.startDocument(); + this.contentHandler.startElement(null, "html", "html", noAttrs); + this.contentHandler.startElement(null, "body", "body", noAttrs); + this.contentHandler.startElement(null, "p", "p", noAttrs); + this.contentHandler.characters(text.toCharArray(), 0, text.length()); + this.contentHandler.endElement(null, "p", "p"); + this.contentHandler.endElement(null, "body", "body"); + this.contentHandler.endElement(null, "html", "html"); + this.contentHandler.endDocument(); + } } 1.4 +54 -49 cocoon-2.1/src/java/org/apache/cocoon/selection/XPathExceptionSelector.java Index: XPathExceptionSelector.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/selection/XPathExceptionSelector.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- XPathExceptionSelector.java 7 May 2003 12:06:42 -0000 1.3 +++ XPathExceptionSelector.java 8 May 2003 10:13:02 -0000 1.4 @@ -64,8 +64,8 @@ /** * Additional to the inherited functionality from its superclass ExceptionSelector, - * this selector allows to define xpath expressions to evaluate supplemental information - * given in the thrown exception. + * this selector allows to define xpath expressions to evaluate supplemental information + * given in the thrown exception. * The configuration of this selector allows to map not only exceptions but also * xpath expressions to symbolic names that are used in the <map:when> alternatives. * <p> @@ -76,7 +76,7 @@ * <xpath name="PasswordWrong" test="authCode=10"/> * <xpath name="PasswordExpired" test="errorCode=11"/> * <xpath name="AccessForbidden" test="errorCode>11"/> - * </exception> + * </exception> * </map:selector> * </pre> * This example shows several features : @@ -88,66 +88,71 @@ * @since 2.1 * @version CVS $Id$ */ +public class XPathExceptionSelector extends ExceptionSelector + implements Configurable { -public class XPathExceptionSelector extends ExceptionSelector implements Configurable { - - private Map exception2XPath = new HashMap(); + private Map exception2XPath = new HashMap(); public void configure(Configuration conf) throws ConfigurationException { super.configure(conf); - + Configuration[] children = conf.getChildren("exception"); - Configuration[] xPathChildren; + Configuration[] xPathChildren; - for (int i = 0; i < children.length; i++) { - // Check if there are XPath-Expressions configured - xPathChildren = children[i].getChildren("xpath"); - Map xPathMap = new HashMap(); - for (int j = 0; j < xPathChildren.length; j++) { - Configuration xPathChild = xPathChildren[j]; - - String xPathName = xPathChild.getAttribute("name"); - CompiledExpression xPath = JXPathContext.compile(xPathChild.getAttribute("test")); - - xPathMap.put(xPath, xPathName); - } - if (xPathMap.size() > 0) - // store xpath - config if there is some - exception2XPath.put(children[i].getAttribute("name", null), xPathMap); + for (int i = 0; i<children.length; i++) { + // Check if there are XPath-Expressions configured + xPathChildren = children[i].getChildren("xpath"); + Map xPathMap = new HashMap(); + + for (int j = 0; j<xPathChildren.length; j++) { + Configuration xPathChild = xPathChildren[j]; + + String xPathName = xPathChild.getAttribute("name"); + CompiledExpression xPath = JXPathContext.compile(xPathChild.getAttribute("test")); + + xPathMap.put(xPath, xPathName); + } + if (xPathMap.size()>0) { + // store xpath - config if there is some + exception2XPath.put(children[i].getAttribute("name", null), + xPathMap); + } } } /** * Compute the exception type, given the configuration and the exception stored in the object model. - * - * @see org.apache.cocoon.environment.ObjectModelHelper#getThrowable(java.util.Map) */ public Object getSelectorContext(Map objectModel, Parameters parameters) { - + // get exception from super class - FindResult selectorContext = (FindResult)super.getSelectorContext(objectModel, parameters); - - if (selectorContext != null) { - String exceptionName = selectorContext.getName(); - Throwable t = selectorContext.getThrowable(); - - Map xPathMap = (Map) exception2XPath.get(exceptionName); - if (xPathMap != null) { - // create a context for the thrown exception - JXPathContext context = JXPathContext.newContext(t); - - for (Iterator iterator = xPathMap.entrySet().iterator(); iterator.hasNext();) { - Entry entry = (Entry) iterator.next(); - if (((CompiledExpression) entry.getKey()).getValue(context).equals(Boolean.TRUE)) { - // set the configured name if the expression is succesfull - selectorContext.setName((String)entry.getValue()); - return selectorContext; - } - } - } - } - + FindResult selectorContext = (FindResult) super.getSelectorContext(objectModel, + parameters); + + if (selectorContext!=null) { + String exceptionName = selectorContext.getName(); + Throwable t = selectorContext.getThrowable(); + + Map xPathMap = (Map) exception2XPath.get(exceptionName); + + if (xPathMap!=null) { + // create a context for the thrown exception + JXPathContext context = JXPathContext.newContext(t); + + for (Iterator iterator = xPathMap.entrySet().iterator(); + iterator.hasNext(); ) { + Entry entry = (Entry) iterator.next(); + + if (((CompiledExpression) entry.getKey()).getValue(context).equals(Boolean.TRUE)) { + // set the configured name if the expression is succesfull + selectorContext.setName((String) entry.getValue()); + return selectorContext; + } + } + } + } + return selectorContext; } } 1.2 +4 -0 cocoon-2.1/src/webapp/samples/errorhandling/main.htm Index: main.htm =================================================================== RCS file: /home/cvs/cocoon-2.1/src/webapp/samples/errorhandling/main.htm,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- main.htm 29 Apr 2003 10:45:19 -0000 1.1 +++ main.htm 8 May 2003 10:13:02 -0000 1.2 @@ -28,6 +28,10 @@ <input type="hidden" name="exception" value="nullPointer"/> <input type="submit" value="NullPointerException"/> </form> + <form action="exception/exception"> + <input type="hidden" name="exception" value="error"/> + <input type="submit" value="Error"/> + </form> <form action="exception/exception"> <input type="submit" value="No exception"/> </form> 1.3 +8 -1 cocoon-2.1/src/webapp/samples/errorhandling/sitemap.xmap Index: sitemap.xmap =================================================================== RCS file: /home/cvs/cocoon-2.1/src/webapp/samples/errorhandling/sitemap.xmap,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- sitemap.xmap 6 May 2003 14:13:03 -0000 1.2 +++ sitemap.xmap 8 May 2003 10:13:02 -0000 1.3 @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> - <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0"> <map:components> + <map:selectors default="browser"> <map:selector name="exception" src="org.apache.cocoon.selection.XPathExceptionSelector"> <exception name="application" class="org.apache.cocoon.samples.errorhandling.ApplicationException"> @@ -13,7 +13,9 @@ <exception class="java.lang.Throwable" unroll="true"/> </map:selector> </map:selectors> + </map:components> + <map:pipelines> <map:pipeline> <map:match pattern="main"> @@ -21,8 +23,10 @@ <map:serialize/> </map:match> </map:pipeline> + <map:pipeline> <map:mount uri-prefix="exception" src="exception/"/> + <map:handle-errors> <map:select type="exception"> <map:when test="resourceNotFound"> @@ -31,10 +35,13 @@ </map:when> </map:select> </map:handle-errors> + </map:pipeline> + <map:handle-errors> <map:generate src="generalerror.htm"/> <map:serialize/> </map:handle-errors> + </map:pipelines> </map:sitemap> 1.3 +7 -0 cocoon-2.1/src/webapp/samples/errorhandling/exception/sitemap.xmap Index: sitemap.xmap =================================================================== RCS file: /home/cvs/cocoon-2.1/src/webapp/samples/errorhandling/exception/sitemap.xmap,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- sitemap.xmap 6 May 2003 14:13:03 -0000 1.2 +++ sitemap.xmap 8 May 2003 10:13:02 -0000 1.3 @@ -6,12 +6,16 @@ <map:generator name="exception" src="org.apache.cocoon.samples.errorhandling.ExceptionGenerator"/> </map:generators> </map:components> + <map:pipelines> + <map:pipeline> + <map:match pattern="exception"> <map:generate type="exception"/> <map:serialize/> </map:match> + <map:handle-errors> <map:select type="exception"> <map:when test="validation"> @@ -20,7 +24,9 @@ </map:when> </map:select> </map:handle-errors> + </map:pipeline> + <map:handle-errors> <map:select type="exception"> <map:when test="application1"> @@ -37,5 +43,6 @@ </map:when> </map:select> </map:handle-errors> + </map:pipelines> </map:sitemap>