Clover coverage report - Code Coverage for tapestry release 4.0-beta-12
Coverage timestamp: Sun Oct 30 2005 16:22:01 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  144 public ServletWebRequest(HttpServletRequest request, HttpServletResponse response)
 49    {
 50  144 Defense.notNull(request, "request");
 51  144 Defense.notNull(response, "response");
 52   
 53  144 _servletRequest = request;
 54  144 _servletResponse = response;
 55    }
 56   
 57  368 public List getParameterNames()
 58    {
 59  368 return WebUtils.toSortedList(_servletRequest.getParameterNames());
 60    }
 61   
 62  1 public String getParameterValue(String parameterName)
 63    {
 64  1 return _servletRequest.getParameter(parameterName);
 65    }
 66   
 67  356 public String[] getParameterValues(String parameterName)
 68    {
 69  356 return _servletRequest.getParameterValues(parameterName);
 70    }
 71   
 72  146 public String getContextPath()
 73    {
 74  146 return _servletRequest.getContextPath();
 75    }
 76   
 77  311 public WebSession getSession(boolean create)
 78    {
 79  311 if (_webSession != null)
 80  79 return _webSession;
 81   
 82  232 HttpSession session = _servletRequest.getSession(create);
 83   
 84  232 if (session != null)
 85  37 _webSession = new ServletWebSession(session);
 86   
 87  232 return _webSession;
 88    }
 89   
 90  1 public List getAttributeNames()
 91    {
 92  1 return WebUtils.toSortedList(_servletRequest.getAttributeNames());
 93    }
 94   
 95  48 public Object getAttribute(String name)
 96    {
 97  48 return _servletRequest.getAttribute(name);
 98    }
 99   
 100  125 public void setAttribute(String name, Object attribute)
 101    {
 102  125 if (attribute == null)
 103  1 _servletRequest.removeAttribute(name);
 104    else
 105  124 _servletRequest.setAttribute(name, attribute);
 106    }
 107   
 108  107 public String getScheme()
 109    {
 110  107 return _servletRequest.getScheme();
 111    }
 112   
 113  107 public String getServerName()
 114    {
 115  107 return _servletRequest.getServerName();
 116    }
 117   
 118  107 public int getServerPort()
 119    {
 120  107 return _servletRequest.getServerPort();
 121    }
 122   
 123  124 public String getRequestURI()
 124    {
 125  124 return _servletRequest.getRequestURI();
 126    }
 127   
 128  7 public void forward(String URL)
 129    {
 130  7 if (HiveMind.isBlank(URL))
 131    {
 132  1 performForward("/");
 133  1 return;
 134    }
 135   
 136  6 boolean internal = !(URL.startsWith("/") || URL.indexOf("://") > 0);
 137   
 138  6 if (internal)
 139  4 performForward("/" + URL);
 140    else
 141  2 sendRedirect(URL);
 142    }
 143   
 144  2 private void sendRedirect(String URL)
 145    {
 146  2 String finalURL = _servletResponse.encodeRedirectURL(URL);
 147   
 148  2 try
 149    {
 150  2 _servletResponse.sendRedirect(finalURL);
 151    }
 152    catch (IOException ex)
 153    {
 154  1 throw new ApplicationRuntimeException(WebMessages.unableToRedirect(URL, ex), ex);
 155    }
 156   
 157    }
 158   
 159  5 private void performForward(String URL)
 160    {
 161  5 RequestDispatcher dispatcher = _servletRequest.getRequestDispatcher(URL);
 162   
 163  5 if (dispatcher == null)
 164  1 throw new ApplicationRuntimeException(WebMessages.unableToFindDispatcher(URL));
 165   
 166  4 try
 167    {
 168  4 dispatcher.forward(_servletRequest, _servletResponse);
 169    }
 170    catch (ServletException ex)
 171    {
 172  1 throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
 173    }
 174    catch (IOException ex)
 175    {
 176  1 throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
 177    }
 178    }
 179   
 180    /**
 181    * Returns {@link HttpServletRequest#getServletPath()}.
 182    */
 183  124 public String getActivationPath()
 184    {
 185  124 return _servletRequest.getServletPath();
 186    }
 187   
 188  124 public String getPathInfo()
 189    {
 190  124 return _servletRequest.getPathInfo();
 191    }
 192   
 193  114 public Locale getLocale()
 194    {
 195  114 return _servletRequest.getLocale();
 196    }
 197   
 198  17 public void describeTo(DescriptionReceiver receiver)
 199    {
 200  17 receiver.describeAlternate(_servletRequest);
 201    }
 202   
 203  1 public String getHeader(String name)
 204    {
 205  1 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    }