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