1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.servlet.HttpMethods;
28 import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.JavaConstants;
31 import com.liferay.portal.model.Portlet;
32 import com.liferay.portal.model.PortletConstants;
33 import com.liferay.portal.model.User;
34 import com.liferay.portal.util.PortalUtil;
35
36 import java.io.BufferedReader;
37 import java.io.IOException;
38 import java.io.UnsupportedEncodingException;
39
40 import java.security.Principal;
41
42 import java.util.Enumeration;
43 import java.util.Locale;
44 import java.util.Map;
45
46 import javax.portlet.PortletRequest;
47
48 import javax.servlet.RequestDispatcher;
49 import javax.servlet.ServletInputStream;
50 import javax.servlet.http.Cookie;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletRequestWrapper;
53 import javax.servlet.http.HttpSession;
54
55
61 public class PortletServletRequest extends HttpServletRequestWrapper {
62
63 public PortletServletRequest(
64 HttpServletRequest request, PortletRequestImpl portletRequestImpl,
65 String pathInfo, String queryString, String requestURI,
66 String servletPath, boolean named, boolean include) {
67
68 super(request);
69
70 _request = request;
71 _portletRequestImpl = portletRequestImpl;
72 _lifecycle = _portletRequestImpl.getLifecycle();
73 _pathInfo = pathInfo;
74 _queryString = queryString;
75 _requestURI = GetterUtil.getString(requestURI);
76 _servletPath = GetterUtil.getString(servletPath);
77 _named = named;
78 _include = include;
79
80 long userId = PortalUtil.getUserId(request);
81 String remoteUser = request.getRemoteUser();
82
83 Portlet portlet = portletRequestImpl.getPortlet();
84
85 String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
86
87 if (userPrincipalStrategy.equals(
88 PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
89
90 try {
91 User user = PortalUtil.getUser(request);
92
93 _remoteUser = user.getScreenName();
94 _userPrincipal = new ProtectedPrincipal(_remoteUser);
95 }
96 catch (Exception e) {
97 _log.error(e);
98 }
99 }
100 else {
101 if ((userId > 0) && (remoteUser == null)) {
102 _remoteUser = String.valueOf(userId);
103 _userPrincipal = new ProtectedPrincipal(_remoteUser);
104 }
105 else {
106 _remoteUser = remoteUser;
107 _userPrincipal = request.getUserPrincipal();
108 }
109 }
110 }
111
112 public Object getAttribute(String name) {
113 if (_include || (name == null)) {
114 return _request.getAttribute(name);
115 }
116
117 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_CONTEXT_PATH)) {
118 if (_named) {
119 return null;
120 }
121 else {
122 return _portletRequestImpl.getContextPath();
123 }
124 }
125
126 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_PATH_INFO)) {
127 if (_named) {
128 return null;
129 }
130 else {
131 return _pathInfo;
132 }
133 }
134
135 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_QUERY_STRING)) {
136 if (_named) {
137 return null;
138 }
139 else {
140 return _queryString;
141 }
142 }
143
144 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_REQUEST_URI)) {
145 if (_named) {
146 return null;
147 }
148 else {
149 return _requestURI;
150 }
151 }
152
153 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_SERVLET_PATH)) {
154 if (_named) {
155 return null;
156 }
157 else {
158 return _servletPath;
159 }
160 }
161
162 return _request.getAttribute(name);
163 }
164
165 public Enumeration<String> getAttributeNames() {
166 return _request.getAttributeNames();
167 }
168
169 public String getAuthType() {
170 return _request.getAuthType();
171 }
172
173 public String getCharacterEncoding() {
174 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
175 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
176
177 return _request.getCharacterEncoding();
178 }
179 else {
180 return null;
181 }
182 }
183
184 public int getContentLength() {
185 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
186 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
187
188 return _request.getContentLength();
189 }
190 else {
191 return 0;
192 }
193 }
194
195 public String getContentType() {
196 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
197 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
198
199 return _request.getContentType();
200 }
201 else {
202 return null;
203 }
204 }
205
206 public String getContextPath() {
207 return _portletRequestImpl.getContextPath();
208 }
209
210 public Cookie[] getCookies() {
211 return _request.getCookies();
212 }
213
214 public long getDateHeader(String name) {
215 return GetterUtil.getLong(getHeader(name), -1);
216 }
217
218 public String getHeader(String name) {
219 HttpServletRequest request =
220 _portletRequestImpl.getHttpServletRequest();
221
222 return request.getHeader(name);
223 }
224
225 public Enumeration<String> getHeaderNames() {
226 HttpServletRequest request =
227 _portletRequestImpl.getHttpServletRequest();
228
229 return request.getHeaderNames();
230 }
231
232 public Enumeration<String> getHeaders(String name) {
233 HttpServletRequest request =
234 _portletRequestImpl.getHttpServletRequest();
235
236 return request.getHeaders(name);
237 }
238
239 public ServletInputStream getInputStream() throws IOException {
240 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
241 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
242
243 return _request.getInputStream();
244 }
245 else {
246 return null;
247 }
248 }
249
250 public int getIntHeader(String name) {
251 return GetterUtil.getInteger(getHeader(name));
252 }
253
254 public String getLocalAddr() {
255 return null;
256 }
257
258 public Locale getLocale() {
259 return _portletRequestImpl.getLocale();
260 }
261
262 public Enumeration<Locale> getLocales() {
263 return _request.getLocales();
264 }
265
266 public String getLocalName() {
267 return null;
268 }
269
270 public int getLocalPort() {
271 return 0;
272 }
273
274 public String getMethod() {
275 if (_lifecycle.equals(PortletRequest.RENDER_PHASE)) {
276 return HttpMethods.GET;
277 }
278 else {
279 return _request.getMethod();
280 }
281 }
282
283 public String getParameter(String name) {
284 return _request.getParameter(name);
285 }
286
287 public Map<String, String[]> getParameterMap() {
288 return _request.getParameterMap();
289 }
290
291 public Enumeration<String> getParameterNames() {
292 return _request.getParameterNames();
293 }
294
295 public String[] getParameterValues(String name) {
296 return _request.getParameterValues(name);
297 }
298
299 public String getPathInfo() {
300 return _pathInfo;
301 }
302
303 public String getPathTranslated() {
304 return _request.getPathTranslated();
305 }
306
307 public String getProtocol() {
308 return "HTTP/1.1";
309 }
310
311 public String getQueryString() {
312 return _queryString;
313 }
314
315 public BufferedReader getReader() throws IOException {
316 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
317 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
318
319 return _request.getReader();
320 }
321 else {
322 return null;
323 }
324 }
325
326 public String getRealPath(String path) {
327 return null;
328 }
329
330 public RequestDispatcher getRequestDispatcher(String path) {
331 return _request.getRequestDispatcher(path);
332 }
333
334 public String getRequestedSessionId() {
335 return _request.getRequestedSessionId();
336 }
337
338 public String getRemoteAddr() {
339 return null;
340 }
341
342 public String getRemoteHost() {
343 return null;
344 }
345
346 public int getRemotePort() {
347 return 0;
348 }
349
350 public String getRequestURI() {
351 return _requestURI;
352 }
353
354 public StringBuffer getRequestURL() {
355 return null;
356 }
357
358 public String getRemoteUser() {
359 return _remoteUser;
360 }
361
362 public String getScheme() {
363 return _request.getScheme();
364 }
365
366 public String getServerName() {
367 return _request.getServerName();
368 }
369
370 public int getServerPort() {
371 return _request.getServerPort();
372 }
373
374 public String getServletPath() {
375 return _servletPath;
376 }
377
378 public HttpSession getSession() {
379 return new PortletServletSession(
380 _request.getSession(), _portletRequestImpl);
381 }
382
383 public HttpSession getSession(boolean create) {
384 return new PortletServletSession(
385 _request.getSession(create), _portletRequestImpl);
386 }
387
388 public Principal getUserPrincipal() {
389 return _userPrincipal;
390 }
391
392 public boolean isRequestedSessionIdFromCookie() {
393 return _request.isRequestedSessionIdFromCookie();
394 }
395
396 public boolean isRequestedSessionIdFromURL() {
397 return _request.isRequestedSessionIdFromURL();
398 }
399
400
403 public boolean isRequestedSessionIdFromUrl() {
404 return _request.isRequestedSessionIdFromUrl();
405 }
406
407 public boolean isRequestedSessionIdValid() {
408 return _request.isRequestedSessionIdValid();
409 }
410
411 public boolean isSecure() {
412 return _request.isSecure();
413 }
414
415 public boolean isUserInRole(String role) {
416 return _portletRequestImpl.isUserInRole(role);
417 }
418
419 public void removeAttribute(String name) {
420 _request.removeAttribute(name);
421 }
422
423 public void setAttribute(String name, Object obj) {
424 _request.setAttribute(name, obj);
425 }
426
427 public void setCharacterEncoding(String encoding)
428 throws UnsupportedEncodingException {
429
430 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
431 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
432
433 _request.setCharacterEncoding(encoding);
434 }
435 }
436
437 private static Log _log =
438 LogFactoryUtil.getLog(PortletServletRequest.class);
439
440 private HttpServletRequest _request;
441 private PortletRequestImpl _portletRequestImpl;
442 private String _lifecycle;
443 private String _pathInfo;
444 private String _queryString;
445 private String _remoteUser;
446 private String _requestURI;
447 private String _servletPath;
448 private Principal _userPrincipal;
449 private boolean _named;
450 private boolean _include;
451
452 }