1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.JavaConstants;
27  import com.liferay.portal.kernel.util.ServerDetector;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.service.RoleLocalServiceUtil;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.util.servlet.ProtectedPrincipal;
32  
33  import java.io.BufferedReader;
34  import java.io.IOException;
35  import java.io.UnsupportedEncodingException;
36  
37  import java.security.Principal;
38  
39  import java.util.Enumeration;
40  import java.util.Locale;
41  
42  import javax.portlet.PortletRequest;
43  
44  import javax.servlet.ServletInputStream;
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.http.HttpServletRequestWrapper;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  /**
52   * <a href="PortletServletRequest.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Brian Myunghun Kim
56   *
57   */
58  public class PortletServletRequest extends HttpServletRequestWrapper {
59  
60      public PortletServletRequest(HttpServletRequest req,
61                                   PortletRequest portletRequest, String pathInfo,
62                                   String queryString, String requestURI,
63                                   String servletPath) {
64  
65          super(req);
66  
67          _req = req;
68          _portletRequest = portletRequest;
69          _pathInfo = GetterUtil.getString(pathInfo);
70          _queryString = GetterUtil.getString(queryString);
71          _requestURI = GetterUtil.getString(requestURI);
72          _servletPath = GetterUtil.getString(servletPath);
73  
74          long userId = PortalUtil.getUserId(req);
75          String remoteUser = req.getRemoteUser();
76  
77          if ((userId > 0) && (remoteUser == null)) {
78              _remoteUser = String.valueOf(userId);
79              _userPrincipal = new ProtectedPrincipal(_remoteUser);
80          }
81          else {
82              _remoteUser = remoteUser;
83              _userPrincipal = req.getUserPrincipal();
84          }
85      }
86  
87      public Object getAttribute(String name) {
88          Object retVal = super.getAttribute(name);
89  
90          if (name == null) {
91              return retVal;
92          }
93  
94          RenderRequestImpl reqImpl = (RenderRequestImpl)_portletRequest;
95  
96          if (ServerDetector.isWebSphere()) {
97              if (reqImpl.getPortlet().isWARFile()) {
98                  if (name.equals(
99                          JavaConstants.JAVAX_SERVLET_INCLUDE_CONTEXT_PATH)) {
100 
101                     retVal = _portletRequest.getContextPath();
102                 }
103                 else if (name.equals(
104                             JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
105 
106                     retVal = _pathInfo;
107                 }
108                 else if (name.equals(
109                             JavaConstants.JAVAX_SERVLET_INCLUDE_QUERY_STRING)) {
110 
111                     retVal = _queryString;
112                 }
113                 else if (name.equals(
114                             JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI)) {
115 
116                     retVal = _requestURI;
117                 }
118                 else if (name.equals(
119                             JavaConstants.JAVAX_SERVLET_INCLUDE_SERVLET_PATH)) {
120 
121                     retVal = _servletPath;
122                 }
123             }
124 
125             if ((name.startsWith(JavaConstants.JAVAX_SERVLET_INCLUDE)) &&
126                 (retVal == null)) {
127 
128                 retVal = StringPool.BLANK;
129             }
130         }
131 
132         return retVal;
133     }
134 
135     public String getCharacterEncoding() {
136         if (_isUploadRequest()) {
137             return super.getCharacterEncoding();
138         }
139         else {
140             return null;
141         }
142     }
143 
144     public void setCharacterEncoding(String encoding)
145         throws UnsupportedEncodingException {
146     }
147 
148     public int getContentLength() {
149         if (_isUploadRequest()) {
150             return super.getContentLength();
151         }
152         else {
153             return 0;
154         }
155     }
156 
157     public String getContentType() {
158         if (_isUploadRequest()) {
159             return super.getContentType();
160         }
161         else {
162             return null;
163         }
164     }
165 
166     public String getContextPath() {
167         return _portletRequest.getContextPath();
168     }
169 
170     public ServletInputStream getInputStream() throws IOException {
171         if (_isUploadRequest()) {
172             return super.getInputStream();
173         }
174         else {
175             return null;
176         }
177     }
178 
179     public Locale getLocale() {
180         return _portletRequest.getLocale();
181     }
182 
183     public Enumeration getLocales() {
184         return _portletRequest.getLocales();
185     }
186 
187     public String getPathInfo() {
188         return _pathInfo;
189     }
190 
191     public String getProtocol() {
192         return null;
193     }
194 
195     public String getQueryString() {
196         return _queryString;
197     }
198 
199     public BufferedReader getReader() throws IOException {
200         if (_isUploadRequest()) {
201             return super.getReader();
202         }
203         else {
204             return null;
205         }
206     }
207 
208     public String getRealPath(String path) {
209         return null;
210     }
211 
212     public String getRemoteAddr() {
213         return null;
214     }
215 
216     public String getRemoteHost() {
217         return null;
218     }
219 
220     public String getRequestURI() {
221         return _requestURI;
222     }
223 
224     public StringBuffer getRequestURL() {
225         return null;
226     }
227 
228     public String getServletPath() {
229         return _servletPath;
230     }
231 
232     public String getRemoteUser() {
233         return _remoteUser;
234     }
235 
236     public Principal getUserPrincipal() {
237         return _userPrincipal;
238     }
239 
240     public boolean isUserInRole(String role) {
241         String remoteUser = getRemoteUser();
242 
243         if (remoteUser == null) {
244             return false;
245         }
246         else {
247             try {
248                 long companyId = PortalUtil.getCompanyId(_req);
249                 long userId = GetterUtil.getLong(remoteUser);
250 
251                 return RoleLocalServiceUtil.hasUserRole(
252                     userId, companyId, role, true);
253             }
254             catch (Exception e) {
255                 _log.warn(e);
256             }
257 
258             return super.isUserInRole(role);
259         }
260     }
261 
262     private boolean _isUploadRequest() {
263         if (!_uploadRequestInvoked) {
264             _uploadRequestInvoked = true;
265 
266             if (PortalUtil.getUploadServletRequest(this) != null) {
267                 _uploadRequest = true;
268             }
269         }
270 
271         return _uploadRequest;
272     }
273 
274     private static Log _log = LogFactory.getLog(PortletServletRequest.class);
275 
276     private HttpServletRequest _req;
277     private PortletRequest _portletRequest;
278     private String _pathInfo;
279     private String _queryString;
280     private String _requestURI;
281     private String _servletPath;
282     private String _remoteUser;
283     private Principal _userPrincipal;
284     private boolean _uploadRequest;
285     private boolean _uploadRequestInvoked;
286 
287 }