KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > validators > CookieValidator


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.latka.validators;
18
19 import org.apache.commons.latka.ValidationException;
20
21 import org.apache.commons.latka.http.Session;
22 import org.apache.commons.latka.http.Response;
23
24 /**
25  * CookieValidator validates cookie existence and/or value in an HTTP session.
26  *
27  * @author <a HREF="mailto:dsale@us.britannica.com">Doug Sale</a>
28  * @author dIon Gillard
29  * @version $Id: CookieValidator.java 155424 2005-02-26 13:09:29Z dirkv $
30  */

31 public class CookieValidator extends BaseConditionalValidator {
32
33     // --------------------------------------------------------------- Attributes
34

35     protected String JavaDoc _cookieValue = null;
36     protected String JavaDoc _cookieName = null;
37
38     protected static final String JavaDoc MESSAGE_INVALID_TEST = "INVALID TEST: COOKIE NAME NOT SET";
39     protected static final String JavaDoc BARE_MESSAGE_EXISTENT_COOKIE = " TO FIND COOKIE IN SESSION";
40     protected static final String JavaDoc BARE_MESSAGE_EQUAL_VALUES = " THAT COOKIE VALUES EQUAL:";
41
42     // needed to generate the correct exception
43
protected String JavaDoc _lastTestedCookieValue = null;
44
45     // ------------------------------------------------------------- Constructors
46

47     public CookieValidator() {
48         this(null,null,null,true);
49     }
50
51     public CookieValidator(String JavaDoc label) {
52         this(label,null,null,true);
53     }
54
55     public CookieValidator(String JavaDoc name, String JavaDoc value) {
56         this(null,name,value,true);
57     }
58
59     public CookieValidator(String JavaDoc label, String JavaDoc name, String JavaDoc value, boolean condition) {
60         super(label, condition);
61         _cookieName = name;
62         _cookieValue = value;
63     }
64
65     // ------------------------------------------------------------------ Methods
66

67     /**
68      * If cookie value is set, this <code>Validator</code> tests for cookie value equivalency, in addition to cookie existence
69      *
70      * @param value the value of the cookie
71      */

72     public void setCookieValue(String JavaDoc value) {
73         _cookieValue = value;
74     }
75
76     /**
77      * The cookie name must be set for this <code>Validator</code> to function properly.
78      *
79      * @param name the name of the cookie
80      */

81     public void setCookieName(String JavaDoc name) {
82         _cookieName = name;
83     }
84
85     /**
86      * @throws org.apache.commons.latka.ValidationException
87      * if cookie name hasn't been set, the cookie doesn't exist in the session, or if cookie value has been set and the cookie values don't match
88      */

89     public boolean assertTrue(Response response)
90     throws ValidationException {
91
92         if (_cookieName != null) {
93             Session session = response.getRequest().getSession();
94
95             _lastTestedCookieValue = session.getCookieValue(_cookieName);
96
97             // existence test
98
if (_lastTestedCookieValue == null) {
99                 return false;
100             }
101             // value test
102
else if (_cookieValue != null) {
103                 if (!_cookieValue.equals(_lastTestedCookieValue)) {
104                     return false;
105                 }
106             }
107         }
108         // invalid test
109
else {
110             fail(MESSAGE_INVALID_TEST);
111         }
112
113         return true;
114     }
115
116     public String JavaDoc generateBareExceptionMessage() {
117
118         if (_lastTestedCookieValue == null) {
119             return BARE_MESSAGE_EXISTENT_COOKIE;
120         }
121         // value test
122
else {
123             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(BARE_MESSAGE_EQUAL_VALUES);
124             buffer.append(" EXPECTED: ");
125             buffer.append(_cookieValue);
126             buffer.append(" RECEIVED: ");
127             buffer.append(_lastTestedCookieValue);
128             return buffer.toString();
129         }
130     }
131
132 }
133
Popular Tags