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