001    // Copyright 2006, 2007, 2008, 2010, 2011 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.tapestry5.internal.services;
016    
017    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
018    import org.apache.tapestry5.services.Request;
019    import org.apache.tapestry5.services.Session;
020    
021    import javax.servlet.http.HttpServletRequest;
022    import java.io.UnsupportedEncodingException;
023    import java.util.List;
024    import java.util.Locale;
025    
026    /**
027     * Basic implementation of {@link org.apache.tapestry5.services.Request} that wraps around an
028     * {@link javax.servlet.http.HttpServletRequest}.
029     */
030    public class RequestImpl implements Request
031    {
032        static final String REQUESTED_WITH_HEADER = "X-Requested-With";
033    
034        static final String XML_HTTP_REQUEST = "XMLHttpRequest";
035    
036        private final HttpServletRequest request;
037    
038        private final String requestEncoding;
039    
040        private final TapestrySessionFactory sessionFactory;
041    
042        private boolean encodingSet;
043    
044        Session session;
045    
046        public RequestImpl(
047                HttpServletRequest request,
048                String requestEncoding,
049                TapestrySessionFactory sessionFactory)
050        {
051            this.request = request;
052            this.requestEncoding = requestEncoding;
053            this.sessionFactory = sessionFactory;
054        }
055    
056        public List<String> getParameterNames()
057        {
058            setupEncoding();
059    
060            return InternalUtils.toList(request.getParameterNames());
061        }
062    
063        public List<String> getHeaderNames()
064        {
065            return InternalUtils.toList(request.getHeaderNames());
066        }
067    
068        public String getParameter(String name)
069        {
070            setupEncoding();
071    
072            return request.getParameter(name);
073        }
074    
075        public String[] getParameters(String name)
076        {
077            setupEncoding();
078    
079            return request.getParameterValues(name);
080        }
081    
082        public String getHeader(String name)
083        {
084            return request.getHeader(name);
085        }
086    
087        public String getPath()
088        {
089            String pathInfo = request.getPathInfo();
090    
091            if (pathInfo == null)
092                return request.getServletPath();
093    
094            // Websphere 6.1 is a bit wonky (see TAPESTRY-1713), and tends to return the empty string
095            // for the servlet path, and return the true path in pathInfo.
096    
097            return pathInfo.length() == 0 ? "/" : pathInfo;
098        }
099    
100        public String getContextPath()
101        {
102            return request.getContextPath();
103        }
104    
105        public Session getSession(boolean create)
106        {
107            if (session != null && session.isInvalidated())
108            {
109                session = null;
110            }
111    
112            if (session == null)
113            {
114                // TAP5-1489 - Re-storage of session attributes at end of request should be configurable
115                session = sessionFactory.getSession(create);
116            }
117    
118            return session;
119        }
120    
121        public Locale getLocale()
122        {
123            return request.getLocale();
124        }
125    
126        public long getDateHeader(String name)
127        {
128            return request.getDateHeader(name);
129        }
130    
131        private void setupEncoding()
132        {
133            if (encodingSet)
134                return;
135    
136            try
137            {
138                request.setCharacterEncoding(requestEncoding);
139            } catch (UnsupportedEncodingException ex)
140            {
141                throw new RuntimeException(ex);
142            }
143    
144            encodingSet = true;
145        }
146    
147        public boolean isXHR()
148        {
149            return XML_HTTP_REQUEST.equals(request.getHeader(REQUESTED_WITH_HEADER));
150        }
151    
152        public boolean isSecure()
153        {
154            return request.isSecure();
155        }
156    
157        public boolean isRequestedSessionIdValid()
158        {
159            return request.isRequestedSessionIdValid();
160        }
161    
162        public Object getAttribute(String name)
163        {
164            return request.getAttribute(name);
165        }
166    
167        public void setAttribute(String name, Object value)
168        {
169            request.setAttribute(name, value);
170        }
171    
172        public String getMethod()
173        {
174            return request.getMethod();
175        }
176    
177        public String getServerName()
178        {
179            return request.getServerName();
180        }
181    
182        public int getLocalPort()
183        {
184            return request.getLocalPort();
185        }
186    
187        /**
188         * @since 5.2.5
189         */
190        public int getServerPort()
191        {
192            return request.getServerPort();
193        }
194    
195        public String getRemoteHost()
196        {
197            return request.getRemoteHost();
198        }
199    }