001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.servlet.HttpMethods;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.JavaConstants;
022 import com.liferay.portal.kernel.util.ServerDetector;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.util.ClassLoaderUtil;
025 import com.liferay.util.servlet.GenericServletInputStream;
026
027 import java.io.BufferedReader;
028 import java.io.IOException;
029 import java.io.InputStream;
030 import java.io.UnsupportedEncodingException;
031
032 import java.lang.reflect.Constructor;
033
034 import java.security.Principal;
035
036 import java.util.Enumeration;
037 import java.util.Locale;
038 import java.util.Map;
039
040 import javax.portlet.ClientDataRequest;
041 import javax.portlet.EventRequest;
042 import javax.portlet.PortletRequest;
043
044 import javax.servlet.RequestDispatcher;
045 import javax.servlet.ServletInputStream;
046 import javax.servlet.http.Cookie;
047 import javax.servlet.http.HttpServletRequest;
048 import javax.servlet.http.HttpServletRequestWrapper;
049 import javax.servlet.http.HttpSession;
050
051
055 public class PortletServletRequest extends HttpServletRequestWrapper {
056
057 public PortletServletRequest(
058 HttpServletRequest request, PortletRequest portletRequest,
059 String pathInfo, String queryString, String requestURI,
060 String servletPath, boolean named, boolean include) {
061
062 super(request);
063
064 _request = request;
065 _portletRequest = portletRequest;
066 _portletRequestImpl = PortletRequestImpl.getPortletRequestImpl(
067 _portletRequest);
068 _pathInfo = pathInfo;
069 _queryString = queryString;
070 _requestURI = GetterUtil.getString(requestURI);
071 _servletPath = GetterUtil.getString(servletPath);
072 _named = named;
073 _include = include;
074
075 _lifecycle = _portletRequestImpl.getLifecycle();
076
077 if (Validator.isNotNull(_queryString)) {
078 _portletRequestImpl.setPortletRequestDispatcherRequest(request);
079 }
080 }
081
082 @Override
083 public Object getAttribute(String name) {
084 if (_include || (name == null)) {
085 return _request.getAttribute(name);
086 }
087
088 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_CONTEXT_PATH)) {
089 if (_named) {
090 return null;
091 }
092 else {
093 return _portletRequest.getContextPath();
094 }
095 }
096
097 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_PATH_INFO)) {
098 if (_named) {
099 return null;
100 }
101 else {
102 return _pathInfo;
103 }
104 }
105
106 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_QUERY_STRING)) {
107 if (_named) {
108 return null;
109 }
110 else {
111 return _queryString;
112 }
113 }
114
115 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_REQUEST_URI)) {
116 if (_named) {
117 return null;
118 }
119 else {
120 return _requestURI;
121 }
122 }
123
124 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_SERVLET_PATH)) {
125 if (_named) {
126 return null;
127 }
128 else {
129 return _servletPath;
130 }
131 }
132
133 return _request.getAttribute(name);
134 }
135
136 @Override
137 public Enumeration<String> getAttributeNames() {
138 return _request.getAttributeNames();
139 }
140
141 @Override
142 public String getAuthType() {
143 return _request.getAuthType();
144 }
145
146 @Override
147 public String getCharacterEncoding() {
148 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
149 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
150
151 return _request.getCharacterEncoding();
152 }
153 else {
154 return null;
155 }
156 }
157
158 @Override
159 public int getContentLength() {
160 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
161 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
162
163 return _request.getContentLength();
164 }
165 else {
166 return 0;
167 }
168 }
169
170 @Override
171 public String getContentType() {
172 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
173 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
174
175 return _request.getContentType();
176 }
177 else {
178 return null;
179 }
180 }
181
182 @Override
183 public String getContextPath() {
184 return _portletRequest.getContextPath();
185 }
186
187 @Override
188 public Cookie[] getCookies() {
189 return _request.getCookies();
190 }
191
192 @Override
193 public long getDateHeader(String name) {
194 return GetterUtil.getLong(getHeader(name), -1);
195 }
196
197 @Override
198 public String getHeader(String name) {
199 return _request.getHeader(name);
200 }
201
202 @Override
203 public Enumeration<String> getHeaderNames() {
204 return _request.getHeaderNames();
205 }
206
207 @Override
208 public Enumeration<String> getHeaders(String name) {
209 return _request.getHeaders(name);
210 }
211
212 @Override
213 public ServletInputStream getInputStream() throws IOException {
214 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
215 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
216
217 ClientDataRequest clientDataRequest = _getClientDataRequest();
218
219 InputStream portletInputStream =
220 clientDataRequest.getPortletInputStream();
221
222 ServletInputStream servletInputStream =
223 new GenericServletInputStream(portletInputStream);
224
225 return servletInputStream;
226 }
227 else {
228 return null;
229 }
230 }
231
232 @Override
233 public int getIntHeader(String name) {
234 return GetterUtil.getInteger(getHeader(name));
235 }
236
237 @Override
238 public String getLocalAddr() {
239 return null;
240 }
241
242 @Override
243 public Locale getLocale() {
244 return _portletRequest.getLocale();
245 }
246
247 @Override
248 public Enumeration<Locale> getLocales() {
249 return _portletRequest.getLocales();
250 }
251
252 @Override
253 public String getLocalName() {
254 return null;
255 }
256
257 @Override
258 public int getLocalPort() {
259 return 0;
260 }
261
262 @Override
263 public String getMethod() {
264 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
265 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
266
267 ClientDataRequest clientDataRequest = _getClientDataRequest();
268
269 return clientDataRequest.getMethod();
270 }
271 else if (_lifecycle.equals(PortletRequest.RENDER_PHASE)) {
272 return HttpMethods.GET;
273 }
274 else {
275 EventRequest eventRequest = _getEventRequest();
276
277 return eventRequest.getMethod();
278 }
279 }
280
281 @Override
282 public String getParameter(String name) {
283 return _portletRequest.getParameter(name);
284 }
285
286 @Override
287 public Map<String, String[]> getParameterMap() {
288 return _portletRequest.getParameterMap();
289 }
290
291 @Override
292 public Enumeration<String> getParameterNames() {
293 return _portletRequest.getParameterNames();
294 }
295
296 @Override
297 public String[] getParameterValues(String name) {
298 return _portletRequest.getParameterValues(name);
299 }
300
301 @Override
302 public String getPathInfo() {
303 return _pathInfo;
304 }
305
306 @Override
307 public String getPathTranslated() {
308 return _request.getPathTranslated();
309 }
310
311 @Override
312 public String getProtocol() {
313 return "HTTP/1.1";
314 }
315
316 @Override
317 public String getQueryString() {
318 return _queryString;
319 }
320
321 @Override
322 public BufferedReader getReader() throws IOException {
323 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
324 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
325
326 ClientDataRequest clientDataRequest = _getClientDataRequest();
327
328 return clientDataRequest.getReader();
329 }
330 else {
331 return null;
332 }
333 }
334
335 @Override
336 public String getRealPath(String path) {
337 return null;
338 }
339
340 @Override
341 public String getRemoteAddr() {
342 return null;
343 }
344
345 @Override
346 public String getRemoteHost() {
347 return null;
348 }
349
350 @Override
351 public int getRemotePort() {
352 return 0;
353 }
354
355 @Override
356 public String getRemoteUser() {
357 return _portletRequest.getRemoteUser();
358 }
359
360 @Override
361 public RequestDispatcher getRequestDispatcher(String path) {
362 return _request.getRequestDispatcher(path);
363 }
364
365 @Override
366 public String getRequestedSessionId() {
367 return _portletRequest.getRequestedSessionId();
368 }
369
370 @Override
371 public String getRequestURI() {
372 return _requestURI;
373 }
374
375 @Override
376 public StringBuffer getRequestURL() {
377 return null;
378 }
379
380 @Override
381 public String getScheme() {
382 return _portletRequest.getScheme();
383 }
384
385 @Override
386 public String getServerName() {
387 return _portletRequest.getServerName();
388 }
389
390 @Override
391 public int getServerPort() {
392 return _portletRequest.getServerPort();
393 }
394
395 @Override
396 public String getServletPath() {
397 return _servletPath;
398 }
399
400 @Override
401 public HttpSession getSession() {
402 HttpSession session = new PortletServletSession(
403 _request.getSession(), _portletRequestImpl);
404
405 if (ServerDetector.isJetty()) {
406 try {
407 session = wrapJettySession(session);
408 }
409 catch (Exception e) {
410 _log.error(e, e);
411 }
412 }
413
414 return session;
415 }
416
417 @Override
418 public HttpSession getSession(boolean create) {
419 HttpSession session = new PortletServletSession(
420 _request.getSession(create), _portletRequestImpl);
421
422 if (ServerDetector.isJetty()) {
423 try {
424 session = wrapJettySession(session);
425 }
426 catch (Exception e) {
427 _log.error(e, e);
428 }
429 }
430
431 return session;
432 }
433
434 @Override
435 public Principal getUserPrincipal() {
436 return _portletRequest.getUserPrincipal();
437 }
438
439 @Override
440 public boolean isRequestedSessionIdFromCookie() {
441 return _request.isRequestedSessionIdFromCookie();
442 }
443
444
447 @Override
448 public boolean isRequestedSessionIdFromUrl() {
449 return _request.isRequestedSessionIdFromUrl();
450 }
451
452 @Override
453 public boolean isRequestedSessionIdFromURL() {
454 return _request.isRequestedSessionIdFromURL();
455 }
456
457 @Override
458 public boolean isRequestedSessionIdValid() {
459 return _portletRequest.isRequestedSessionIdValid();
460 }
461
462 @Override
463 public boolean isSecure() {
464 return _portletRequest.isSecure();
465 }
466
467 @Override
468 public boolean isUserInRole(String role) {
469 return _portletRequest.isUserInRole(role);
470 }
471
472 @Override
473 public void removeAttribute(String name) {
474 _portletRequest.removeAttribute(name);
475 }
476
477 @Override
478 public void setAttribute(String name, Object obj) {
479 _portletRequest.setAttribute(name, obj);
480 }
481
482 @Override
483 public void setCharacterEncoding(String encoding)
484 throws UnsupportedEncodingException {
485
486 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
487 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
488
489 ClientDataRequest clientDataRequest = _getClientDataRequest();
490
491 clientDataRequest.setCharacterEncoding(encoding);
492 }
493 }
494
495 protected HttpSession wrapJettySession(HttpSession session)
496 throws Exception {
497
498
499
500
501 ClassLoader classLoader = ClassLoaderUtil.getPortalClassLoader();
502
503 Class<?> jettyHttpSessionWrapperClass = classLoader.loadClass(
504 "com.liferay.portal.servlet.JettyHttpSessionWrapper");
505
506 Constructor<?> constructor =
507 jettyHttpSessionWrapperClass.getConstructor(
508 new Class[] {HttpSession.class});
509
510 return(HttpSession)constructor.newInstance(new Object[] {session});
511 }
512
513 private ClientDataRequest _getClientDataRequest() {
514 return (ClientDataRequest)_portletRequest;
515 }
516
517 private EventRequest _getEventRequest() {
518 return (EventRequest)_portletRequest;
519 }
520
521 private static Log _log = LogFactoryUtil.getLog(
522 PortletServletRequest.class);
523
524 private boolean _include;
525 private String _lifecycle;
526 private boolean _named;
527 private String _pathInfo;
528 private PortletRequest _portletRequest;
529 private PortletRequestImpl _portletRequestImpl;
530 private String _queryString;
531 private HttpServletRequest _request;
532 private String _requestURI;
533 private String _servletPath;
534
535 }