Clover coverage report - Code Coverage for tapestry release 4.0-beta-2
Coverage timestamp: Sat Jul 9 2005 22:02:17 EDT
file stats: LOC: 204   Methods: 20
NCLOC: 142   Classes: 1
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com 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  153 public ServletWebRequest(HttpServletRequest request, HttpServletResponse response)
 48    {
 49  153 Defense.notNull(request, "request");
 50  153 Defense.notNull(response, "response");
 51   
 52  153 _servletRequest = request;
 53  153 _servletResponse = response;
 54    }
 55   
 56  395 public List getParameterNames()
 57    {
 58  395 return WebUtils.toSortedList(_servletRequest.getParameterNames());
 59    }
 60   
 61  1 public String getParameterValue(String parameterName)
 62    {
 63  1 return _servletRequest.getParameter(parameterName);
 64    }
 65   
 66  371 public String[] getParameterValues(String parameterName)
 67    {
 68  371 return _servletRequest.getParameterValues(parameterName);
 69    }
 70   
 71  160 public String getContextPath()
 72    {
 73  160 return _servletRequest.getContextPath();
 74    }
 75   
 76  351 public WebSession getSession(boolean create)
 77    {
 78  351 if (_webSession != null)
 79  79 return _webSession;
 80   
 81  272 HttpSession session = _servletRequest.getSession(create);
 82   
 83  272 if (session != null)
 84  37 _webSession = new ServletWebSession(session);
 85   
 86  272 return _webSession;
 87    }
 88   
 89  1 public List getAttributeNames()
 90    {
 91  1 return WebUtils.toSortedList(_servletRequest.getAttributeNames());
 92    }
 93   
 94  50 public Object getAttribute(String name)
 95    {
 96  50 return _servletRequest.getAttribute(name);
 97    }
 98   
 99  134 public void setAttribute(String name, Object attribute)
 100    {
 101  134 if (attribute == null)
 102  1 _servletRequest.removeAttribute(name);
 103    else
 104  133 _servletRequest.setAttribute(name, attribute);
 105    }
 106   
 107  120 public String getScheme()
 108    {
 109  120 return _servletRequest.getScheme();
 110    }
 111   
 112  120 public String getServerName()
 113    {
 114  120 return _servletRequest.getServerName();
 115    }
 116   
 117  120 public int getServerPort()
 118    {
 119  120 return _servletRequest.getServerPort();
 120    }
 121   
 122  133 public String getRequestURI()
 123    {
 124  133 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  134 public String getActivationPath()
 183    {
 184  134 String servletPath = _servletRequest.getServletPath();
 185  134 String pathInfo = _servletRequest.getPathInfo();
 186   
 187  134 return pathInfo == null ? servletPath : servletPath + pathInfo;
 188    }
 189   
 190  123 public Locale getLocale()
 191    {
 192  123 return _servletRequest.getLocale();
 193    }
 194   
 195  21 public void describeTo(DescriptionReceiver receiver)
 196    {
 197  21 receiver.describeAlternate(_servletRequest);
 198    }
 199   
 200  1 public String getHeader(String name)
 201    {
 202  1 return _servletRequest.getHeader(name);
 203    }
 204    }