1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.engine.servlet;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.UnsupportedEncodingException;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Enumeration;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.Locale;
30 import java.util.Map;
31
32 import javax.portlet.PortletRequest;
33 import javax.servlet.ServletInputStream;
34 import javax.servlet.ServletRequest;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletRequestWrapper;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.jetspeed.Jetspeed;
41 import org.apache.jetspeed.PortalReservedParameters;
42 import org.apache.jetspeed.aggregator.CurrentWorkerContext;
43 import org.apache.jetspeed.container.PortletDispatcherIncludeAware;
44 import org.apache.jetspeed.container.namespace.JetspeedNamespaceMapper;
45 import org.apache.jetspeed.container.namespace.JetspeedNamespaceMapperFactory;
46 import org.apache.jetspeed.container.state.NavigationalState;
47 import org.apache.jetspeed.container.url.PortalURL;
48 import org.apache.jetspeed.om.common.GenericMetadata;
49 import org.apache.jetspeed.om.common.LocalizedField;
50 import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
51 import org.apache.jetspeed.request.JetspeedRequestContext;
52 import org.apache.jetspeed.request.RequestContext;
53 import org.apache.pluto.om.entity.PortletEntity;
54 import org.apache.pluto.om.portlet.PortletApplicationDefinition;
55 import org.apache.pluto.om.window.PortletWindow;
56 import org.apache.pluto.util.Enumerator;
57
58 /***
59 * This request wrappers the servlet request and is used within the container to
60 * communicate to the invoked servlet.
61 *
62 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
63 * @version $Id: ServletRequestImpl.java 587064 2007-10-22 11:54:11Z woonsan $
64 */
65 public class ServletRequestImpl extends HttpServletRequestWrapper implements PortletDispatcherIncludeAware
66 {
67 public static final String ACCEPT_LANGUAGE = "Accept-Language";
68 /*** Logger */
69 private static final Log log = LogFactory.getLog(ServletRequestImpl.class);
70
71 PortletWindow portletWindow = null;
72 private JetspeedNamespaceMapper nameSpaceMapper = null;
73 private ServletRequest currentRequest = null;
74
75 private Map portletParameters;
76
77 private boolean included;
78
79 private static Boolean mergePortalParametersWithPortletParameters;
80 private static Boolean mergePortalParametersBeforePortletParameters;
81
82 private boolean portletMergePortalParametersWithPortletParameters;
83 private boolean portletMergePortalParametersBeforePortletParameters;
84
85 private Map portalParameters;
86
87 private String currentIncludeQueryString;
88 private String currentForwardQueryString;
89
90
91
92 private Map cachedAttributes;
93
94 public ServletRequestImpl( HttpServletRequest servletRequest, PortletWindow window )
95 {
96 super(servletRequest);
97 nameSpaceMapper = ((JetspeedNamespaceMapperFactory) Jetspeed.getComponentManager().getComponent(
98 org.apache.pluto.util.NamespaceMapper.class)).getJetspeedNamespaceMapper();
99 this.portletWindow = window;
100
101
102 String encoding = (String) servletRequest.getAttribute(PortalReservedParameters.PREFERED_CHARACTERENCODING_ATTRIBUTE);
103 boolean decode = servletRequest.getAttribute(PortalReservedParameters.PARAMETER_ALREADY_DECODED_ATTRIBUTE) == null
104 && encoding != null;
105 if (decode)
106 {
107 servletRequest.setAttribute(PortalReservedParameters.PARAMETER_ALREADY_DECODED_ATTRIBUTE,
108 new Boolean(true));
109 }
110
111
112 portalParameters = new HashMap();
113 for (Enumeration parameters = servletRequest.getParameterNames(); parameters.hasMoreElements();)
114 {
115 String paramName = (String) parameters.nextElement();
116 String[] paramValues = servletRequest.getParameterValues(paramName);
117
118 if (decode)
119 {
120 for (int i = 0; i < paramValues.length; i++)
121 {
122 try
123 {
124 paramValues[i] = new String(paramValues[i].getBytes("ISO-8859-1"), encoding);
125 }
126 catch (UnsupportedEncodingException e)
127 {
128 ;
129 }
130 }
131 }
132 portalParameters.put(paramName, paramValues);
133 }
134
135 if (mergePortalParametersWithPortletParameters == null )
136 {
137 mergePortalParametersWithPortletParameters =
138 new Boolean(Jetspeed.getContext().getConfiguration().getBoolean("merge.portal.parameters.with.portlet.parameters", false));
139 }
140
141 if (mergePortalParametersBeforePortletParameters == null)
142 {
143 mergePortalParametersBeforePortletParameters =
144 new Boolean(Jetspeed.getContext().getConfiguration().getBoolean("merge.portal.parameters.before.portlet.parameters", false));
145 }
146
147
148 PortletDefinitionComposite portletDef = (PortletDefinitionComposite)portletWindow.getPortletEntity().getPortletDefinition();
149 if(portletDef != null)
150 {
151 GenericMetadata metaData = portletDef.getMetadata();
152
153 portletMergePortalParametersWithPortletParameters =
154 getMetaDataBooleanValue(
155 metaData,
156 PortalReservedParameters.PORTLET_EXTENDED_DESCRIPTOR_MERGE_PORTAL_PARAMETERS_WITH_PORTLET_PARAMETERS,
157 mergePortalParametersWithPortletParameters.booleanValue());
158 portletMergePortalParametersBeforePortletParameters =
159 getMetaDataBooleanValue(
160 metaData,
161 PortalReservedParameters.PORTLET_EXTENDED_DESCRIPTOR_MERGE_PORTAL_PARAMETERS_BEFORE_PORTLET_PARAMETERS,
162 mergePortalParametersBeforePortletParameters.booleanValue());
163
164 }
165 else
166 {
167
168 portletMergePortalParametersWithPortletParameters = mergePortalParametersWithPortletParameters.booleanValue();
169 portletMergePortalParametersBeforePortletParameters = mergePortalParametersBeforePortletParameters.booleanValue();
170 }
171 }
172
173 private boolean getMetaDataBooleanValue(GenericMetadata metaData, String fieldName, boolean defaultValue )
174 {
175 String value = null;
176 if ( metaData != null )
177 {
178 Collection fields = metaData.getFields(fieldName);
179 if ( fields != null && !fields.isEmpty() )
180 {
181 value = ((LocalizedField)fields.iterator().next()).getValue();
182 }
183 }
184 if ( value != null )
185 {
186 return Boolean.valueOf(value).booleanValue();
187 }
188 return defaultValue;
189 }
190
191 protected HttpServletRequest _getHttpServletRequest()
192 {
193 return (HttpServletRequest) super.getRequest();
194 }
195
196
197
198 public String getParameter( String name )
199 {
200 Object value = this.getParameterMap().get(name);
201 if (value == null)
202 {
203 return (null);
204 }
205 else if (value instanceof String[])
206 {
207 return (((String[]) value)[0]);
208 }
209 else if (value instanceof String)
210 {
211 return ((String) value);
212 }
213 else
214 {
215 return (value.toString());
216 }
217 }
218
219 private boolean isEqual(String one, String two)
220 {
221 return (one == null && two == null) || (one != null && two != null && one.equals(two));
222 }
223
224 private boolean checkQueryStringChanged()
225 {
226 boolean changed = false;
227 ServletRequest request = getRequest();
228 String includeQueryString = (String)request.getAttribute("javax.servlet.include.query_string");
229 String forwardQueryString = (String)request.getAttribute("javax.servlet.forward.query_string");
230
231 if (!isEqual(currentIncludeQueryString,includeQueryString))
232 {
233 currentIncludeQueryString = includeQueryString;
234 changed = true;
235 }
236 if (!isEqual(currentForwardQueryString,forwardQueryString))
237 {
238 currentForwardQueryString = forwardQueryString;
239 changed = true;
240 }
241 return changed;
242 }
243
244 public Map getParameterMap()
245 {
246
247
248
249
250 boolean queryStringChanged = checkQueryStringChanged();
251
252 if (queryStringChanged || currentRequest == null || currentRequest != getRequest() )
253 {
254
255
256
257
258
259
260
261 currentRequest = getRequest();
262
263 boolean postAllowed = false;
264
265
266
267
268 HashMap queryParameters = new HashMap();
269 for ( Iterator iter = getRequest().getParameterMap().entrySet().iterator(); iter.hasNext(); )
270 {
271 Map.Entry entry = (Map.Entry)iter.next();
272 String[] values = (String[])entry.getValue();
273 String[] original = (String[])portalParameters.get(entry.getKey());
274 String[] diff = null;
275 if ( original == null )
276 {
277
278 diff = new String[values.length];
279 System.arraycopy(values,0,diff,0,values.length);
280 }
281 else if ( values.length > original.length )
282 {
283
284 diff = new String[values.length - original.length];
285 System.arraycopy(values,0,diff,0,values.length-original.length);
286 }
287 if ( diff != null )
288 {
289 queryParameters.put(entry.getKey(), diff);
290 }
291 }
292
293
294 HashMap navParameters = new HashMap();
295 JetspeedRequestContext context = (JetspeedRequestContext) getAttribute("org.apache.jetspeed.request.RequestContext");
296 if (context != null)
297 {
298 NavigationalState ns = context.getPortalURL().getNavigationalState();
299 postAllowed = ns.getPortletWindowOfAction() != null || ns.getPortletWindowOfResource() != null;
300 Iterator iter = ns.getParameterNames(portletWindow);
301 while (iter.hasNext())
302 {
303 String name = (String) iter.next();
304 String[] values = ns.getParameterValues(portletWindow, name);
305 navParameters.put(name, values);
306 }
307 }
308
309
310 HashSet keys = new HashSet();
311 keys.addAll(portalParameters.keySet());
312 keys.addAll(queryParameters.keySet());
313 keys.addAll(navParameters.keySet());
314
315
316
317
318
319
320
321
322
323
324
325 portletParameters = new HashMap();
326 for ( Iterator iter = keys.iterator(); iter.hasNext(); )
327 {
328 String key = (String)iter.next();
329 String[] first = (String[])queryParameters.get(key);
330 String[] next = null, last = null, result = null;
331
332 if ( portletMergePortalParametersWithPortletParameters == false && !postAllowed )
333 {
334 next = (String[])navParameters.get(key);
335 }
336 else if ( portletMergePortalParametersBeforePortletParameters )
337 {
338 next = (String[])portalParameters.get(key);
339 last = (String[])navParameters.get(key);
340 }
341 else
342 {
343 next = (String[])navParameters.get(key);
344 last = (String[])portalParameters.get(key);
345 }
346 if ( first == null )
347 {
348 if ( next == null )
349 {
350 first = last;
351 last = null;
352 }
353 else
354 {
355 first = next;
356 next = last;
357 last = null;
358 }
359 }
360 else if ( next == null )
361 {
362 next = last;
363 last = null;
364 }
365
366 if ( last == null )
367 {
368 if ( next == null && first != null )
369 {
370 result = new String[first.length];
371 System.arraycopy(first,0,result,0,first.length);
372 }
373 else if (next != null )
374 {
375 result = new String[first.length + next.length];
376 System.arraycopy(first,0,result,0,first.length);
377 System.arraycopy(next,0,result,first.length,next.length);
378 }
379 }
380 else
381 {
382 result = new String[first.length + next.length + last.length];
383 System.arraycopy(first,0,result,0,first.length);
384 System.arraycopy(next,0,result,first.length,next.length);
385 System.arraycopy(last,0,result,first.length+next.length,last.length);
386
387 }
388 if ( result != null )
389 {
390 portletParameters.put(key, result);
391 }
392 }
393 }
394 return Collections.unmodifiableMap(portletParameters);
395
396 }
397
398 public Enumeration getParameterNames()
399 {
400 return Collections.enumeration(this.getParameterMap().keySet());
401 }
402
403 public String[] getParameterValues( String name )
404 {
405 return (String[]) this.getParameterMap().get(name);
406 }
407
408 /***
409 * @see javax.servlet.http.HttpServletRequest#getAttributeNames()
410 */
411 public Enumeration getAttributeNames()
412 {
413 Enumeration attrNames = super.getAttributeNames();
414
415
416
417 if (CurrentWorkerContext.getParallelRenderingMode())
418 {
419
420
421 if (cachedAttributes == null)
422 {
423 HashMap adjustedAttrMap = new HashMap();
424
425
426
427 while (attrNames.hasMoreElements())
428 {
429 String key = (String) attrNames.nextElement();
430 adjustedAttrMap.put(key, super.getAttribute(key));
431 }
432
433
434
435 Enumeration cwAttrNames = CurrentWorkerContext.getAttributeNames();
436
437 while (cwAttrNames.hasMoreElements())
438 {
439 String key = (String) cwAttrNames.nextElement();
440 adjustedAttrMap.put(key, CurrentWorkerContext.getAttribute(key));
441 }
442
443 cachedAttributes = Collections.unmodifiableMap(adjustedAttrMap);
444 }
445
446 attrNames = Collections.enumeration(cachedAttributes.keySet());
447 }
448
449 return attrNames;
450 }
451
452 /***
453 * @see javax.servlet.http.HttpServletRequest#getAttribute(java.lang.String)
454 */
455 public Object getAttribute( String name )
456 {
457 Object value = null;
458
459
460
461 if (CurrentWorkerContext.getParallelRenderingMode())
462 {
463 value = CurrentWorkerContext.getAttribute(name);
464
465
466
467 if (null == value)
468 {
469
470
471
472 value = CurrentWorkerContext.getAttribute(nameSpaceMapper.encode(portletWindow.getId(), name));
473 }
474 }
475
476
477 if (null == value)
478 {
479 value = getAttributeInternal(name);
480 }
481
482 return value;
483 }
484
485 private Object getAttributeInternal( String name )
486 {
487 Object value = super.getAttribute(name);
488 if (name.equals(PortletRequest.USER_INFO))
489 {
490 JetspeedRequestContext context = (JetspeedRequestContext) getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
491 if (null != context)
492 {
493 String entityID = "--NULL--";
494 PortletEntity entity = portletWindow.getPortletEntity();
495 if (entity != null)
496 {
497 entityID = entity.getId().toString();
498 }
499 PortletApplicationDefinition portletAppDef = entity.getPortletDefinition()
500 .getPortletApplicationDefinition();
501
502 if (null != portletAppDef)
503 {
504 value = context.getUserInfoMap(portletAppDef.getId());
505 if (log.isDebugEnabled() && (null != value))
506 log.debug(PortletRequest.USER_INFO + " map size: " + ((Map) value).size());
507 }
508 else
509 {
510 log.error("Entity is null:" + entityID);
511 }
512
513 }
514 }
515 else
516 {
517 if (null == value)
518 {
519 PortletRequest pr = (PortletRequest) super.getAttribute("javax.portlet.request");
520 if (pr != null)
521 {
522 value = super.getAttribute(nameSpaceMapper.encode(portletWindow.getId(),
523 name));
524 }
525 }
526 }
527 return value;
528 }
529
530 /***
531 * @see javax.servlet.ServletRequest#getLocale()
532 */
533 public Locale getLocale()
534 {
535
536 RequestContext requestContext = (RequestContext) _getHttpServletRequest().getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
537 Locale preferedLocale = requestContext.getLocale();
538 if (preferedLocale != null)
539 {
540 return preferedLocale;
541 }
542
543 return super.getLocale();
544 }
545
546 /***
547 * @see javax.servlet.ServletRequest#getLocales()
548 */
549 public Enumeration getLocales()
550 {
551 RequestContext requestContext = (RequestContext) _getHttpServletRequest().getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
552 Locale preferedLocale = requestContext.getLocale();
553 if (preferedLocale != null)
554 {
555 return getLocaleEnum(preferedLocale);
556 }
557
558 return super.getLocales();
559 }
560
561 /***
562 * <p>
563 * getLocaleEnum
564 * </p>
565 *
566 * @param preferedLocale
567 * @return
568 */
569 protected Enumeration getLocaleEnum( Locale preferedLocale )
570 {
571 ArrayList locales = new ArrayList();
572 locales.add(preferedLocale);
573 Enumeration localeEnums = super.getLocales();
574 while (localeEnums.hasMoreElements())
575 {
576 locales.add(localeEnums.nextElement());
577 }
578 return new Enumerator(locales);
579 }
580
581 /***
582 * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
583 */
584 public String getHeader( String name )
585 {
586 if(name.equals(ACCEPT_LANGUAGE))
587 {
588 return getLocale().getLanguage();
589 }
590 else
591 {
592 return super.getHeader(name);
593 }
594 }
595
596 /***
597 * @see javax.servlet.http.HttpServletRequest#getHeaders(java.lang.String)
598 */
599 public Enumeration getHeaders( String name )
600 {
601 if(name.equals(ACCEPT_LANGUAGE))
602 {
603 return getLocaleEnum(getLocale());
604 }
605 else
606 {
607 return super.getHeaders(name);
608 }
609
610 }
611
612 /***
613 * <p>
614 * setAttribute
615 * </p>
616 *
617 * @see javax.servlet.ServletRequest#setAttribute(java.lang.String,
618 * java.lang.Object)
619 * @param arg0
620 * @param arg1
621 */
622 public void setAttribute( String name, Object value )
623 {
624 if (name == null)
625 {
626 throw new IllegalArgumentException("Attribute name == null");
627 }
628
629
630
631 if (CurrentWorkerContext.getParallelRenderingMode())
632 {
633
634 cachedAttributes = null;
635
636 if (null == value)
637 {
638 CurrentWorkerContext.removeAttribute(name);
639 }
640 else
641 {
642 CurrentWorkerContext.setAttribute(name, value);
643 }
644
645 if (name.startsWith("org.apache.jetspeed"))
646 {
647 setAttributeInternal(name, value);
648 }
649 }
650 else
651 {
652
653 setAttributeInternal(name, value);
654 }
655 }
656
657 private void setAttributeInternal( String name, Object value )
658 {
659
660
661 if (name.startsWith("org.apache.jetspeed"))
662 {
663 if (value == null)
664 {
665 this.removeAttribute(name);
666 }
667 else
668 {
669 String encodedKey = nameSpaceMapper.encode(portletWindow.getId(), name);
670 this._getHttpServletRequest().setAttribute(
671 encodedKey, value);
672 }
673 }
674 super.setAttribute(name, value);
675 }
676
677 /***
678 * <p>
679 * removeAttribute
680 * </p>
681 *
682 * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
683 * @param arg0
684 */
685 public void removeAttribute( String name )
686 {
687 if (name == null)
688 {
689 throw new IllegalArgumentException("Attribute name == null");
690 }
691
692
693
694 if (CurrentWorkerContext.getParallelRenderingMode())
695 {
696
697 cachedAttributes = null;
698
699 CurrentWorkerContext.removeAttribute(name);
700
701 if (name.startsWith("org.apache.jetspeed")) {
702 super.removeAttribute(name);
703 }
704 }
705 else
706 {
707
708 super.removeAttribute(name);
709 }
710 }
711
712 /***
713 * <p>
714 * getHeaderNames
715 * </p>
716 *
717 * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
718 * @return
719 */
720 public Enumeration getHeaderNames()
721 {
722 return super.getHeaderNames();
723 }
724
725 /***
726 * @param included when true, JSR-168 PLT.16.3.3 rules need to be enforced
727 */
728 public void setPortletDispatcherIncluded(boolean included)
729 {
730 this.included = included;
731 }
732
733
734
735
736 public String getProtocol()
737 {
738 return (included ? null : super.getProtocol() );
739 }
740
741
742
743
744 public String getRemoteAddr()
745 {
746 return (included ? null : super.getRemoteAddr() );
747 }
748
749
750
751
752 public String getRemoteHost()
753 {
754 return (included ? null : super.getRemoteHost() );
755 }
756
757
758
759
760 public StringBuffer getRequestURL()
761 {
762 return (included ? null : super.getRequestURL());
763 }
764
765
766
767
768 public String getPathInfo()
769 {
770 return (included ? (String)super.getAttribute("javax.servlet.include.path_info") : super.getPathInfo());
771 }
772
773
774
775
776 public String getPathTranslated()
777 {
778 return (included ? null : super.getPathTranslated());
779 }
780
781
782
783
784 public String getQueryString()
785 {
786 return (included ? (String)super.getAttribute("javax.servlet.include.query_string") : super.getQueryString());
787 }
788
789
790
791
792 public String getRequestURI()
793 {
794 return (included ? (String)super.getAttribute("javax.servlet.include.request_uri") : super.getRequestURI());
795 }
796
797
798
799
800 public String getServletPath()
801 {
802 return (included ? (String)super.getAttribute("javax.servlet.include.servlet_path") : super.getServletPath());
803 }
804
805
806
807
808 public String getContextPath()
809 {
810 return (included ? (String)super.getAttribute("javax.servlet.include.context_path") : super.getContextPath());
811 }
812
813
814
815
816 public int getContentLength()
817 {
818 return (included ? 0 : super.getContentLength());
819 }
820
821
822
823
824 public String getRealPath(String arg0)
825 {
826 return (included ? null : super.getRealPath(arg0));
827 }
828
829
830
831
832 public BufferedReader getReader() throws IOException
833 {
834 return (included ? null : super.getReader());
835 }
836
837
838
839
840 public String getCharacterEncoding()
841 {
842 return (included ? null : super.getCharacterEncoding());
843 }
844
845
846
847
848 public String getContentType()
849 {
850 return (included ? null : super.getContentType());
851 }
852
853
854
855
856 public ServletInputStream getInputStream() throws IOException
857 {
858 return (included ? null : super.getInputStream());
859 }
860
861
862
863
864 public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException
865 {
866 if ( !included )
867 {
868 super.setCharacterEncoding(arg0);
869 }
870 }
871 }