Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HttpValidatorTagSupport |
|
| 1.7142857142857142;1.714 |
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 | 0 | protected Response _response = null; |
45 | /** the listener handling request success/failure etc */ | |
46 | 0 | protected LatkaEventInfo _listener = null; |
47 | /** label for the test */ | |
48 | 0 | protected String _label = null; |
49 | ||
50 | 0 | protected static final Category _log = Category.getInstance(HttpValidatorTagSupport.class); |
51 | ||
52 | 0 | public HttpValidatorTagSupport() { |
53 | 0 | } |
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 | 0 | invokeBody(xmlOutput); |
67 | ||
68 | 0 | Validator validator = getValidator(); |
69 | 0 | validate(validator); |
70 | 0 | } |
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 | 0 | RequestTag tag = |
82 | (RequestTag) findAncestorWithClass(RequestTag.class); | |
83 | try { | |
84 | 0 | _response = tag.getResponse(); |
85 | 0 | } catch (LatkaException e) { |
86 | 0 | _log.error("Unexpected exception, should have been caught earlier."); |
87 | 0 | } |
88 | 0 | _listener = JellyUtils.getInstance().getLatkaEventInfo(getContext()); |
89 | 0 | } |
90 | ||
91 | public void setLabel(String label) { | |
92 | 0 | _label = label; |
93 | 0 | } |
94 | ||
95 | /** | |
96 | * the response being validated | |
97 | * @return the response being validated | |
98 | */ | |
99 | public Response getResponse() { | |
100 | 0 | 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 | 0 | init(); |
114 | 0 | if (_log.isDebugEnabled()) { |
115 | 0 | _log.debug("performing custom validation"); |
116 | 0 | _log.debug("validator = " + validator); |
117 | 0 | _log.debug("response = " + _response); |
118 | } | |
119 | ||
120 | // if there's no response, or the request failed, skip it | |
121 | 0 | if (_response == null |
122 | || !_listener.didRequestSucceed(_response.getRequest())) { | |
123 | 0 | _log.debug("Validator skipped"); |
124 | 0 | return false; |
125 | } | |
126 | ||
127 | 0 | boolean valid = true; |
128 | ||
129 | try { | |
130 | 0 | validator.validate(_response); |
131 | 0 | } catch (ValidationException e) { |
132 | 0 | _listener.requestFailed( |
133 | new RequestFailedEvent(_response.getRequest(), _response, e)); | |
134 | 0 | valid = false; |
135 | 0 | } |
136 | ||
137 | 0 | _log.debug("custom validation complete"); |
138 | ||
139 | 0 | return valid; |
140 | } | |
141 | ||
142 | } |