KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > jelly > validators > HttpValidatorTagSupport


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.jelly.validators;
18
19 import org.apache.commons.jelly.JellyTagException;
20 import org.apache.commons.jelly.TagSupport;
21 import org.apache.commons.jelly.XMLOutput;
22
23 import org.apache.commons.latka.LatkaException;
24 import org.apache.commons.latka.Validator;
25 import org.apache.commons.latka.ValidationException;
26 import org.apache.commons.latka.event.LatkaEventInfo;
27 import org.apache.commons.latka.event.RequestFailedEvent;
28 import org.apache.commons.latka.http.Response;
29 import org.apache.commons.latka.jelly.JellyUtils;
30 import org.apache.commons.latka.jelly.RequestTag;
31
32 import org.apache.log4j.Category;
33
34 /**
35  * A base class for validation tags
36  *
37  * @author Morgan Delagrange
38  * @author dIon Gillard
39  * @version $Id: HttpValidatorTagSupport.java 155424 2005-02-26 13:09:29Z dirkv $
40  */

41 public abstract class HttpValidatorTagSupport extends TagSupport {
42
43     /** the response being validated */
44     protected Response _response = null;
45     /** the listener handling request success/failure etc */
46     protected LatkaEventInfo _listener = null;
47     /** label for the test */
48     protected String JavaDoc _label = null;
49
50     protected static final Category _log = Category.getInstance(HttpValidatorTagSupport.class);
51
52     public HttpValidatorTagSupport() {
53     }
54
55     public abstract Validator getValidator();
56
57     /**
58      * Provides a default implementation for simple validation
59      * tags. Will execute the validator produced by
60      * getValidator(). Override if necessary.
61      *
62      * @param xmlOutput a place to write output
63      * @exception JellyTagException if the tag body could not be invoked
64      */

65     public void doTag(XMLOutput xmlOutput) throws JellyTagException {
66         invokeBody(xmlOutput);
67         
68         Validator validator = getValidator();
69         validate(validator);
70     }
71
72     /**
73      * Called by the ValidationFactory.
74      *
75      * @param listener supplier of information about the suite/progress
76      * @param tagName name of the validating tag
77      * @param reader xml to process
78      * @param response response to validate
79      */

80     public void init() {
81         RequestTag tag =
82             (RequestTag) findAncestorWithClass(RequestTag.class);
83         try {
84             _response = tag.getResponse();
85         } catch (LatkaException e) {
86             _log.error("Unexpected exception, should have been caught earlier.");
87         }
88         _listener = JellyUtils.getInstance().getLatkaEventInfo(getContext());
89     }
90
91     public void setLabel(String JavaDoc label) {
92         _label = label;
93     }
94
95     /**
96      * the response being validated
97      * @return the response being validated
98      */

99     public Response getResponse() {
100         return _response;
101     }
102
103     /**
104      * validate the response using the validator provided.
105      * This method will notify the listener in the event
106      * of a failure. Will return false and not execute
107      * the validation if the request is already invalid.
108      *
109      * @param validator the object that performs validation
110      * @return whether or not the request passed validation
111      */

112     public boolean validate(Validator validator) {
113         init();
114         if (_log.isDebugEnabled()) {
115             _log.debug("performing custom validation");
116             _log.debug("validator = " + validator);
117             _log.debug("response = " + _response);
118         }
119
120         // if there's no response, or the request failed, skip it
121
if (_response == null
122             || !_listener.didRequestSucceed(_response.getRequest())) {
123             _log.debug("Validator skipped");
124             return false;
125         }
126
127         boolean valid = true;
128
129         try {
130             validator.validate(_response);
131         } catch (ValidationException e) {
132             _listener.requestFailed(
133                 new RequestFailedEvent(_response.getRequest(), _response, e));
134             valid = false;
135         }
136
137         _log.debug("custom validation complete");
138
139         return valid;
140     }
141   
142 }
Popular Tags