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