1 16 17 package org.apache.commons.latka.jelly; 18 19 import java.io.IOException ; 20 import java.net.URL ; 21 22 import org.apache.commons.jelly.JellyTagException; 23 import org.apache.commons.jelly.TagSupport; 24 import org.apache.commons.jelly.XMLOutput; 25 26 import org.apache.commons.latka.LatkaException; 27 import org.apache.commons.latka.event.LatkaEventInfo; 28 import org.apache.commons.latka.event.RequestErrorEvent; 29 import org.apache.commons.latka.event.RequestSkippedEvent; 30 import org.apache.commons.latka.event.RequestSucceededEvent; 31 import org.apache.commons.latka.http.Proxy; 32 import org.apache.commons.latka.http.Request; 33 import org.apache.commons.latka.http.Response; 34 import org.apache.commons.latka.http.Session; 35 import org.apache.commons.latka.http.SessionImpl; 36 37 import org.apache.log4j.Category; 38 39 43 public class RequestTag extends TagSupport { 44 45 protected String _host = null; 46 protected int _port = -1; 47 protected String _proxyHost = null; 48 protected int _proxyPort = -1; 49 protected String _label = null; 50 protected int _method = Request.HTTP_METHOD_GET; 51 protected String _path = null; 52 protected boolean _secure = false; 53 protected boolean _followRedirects = true; 54 protected String _httpVersion = "1.1"; 55 56 protected Request _request = null; 57 protected Response _response = null; 58 protected Session _session = null; 59 protected boolean _requestExecuted = false; 60 61 protected static final Category _log = Category.getInstance(RequestTag.class); 62 63 69 public void doTag(XMLOutput xmlOutput) throws JellyTagException { 70 try { 71 _request = createRequest(); 72 } catch (LatkaException e) { 73 throw new JellyTagException("could not create HTTP request",e); 74 } 75 76 LatkaEventInfo listener = 77 JellyUtils.getInstance().getLatkaEventInfo(getContext()); 78 if (listener.didSessionSucceed(findSession()) == false) { 79 listener.requestSkipped(new RequestSkippedEvent(_request,null)); 80 return; 81 } 82 83 invokeBody(xmlOutput); 85 86 89 Response response = null; 92 try { 93 response = getResponse(); 94 } catch (LatkaException e) { 95 throw new JellyTagException("could not obtain HTTP response",e); 96 } 97 98 if (response != null && _label != null) { 101 getContext().setVariable(_label, response); 102 } 103 104 if (listener.didRequestSucceed(_request)) { 105 listener.requestSucceeded(new RequestSucceededEvent( 106 response.getRequest(), response)); 107 } 108 109 } 110 111 118 private Request createRequest() throws LatkaException { 119 String host = _host; 120 int port = _port; 121 String proxyHost = _proxyHost; 122 int proxyPort = _proxyPort; 123 124 if (host == null || port == -1 || proxyHost == null || proxyPort == -1) { 125 SuiteSettings settings = getSuiteSettings(); 126 if (host == null) { 127 host = settings.getDefaultHost(); 128 } 129 if (port == -1) { 130 port = settings.getDefaultPort(); 131 } 132 if (proxyHost == null) { 133 proxyHost = settings.getDefaultProxyHost(); 134 } 135 if (proxyPort == -1) { 136 proxyPort = settings.getDefaultProxyPort(); 137 } 138 } 139 140 Session session = findSession(); 141 142 Proxy proxy = null; 143 if (proxyHost != null) { 144 proxy = new Proxy(proxyHost,proxyPort); 145 } 146 147 URL url = null; 148 try { 149 url = new URL (_secure ? "https" : "http", host, port, _path); 150 } catch (IOException e) { 151 throw new LatkaException(e); 152 } 153 return session.createRequest(_label,url,_method,_httpVersion,_followRedirects,proxy); 154 } 155 156 public Request getRequest() { 157 return _request; 158 } 159 160 protected Session findSession() { 161 if (_session == null) { 162 SessionTag tag = (SessionTag) findAncestorWithClass(SessionTag.class); 163 if (tag == null) { 164 _session = new SessionImpl(); 165 } else { 166 _session = tag.getSession(); 167 } 168 } 169 170 return _session; 171 } 172 173 public boolean getRequestExecuted() { 174 return _requestExecuted; 175 } 176 177 187 public Response getResponse() throws LatkaException { 188 if (_requestExecuted == false) { 189 _requestExecuted = true; 190 191 LatkaEventInfo listener = 192 JellyUtils.getInstance().getLatkaEventInfo(getContext()); 193 try { 194 _response = _request.execute(); 195 196 _log.warn("Eventually this debug needs to go."); 197 if (_log.isDebugEnabled()) { 198 _log.debug(_response.getResource()); 199 } 200 } catch (IOException e) { 201 listener.requestError(new RequestErrorEvent(_request, null, e)); 202 return null; 203 } 204 205 _request = _response.getRequest(); 208 } 209 210 return _response; 211 } 212 213 218 protected SuiteSettings getSuiteSettings() { 219 SuiteTag tag = 220 (SuiteTag) findAncestorWithClass(org.apache.commons.latka.jelly.SuiteTag.class); 221 return tag.getSuiteSettings(); 222 } 223 224 230 public void setHost(String host) { 231 _host = host; 232 } 233 234 240 public void setPort(int port) { 241 _port = port; 242 } 243 244 245 251 public void setProxyHost(String host) { 252 _proxyHost = host; 253 } 254 255 262 public void setProxyPort(int port) { 263 _proxyPort = port; 264 } 265 266 271 public void setLabel(String label) { 272 _label = label; 273 } 274 275 283 public void setMethod(String method) throws UnsupportedOperationException { 284 if (method.equals("get")) { 285 _method = Request.HTTP_METHOD_GET; 286 } else if (method.equals("post")) { 287 _method = Request.HTTP_METHOD_POST; 288 } else if (method.equals("head")) { 289 _method = Request.HTTP_METHOD_HEAD; 290 } else { 291 throw new UnsupportedOperationException ("Unkonwn HTTP method: " + method); 292 } 293 } 294 295 301 public void setPath(String path) { 302 _path = path; 303 } 304 305 311 public void setSecure(String secure) { 312 _secure = Boolean.valueOf(secure).booleanValue(); 313 } 314 315 316 322 public void setFollowRedirects(String followRedirects) { 323 _followRedirects = Boolean.valueOf(followRedirects).booleanValue(); 324 } 325 326 332 public void setVersion(String version) { 333 _httpVersion = version; 334 } 335 336 } 337 | Popular Tags |