Coverage Report - org.apache.commons.latka.jelly.RequestTag
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestTag
0%
0/108
0%
0/46
2.529
 
 1  
 /*
 2  
  * Copyright 1999-2001,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;
 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  
 /**
 40  
  *
 41  
  * @author  Morgan Delagrange
 42  
  */
 43  0
 public class RequestTag extends TagSupport {
 44  
 
 45  0
     protected String  _host = null;
 46  0
     protected int     _port = -1;
 47  0
     protected String  _proxyHost = null;
 48  0
     protected int     _proxyPort = -1;
 49  0
     protected String  _label = null;
 50  0
     protected int  _method = Request.HTTP_METHOD_GET;
 51  0
     protected String  _path = null;
 52  0
     protected boolean _secure = false;
 53  0
     protected boolean _followRedirects = true;
 54  0
     protected String  _httpVersion = "1.1";
 55  
     
 56  0
     protected Request  _request  = null;
 57  0
     protected Response _response = null;
 58  0
     protected Session  _session  = null;
 59  0
     protected boolean  _requestExecuted = false;
 60  
 
 61  0
     protected static final Category _log = Category.getInstance(RequestTag.class);
 62  
 
 63  
     /**
 64  
      *  Wraps Latka tests, provides some defaults for host, port etc.
 65  
      *
 66  
      * @param xmlOutput a place to write output
 67  
      * @throws JellyTagException if an HTTP request could not be created
 68  
      */
 69  
     public void doTag(XMLOutput xmlOutput) throws JellyTagException {
 70  
         try {
 71  0
             _request = createRequest();
 72  0
         } catch (LatkaException e) {
 73  0
             throw new JellyTagException("could not create HTTP request",e);
 74  0
         }
 75  
 
 76  0
         LatkaEventInfo listener =
 77  
             JellyUtils.getInstance().getLatkaEventInfo(getContext());
 78  0
         if (listener.didSessionSucceed(findSession()) == false) {
 79  0
             listener.requestSkipped(new RequestSkippedEvent(_request,null));
 80  0
             return;
 81  
         }
 82  
 
 83  
         // may set headers and such
 84  0
         invokeBody(xmlOutput);
 85  
 
 86  
         // even when there are no validations, we execute the request
 87  
         // to make sure the URL is accessible
 88  
         
 89  
         // will throw an unrecoverable LatkaException if the request could not
 90  
         // be created, typically because of a malformed URL
 91  0
         Response response = null;
 92  
         try {
 93  0
             response = getResponse();
 94  0
         } catch (LatkaException e) {
 95  0
             throw new JellyTagException("could not obtain HTTP response",e);
 96  0
         }
 97  
         
 98  
         // if there's been a response, and the request has a label
 99  
         // make the response available to the jelly context
 100  0
         if (response != null && _label != null) {
 101  0
             getContext().setVariable(_label, response);
 102  
         }
 103  
 
 104  0
         if (listener.didRequestSucceed(_request)) {
 105  0
             listener.requestSucceeded(new RequestSucceededEvent(
 106  
               response.getRequest(), response));            
 107  
         }
 108  
 
 109  0
     }
 110  
 
 111  
     /**
 112  
      * 
 113  
      * @return Request
 114  
      * @exception IOException
 115  
      *                   Error creating the request (unrecoverable, the script
 116  
      *                   must fail)
 117  
      */
 118  
     private Request createRequest() throws LatkaException {
 119  0
         String host = _host;
 120  0
         int port    = _port;
 121  0
         String proxyHost = _proxyHost;
 122  0
         int proxyPort    = _proxyPort;
 123  
 
 124  0
         if (host == null || port == -1 || proxyHost == null || proxyPort == -1) {
 125  0
             SuiteSettings settings = getSuiteSettings();
 126  0
             if (host == null) {
 127  0
                 host = settings.getDefaultHost();
 128  
             }
 129  0
             if (port == -1) {
 130  0
                 port = settings.getDefaultPort();
 131  
             }
 132  0
             if (proxyHost == null) {
 133  0
                 proxyHost = settings.getDefaultProxyHost();
 134  
             }
 135  0
             if (proxyPort == -1) {
 136  0
                 proxyPort = settings.getDefaultProxyPort();
 137  
             }
 138  
         }
 139  
 
 140  0
         Session session = findSession();
 141  
 
 142  0
         Proxy proxy = null;
 143  0
         if (proxyHost != null) {
 144  0
             proxy = new Proxy(proxyHost,proxyPort);
 145  
         }
 146  
         
 147  0
         URL url = null;
 148  
         try {
 149  0
             url = new URL(_secure ? "https" : "http", host, port, _path);
 150  0
         } catch (IOException e) {
 151  0
             throw new LatkaException(e);
 152  0
         }
 153  0
         return session.createRequest(_label,url,_method,_httpVersion,_followRedirects,proxy);
 154  
     }
 155  
 
 156  
     public Request getRequest() {
 157  0
         return _request;
 158  
     }
 159  
 
 160  
     protected Session findSession() {
 161  0
         if (_session == null) {
 162  0
             SessionTag tag = (SessionTag) findAncestorWithClass(SessionTag.class);
 163  0
             if (tag == null) {
 164  0
                 _session = new SessionImpl();
 165  
             } else {
 166  0
                 _session = tag.getSession();
 167  
             }
 168  
         }
 169  
 
 170  0
         return _session;
 171  
     }
 172  
 
 173  
     public boolean getRequestExecuted() {
 174  0
         return _requestExecuted;
 175  
     }
 176  
 
 177  
     /**
 178  
      * The first time this method is called, a live HTTP
 179  
      * call will be made to the server, returning null
 180  
      * if the response cannot be obtained.  Subsequent
 181  
      * calls return a cached response.
 182  
      * 
 183  
      * @return Response for the request specified by the Latka script
 184  
      * @exception LatkaException
 185  
      *                   error creating the Request (unrecoverable)
 186  
      */
 187  
     public Response getResponse() throws LatkaException {
 188  0
         if (_requestExecuted == false) {
 189  0
             _requestExecuted = true;
 190  
 
 191  0
             LatkaEventInfo listener = 
 192  
                 JellyUtils.getInstance().getLatkaEventInfo(getContext());
 193  
             try {
 194  0
                 _response = _request.execute();
 195  
                 
 196  0
                 _log.warn("Eventually this debug needs to go.");
 197  0
                 if (_log.isDebugEnabled()) {
 198  0
                   _log.debug(_response.getResource());
 199  
                 }            
 200  0
             } catch (IOException e) {
 201  0
                 listener.requestError(new RequestErrorEvent(_request, null, e));
 202  0
                 return null;
 203  0
             }
 204  
 
 205  
             // hack because sometimes we need to generate a new request after
 206  
             // a redirect
 207  0
             _request = _response.getRequest();
 208  
         }
 209  
 
 210  0
         return _response;
 211  
     }
 212  
 
 213  
     /**
 214  
      * get the suite settings from SuiteTag
 215  
      * 
 216  
      * @return SuiteSettings object
 217  
      */
 218  
     protected SuiteSettings getSuiteSettings() {
 219  0
         SuiteTag tag = 
 220  
         (SuiteTag) findAncestorWithClass(org.apache.commons.latka.jelly.SuiteTag.class);
 221  0
         return tag.getSuiteSettings();
 222  
     }
 223  
 
 224  
     /**
 225  
      * Setter for host
 226  
      * 
 227  
      * @param host
 228  
      *               host for the request
 229  
      */
 230  
     public void setHost(String host) {
 231  0
         _host = host;
 232  0
     }
 233  
 
 234  
     /**
 235  
      * Setter for port
 236  
      * 
 237  
      * @param port
 238  
      *               port for all requests 
 239  
      */
 240  
     public void setPort(int port) {
 241  0
         _port = port;
 242  0
     }
 243  
 
 244  
 
 245  
     /**
 246  
      * Setter for defaultProxyHost
 247  
      * 
 248  
      * @param defaultHost
 249  
      *               defaultProxyHost for all requests
 250  
      */
 251  
     public void setProxyHost(String host) {
 252  0
         _proxyHost = host;
 253  0
     }
 254  
 
 255  
     /**
 256  
      * Setter for defaultProxyPort
 257  
      * 
 258  
      * @param defaultPort
 259  
      *               defaultProxyPort for all requests
 260  
      * @return 
 261  
      */
 262  
     public void setProxyPort(int port) {
 263  0
         _proxyPort = port;
 264  0
     }
 265  
 
 266  
     /**
 267  
      * Set the label for this suite
 268  
      * 
 269  
      * @param label  suite label
 270  
      */
 271  
     public void setLabel(String label) {
 272  0
         _label = label;
 273  0
     }
 274  
 
 275  
     /**
 276  
      * Sets the HTTP method to use.  Supports post, get,
 277  
      * and head.  Default is "get".
 278  
      * 
 279  
      * @param method set method to post, get or head
 280  
      * @exception UnsupportedOperationException
 281  
      *                   if an unsupported HTTP method is set
 282  
      */
 283  
     public void setMethod(String method) throws UnsupportedOperationException {
 284  0
         if (method.equals("get")) {
 285  0
             _method = Request.HTTP_METHOD_GET;
 286  0
         } else if (method.equals("post")) {
 287  0
             _method = Request.HTTP_METHOD_POST;
 288  0
         } else if (method.equals("head")) {
 289  0
             _method = Request.HTTP_METHOD_HEAD;
 290  
         } else {
 291  0
             throw new UnsupportedOperationException("Unkonwn HTTP method: " + method);
 292  
         }
 293  0
     }
 294  
 
 295  
     /**
 296  
      * Sets the path of the document on the server, combined
 297  
      * with the host and port
 298  
      * 
 299  
      * @param path   Path to the document on the server
 300  
      */
 301  
     public void setPath(String path) {
 302  0
         _path = path;
 303  0
     }
 304  
 
 305  
     /**
 306  
      * Sets whether or not to transmit the request over
 307  
      * SSL.
 308  
      * 
 309  
      * @param secure whether or not this request is SSL
 310  
      */
 311  
     public void setSecure(String secure) {
 312  0
         _secure = Boolean.valueOf(secure).booleanValue();
 313  0
     }
 314  
 
 315  
 
 316  
     /**
 317  
      * Sets whether or not to transmit the request over
 318  
      * SSL.
 319  
      * 
 320  
      * @param secure whether or not this request is SSL
 321  
      */
 322  
     public void setFollowRedirects(String followRedirects) {
 323  0
         _followRedirects = Boolean.valueOf(followRedirects).booleanValue();
 324  0
     }
 325  
 
 326  
     /**
 327  
      * HTTP version to use.  Legal values are 1.0 and 1.1.
 328  
      * 1.1 is the default.
 329  
      * 
 330  
      * @param version HTTP specification version
 331  
      */
 332  
     public void setVersion(String version) {
 333  0
         _httpVersion = version;
 334  0
     }
 335  
 
 336  
 }