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
015package org.apache.tapestry.web;
016
017import java.io.IOException;
018import java.security.Principal;
019import java.util.List;
020import java.util.Locale;
021
022import javax.servlet.RequestDispatcher;
023import javax.servlet.ServletException;
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026import javax.servlet.http.HttpSession;
027
028import org.apache.hivemind.ApplicationRuntimeException;
029import org.apache.hivemind.HiveMind;
030import org.apache.hivemind.util.Defense;
031import org.apache.tapestry.describe.DescriptionReceiver;
032
033/**
034 * Adapter from {@link javax.servlet.http.HttpServletRequest} to
035 * {@link org.apache.tapestry.web.WebRequest}.
036 * 
037 * @author Howard M. Lewis Ship
038 * @since 4.0
039 */
040public class ServletWebRequest implements WebRequest
041{
042    private final HttpServletRequest _servletRequest;
043
044    private final HttpServletResponse _servletResponse;
045
046    private WebSession _webSession;
047
048    public ServletWebRequest(HttpServletRequest request, HttpServletResponse response)
049    {
050        Defense.notNull(request, "request");
051        Defense.notNull(response, "response");
052
053        _servletRequest = request;
054        _servletResponse = response;
055    }
056
057    public List getParameterNames()
058    {
059        return WebUtils.toSortedList(_servletRequest.getParameterNames());
060    }
061
062    public String getParameterValue(String parameterName)
063    {
064        return _servletRequest.getParameter(parameterName);
065    }
066
067    public String[] getParameterValues(String parameterName)
068    {
069        return _servletRequest.getParameterValues(parameterName);
070    }
071
072    public String getContextPath()
073    {
074        return _servletRequest.getContextPath();
075    }
076
077    public WebSession getSession(boolean create)
078    {
079        if (_webSession != null)
080            return _webSession;
081
082        HttpSession session = _servletRequest.getSession(create);
083
084        if (session != null)
085            _webSession = new ServletWebSession(session);
086
087        return _webSession;
088    }
089
090    public List getAttributeNames()
091    {
092        return WebUtils.toSortedList(_servletRequest.getAttributeNames());
093    }
094
095    public Object getAttribute(String name)
096    {
097        return _servletRequest.getAttribute(name);
098    }
099
100    public void setAttribute(String name, Object attribute)
101    {
102        if (attribute == null)
103            _servletRequest.removeAttribute(name);
104        else
105            _servletRequest.setAttribute(name, attribute);
106    }
107
108    public String getScheme()
109    {
110        return _servletRequest.getScheme();
111    }
112
113    public String getServerName()
114    {
115        return _servletRequest.getServerName();
116    }
117
118    public int getServerPort()
119    {
120        return _servletRequest.getServerPort();
121    }
122
123    public String getRequestURI()
124    {
125        return _servletRequest.getRequestURI();
126    }
127
128    public void forward(String URL)
129    {
130        if (HiveMind.isBlank(URL))
131        {
132            performForward("/");
133            return;
134        }
135
136        boolean internal = !(URL.startsWith("/") || URL.indexOf("://") > 0);
137
138        if (internal)
139            performForward("/" + URL);
140        else
141            sendRedirect(URL);
142    }
143
144    private void sendRedirect(String URL)
145    {
146        String finalURL = _servletResponse.encodeRedirectURL(URL);
147
148        try
149        {
150            _servletResponse.sendRedirect(finalURL);
151        }
152        catch (IOException ex)
153        {
154            throw new ApplicationRuntimeException(WebMessages.unableToRedirect(URL, ex), ex);
155        }
156
157    }
158
159    private void performForward(String URL)
160    {
161        RequestDispatcher dispatcher = _servletRequest.getRequestDispatcher(URL);
162
163        if (dispatcher == null)
164            throw new ApplicationRuntimeException(WebMessages.unableToFindDispatcher(URL));
165
166        try
167        {
168            dispatcher.forward(_servletRequest, _servletResponse);
169        }
170        catch (ServletException ex)
171        {
172            throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
173        }
174        catch (IOException ex)
175        {
176            throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
177        }
178    }
179
180    /**
181     * Returns {@link HttpServletRequest#getServletPath()}.
182     */
183    public String getActivationPath()
184    {
185        return _servletRequest.getServletPath();
186    }
187
188    public String getPathInfo()
189    {
190        return _servletRequest.getPathInfo();
191    }
192
193    public Locale getLocale()
194    {
195        return _servletRequest.getLocale();
196    }
197
198    public void describeTo(DescriptionReceiver receiver)
199    {
200        receiver.describeAlternate(_servletRequest);
201    }
202
203    public String getHeader(String name)
204    {
205        return _servletRequest.getHeader(name);
206    }
207
208    public String getRemoteUser()
209    {
210        return _servletRequest.getRemoteUser();
211    }
212
213    public Principal getUserPrincipal()
214    {
215        return _servletRequest.getUserPrincipal();
216    }
217
218    public boolean isUserInRole(String role)
219    {
220        return _servletRequest.isUserInRole(role);
221    }
222}