1 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 41 public abstract class HttpValidatorTagSupport extends TagSupport { 42 43 44 protected Response _response = null; 45 46 protected LatkaEventInfo _listener = null; 47 48 protected String _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 65 public void doTag(XMLOutput xmlOutput) throws JellyTagException { 66 invokeBody(xmlOutput); 67 68 Validator validator = getValidator(); 69 validate(validator); 70 } 71 72 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 label) { 92 _label = label; 93 } 94 95 99 public Response getResponse() { 100 return _response; 101 } 102 103 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 (_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 |