001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.JavaConstants;
026: import com.liferay.portal.kernel.util.ServerDetector;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.model.Portlet;
029: import com.liferay.portal.model.User;
030: import com.liferay.portal.model.impl.PortletImpl;
031: import com.liferay.portal.service.RoleLocalServiceUtil;
032: import com.liferay.portal.util.PortalUtil;
033:
034: import java.io.BufferedReader;
035: import java.io.IOException;
036: import java.io.UnsupportedEncodingException;
037:
038: import java.security.Principal;
039:
040: import java.util.Enumeration;
041: import java.util.Locale;
042:
043: import javax.servlet.ServletInputStream;
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpServletRequestWrapper;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049:
050: /**
051: * <a href="PortletServletRequest.java.html"><b><i>View Source</i></b></a>
052: *
053: * @author Brian Wing Shun Chan
054: * @author Brian Myunghun Kim
055: *
056: */
057: public class PortletServletRequest extends HttpServletRequestWrapper {
058:
059: public PortletServletRequest(HttpServletRequest req,
060: RenderRequestImpl renderReq, String pathInfo,
061: String queryString, String requestURI, String servletPath) {
062:
063: super (req);
064:
065: _req = req;
066: _renderReq = renderReq;
067: _pathInfo = GetterUtil.getString(pathInfo);
068: _queryString = GetterUtil.getString(queryString);
069: _requestURI = GetterUtil.getString(requestURI);
070: _servletPath = GetterUtil.getString(servletPath);
071:
072: long userId = PortalUtil.getUserId(req);
073: String remoteUser = req.getRemoteUser();
074:
075: Portlet portlet = renderReq.getPortlet();
076:
077: String userPrincipalStrategy = portlet
078: .getUserPrincipalStrategy();
079:
080: if (userPrincipalStrategy
081: .equals(PortletImpl.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
082:
083: try {
084: User user = PortalUtil.getUser(req);
085:
086: _remoteUser = user.getScreenName();
087: _remoteUserId = user.getUserId();
088: _userPrincipal = new ProtectedPrincipal(_remoteUser);
089: } catch (Exception e) {
090: _log.error(e);
091: }
092: } else {
093: if ((userId > 0) && (remoteUser == null)) {
094: _remoteUser = String.valueOf(userId);
095: _remoteUserId = userId;
096: _userPrincipal = new ProtectedPrincipal(_remoteUser);
097: } else {
098: _remoteUser = remoteUser;
099: _remoteUserId = GetterUtil.getLong(remoteUser);
100: _userPrincipal = req.getUserPrincipal();
101: }
102: }
103: }
104:
105: public Object getAttribute(String name) {
106: Object retVal = super .getAttribute(name);
107:
108: if (name == null) {
109: return retVal;
110: }
111:
112: if (ServerDetector.isWebSphere()) {
113: if (_renderReq.getPortlet().isWARFile()) {
114: if (name
115: .equals(JavaConstants.JAVAX_SERVLET_INCLUDE_CONTEXT_PATH)) {
116:
117: retVal = _renderReq.getContextPath();
118: } else if (name
119: .equals(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
120:
121: retVal = _pathInfo;
122: } else if (name
123: .equals(JavaConstants.JAVAX_SERVLET_INCLUDE_QUERY_STRING)) {
124:
125: retVal = _queryString;
126: } else if (name
127: .equals(JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI)) {
128:
129: retVal = _requestURI;
130: } else if (name
131: .equals(JavaConstants.JAVAX_SERVLET_INCLUDE_SERVLET_PATH)) {
132:
133: retVal = _servletPath;
134: }
135: }
136:
137: if ((name.startsWith(JavaConstants.JAVAX_SERVLET_INCLUDE))
138: && (retVal == null)) {
139:
140: retVal = StringPool.BLANK;
141: }
142: }
143:
144: return retVal;
145: }
146:
147: public String getCharacterEncoding() {
148: if (_isUploadRequest()) {
149: return super .getCharacterEncoding();
150: } else {
151: return null;
152: }
153: }
154:
155: public void setCharacterEncoding(String encoding)
156: throws UnsupportedEncodingException {
157: }
158:
159: public int getContentLength() {
160: if (_isUploadRequest()) {
161: return super .getContentLength();
162: } else {
163: return 0;
164: }
165: }
166:
167: public String getContentType() {
168: if (_isUploadRequest()) {
169: return super .getContentType();
170: } else {
171: return null;
172: }
173: }
174:
175: public String getContextPath() {
176: return _renderReq.getContextPath();
177: }
178:
179: public ServletInputStream getInputStream() throws IOException {
180: if (_isUploadRequest()) {
181: return super .getInputStream();
182: } else {
183: return null;
184: }
185: }
186:
187: public Locale getLocale() {
188: return _renderReq.getLocale();
189: }
190:
191: public Enumeration getLocales() {
192: return _renderReq.getLocales();
193: }
194:
195: public String getPathInfo() {
196: return _pathInfo;
197: }
198:
199: public String getProtocol() {
200: return null;
201: }
202:
203: public String getQueryString() {
204: return _queryString;
205: }
206:
207: public BufferedReader getReader() throws IOException {
208: if (_isUploadRequest()) {
209: return super .getReader();
210: } else {
211: return null;
212: }
213: }
214:
215: public String getRealPath(String path) {
216: return null;
217: }
218:
219: public String getRemoteAddr() {
220: return null;
221: }
222:
223: public String getRemoteHost() {
224: return null;
225: }
226:
227: public String getRequestURI() {
228: return _requestURI;
229: }
230:
231: public StringBuffer getRequestURL() {
232: return null;
233: }
234:
235: public String getServletPath() {
236: return _servletPath;
237: }
238:
239: public String getRemoteUser() {
240: return _remoteUser;
241: }
242:
243: public Principal getUserPrincipal() {
244: return _userPrincipal;
245: }
246:
247: public boolean isUserInRole(String role) {
248: if (_remoteUserId <= 0) {
249: return false;
250: } else {
251: try {
252: long companyId = PortalUtil.getCompanyId(_req);
253:
254: return RoleLocalServiceUtil.hasUserRole(_remoteUserId,
255: companyId, role, true);
256: } catch (Exception e) {
257: _log.error(e);
258: }
259:
260: return super .isUserInRole(role);
261: }
262: }
263:
264: private boolean _isUploadRequest() {
265: if (!_uploadRequestInvoked) {
266: _uploadRequestInvoked = true;
267:
268: if (PortalUtil.getUploadServletRequest(this ) != null) {
269: _uploadRequest = true;
270: }
271: }
272:
273: return _uploadRequest;
274: }
275:
276: private static Log _log = LogFactory
277: .getLog(PortletServletRequest.class);
278:
279: private HttpServletRequest _req;
280: private RenderRequestImpl _renderReq;
281: private String _pathInfo;
282: private String _queryString;
283: private String _requestURI;
284: private String _servletPath;
285: private String _remoteUser;
286: private long _remoteUserId;
287: private Principal _userPrincipal;
288: private boolean _uploadRequest;
289: private boolean _uploadRequestInvoked;
290:
291: }
|