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