View Javadoc

1   /*
2    * $Id: RequiredSessionAttributeValidator.java,v 1.4 2004/04/25 10:02:36 eelco12 Exp $
3    * $Revision: 1.4 $
4    * $Date: 2004/04/25 10:02:36 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package nl.openedge.baritus.validation.impl;
32  
33  import javax.servlet.http.HttpSession;
34  
35  import nl.openedge.baritus.FormBeanContext;
36  import nl.openedge.baritus.validation.AbstractFormValidator;
37  import nl.openedge.baritus.validation.ValidationActivationRule;
38  
39  import org.infohazard.maverick.flow.ControllerContext;
40  
41  /***
42   * Checks whether a session attribute exists with the key
43   * that was set for property sessionAttributeKey.
44   * 
45   * @author Eelco Hillenius
46   */
47  public class RequiredSessionAttributeValidator extends AbstractFormValidator
48  {
49  
50  	private String sessionAttributeKey = null;
51  	
52  	private String errorMessageKey = "required.session.attribute.not.found";
53  
54  	/***
55  	 * Construct.
56  	 */
57  	public RequiredSessionAttributeValidator()
58  	{
59  
60  	}
61  
62  	/***
63  	 * Construct with errorMessageKey.
64  	 * @param errorMessageKey
65  	 */
66  	public RequiredSessionAttributeValidator(String errorMessageKey)
67  	{
68  		setErrorMessageKey(errorMessageKey);
69  	}
70  	
71  	/***
72  	 * Construct with rule.
73  	 * @param rule
74  	 */
75  	public RequiredSessionAttributeValidator(ValidationActivationRule rule)
76  	{
77  		super(rule);
78  	}
79  
80  	/***
81  	 * Construct with errorMessageKey and rule.
82  	 * @param errorMessageKey
83  	 * @param rule
84  	 */
85  	public RequiredSessionAttributeValidator(
86  		String errorMessageKey,
87  		ValidationActivationRule rule)
88  	{
89  		setErrorMessageKey(errorMessageKey);
90  		setValidationRule(rule);
91  	}
92  
93  	/***
94  	 * Construct with message prefix and session attribute key to check for.
95  	 * @param errorMessageKey
96  	 */
97  	public RequiredSessionAttributeValidator(String errorMessageKey, String sessionAttributeKey)
98  	{
99  		setErrorMessageKey(errorMessageKey);
100 		setSessionAttributeKey(sessionAttributeKey);
101 	} 
102 
103 	/* (non-Javadoc)
104 	 * @see nl.openedge.baritus.validation.FormValidator#isValid(org.infohazard.maverick.flow.ControllerContext, nl.openedge.baritus.FormBeanContext)
105 	 */
106 	public boolean isValid(ControllerContext cctx, FormBeanContext formBeanContext)
107 	{
108 		boolean valid = false;
109 		HttpSession session = cctx.getRequest().getSession(false);
110 		if(session != null)
111 		{
112 			valid = (session.getAttribute(sessionAttributeKey) != null);
113 		}
114 		
115 		if(!valid)
116 		{
117 			setErrorMessage(formBeanContext, sessionAttributeKey, getErrorMessageKey(), 
118 				new Object[]{getFieldName(formBeanContext, sessionAttributeKey)});	
119 		}
120 		
121 		return valid;
122 	}
123 
124 	/***
125 	 * get the session attribute key that will be checked for
126 	 * @return String the session attribute key that will be checked for
127 	 */
128 	public String getSessionAttributeKey()
129 	{
130 		return sessionAttributeKey;
131 	}
132 
133 	/***
134 	 * set the session attribute key that will be checked for
135 	 * @param string the session attribute key that will be checked for
136 	 */
137 	public void setSessionAttributeKey(String string)
138 	{
139 		sessionAttributeKey = string;
140 	}
141 	
142 	/***
143 	 * Get key of error message.
144 	 * @return String key of error message
145 	 */
146 	public String getErrorMessageKey()
147 	{
148 		return errorMessageKey;
149 	}
150 
151 	/***
152 	 * Set key of error message.
153 	 * @param string key of error message
154 	 */
155 	public void setErrorMessageKey(String string)
156 	{
157 		errorMessageKey = string;
158 	}
159 
160 }