Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 199   Methods: 19
NCLOC: 138   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ServletWebRequest.java 100% 100% 100% 100%
coverage
 1   
 // Copyright 2005 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.tapestry.web;
 16   
 
 17   
 import java.io.IOException;
 18   
 import java.util.List;
 19   
 import java.util.Locale;
 20   
 
 21   
 import javax.servlet.RequestDispatcher;
 22   
 import javax.servlet.ServletException;
 23   
 import javax.servlet.http.HttpServletRequest;
 24   
 import javax.servlet.http.HttpServletResponse;
 25   
 import javax.servlet.http.HttpSession;
 26   
 
 27   
 import org.apache.hivemind.ApplicationRuntimeException;
 28   
 import org.apache.hivemind.HiveMind;
 29   
 import org.apache.hivemind.util.Defense;
 30   
 import org.apache.tapestry.describe.DescriptionReceiver;
 31   
 
 32   
 /**
 33   
  * Adapter from {@link javax.servlet.http.HttpServletRequest} to
 34   
  * {@link org.apache.tapestry.web.WebRequest}.
 35   
  * 
 36   
  * @author Howard M. Lewis Ship
 37   
  * @since 4.0
 38   
  */
 39   
 public class ServletWebRequest implements WebRequest
 40   
 {
 41   
     private final HttpServletRequest _servletRequest;
 42   
 
 43   
     private final HttpServletResponse _servletResponse;
 44   
 
 45   
     private WebSession _webSession;
 46   
 
 47  169
     public ServletWebRequest(HttpServletRequest request, HttpServletResponse response)
 48   
     {
 49  169
         Defense.notNull(request, "request");
 50  169
         Defense.notNull(response, "response");
 51   
 
 52  169
         _servletRequest = request;
 53  169
         _servletResponse = response;
 54   
     }
 55   
 
 56  296
     public List getParameterNames()
 57   
     {
 58  296
         return WebUtils.toSortedList(_servletRequest.getParameterNames());
 59   
     }
 60   
 
 61  1
     public String getParameterValue(String parameterName)
 62   
     {
 63  1
         return _servletRequest.getParameter(parameterName);
 64   
     }
 65   
 
 66  453
     public String[] getParameterValues(String parameterName)
 67   
     {
 68  453
         return _servletRequest.getParameterValues(parameterName);
 69   
     }
 70   
 
 71  181
     public String getContextPath()
 72   
     {
 73  181
         return _servletRequest.getContextPath();
 74   
     }
 75   
 
 76  413
     public WebSession getSession(boolean create)
 77   
     {
 78  413
         if (_webSession != null)
 79  109
             return _webSession;
 80   
 
 81  304
         HttpSession session = _servletRequest.getSession(create);
 82   
 
 83  304
         if (session != null)
 84  45
             _webSession = new ServletWebSession(session);
 85   
 
 86  304
         return _webSession;
 87   
     }
 88   
 
 89  1
     public List getAttributeNames()
 90   
     {
 91  1
         return WebUtils.toSortedList(_servletRequest.getAttributeNames());
 92   
     }
 93   
 
 94  54
     public Object getAttribute(String name)
 95   
     {
 96  54
         return _servletRequest.getAttribute(name);
 97   
     }
 98   
 
 99  150
     public void setAttribute(String name, Object attribute)
 100   
     {
 101  150
         if (attribute == null)
 102  1
             _servletRequest.removeAttribute(name);
 103   
         else
 104  149
             _servletRequest.setAttribute(name, attribute);
 105   
     }
 106   
 
 107  137
     public String getScheme()
 108   
     {
 109  137
         return _servletRequest.getScheme();
 110   
     }
 111   
 
 112  137
     public String getServerName()
 113   
     {
 114  137
         return _servletRequest.getServerName();
 115   
     }
 116   
 
 117  137
     public int getServerPort()
 118   
     {
 119  137
         return _servletRequest.getServerPort();
 120   
     }
 121   
 
 122  149
     public String getRequestURI()
 123   
     {
 124  149
         return _servletRequest.getRequestURI();
 125   
     }
 126   
 
 127  7
     public void forward(String URL)
 128   
     {
 129  7
         if (HiveMind.isBlank(URL))
 130   
         {
 131  1
             performForward("/");
 132  1
             return;
 133   
         }
 134   
 
 135  6
         boolean internal = !(URL.startsWith("/") || URL.indexOf("://") > 0);
 136   
 
 137  6
         if (internal)
 138  4
             performForward("/" + URL);
 139   
         else
 140  2
             sendRedirect(URL);
 141   
     }
 142   
 
 143  2
     private void sendRedirect(String URL)
 144   
     {
 145  2
         String finalURL = _servletResponse.encodeRedirectURL(URL);
 146   
 
 147  2
         try
 148   
         {
 149  2
             _servletResponse.sendRedirect(finalURL);
 150   
         }
 151   
         catch (IOException ex)
 152   
         {
 153  1
             throw new ApplicationRuntimeException(WebMessages.unableToRedirect(URL, ex), ex);
 154   
         }
 155   
 
 156   
     }
 157   
 
 158  5
     private void performForward(String URL)
 159   
     {
 160  5
         RequestDispatcher dispatcher = _servletRequest.getRequestDispatcher(URL);
 161   
 
 162  5
         if (dispatcher == null)
 163  1
             throw new ApplicationRuntimeException(WebMessages.unableToFindDispatcher(URL));
 164   
 
 165  4
         try
 166   
         {
 167  4
             dispatcher.forward(_servletRequest, _servletResponse);
 168   
         }
 169   
         catch (ServletException ex)
 170   
         {
 171  1
             throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
 172   
         }
 173   
         catch (IOException ex)
 174   
         {
 175  1
             throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
 176   
         }
 177   
     }
 178   
 
 179   
     /**
 180   
      * Returns {@link HttpServletRequest#getServletPath()}.
 181   
      */
 182  150
     public String getActivationPath()
 183   
     {
 184  150
         String servletPath = _servletRequest.getServletPath();
 185  150
         String pathInfo = _servletRequest.getPathInfo();
 186   
 
 187  150
         return pathInfo == null ? servletPath : servletPath + pathInfo;
 188   
     }
 189   
 
 190  139
     public Locale getLocale()
 191   
     {
 192  139
         return _servletRequest.getLocale();
 193   
     }
 194   
 
 195  22
     public void describeTo(DescriptionReceiver receiver)
 196   
     {
 197  22
         receiver.describeAlternate(_servletRequest);
 198   
     }
 199   
 }