1 /*-- 2 Copyright (C) 2005 Tim Solley. 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions 7 are met: 8 9 1. Redistributions of source code must retain the above copyright 10 notice, this list of conditions, and the following disclaimer. 11 12 2. Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions, and the disclaimer that follows 14 these conditions in the documentation and/or other materials 15 provided with the distribution. 16 17 3. The name "Deadbolt" may be used to endorse or promote products 18 derived from this software without prior written permission. 19 20 4. Products derived from this software may not be called "Deadbolt", nor 21 may "Deadbolt" appear in their name, without prior written permission 22 from the Deadbolt Project Management timsolley@yahoo.com. 23 24 In addition, we request (but do not require) that you include in the 25 end-user documentation provided with the redistribution and/or in the 26 software itself an acknowledgement equivalent to the following: 27 "This product includes software developed by the 28 Deadbolt Project (http://deadbolt.sourceforge.net/)." 29 Alternatively, the acknowledgment may be graphical using the logos 30 available at http://deadbolt.sourceforge.net. 31 32 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 33 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 34 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 35 DISCLAIMED. IN NO EVENT SHALL THE DEADBOLT AUTHORS OR THE PROJECT 36 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 37 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 38 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 39 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 41 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 42 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 SUCH DAMAGE. 44 45 This software consists of voluntary contributions made by many 46 individuals on behalf of the Deadbolt Project and was originally 47 created by Tim Solley timsolley@yahoo.com. For more information 48 on the Deadbolt Project, please see <http://deadbolt.sourceforge.net/>. 49 */ 50 51 package net.sf.deadbolt.tags; 52 53 import java.io.IOException; 54 import java.util.Iterator; 55 import java.util.List; 56 57 import javax.servlet.http.HttpServletRequest; 58 import javax.servlet.jsp.JspWriter; 59 import javax.servlet.jsp.tagext.BodyTagSupport; 60 61 import net.sf.deadbolt.DeadboltEnvironment; 62 63 import org.apache.log4j.Logger; 64 65 /*** 66 * This class backs a custom tag that iterates over the errors stored in the 67 * request and displays them on the JSP page. 68 * 69 * @author Tim Solley <timsolley@yahoo.com> 70 */ 71 public class DisplayErrorsTag extends BodyTagSupport { 72 private static Logger logger = Logger.getLogger(DisplayErrorsTag.class.getName()); 73 74 public DisplayErrorsTag() { 75 } 76 77 /*** 78 * This method will execute when the end tag is reached and will output the 79 * list of errors stored in the request object. 80 */ 81 public int doEndTag() { 82 logger.debug("ENTERING: doEndTag"); 83 HttpServletRequest request = (HttpServletRequest) pageContext 84 .getRequest(); 85 JspWriter out = pageContext.getOut(); 86 List errors = (List) request 87 .getAttribute(DeadboltEnvironment.GLOBAL_ERROR_KEY); 88 try { 89 if (errors != null) { 90 for (Iterator errorIterator = errors.iterator(); errorIterator 91 .hasNext();) { 92 String body = getBodyContent().getString(); 93 out.write(body + (String) errorIterator.next()); 94 } 95 } 96 } catch (IOException e) { 97 logger.warn(e.getMessage()); 98 } 99 100 logger.debug("EXITING: doEndTag"); 101 return EVAL_PAGE; 102 } 103 }