Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
file stats: LOC: 222   Methods: 24
NCLOC: 157   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ServletWebRequest.java 100% 93.9% 87.5% 92.9%
coverage 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.security.Principal;
 19    import java.util.List;
 20    import java.util.Locale;
 21   
 22    import javax.servlet.RequestDispatcher;
 23    import javax.servlet.ServletException;
 24    import javax.servlet.http.HttpServletRequest;
 25    import javax.servlet.http.HttpServletResponse;
 26    import javax.servlet.http.HttpSession;
 27   
 28    import org.apache.hivemind.ApplicationRuntimeException;
 29    import org.apache.hivemind.HiveMind;
 30    import org.apache.hivemind.util.Defense;
 31    import org.apache.tapestry.describe.DescriptionReceiver;
 32   
 33    /**
 34    * Adapter from {@link javax.servlet.http.HttpServletRequest} to
 35    * {@link org.apache.tapestry.web.WebRequest}.
 36    *
 37    * @author Howard M. Lewis Ship
 38    * @since 4.0
 39    */
 40    public class ServletWebRequest implements WebRequest
 41    {
 42    private final HttpServletRequest _servletRequest;
 43   
 44    private final HttpServletResponse _servletResponse;
 45   
 46    private WebSession _webSession;
 47   
 48  288 public ServletWebRequest(HttpServletRequest request, HttpServletResponse response)
 49    {
 50  288 Defense.notNull(request, "request");
 51  288 Defense.notNull(response, "response");
 52   
 53  288 _servletRequest = request;
 54  288 _servletResponse = response;
 55    }
 56   
 57  736 public List getParameterNames()
 58    {
 59  736 return WebUtils.toSortedList(_servletRequest.getParameterNames());
 60    }
 61   
 62  2 public String getParameterValue(String parameterName)
 63    {
 64  2 return _servletRequest.getParameter(parameterName);
 65    }
 66   
 67  712 public String[] getParameterValues(String parameterName)
 68    {
 69  712 return _servletRequest.getParameterValues(parameterName);
 70    }
 71   
 72  292 public String getContextPath()
 73    {
 74  292 return _servletRequest.getContextPath();
 75    }
 76   
 77  622 public WebSession getSession(boolean create)
 78    {
 79  622 if (_webSession != null)
 80  158 return _webSession;
 81   
 82  464 HttpSession session = _servletRequest.getSession(create);
 83   
 84  464 if (session != null)
 85  74 _webSession = new ServletWebSession(session);
 86   
 87  464 return _webSession;
 88    }
 89   
 90  2 public List getAttributeNames()
 91    {
 92  2 return WebUtils.toSortedList(_servletRequest.getAttributeNames());
 93    }
 94   
 95  96 public Object getAttribute(String name)
 96    {
 97  96 return _servletRequest.getAttribute(name);
 98    }
 99   
 100  250 public void setAttribute(String name, Object attribute)
 101    {
 102  250 if (attribute == null)
 103  2 _servletRequest.removeAttribute(name);
 104    else
 105  248 _servletRequest.setAttribute(name, attribute);
 106    }
 107   
 108  214 public String getScheme()
 109    {
 110  214 return _servletRequest.getScheme();
 111    }
 112   
 113  214 public String getServerName()
 114    {
 115  214 return _servletRequest.getServerName();
 116    }
 117   
 118  214 public int getServerPort()
 119    {
 120  214 return _servletRequest.getServerPort();
 121    }
 122   
 123  248 public String getRequestURI()
 124    {
 125  248 return _servletRequest.getRequestURI();
 126    }
 127   
 128  14 public void forward(String URL)
 129    {
 130  14 if (HiveMind.isBlank(URL))
 131    {
 132  2 performForward("/");
 133  2 return;
 134    }
 135   
 136  12 boolean internal = !(URL.startsWith("/") || URL.indexOf("://") > 0);
 137   
 138  12 if (internal)
 139  8 performForward("/" + URL);
 140    else
 141  4 sendRedirect(URL);
 142    }
 143   
 144  4 private void sendRedirect(String URL)
 145    {
 146  4 String finalURL = _servletResponse.encodeRedirectURL(URL);
 147   
 148  4 try
 149    {
 150  4 _servletResponse.sendRedirect(finalURL);
 151    }
 152    catch (IOException ex)
 153    {
 154  2 throw new ApplicationRuntimeException(WebMessages.unableToRedirect(URL, ex), ex);
 155    }
 156   
 157    }
 158   
 159  10 private void performForward(String URL)
 160    {
 161  10 RequestDispatcher dispatcher = _servletRequest.getRequestDispatcher(URL);
 162   
 163  10 if (dispatcher == null)
 164  2 throw new ApplicationRuntimeException(WebMessages.unableToFindDispatcher(URL));
 165   
 166  8 try
 167    {
 168  8 dispatcher.forward(_servletRequest, _servletResponse);
 169    }
 170    catch (ServletException ex)
 171    {
 172  2 throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
 173    }
 174    catch (IOException ex)
 175    {
 176  2 throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
 177    }
 178    }
 179   
 180    /**
 181    * Returns {@link HttpServletRequest#getServletPath()}.
 182    */
 183  248 public String getActivationPath()
 184    {
 185  248 return _servletRequest.getServletPath();
 186    }
 187   
 188  248 public String getPathInfo()
 189    {
 190  248 return _servletRequest.getPathInfo();
 191    }
 192   
 193  228 public Locale getLocale()
 194    {
 195  228 return _servletRequest.getLocale();
 196    }
 197   
 198  34 public void describeTo(DescriptionReceiver receiver)
 199    {
 200  34 receiver.describeAlternate(_servletRequest);
 201    }
 202   
 203  2 public String getHeader(String name)
 204    {
 205  2 return _servletRequest.getHeader(name);
 206    }
 207   
 208  0 public String getRemoteUser()
 209    {
 210  0 return _servletRequest.getRemoteUser();
 211    }
 212   
 213  0 public Principal getUserPrincipal()
 214    {
 215  0 return _servletRequest.getUserPrincipal();
 216    }
 217   
 218  0 public boolean isUserInRole(String role)
 219    {
 220  0 return _servletRequest.isUserInRole(role);
 221    }
 222    }