001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.web;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.HiveMind;
019    import org.apache.hivemind.util.Defense;
020    import org.apache.tapestry.describe.DescriptionReceiver;
021    
022    import javax.servlet.RequestDispatcher;
023    import javax.servlet.ServletException;
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    import javax.servlet.http.HttpSession;
027    import java.io.IOException;
028    import java.security.Principal;
029    import java.util.List;
030    import java.util.Locale;
031    
032    /**
033     * Adapter from {@link javax.servlet.http.HttpServletRequest} to
034     * {@link org.apache.tapestry.web.WebRequest}.
035     * 
036     * @author Howard M. Lewis Ship
037     * @since 4.0
038     */
039    public class ServletWebRequest implements WebRequest
040    {
041        private final HttpServletRequest _servletRequest;
042    
043        private final HttpServletResponse _servletResponse;
044    
045        private WebSession _webSession;
046    
047        public ServletWebRequest(HttpServletRequest request, HttpServletResponse response)
048        {
049            Defense.notNull(request, "request");
050            Defense.notNull(response, "response");
051            
052            _servletRequest = request;
053            _servletResponse = response;
054        }
055    
056        public List getParameterNames()
057        {
058            return WebUtils.toSortedList(_servletRequest.getParameterNames());
059        }
060    
061        public String getParameterValue(String parameterName)
062        {
063            return _servletRequest.getParameter(parameterName);
064        }
065    
066        public String[] getParameterValues(String parameterName)
067        {
068            return _servletRequest.getParameterValues(parameterName);
069        }
070    
071        public String getContextPath()
072        {
073            return _servletRequest.getContextPath();
074        }
075    
076        public WebSession getSession(boolean create)
077        {
078            if (_webSession != null)
079                return _webSession;
080    
081            HttpSession session = _servletRequest.getSession(create);
082    
083            if (session != null)
084                _webSession = new ServletWebSession(session);
085    
086            return _webSession;
087        }
088    
089        public List getAttributeNames()
090        {
091            return WebUtils.toSortedList(_servletRequest.getAttributeNames());
092        }
093    
094        public Object getAttribute(String name)
095        {
096            return _servletRequest.getAttribute(name);
097        }
098    
099        public void setAttribute(String name, Object attribute)
100        {
101            if (attribute == null)
102                _servletRequest.removeAttribute(name);
103            else
104                _servletRequest.setAttribute(name, attribute);
105        }
106    
107        public String getScheme()
108        {
109            return _servletRequest.getScheme();
110        }
111    
112        public String getServerName()
113        {
114            return _servletRequest.getServerName();
115        }
116    
117        public int getServerPort()
118        {
119            return _servletRequest.getServerPort();
120        }
121    
122        public String getRequestURI()
123        {
124            return _servletRequest.getRequestURI();
125        }
126    
127        public void forward(String URL)
128        {
129            if (HiveMind.isBlank(URL))
130            {
131                performForward("/");
132                return;
133            }
134    
135            boolean internal = !(URL.startsWith("/") || URL.indexOf("://") > 0);
136    
137            if (internal)
138                performForward("/" + URL);
139            else
140                sendRedirect(URL);
141        }
142    
143        private void sendRedirect(String URL)
144        {
145            String finalURL = _servletResponse.encodeRedirectURL(URL);
146    
147            try
148            {
149                _servletResponse.sendRedirect(finalURL);
150            }
151            catch (IOException ex)
152            {
153                throw new ApplicationRuntimeException(WebMessages.unableToRedirect(URL, ex), ex);
154            }
155    
156        }
157    
158        private void performForward(String URL)
159        {
160            RequestDispatcher dispatcher = _servletRequest.getRequestDispatcher(URL);
161    
162            if (dispatcher == null)
163                throw new ApplicationRuntimeException(WebMessages.unableToFindDispatcher(URL));
164    
165            try
166            {
167                dispatcher.forward(_servletRequest, _servletResponse);
168            }
169            catch (ServletException ex)
170            {
171                throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
172            }
173            catch (IOException ex)
174            {
175                throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
176            }
177        }
178    
179        /**
180         * Returns {@link HttpServletRequest#getServletPath()}.
181         */
182        public String getActivationPath()
183        {
184            return _servletRequest.getServletPath();
185        }
186    
187        public String getPathInfo()
188        {
189            return _servletRequest.getPathInfo();
190        }
191    
192        public Locale getLocale()
193        {
194            return _servletRequest.getLocale();
195        }
196    
197        public void describeTo(DescriptionReceiver receiver)
198        {
199            receiver.describeAlternate(_servletRequest);
200        }
201    
202        public String getHeader(String name)
203        {
204            return _servletRequest.getHeader(name);
205        }
206    
207        public long getDateHeader(String name)
208        {
209            return _servletRequest.getDateHeader(name);
210        }
211    
212        public int getIntHeader(String name)
213        {
214            return _servletRequest.getIntHeader(name);
215        }
216    
217        public String getRemoteUser()
218        {
219            return _servletRequest.getRemoteUser();
220        }
221    
222        public Principal getUserPrincipal()
223        {
224            return _servletRequest.getUserPrincipal();
225        }
226    
227        public boolean isUserInRole(String role)
228        {
229            return _servletRequest.isUserInRole(role);
230        }
231        
232        public boolean isSecure()
233        {
234            return _servletRequest.isSecure();
235        }
236    }