Coverage Report - org.apache.tapestry.web.ServletWebRequest
 
Classes in this File Line Coverage Branch Coverage Complexity
ServletWebRequest
0%
0/62
0%
0/16
1.593
 
 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 org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.hivemind.HiveMind;
 19  
 import org.apache.hivemind.util.Defense;
 20  
 import org.apache.tapestry.describe.DescriptionReceiver;
 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  
 import java.io.IOException;
 28  
 import java.security.Principal;
 29  
 import java.util.List;
 30  
 import java.util.Locale;
 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  
     public ServletWebRequest(HttpServletRequest request, HttpServletResponse response)
 48  0
     {
 49  0
         Defense.notNull(request, "request");
 50  0
         Defense.notNull(response, "response");
 51  
         
 52  0
         _servletRequest = request;
 53  0
         _servletResponse = response;
 54  0
     }
 55  
 
 56  
     public List getParameterNames()
 57  
     {
 58  0
         return WebUtils.toSortedList(_servletRequest.getParameterNames());
 59  
     }
 60  
 
 61  
     public String getParameterValue(String parameterName)
 62  
     {
 63  0
         return _servletRequest.getParameter(parameterName);
 64  
     }
 65  
 
 66  
     public String[] getParameterValues(String parameterName)
 67  
     {
 68  0
         return _servletRequest.getParameterValues(parameterName);
 69  
     }
 70  
 
 71  
     public String getContextPath()
 72  
     {
 73  0
         return _servletRequest.getContextPath();
 74  
     }
 75  
 
 76  
     public WebSession getSession(boolean create)
 77  
     {
 78  0
         if (_webSession != null)
 79  0
             return _webSession;
 80  
 
 81  0
         HttpSession session = _servletRequest.getSession(create);
 82  
 
 83  0
         if (session != null)
 84  0
             _webSession = new ServletWebSession(session);
 85  
 
 86  0
         return _webSession;
 87  
     }
 88  
 
 89  
     public List getAttributeNames()
 90  
     {
 91  0
         return WebUtils.toSortedList(_servletRequest.getAttributeNames());
 92  
     }
 93  
 
 94  
     public Object getAttribute(String name)
 95  
     {
 96  0
         return _servletRequest.getAttribute(name);
 97  
     }
 98  
 
 99  
     public void setAttribute(String name, Object attribute)
 100  
     {
 101  0
         if (attribute == null)
 102  0
             _servletRequest.removeAttribute(name);
 103  
         else
 104  0
             _servletRequest.setAttribute(name, attribute);
 105  0
     }
 106  
 
 107  
     public String getScheme()
 108  
     {
 109  0
         return _servletRequest.getScheme();
 110  
     }
 111  
 
 112  
     public String getServerName()
 113  
     {
 114  0
         return _servletRequest.getServerName();
 115  
     }
 116  
 
 117  
     public int getServerPort()
 118  
     {
 119  0
         return _servletRequest.getServerPort();
 120  
     }
 121  
 
 122  
     public String getRequestURI()
 123  
     {
 124  0
         return _servletRequest.getRequestURI();
 125  
     }
 126  
 
 127  
     public void forward(String URL)
 128  
     {
 129  0
         if (HiveMind.isBlank(URL))
 130  
         {
 131  0
             performForward("/");
 132  0
             return;
 133  
         }
 134  
 
 135  0
         boolean internal = !(URL.startsWith("/") || URL.indexOf("://") > 0);
 136  
 
 137  0
         if (internal)
 138  0
             performForward("/" + URL);
 139  
         else
 140  0
             sendRedirect(URL);
 141  0
     }
 142  
 
 143  
     private void sendRedirect(String URL)
 144  
     {
 145  0
         String finalURL = _servletResponse.encodeRedirectURL(URL);
 146  
 
 147  
         try
 148  
         {
 149  0
             _servletResponse.sendRedirect(finalURL);
 150  
         }
 151  0
         catch (IOException ex)
 152  
         {
 153  0
             throw new ApplicationRuntimeException(WebMessages.unableToRedirect(URL, ex), ex);
 154  0
         }
 155  
 
 156  0
     }
 157  
 
 158  
     private void performForward(String URL)
 159  
     {
 160  0
         RequestDispatcher dispatcher = _servletRequest.getRequestDispatcher(URL);
 161  
 
 162  0
         if (dispatcher == null)
 163  0
             throw new ApplicationRuntimeException(WebMessages.unableToFindDispatcher(URL));
 164  
 
 165  
         try
 166  
         {
 167  0
             dispatcher.forward(_servletRequest, _servletResponse);
 168  
         }
 169  0
         catch (ServletException ex)
 170  
         {
 171  0
             throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
 172  
         }
 173  0
         catch (IOException ex)
 174  
         {
 175  0
             throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
 176  0
         }
 177  0
     }
 178  
 
 179  
     /**
 180  
      * Returns {@link HttpServletRequest#getServletPath()}.
 181  
      */
 182  
     public String getActivationPath()
 183  
     {
 184  0
         return _servletRequest.getServletPath();
 185  
     }
 186  
 
 187  
     public String getPathInfo()
 188  
     {
 189  0
         return _servletRequest.getPathInfo();
 190  
     }
 191  
 
 192  
     public Locale getLocale()
 193  
     {
 194  0
         return _servletRequest.getLocale();
 195  
     }
 196  
 
 197  
     public void describeTo(DescriptionReceiver receiver)
 198  
     {
 199  0
         receiver.describeAlternate(_servletRequest);
 200  0
     }
 201  
 
 202  
     public String getHeader(String name)
 203  
     {
 204  0
         return _servletRequest.getHeader(name);
 205  
     }
 206  
 
 207  
     public long getDateHeader(String name)
 208  
     {
 209  0
         return _servletRequest.getDateHeader(name);
 210  
     }
 211  
 
 212  
     public int getIntHeader(String name)
 213  
     {
 214  0
         return _servletRequest.getIntHeader(name);
 215  
     }
 216  
 
 217  
     public String getRemoteUser()
 218  
     {
 219  0
         return _servletRequest.getRemoteUser();
 220  
     }
 221  
 
 222  
     public Principal getUserPrincipal()
 223  
     {
 224  0
         return _servletRequest.getUserPrincipal();
 225  
     }
 226  
 
 227  
     public boolean isUserInRole(String role)
 228  
     {
 229  0
         return _servletRequest.isUserInRole(role);
 230  
     }
 231  
     
 232  
     public boolean isSecure()
 233  
     {
 234  0
         return _servletRequest.isSecure();
 235  
     }
 236  
 }