KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999,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 java.util.StringTokenizer JavaDoc;
20
21 import org.apache.commons.latka.ValidationException;
22
23 import org.apache.commons.latka.http.Response;
24
25 /**
26  * ResponseHeaderValidator validates response headers in
27  * an HTTP session. Setting just the header name tests
28  * for the existence of a header. If you set the
29  * headerValue and there is more than one header of that
30  * name, the validator matches on ANY header with that
31  * value.
32  *
33  * @author Morgan Delagrange
34  */

35 public class ResponseHeaderValidator extends BaseConditionalValidator {
36
37     // --------------------------------------------------------------- Attributes
38

39     protected String JavaDoc _headerName = null;
40     protected String JavaDoc _headerValue = null;
41
42     protected boolean _checkValue = false;
43
44     protected static final String JavaDoc BARE_MESSAGE_EXISTENT_HEADER = " TO FIND HEADER IN RESPONSE";
45     protected static final String JavaDoc BARE_MESSAGE_EQUAL_VALUES = " THAT HEADER VALUES EQUAL:";
46
47     // need to store the value of the tested header, in order
48
// to generate the right exception
49
protected String JavaDoc _actualValue = null;
50
51     // ------------------------------------------------------------- Constructors
52

53     /**
54      * Constructs a validator for the given header with no label.
55      *
56      * @param headerName Name of the header to validate
57      */

58     public ResponseHeaderValidator(String JavaDoc headerName) {
59         this(null,headerName,true);
60     }
61
62     /**
63      * Constructs a labeled validator for the given
64      * header
65      *
66      * @param label name of the header to validate
67      * @param headerName
68      */

69     public ResponseHeaderValidator(String JavaDoc label, String JavaDoc headerName) {
70         this(label,headerName,true);
71     }
72
73     /**
74      * Constructs a labeled validator for the given
75      * header
76      *
77      * @param label name of the header to validate
78      * @param headerName
79      */

80     public ResponseHeaderValidator(String JavaDoc label, String JavaDoc headerName, boolean condition) {
81         super(label,condition);
82         _headerName = headerName;
83     }
84
85     /**
86      * Sets the desired value of the header to be validated.
87      * If this method is not called, then the validator will
88      * only check for the existence of the header and
89      * disregard its value.
90      *
91      * @param headerValue
92      * desired value of the header, or null to look for a
93      * header with the String value "null"
94      */

95     public void setHeaderValue(String JavaDoc headerValue) {
96         _checkValue = true;
97
98         if (headerValue == null) {
99             headerValue = "null";
100         }
101         _headerValue = headerValue;
102     }
103
104
105     /**
106      * Checks to see that the header exists, and also checks
107      * its value if setHeaderValue(String) has been called
108      *
109      * @param response The HTTP response to validate
110      * @exception ValidationException
111      * if the header does not meet the criteria of the test
112      */

113     public boolean assertTrue(Response response)
114     throws ValidationException {
115
116         _actualValue = response.getHeader(_headerName);
117
118         if (_actualValue == null) {
119             return false;
120         }
121
122         // if checkValue == false, we only care if the header exists
123
if (_checkValue == false) {
124             return true;
125         }
126
127         // check the entire value against the actual value
128
if (_actualValue.equals(_headerValue)) {
129             return true;
130         }
131
132         // if it does not match, see if the header has multiple values
133
if (_actualValue.indexOf(",") != -1) {
134             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(_actualValue,",");
135             while (tokenizer.hasMoreTokens()) {
136                 String JavaDoc token = tokenizer.nextToken().trim();
137                 if (token.equals(_headerValue)) {
138                     return true;
139                 }
140             }
141
142         }
143
144         return false;
145     }
146
147     public String JavaDoc generateBareExceptionMessage() {
148
149         if (_actualValue == null) {
150             return BARE_MESSAGE_EXISTENT_HEADER;
151         } else {
152             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(BARE_MESSAGE_EQUAL_VALUES);
153             buffer.append(" EXPECTED: ");
154             buffer.append(_headerValue);
155             buffer.append(" RECEIVED: ");
156             buffer.append(_actualValue);
157             return buffer.toString();
158         }
159     }
160
161 }
162
Popular Tags