Coverage Report - org.webmacro.servlet.WebContext
 
Classes in this File Line Coverage Branch Coverage Complexity
WebContext
25%
12/47
N/A
2.182
 
 1  
 /*
 2  
  * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
 3  
  *
 4  
  * Redistribution and use in source and binary forms, with or without
 5  
  * modification, are permitted under the terms of either of the following
 6  
  * Open Source licenses:
 7  
  *
 8  
  * The GNU General Public License, version 2, or any later version, as
 9  
  * published by the Free Software Foundation
 10  
  * (http://www.fsf.org/copyleft/gpl.html);
 11  
  *
 12  
  *  or
 13  
  *
 14  
  * The Semiotek Public License (http://webmacro.org/LICENSE.)
 15  
  *
 16  
  * This software is provided "as is", with NO WARRANTY, not even the
 17  
  * implied warranties of fitness to purpose, or merchantability. You
 18  
  * assume all risks and liabilities associated with its use.
 19  
  *
 20  
  * See www.webmacro.org for more information on the WebMacro project.
 21  
  *
 22  
  *        @author        Marcel Huijkman
 23  
  *
 24  
  *        @version        15-07-2002
 25  
  *
 26  
  */
 27  
 
 28  
 
 29  
 package org.webmacro.servlet;
 30  
 
 31  
 import org.slf4j.Logger;
 32  
 import org.slf4j.LoggerFactory;
 33  
 
 34  
 import org.webmacro.Broker;
 35  
 import org.webmacro.Context;
 36  
 import org.webmacro.engine.Block;
 37  
 import org.webmacro.util.Bag;
 38  
 
 39  
 import javax.servlet.http.Cookie;
 40  
 import javax.servlet.http.HttpServletRequest;
 41  
 import javax.servlet.http.HttpServletResponse;
 42  
 import javax.servlet.http.HttpSession;
 43  
 
 44  
 /**
 45  
  * This is an implementation of the WebContext interface. It has the
 46  
  * ability to hook in commonly re-usable utilities via the configuraiton
 47  
  * file for the Broker. These commonly re-usable utilities are then made
 48  
  * available for use in the context. They include: Cookie, Form, FormList,
 49  
  * and several others--see the WebMacro.properties file for the actual
 50  
  * list, and a description of what's been loaded.
 51  
  * <p>
 52  
  * This class is made to be prototyped. You create
 53  
  * a prototypical instance of the WebContext containing all the desired
 54  
  * tools and a broker. You then use the newInstance(req,resp) method
 55  
  * to create an instance of the WebContext to use versus a particular
 56  
  * request.
 57  
  * <p>
 58  
  * IMPLEMENTATION NOTE: If you subclass this method you must provide a
 59  
  * sensible implementation of the clone() method. This class uses clone()
 60  
  * to create instances of the prototype in the newInstance method. You
 61  
  * should also be sure and implement the clear() method as well.
 62  
  * <p>
 63  
  */
 64  
 public class WebContext extends Context
 65  
 {
 66  
 
 67  
     /**
 68  
      * Logger configuration errors, context errors, etc.
 69  
      */
 70  2
     static Logger _log =  LoggerFactory.getLogger(WebContext.class);
 71  
 
 72  
     /**
 73  
      * The request for this http connect
 74  
      */
 75  1075
     private HttpServletRequest _request = null;
 76  
 
 77  
     /**
 78  
      * The response for this http connect
 79  
      */
 80  1075
     private HttpServletResponse _response = null;
 81  
 
 82  
     // property interface fields that are lazily set, non-final, and private
 83  
 
 84  
     /**
 85  
      * Construct a new WebContext. The WebContext will have WebContextTools
 86  
      * in addition to the ordinary ContextTools loaded from config.
 87  
      */
 88  
     public WebContext (Broker broker, HttpServletRequest req, HttpServletResponse resp)
 89  
     {
 90  1075
         super(broker);
 91  1075
         _request = req;
 92  1075
         _response = resp;
 93  1075
     }
 94  
 
 95  
 
 96  
     /**
 97  
      * Clear a WebContext of it's non-shared data
 98  
      */
 99  
     public void clear ()
 100  
     {
 101  0
         _request = null;
 102  0
         _response = null;
 103  0
         super.clear();
 104  0
     }
 105  
 
 106  
     /**
 107  
      * The HttpServletRequest object which contains information
 108  
      * provided by the HttpServlet superclass about the Request.
 109  
      * Much of this data is provided in other forms later on;
 110  
      * those interfaces get their data from this object.
 111  
      * In particular the form data has already been parsed.
 112  
      * <p>
 113  
      * @see HttpServletRequest
 114  
      */
 115  
     public final HttpServletRequest getRequest ()
 116  
     {
 117  1128
         return _request;
 118  
     }
 119  
 
 120  
     /**
 121  
      * The HttpServletResponse object which contains information
 122  
      * about the response we are going to send back. Many of these
 123  
      * services are provided through other interfaces here as well;
 124  
      * they are built on top of this object.
 125  
      * <p>
 126  
      * @see HttpServletResponse
 127  
      */
 128  
     public final HttpServletResponse getResponse ()
 129  
     {
 130  32
         return _response;
 131  
     }
 132  
 
 133  
     // CONVENIENCE METHODS
 134  
 
 135  
     /**
 136  
      * Try to get the value of a form variable from the request
 137  
      */
 138  
     final public Object getPossibleForm (String strKey)
 139  
     {
 140  
         try
 141  
         {
 142  0
             Form obForm = (Form) getProperty("Form");
 143  0
             return obForm.getPossibleForm(strKey);
 144  
         }
 145  0
         catch (Exception e)
 146  
         {
 147  0
             _log.error("Could not load Form tool", e);
 148  0
             return null;
 149  
         }
 150  
     }
 151  
 
 152  
     /**
 153  
      * Get the value of a form variable from the request
 154  
      */
 155  
     final public String getForm (String field)
 156  
     {
 157  
         try
 158  
         {
 159  4960
             Bag ct = (Bag) getProperty("Form");
 160  4960
             return (String) ct.get(field);
 161  
         }
 162  0
         catch (Exception e)
 163  
         {
 164  0
             _log.error("Could not load Form tool", e);
 165  0
             return null;
 166  
         }
 167  
     }
 168  
 
 169  
     /**
 170  
      * Get the value of a form variable from the request as an array
 171  
      */
 172  
     final public String[] getFormList (String field)
 173  
     {
 174  
         try
 175  
         {
 176  0
             Bag ct = (Bag) getProperty("FormList");
 177  0
             return (String[]) ct.get(field);
 178  
         }
 179  0
         catch (Exception e)
 180  
         {
 181  0
             _log.error("Could not load FormList tool", e);
 182  0
             return null;
 183  
         }
 184  
     }
 185  
 
 186  
     /**
 187  
      * Get the CGI Tool
 188  
      */
 189  
     final public CGI_Impersonator getCGI ()
 190  
     {
 191  
         try
 192  
         {
 193  0
             return (CGI_Impersonator) getProperty("CGI");
 194  
         }
 195  0
         catch (Exception e)
 196  
         {
 197  0
             _log.error("Could not load CGI tool", e);
 198  0
             return null;
 199  
         }
 200  
     }
 201  
 
 202  
     /**
 203  
      * get a cookie from the request
 204  
      */
 205  
     final public Cookie getCookie (String name)
 206  
     {
 207  
         try
 208  
         {
 209  0
             CookieJar cj = (CookieJar) getProperty("Cookie");
 210  0
             return (Cookie) cj.get(name);
 211  
         }
 212  0
         catch (Exception e)
 213  
         {
 214  0
             _log.error("Could not load Cookie tool", e);
 215  0
             return null;
 216  
         }
 217  
     }
 218  
 
 219  
     /**
 220  
      * send a cookie in the response
 221  
      */
 222  
     final public void setCookie (String name, String value)
 223  
     {
 224  
         try
 225  
         {
 226  0
             CookieJar cj = (CookieJar) getProperty("Cookie");
 227  0
             cj.set(name, value);
 228  
         }
 229  0
         catch (Exception e)
 230  
         {
 231  0
             _log.error("Could not load Cookie tool", e);
 232  0
         }
 233  0
     }
 234  
 
 235  
     /**
 236  
      * get the session object
 237  
      */
 238  
     final public HttpSession getSession ()
 239  
     {
 240  
         try
 241  
         {
 242  104
             return (HttpSession) getProperty("Session");
 243  
         }
 244  0
         catch (Exception e)
 245  
         {
 246  0
             _log.error("Could not load Session tool", e);
 247  0
             return null;
 248  
         }
 249  
     }
 250  
 }