Clover coverage report - Code Coverage for tapestry release 4.0-beta-6
Coverage timestamp: Wed Sep 7 2005 18:41:34 EDT
file stats: LOC: 220   Methods: 23
NCLOC: 155   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ServletWebRequest.java 100% 94% 87% 93.1%
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  154 public ServletWebRequest(HttpServletRequest request, HttpServletResponse response)
 49    {
 50  154 Defense.notNull(request, "request");
 51  154 Defense.notNull(response, "response");
 52   
 53  154 _servletRequest = request;
 54  154 _servletResponse = response;
 55    }
 56   
 57  398 public List getParameterNames()
 58    {
 59  398 return WebUtils.toSortedList(_servletRequest.getParameterNames());
 60    }
 61   
 62  1 public String getParameterValue(String parameterName)
 63    {
 64  1 return _servletRequest.getParameter(parameterName);
 65    }
 66   
 67  378 public String[] getParameterValues(String parameterName)
 68    {
 69  378 return _servletRequest.getParameterValues(parameterName);
 70    }
 71   
 72  160 public String getContextPath()
 73    {
 74  160 return _servletRequest.getContextPath();
 75    }
 76   
 77  350 public WebSession getSession(boolean create)
 78    {
 79  350 if (_webSession != null)
 80  79 return _webSession;
 81   
 82  271 HttpSession session = _servletRequest.getSession(create);
 83   
 84  271 if (session != null)
 85  37 _webSession = new ServletWebSession(session);
 86   
 87  271 return _webSession;
 88    }
 89   
 90  1 public List getAttributeNames()
 91    {
 92  1 return WebUtils.toSortedList(_servletRequest.getAttributeNames());
 93    }
 94   
 95  50 public Object getAttribute(String name)
 96    {
 97  50 return _servletRequest.getAttribute(name);
 98    }
 99   
 100  135 public void setAttribute(String name, Object attribute)
 101    {
 102  135 if (attribute == null)
 103  1 _servletRequest.removeAttribute(name);
 104    else
 105  134 _servletRequest.setAttribute(name, attribute);
 106    }
 107   
 108  120 public String getScheme()
 109    {
 110  120 return _servletRequest.getScheme();
 111    }
 112   
 113  120 public String getServerName()
 114    {
 115  120 return _servletRequest.getServerName();
 116    }
 117   
 118  120 public int getServerPort()
 119    {
 120  120 return _servletRequest.getServerPort();
 121    }
 122   
 123  134 public String getRequestURI()
 124    {
 125  134 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  135 public String getActivationPath()
 184    {
 185  135 String servletPath = _servletRequest.getServletPath();
 186  135 String pathInfo = _servletRequest.getPathInfo();
 187   
 188  135 return pathInfo == null ? servletPath : servletPath + pathInfo;
 189    }
 190   
 191  124 public Locale getLocale()
 192    {
 193  124 return _servletRequest.getLocale();
 194    }
 195   
 196  21 public void describeTo(DescriptionReceiver receiver)
 197    {
 198  21 receiver.describeAlternate(_servletRequest);
 199    }
 200   
 201  1 public String getHeader(String name)
 202    {
 203  1 return _servletRequest.getHeader(name);
 204    }
 205   
 206  0 public String getRemoteUser()
 207    {
 208  0 return _servletRequest.getRemoteUser();
 209    }
 210   
 211  0 public Principal getUserPrincipal()
 212    {
 213  0 return _servletRequest.getUserPrincipal();
 214    }
 215   
 216  0 public boolean isUserInRole(String role)
 217    {
 218  0 return _servletRequest.isUserInRole(role);
 219    }
 220    }