1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
104
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 }