001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.servlet.HttpMethods;
020 import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.JavaConstants;
023 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
024 import com.liferay.portal.kernel.util.ServerDetector;
025 import com.liferay.portal.model.Portlet;
026 import com.liferay.portal.model.PortletConstants;
027 import com.liferay.portal.model.User;
028 import com.liferay.portal.util.PortalUtil;
029
030 import java.io.BufferedReader;
031 import java.io.IOException;
032 import java.io.UnsupportedEncodingException;
033
034 import java.lang.reflect.Constructor;
035
036 import java.security.Principal;
037
038 import java.util.Enumeration;
039 import java.util.Locale;
040 import java.util.Map;
041
042 import javax.portlet.PortletRequest;
043
044 import javax.servlet.RequestDispatcher;
045 import javax.servlet.ServletInputStream;
046 import javax.servlet.http.Cookie;
047 import javax.servlet.http.HttpServletRequest;
048 import javax.servlet.http.HttpServletRequestWrapper;
049 import javax.servlet.http.HttpSession;
050
051
055 public class PortletServletRequest extends HttpServletRequestWrapper {
056
057 public PortletServletRequest(
058 HttpServletRequest request, PortletRequestImpl portletRequestImpl,
059 String pathInfo, String queryString, String requestURI,
060 String servletPath, boolean named, boolean include) {
061
062 super(request);
063
064 _request = request;
065 _portletRequestImpl = portletRequestImpl;
066 _lifecycle = _portletRequestImpl.getLifecycle();
067 _pathInfo = pathInfo;
068 _queryString = queryString;
069 _requestURI = GetterUtil.getString(requestURI);
070 _servletPath = GetterUtil.getString(servletPath);
071 _named = named;
072 _include = include;
073
074 long userId = PortalUtil.getUserId(request);
075 String remoteUser = request.getRemoteUser();
076
077 Portlet portlet = portletRequestImpl.getPortlet();
078
079 String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
080
081 if (userPrincipalStrategy.equals(
082 PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
083
084 try {
085 User user = PortalUtil.getUser(request);
086
087 if (user != null) {
088 _remoteUser = user.getScreenName();
089 _userPrincipal = new ProtectedPrincipal(_remoteUser);
090 }
091 }
092 catch (Exception e) {
093 _log.error(e);
094 }
095 }
096 else {
097 if ((userId > 0) && (remoteUser == null)) {
098 _remoteUser = String.valueOf(userId);
099 _userPrincipal = new ProtectedPrincipal(_remoteUser);
100 }
101 else {
102 _remoteUser = remoteUser;
103 _userPrincipal = request.getUserPrincipal();
104 }
105 }
106 }
107
108 public Object getAttribute(String name) {
109 if (_include || (name == null)) {
110 return _request.getAttribute(name);
111 }
112
113 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_CONTEXT_PATH)) {
114 if (_named) {
115 return null;
116 }
117 else {
118 return _portletRequestImpl.getContextPath();
119 }
120 }
121
122 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_PATH_INFO)) {
123 if (_named) {
124 return null;
125 }
126 else {
127 return _pathInfo;
128 }
129 }
130
131 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_QUERY_STRING)) {
132 if (_named) {
133 return null;
134 }
135 else {
136 return _queryString;
137 }
138 }
139
140 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_REQUEST_URI)) {
141 if (_named) {
142 return null;
143 }
144 else {
145 return _requestURI;
146 }
147 }
148
149 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_SERVLET_PATH)) {
150 if (_named) {
151 return null;
152 }
153 else {
154 return _servletPath;
155 }
156 }
157
158 return _request.getAttribute(name);
159 }
160
161 public Enumeration<String> getAttributeNames() {
162 return _request.getAttributeNames();
163 }
164
165 public String getAuthType() {
166 return _request.getAuthType();
167 }
168
169 public String getCharacterEncoding() {
170 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
171 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
172
173 return _request.getCharacterEncoding();
174 }
175 else {
176 return null;
177 }
178 }
179
180 public int getContentLength() {
181 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
182 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
183
184 return _request.getContentLength();
185 }
186 else {
187 return 0;
188 }
189 }
190
191 public String getContentType() {
192 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
193 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
194
195 return _request.getContentType();
196 }
197 else {
198 return null;
199 }
200 }
201
202 public String getContextPath() {
203 return _portletRequestImpl.getContextPath();
204 }
205
206 public Cookie[] getCookies() {
207 return _request.getCookies();
208 }
209
210 public long getDateHeader(String name) {
211 return GetterUtil.getLong(getHeader(name), -1);
212 }
213
214 public String getHeader(String name) {
215 HttpServletRequest request =
216 _portletRequestImpl.getHttpServletRequest();
217
218 return request.getHeader(name);
219 }
220
221 public Enumeration<String> getHeaderNames() {
222 HttpServletRequest request =
223 _portletRequestImpl.getHttpServletRequest();
224
225 return request.getHeaderNames();
226 }
227
228 public Enumeration<String> getHeaders(String name) {
229 HttpServletRequest request =
230 _portletRequestImpl.getHttpServletRequest();
231
232 return request.getHeaders(name);
233 }
234
235 public ServletInputStream getInputStream() throws IOException {
236 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
237 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
238
239 return _request.getInputStream();
240 }
241 else {
242 return null;
243 }
244 }
245
246 public int getIntHeader(String name) {
247 return GetterUtil.getInteger(getHeader(name));
248 }
249
250 public String getLocalAddr() {
251 return null;
252 }
253
254 public Locale getLocale() {
255 return _portletRequestImpl.getLocale();
256 }
257
258 public Enumeration<Locale> getLocales() {
259 return _request.getLocales();
260 }
261
262 public String getLocalName() {
263 return null;
264 }
265
266 public int getLocalPort() {
267 return 0;
268 }
269
270 public String getMethod() {
271 if (_lifecycle.equals(PortletRequest.RENDER_PHASE)) {
272 return HttpMethods.GET;
273 }
274 else {
275 return _request.getMethod();
276 }
277 }
278
279 public String getParameter(String name) {
280 return _request.getParameter(name);
281 }
282
283 public Map<String, String[]> getParameterMap() {
284 return _request.getParameterMap();
285 }
286
287 public Enumeration<String> getParameterNames() {
288 return _request.getParameterNames();
289 }
290
291 public String[] getParameterValues(String name) {
292 return _request.getParameterValues(name);
293 }
294
295 public String getPathInfo() {
296 return _pathInfo;
297 }
298
299 public String getPathTranslated() {
300 return _request.getPathTranslated();
301 }
302
303 public String getProtocol() {
304 return "HTTP/1.1";
305 }
306
307 public String getQueryString() {
308 return _queryString;
309 }
310
311 public BufferedReader getReader() throws IOException {
312 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
313 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
314
315 return _request.getReader();
316 }
317 else {
318 return null;
319 }
320 }
321
322 public String getRealPath(String path) {
323 return null;
324 }
325
326 public RequestDispatcher getRequestDispatcher(String path) {
327 return _request.getRequestDispatcher(path);
328 }
329
330 public String getRequestedSessionId() {
331 return _request.getRequestedSessionId();
332 }
333
334 public String getRemoteAddr() {
335 return null;
336 }
337
338 public String getRemoteHost() {
339 return null;
340 }
341
342 public int getRemotePort() {
343 return 0;
344 }
345
346 public String getRequestURI() {
347 return _requestURI;
348 }
349
350 public StringBuffer getRequestURL() {
351 return null;
352 }
353
354 public String getRemoteUser() {
355 return _remoteUser;
356 }
357
358 public String getScheme() {
359 return _request.getScheme();
360 }
361
362 public String getServerName() {
363 return _request.getServerName();
364 }
365
366 public int getServerPort() {
367 return _request.getServerPort();
368 }
369
370 public String getServletPath() {
371 return _servletPath;
372 }
373
374 public HttpSession getSession() {
375 HttpSession session = new PortletServletSession(
376 _request.getSession(), _portletRequestImpl);
377
378 if (ServerDetector.isJetty()) {
379 try {
380 session = wrapJettySession(session);
381 }
382 catch (Exception e) {
383 _log.error(e, e);
384 }
385 }
386
387 return session;
388 }
389
390 public HttpSession getSession(boolean create) {
391 HttpSession session = new PortletServletSession(
392 _request.getSession(create), _portletRequestImpl);
393
394 if (ServerDetector.isJetty()) {
395 try {
396 session = wrapJettySession(session);
397 }
398 catch (Exception e) {
399 _log.error(e, e);
400 }
401 }
402
403 return session;
404 }
405
406 public Principal getUserPrincipal() {
407 return _userPrincipal;
408 }
409
410 public boolean isRequestedSessionIdFromCookie() {
411 return _request.isRequestedSessionIdFromCookie();
412 }
413
414 public boolean isRequestedSessionIdFromURL() {
415 return _request.isRequestedSessionIdFromURL();
416 }
417
418
421 public boolean isRequestedSessionIdFromUrl() {
422 return _request.isRequestedSessionIdFromUrl();
423 }
424
425 public boolean isRequestedSessionIdValid() {
426 return _request.isRequestedSessionIdValid();
427 }
428
429 public boolean isSecure() {
430 return _request.isSecure();
431 }
432
433 public boolean isUserInRole(String role) {
434 return _portletRequestImpl.isUserInRole(role);
435 }
436
437 public void removeAttribute(String name) {
438 _request.removeAttribute(name);
439 }
440
441 public void setAttribute(String name, Object obj) {
442 _request.setAttribute(name, obj);
443 }
444
445 public void setCharacterEncoding(String encoding)
446 throws UnsupportedEncodingException {
447
448 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
449 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
450
451 _request.setCharacterEncoding(encoding);
452 }
453 }
454
455 protected HttpSession wrapJettySession(HttpSession session)
456 throws Exception {
457
458
459
460
461 ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
462
463 Class<?> jettyHttpSessionWrapperClass = classLoader.loadClass(
464 "com.liferay.util.servlet.JettyHttpSessionWrapper");
465
466 Constructor<?> constructor =
467 jettyHttpSessionWrapperClass.getConstructor(
468 new Class[] {HttpSession.class});
469
470 return(HttpSession)constructor.newInstance(new Object[] {session});
471 }
472
473 private static Log _log = LogFactoryUtil.getLog(
474 PortletServletRequest.class);
475
476 private HttpServletRequest _request;
477 private PortletRequestImpl _portletRequestImpl;
478 private String _lifecycle;
479 private String _pathInfo;
480 private String _queryString;
481 private String _remoteUser;
482 private String _requestURI;
483 private String _servletPath;
484 private Principal _userPrincipal;
485 private boolean _named;
486 private boolean _include;
487
488 }