1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  /**
24   * The contents of this file are subject to the terms of the Common Development
25   * and Distribution License (the License). You may not use this file except in
26   * compliance with the License.
27   *
28   * You can obtain a copy of the License at http://www.sun.com/cddl/cddl.html and
29   * legal/CDDLv1.0.txt. See the License for the specific language governing
30   * permission and limitations under the License.
31   *
32   * When distributing Covered Code, include this CDDL Header Notice in each file
33   * and include the License file at legal/CDDLv1.0.txt.
34   *
35   * If applicable, add the following below the CDDL Header, with the fields
36   * enclosed by brackets [] replaced by your own identifying information:
37   * "Portions Copyrighted [year] [name of copyright owner]"
38   *
39   * Copyright 2009 Sun Microsystems Inc. All rights reserved.
40   */
41  
42  package com.liferay.portal.portletcontainer;
43  
44  import com.liferay.portal.ccpp.PortalProfileFactory;
45  import com.liferay.portal.kernel.language.LanguageUtil;
46  import com.liferay.portal.kernel.log.Log;
47  import com.liferay.portal.kernel.log.LogFactoryUtil;
48  import com.liferay.portal.kernel.portlet.LiferayPortletSession;
49  import com.liferay.portal.kernel.portlet.LiferayWindowState;
50  import com.liferay.portal.kernel.servlet.PortletSessionTracker;
51  import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
52  import com.liferay.portal.kernel.util.GetterUtil;
53  import com.liferay.portal.kernel.util.JavaConstants;
54  import com.liferay.portal.kernel.util.ListUtil;
55  import com.liferay.portal.kernel.util.LocaleUtil;
56  import com.liferay.portal.kernel.util.StringPool;
57  import com.liferay.portal.model.Layout;
58  import com.liferay.portal.model.PortletConstants;
59  import com.liferay.portal.model.Role;
60  import com.liferay.portal.model.User;
61  import com.liferay.portal.service.RoleLocalServiceUtil;
62  import com.liferay.portal.theme.ThemeDisplay;
63  import com.liferay.portal.util.CookieKeys;
64  import com.liferay.portal.util.PortalUtil;
65  import com.liferay.portal.util.PropsValues;
66  import com.liferay.portal.util.WebKeys;
67  import com.liferay.portal.wsrp.ProfileMapManager;
68  import com.liferay.portal.wsrp.WSRPFactoryUtil;
69  import com.liferay.portlet.ActionRequestImpl;
70  import com.liferay.portlet.ActionResponseImpl;
71  import com.liferay.portlet.InvokerPortletImpl;
72  import com.liferay.portlet.RenderRequestImpl;
73  import com.liferay.portlet.RenderResponseImpl;
74  import com.liferay.portlet.ResourceRequestImpl;
75  import com.liferay.portlet.ResourceResponseImpl;
76  import com.liferay.portlet.UserInfoFactory;
77  
78  import com.sun.portal.container.ChannelMode;
79  import com.sun.portal.container.ChannelState;
80  import com.sun.portal.container.ChannelURLType;
81  import com.sun.portal.container.Container;
82  import com.sun.portal.container.ContainerFactory;
83  import com.sun.portal.container.ContainerRequest;
84  import com.sun.portal.container.ContainerResponse;
85  import com.sun.portal.container.ContainerType;
86  import com.sun.portal.container.ContainerUtil;
87  import com.sun.portal.container.EntityID;
88  import com.sun.portal.container.ExecuteActionRequest;
89  import com.sun.portal.container.ExecuteActionResponse;
90  import com.sun.portal.container.GetMarkupRequest;
91  import com.sun.portal.container.GetMarkupResponse;
92  import com.sun.portal.container.GetResourceRequest;
93  import com.sun.portal.container.GetResourceResponse;
94  import com.sun.portal.container.PortletID;
95  import com.sun.portal.container.PortletType;
96  import com.sun.portal.container.PortletWindowContext;
97  import com.sun.portal.container.PortletWindowContextException;
98  import com.sun.portal.container.WindowRequestReader;
99  import com.sun.portal.portletcontainer.appengine.PortletAppEngineUtils;
100 import com.sun.portal.portletcontainer.portlet.impl.PortletRequestConstants;
101 
102 import java.io.IOException;
103 import java.io.PrintWriter;
104 import java.io.StringWriter;
105 
106 import java.net.URL;
107 
108 import java.security.Principal;
109 
110 import java.util.ArrayList;
111 import java.util.Collections;
112 import java.util.HashMap;
113 import java.util.List;
114 import java.util.Locale;
115 import java.util.Map;
116 import java.util.Set;
117 
118 import javax.ccpp.Profile;
119 
120 import javax.portlet.ActionRequest;
121 import javax.portlet.ActionResponse;
122 import javax.portlet.EventRequest;
123 import javax.portlet.EventResponse;
124 import javax.portlet.MimeResponse;
125 import javax.portlet.Portlet;
126 import javax.portlet.PortletConfig;
127 import javax.portlet.PortletContext;
128 import javax.portlet.PortletException;
129 import javax.portlet.PortletMode;
130 import javax.portlet.PortletRequest;
131 import javax.portlet.PortletResponse;
132 import javax.portlet.RenderRequest;
133 import javax.portlet.RenderResponse;
134 import javax.portlet.ResourceRequest;
135 import javax.portlet.ResourceResponse;
136 import javax.portlet.WindowState;
137 
138 import javax.servlet.http.Cookie;
139 import javax.servlet.http.HttpServletRequest;
140 import javax.servlet.http.HttpServletResponse;
141 import javax.servlet.http.HttpSession;
142 
143 import javax.xml.transform.OutputKeys;
144 import javax.xml.transform.Transformer;
145 import javax.xml.transform.TransformerException;
146 import javax.xml.transform.TransformerFactory;
147 import javax.xml.transform.dom.DOMSource;
148 import javax.xml.transform.stream.StreamResult;
149 
150 import org.w3c.dom.Element;
151 
152 /**
153  * <a href="WindowInvoker.java.html"><b><i>View Source</i></b></a>
154  *
155  * @author Deepak Gothe
156  * @author Brian Wing Shun Chan
157  * @author Manish Gupta
158  *
159  */
160 public class WindowInvoker extends InvokerPortletImpl {
161 
162     public static final List<String> REQUEST_SHARED_ATTRIBUTES =
163         ListUtil.toList(PropsValues.REQUEST_SHARED_ATTRIBUTES);
164 
165     public void init(PortletConfig portletConfig) throws PortletException {
166         if (_remotePortlet) {
167             _portletConfig = portletConfig;
168         }
169         else {
170             super.init(portletConfig);
171         }
172     }
173 
174     public void prepare(
175             com.liferay.portal.model.Portlet portletModel, Portlet portlet,
176             PortletContext portletContext)
177         throws PortletException {
178 
179         super.prepare(portletModel, portlet, portletContext);
180 
181         _portletModel = portletModel;
182         _remotePortlet = portletModel.isRemote();
183         _container = _getContainer();
184     }
185 
186     public void prepare(
187             com.liferay.portal.model.Portlet portletModel, Portlet portlet,
188             PortletConfig portletConfig, PortletContext portletContext,
189             boolean facesPortlet, boolean strutsPortlet,
190             boolean strutsBridgePortlet)
191         throws PortletException {
192 
193         super.prepare(
194             portletModel, portlet, portletConfig, portletContext, facesPortlet,
195             strutsPortlet, strutsBridgePortlet);
196 
197         _portletModel = portletModel;
198         _remotePortlet = portletModel.isRemote();
199         _container = _getContainer();
200     }
201 
202     protected void invokeAction(
203             ActionRequest actionRequest, ActionResponse actionResponse)
204         throws IOException, PortletException {
205 
206         if (!_isWARFile()) {
207             super.invokeAction(actionRequest, actionResponse);
208 
209             return;
210         }
211 
212         try {
213             ActionRequestImpl actionRequestImpl =
214                 (ActionRequestImpl)actionRequest;
215             ActionResponseImpl actionResponseImpl =
216                 (ActionResponseImpl)actionResponse;
217 
218             HttpServletRequest request =
219                 actionRequestImpl.getOriginalHttpServletRequest();
220             HttpServletResponse response =
221                 actionResponseImpl.getHttpServletResponse();
222 
223             _initUser(request, _portletModel);
224 
225             WindowState currentWindowState = actionRequestImpl.getWindowState();
226             PortletMode currentPortletMode = actionRequestImpl.getPortletMode();
227 
228             if (_remotePortlet) {
229                 WindowRequestReader requestReader =
230                     WSRPFactoryUtil.getWindowRequestReader();
231 
232                 ChannelState newChannelState = requestReader.readNewWindowState(
233                     request);
234 
235                 if (newChannelState != null) {
236                     currentWindowState = PortletAppEngineUtils.getWindowState(
237                         newChannelState);
238                 }
239 
240                 ChannelMode newChannelMode =
241                     requestReader.readNewPortletWindowMode(request);
242 
243                 if (newChannelMode != null) {
244                     currentPortletMode = PortletAppEngineUtils.getPortletMode(
245                         newChannelMode);
246                 }
247             }
248 
249             long plid = _getPlid(actionRequest);
250 
251             ExecuteActionRequest executeActionRequest =
252                 ContainerRequestFactory.createExecuteActionRequest(
253                     request, _portletModel, currentWindowState,
254                     currentPortletMode, plid, isFacesPortlet(), _remotePortlet);
255 
256             _populateContainerRequest(
257                 request, response, executeActionRequest, actionRequest);
258 
259             if (!_portletModel.getPublishingEvents().isEmpty()) {
260                 executeActionRequest.setPortletNamespaces(
261                     _getPortletNamespaces(executeActionRequest));
262                 executeActionRequest.setPortletWindowIDs(
263                     _getPortletWindowIDs(executeActionRequest, plid));
264             }
265 
266             ExecuteActionResponse executeActionResponse =
267                 ContainerResponseFactory.createExecuteActionResponse(response);
268 
269             ChannelURLType urlType =
270                 executeActionRequest.getWindowRequestReader().readURLType(
271                     request);
272 
273             _container.executeAction(
274                 executeActionRequest, executeActionResponse, urlType);
275 
276             URL redirectURL = executeActionResponse.getRedirectURL();
277 
278             if (redirectURL != null) {
279                 actionResponseImpl.setRedirectLocation(redirectURL.toString());
280             }
281             else {
282                 ChannelState newWindowState =
283                     executeActionResponse.getNewWindowState();
284 
285                 if (newWindowState != null) {
286                     actionResponseImpl.setWindowState(
287                         PortletAppEngineUtils.getWindowState(newWindowState));
288                 }
289                 else {
290                     actionResponseImpl.setWindowState(currentWindowState);
291                 }
292 
293                 ChannelMode newPortletMode =
294                     executeActionResponse.getNewChannelMode();
295 
296                 if (newPortletMode != null) {
297                     actionResponseImpl.setPortletMode(
298                         PortletAppEngineUtils.getPortletMode(newPortletMode));
299                 }
300                 else {
301                     actionResponseImpl.setPortletMode(currentPortletMode);
302                 }
303 
304                 List<EntityID> eventUpdatedPortlets =
305                     executeActionResponse.getEventUpdatedPortlets();
306 
307                 if (eventUpdatedPortlets != null) {
308                     _updatePortletModeAndState(request, eventUpdatedPortlets);
309                     _clearEventPortletsResponse(request, eventUpdatedPortlets);
310                 }
311 
312                 _transferHeaders(request, response, executeActionResponse);
313 
314                 _setHttpSession(executeActionRequest, actionRequest);
315             }
316         }
317         catch (Exception e) {
318             throw new PortletException(e);
319         }
320         finally {
321             _setPortletAttributes(actionRequest, actionResponse);
322         }
323     }
324 
325     protected void invokeEvent(
326             EventRequest eventRequest, EventResponse eventResponse)
327         throws IOException, PortletException {
328 
329         if (!_isWARFile()) {
330             super.invokeEvent(eventRequest, eventResponse);
331         }
332     }
333 
334     protected String invokeRender(
335             RenderRequest renderRequest, RenderResponse renderResponse)
336         throws IOException, PortletException {
337 
338         if (!_isWARFile()) {
339             return super.invokeRender(renderRequest, renderResponse);
340         }
341 
342         try {
343             RenderRequestImpl renderRequestImpl =
344                 (RenderRequestImpl)renderRequest;
345             RenderResponseImpl renderResponseImpl =
346                 (RenderResponseImpl)renderResponse;
347 
348             HttpServletRequest request =
349                 renderRequestImpl.getOriginalHttpServletRequest();
350             HttpServletResponse response =
351                 renderResponseImpl.getHttpServletResponse();
352 
353             _initUser(request, _portletModel);
354 
355             long plid = _getPlid(renderRequest);
356 
357             GetMarkupRequest getMarkupRequest =
358                 ContainerRequestFactory.createGetMarkUpRequest(
359                     request, _portletModel, renderRequestImpl.getWindowState(),
360                     renderRequestImpl.getPortletMode(), plid, isFacesPortlet(),
361                     _remotePortlet);
362 
363             _populateContainerRequest(
364                 request, response, getMarkupRequest, renderRequest);
365 
366             if (!_portletModel.getPublishingEvents().isEmpty()) {
367                 getMarkupRequest.setPortletNamespaces(
368                     _getPortletNamespaces(getMarkupRequest));
369             }
370 
371             GetMarkupResponse getMarkupResponse =
372                 ContainerResponseFactory.createGetMarkUpResponse(response);
373 
374             List<String> allowableContentTypes =
375                 getMarkupRequest.getAllowableContentTypes();
376 
377             if (renderRequest.getWindowState().equals(
378                     LiferayWindowState.EXCLUSIVE)) {
379 
380                 allowableContentTypes.add("*/*");
381             }
382 
383             getMarkupRequest.setAllowableContentTypes(allowableContentTypes);
384 
385             _container.getMarkup(getMarkupRequest, getMarkupResponse);
386 
387             Map<String, List<String>> properties =
388                 getMarkupResponse.getStringProperties();
389 
390             if ((properties != null) &&
391                 (properties.containsKey("clear-request-parameters"))) {
392 
393                 getMarkupRequest.getRenderParameters().clear();
394             }
395 
396             StringBuffer sb = getMarkupResponse.getMarkup();
397 
398             PrintWriter pw = response.getWriter();
399 
400             pw.print(sb);
401 
402             String title = getMarkupResponse.getTitle();
403 
404             renderResponseImpl.setTitle(title);
405 
406             List<String> markupHeaders = _getMarkupHeaders(getMarkupResponse);
407 
408             if (markupHeaders != null) {
409                 List<String> allMarkupHeaders = (List<String>)
410                     request.getAttribute(MimeResponse.MARKUP_HEAD_ELEMENT);
411 
412                 if (allMarkupHeaders == null) {
413                     allMarkupHeaders = new ArrayList<String>();
414                 }
415                 allMarkupHeaders.addAll(markupHeaders);
416 
417                 request.setAttribute(
418                     MimeResponse.MARKUP_HEAD_ELEMENT, allMarkupHeaders);
419             }
420 
421             _setHttpSession(getMarkupRequest, renderRequest);
422 
423             return title;
424         }
425         catch (Exception e) {
426             throw new PortletException(e);
427         }
428         finally {
429             _setPortletAttributes(renderRequest, renderResponse);
430         }
431     }
432 
433     protected void invokeResource(
434             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
435         throws IOException, PortletException {
436 
437         if (!_isWARFile()) {
438             super.invokeResource(resourceRequest, resourceResponse);
439 
440             return;
441         }
442 
443         try {
444             ResourceRequestImpl resourceRequestImpl =
445                 (ResourceRequestImpl)resourceRequest;
446             ResourceResponseImpl resourceResponseImpl =
447                 (ResourceResponseImpl)resourceResponse;
448 
449             HttpServletRequest request =
450                 resourceRequestImpl.getOriginalHttpServletRequest();
451             HttpServletResponse response =
452                 resourceResponseImpl.getHttpServletResponse();
453 
454             _initUser(request, _portletModel);
455 
456             long plid = _getPlid(resourceRequest);
457 
458             GetResourceRequest getResourceRequest =
459                 ContainerRequestFactory.createGetResourceRequest(
460                     request, _portletModel,
461                     resourceRequestImpl.getWindowState(),
462                     resourceRequestImpl.getPortletMode(), plid,
463                     isFacesPortlet(), _remotePortlet);
464 
465             _populateContainerRequest(
466                 request, response, getResourceRequest, resourceRequest);
467 
468             GetResourceResponse getResourceResponse =
469                 ContainerResponseFactory .createGetResourceResponse(response);
470 
471             _container.getResources(
472                 getResourceRequest, getResourceResponse);
473 
474             String contentType = getResourceResponse.getContentType();
475 
476             if (contentType != null) {
477                 resourceResponseImpl.setContentType(contentType);
478             }
479 
480             _transferHeaders(request, response, getResourceResponse);
481 
482             _setHttpSession(getResourceRequest, resourceRequest);
483 
484             StringBuffer sb = getResourceResponse.getContentAsBuffer();
485 
486             byte[] bytes = getResourceResponse.getContentAsBytes();
487 
488             if (sb != null) {
489                 response.getWriter().print(sb);
490             }
491             else if ((bytes != null) && (bytes.length > 0)) {
492                 response.getOutputStream().write(bytes);
493             }
494             else {
495                 response.getWriter().print(StringPool.BLANK);
496             }
497 
498         }
499         catch (Exception e) {
500             throw new PortletException(e);
501         }
502         finally {
503             _setPortletAttributes(resourceRequest, resourceResponse);
504         }
505     }
506 
507     private static String _convertElementToString(Element element) {
508         try {
509             TransformerFactory transformerFactory =
510                 TransformerFactory.newInstance();
511 
512             Transformer transformer = transformerFactory.newTransformer();
513 
514             transformer.setOutputProperty(OutputKeys.METHOD, "html");
515             transformer.setOutputProperty(
516                 OutputKeys.OMIT_XML_DECLARATION, "yes");
517 
518             StringWriter sw = new StringWriter();
519 
520             transformer.transform(new DOMSource(element), new StreamResult(sw));
521 
522             return sw.toString();
523         }
524         catch (TransformerException te) {
525             if (_log.isWarnEnabled()) {
526                 _log.warn(te, te);
527             }
528         }
529 
530         return null;
531     }
532 
533     private Profile _getCCPPProfile(HttpServletRequest request) {
534         if (_profile == null) {
535             _profile = PortalProfileFactory.getCCPPProfile(request);
536         }
537 
538         return _profile;
539     }
540 
541     private Container _getContainer() {
542         if (_remotePortlet) {
543             return WSRPFactoryUtil.getContainer();
544         }
545         else {
546             return ContainerFactory.getContainer(
547                 ContainerType.PORTLET_CONTAINER);
548         }
549     }
550 
551     private Locale _getLocale(
552         HttpServletRequest request, PortletRequest portletRequest) {
553 
554         ThemeDisplay themeDisplay = _getThemeDisplay(portletRequest);
555 
556         Locale locale = themeDisplay.getLocale();
557 
558         if (locale == null) {
559             locale = request.getLocale();
560         }
561 
562         if (locale == null) {
563             locale = LocaleUtil.getDefault();
564         }
565 
566         return locale;
567     }
568 
569     private List<String> _getMarkupHeaders(
570         ContainerResponse containerResponse) {
571 
572         Map<String, List<Element>> elementProperties =
573             containerResponse.getElementProperties();
574 
575         if (elementProperties == null) {
576             return null;
577         }
578 
579         List<Element> markupHeadElements = elementProperties.get(
580             MimeResponse.MARKUP_HEAD_ELEMENT);
581 
582         if ((markupHeadElements == null) || markupHeadElements.isEmpty()) {
583             return null;
584         }
585 
586         List<String> markupHeaders = new ArrayList<String>();
587 
588         for (Element markupHeadElement : markupHeadElements) {
589             markupHeaders.add(_convertElementToString(markupHeadElement));
590         }
591 
592         return markupHeaders;
593     }
594 
595     private long _getPlid(PortletRequest portletRequest) {
596         ThemeDisplay themeDisplay = _getThemeDisplay(portletRequest);
597 
598         return themeDisplay.getPlid();
599     }
600 
601     private Map<PortletID, List<String>> _getPortletNamespaces(
602         ContainerRequest containerRequest) {
603 
604         Map<PortletID, List<String>> portletNamespaces =
605             new HashMap<PortletID, List<String>>();
606 
607         try {
608             PortletWindowContext portletWindowContext =
609                 containerRequest.getPortletWindowContext();
610 
611             List<EntityID> portletEntityIDs =
612                 portletWindowContext.getPortletWindows(
613                     PortletType.LOCAL,
614                     ContainerUtil.getEventDistributionType(containerRequest));
615 
616             if (portletEntityIDs == null) {
617                 return portletNamespaces;
618             }
619 
620             for (EntityID portletEntityID : portletEntityIDs) {
621                 List<String> namespaces = portletNamespaces.get(
622                     portletEntityID.getPortletID());
623 
624                 if (namespaces == null) {
625                     namespaces = new ArrayList<String>();
626 
627                     portletNamespaces.put(
628                         portletEntityID.getPortletID(), namespaces);
629                 }
630 
631                 String namespace = WindowInvokerUtil.getPortletNamespace(
632                     portletEntityID);
633 
634                 namespaces.add(namespace);
635             }
636         }
637         catch (PortletWindowContextException pwce) {
638             if (_log.isWarnEnabled()) {
639                 _log.warn(pwce, pwce);
640             }
641         }
642 
643         return portletNamespaces;
644     }
645 
646     private Map<PortletID, List<String>> _getPortletWindowIDs(
647         ContainerRequest containerRequest, long plid) {
648 
649         Map<PortletID, List<String>> portletWindowIDs =
650             new HashMap<PortletID, List<String>>();
651 
652         try {
653             PortletWindowContext portletWindowContext =
654                 containerRequest.getPortletWindowContext();
655 
656             List<EntityID> portletEntityIDs =
657                 portletWindowContext.getPortletWindows(
658                     PortletType.LOCAL,
659                     ContainerUtil.getEventDistributionType(containerRequest));
660 
661             if (portletEntityIDs == null) {
662                 return portletWindowIDs;
663             }
664 
665             for (EntityID portletEntityID : portletEntityIDs) {
666                 List<String> windowIDs = portletWindowIDs.get(
667                     portletEntityID.getPortletID());
668 
669                 if (windowIDs == null) {
670                     windowIDs = new ArrayList<String>();
671 
672                     portletWindowIDs.put(
673                         portletEntityID.getPortletID(), windowIDs);
674                 }
675 
676                 String windowID = WindowInvokerUtil.getPortletWindowID(
677                     portletEntityID, plid);
678 
679                 windowIDs.add(windowID);
680             }
681         }
682         catch (PortletWindowContextException pwce) {
683             if (_log.isWarnEnabled()) {
684                 _log.warn(pwce, pwce);
685             }
686         }
687 
688         return portletWindowIDs;
689     }
690 
691     private List<String> _getRoles(HttpServletRequest request) {
692         if (_remoteUserId <= 0) {
693             return Collections.EMPTY_LIST;
694         }
695 
696         try {
697             List<Role> roles = RoleLocalServiceUtil.getUserRoles(_remoteUserId);
698 
699             List<String> roleNames = new ArrayList<String>(roles.size());
700 
701             for (Role role : roles) {
702                 roleNames.add(role.getName());
703             }
704 
705             return roleNames;
706         }
707         catch (Exception e) {
708             _log.error(e, e);
709 
710             return Collections.EMPTY_LIST;
711         }
712     }
713 
714     private ThemeDisplay _getThemeDisplay(PortletRequest portletRequest) {
715         ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
716             WebKeys.THEME_DISPLAY);
717 
718         return themeDisplay;
719     }
720 
721     private void _initUser(
722         HttpServletRequest request, com.liferay.portal.model.Portlet portlet) {
723 
724         long userId = PortalUtil.getUserId(request);
725         String remoteUser = request.getRemoteUser();
726 
727         String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
728 
729         if (userPrincipalStrategy.equals(
730                 PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
731 
732             try {
733                 User user = PortalUtil.getUser(request);
734 
735                 _remoteUser = user.getScreenName();
736                 _remoteUserId = user.getUserId();
737                 _userPrincipal = new ProtectedPrincipal(_remoteUser);
738             }
739             catch (Exception e) {
740                 _log.error(e);
741             }
742         }
743         else {
744             if ((userId > 0) && (remoteUser == null)) {
745                 _remoteUser = String.valueOf(userId);
746                 _remoteUserId = userId;
747                 _userPrincipal = new ProtectedPrincipal(_remoteUser);
748             }
749             else {
750                 _remoteUser = remoteUser;
751                 _remoteUserId = GetterUtil.getLong(remoteUser);
752                 _userPrincipal = request.getUserPrincipal();
753             }
754         }
755     }
756 
757     private boolean _isWARFile() {
758         if (_remotePortlet) {
759             return true;
760         }
761         else {
762             return getPortletConfig().isWARFile();
763         }
764     }
765 
766     private void _populateContainerRequest(
767         HttpServletRequest request, HttpServletResponse response,
768         ContainerRequest containerRequest, PortletRequest portletRequest) {
769 
770         containerRequest.setRoles(_getRoles(request));
771         containerRequest.setUserID(_remoteUser);
772         containerRequest.setUserPrincipal(_userPrincipal);
773         containerRequest.setLocale(_getLocale(request, portletRequest));
774 
775         Map<String, String> userInfoMap = UserInfoFactory.getUserInfo(
776             _remoteUserId, _portletModel);
777 
778         if (_remotePortlet && (userInfoMap != null)) {
779             userInfoMap = _processUserInfoMap(userInfoMap);
780         }
781 
782         containerRequest.setUserInfo(userInfoMap);
783 
784         containerRequest.setAttribute(
785             WebKeys.THEME_DISPLAY, _getThemeDisplay(portletRequest));
786 
787         containerRequest.setAttribute(
788             PortletRequest.CCPP_PROFILE, _getCCPPProfile(request));
789 
790         containerRequest.setAttribute(
791             WebKeys.PORTLET_ID, _portletModel.getPortletId());
792 
793         containerRequest.setAttribute(WebKeys.RENDER_PORTLET, _portletModel);
794 
795         containerRequest.setAttribute(
796             PortletRequestConstants.ESCAPE_XML_VALUE,
797             Boolean.valueOf(PropsValues.PORTLET_URL_ESCAPE_XML));
798 
799         containerRequest.setRequestSharedAttributes(REQUEST_SHARED_ATTRIBUTES);
800     }
801 
802     private Map<String, String> _processUserInfoMap(
803         Map<String, String> userInfoMap) {
804 
805         Map<String, String> wsrpUserInfoMap = null;
806 
807         try {
808             wsrpUserInfoMap = new HashMap<String, String>();
809 
810             ProfileMapManager profileMapManager =
811                 WSRPFactoryUtil.getProfileMapManager();
812 
813             Map<String, String> wsrpDefaultUserInfoMap =
814                 profileMapManager.getPortletMap();
815 
816             Set<String> userAttributes = userInfoMap.keySet();
817 
818             for (String userAttribute : userAttributes) {
819                 String wsrpUserAttribute = wsrpDefaultUserInfoMap.get(
820                     userAttribute);
821 
822                 if (wsrpUserAttribute != null) {
823                     String value = userInfoMap.get(userAttribute);
824 
825                     wsrpUserInfoMap.put(wsrpUserAttribute, value);
826                 }
827             }
828         }
829         catch (Exception e) {
830             if (_log.isWarnEnabled()) {
831                 _log.warn(e, e);
832             }
833 
834             wsrpUserInfoMap = userInfoMap;
835         }
836 
837         return wsrpUserInfoMap;
838     }
839 
840     private void _clearEventPortletsResponse(
841         HttpServletRequest request, List<EntityID> eventUpdatedPortlets) {
842 
843         Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
844 
845         for (EntityID entityID : eventUpdatedPortlets) {
846             clearResponse(
847                 request.getSession(), layout.getPlid(),
848                 entityID.getPortletWindowName(),
849                 LanguageUtil.getLanguageId(request));
850         }
851     }
852 
853     private void _setHttpSession(
854         ContainerRequest containerRequest, PortletRequest portletRequest) {
855 
856         HttpSession session = containerRequest.getHttpSession();
857 
858         if (session != null) {
859             LiferayPortletSession portletSession =
860                 (LiferayPortletSession)portletRequest.getPortletSession();
861 
862             portletSession.setHttpSession(session);
863 
864             PortletSessionTracker.add(session);
865         }
866     }
867 
868     private void _setPortletAttributes(
869         PortletRequest portletRequest, PortletResponse portletResponse) {
870 
871         HttpServletRequest request = PortalUtil.getHttpServletRequest(
872             portletRequest);
873 
874         if (_portletConfig != null) {
875             request.setAttribute(
876                 JavaConstants.JAVAX_PORTLET_CONFIG, _portletConfig);
877         }
878         else {
879             request.setAttribute(
880                 JavaConstants.JAVAX_PORTLET_CONFIG, getPortletConfig());
881         }
882 
883         request.setAttribute(
884             JavaConstants.JAVAX_PORTLET_REQUEST, portletRequest);
885         request.setAttribute(
886             JavaConstants.JAVAX_PORTLET_RESPONSE, portletResponse);
887     }
888 
889     private void _transferHeaders(
890         HttpServletRequest request, HttpServletResponse response,
891         ContainerResponse containerResponse) {
892 
893         Map<String, List<String>> stringProperties =
894             containerResponse.getStringProperties();
895 
896         if (stringProperties != null) {
897             Set<Map.Entry<String, List<String>>> entries =
898                 stringProperties.entrySet();
899 
900             for (Map.Entry<String, List<String>> mapEntry : entries) {
901                 String headerName = mapEntry.getKey();
902 
903                 for (String headerValue : mapEntry.getValue()) {
904                     response.addHeader(headerName, headerValue);
905                 }
906             }
907         }
908 
909         List<Cookie> cookies = containerResponse.getCookieProperties();
910 
911         if (cookies != null) {
912             for (Cookie cookie : cookies) {
913                 CookieKeys.addCookie(request, response, cookie);
914             }
915         }
916     }
917 
918     private void _updatePortletModeAndState(
919         HttpServletRequest request, List<EntityID> eventUpdatedPortlets) {
920 
921         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
922             WebKeys.THEME_DISPLAY);
923 
924         User user = themeDisplay.getUser();
925         Layout layout = themeDisplay.getLayout();
926 
927         for (EntityID entityID : eventUpdatedPortlets) {
928             PortalUtil.updatePortletMode(
929                 entityID.getPortletWindowName(), user, layout, PortletMode.VIEW,
930                 request);
931 
932             PortalUtil.updateWindowState(
933                 entityID.getPortletWindowName(), user, layout,
934                 WindowState.NORMAL, request);
935         }
936     }
937 
938     private static Log _log = LogFactoryUtil.getLog(WindowInvoker.class);
939 
940     private com.liferay.portal.model.Portlet _portletModel;
941     private PortletConfig _portletConfig;
942     private Container _container;
943     private boolean _remotePortlet;
944     private Profile _profile;
945     private String _remoteUser;
946     private long _remoteUserId;
947     private Principal _userPrincipal;
948 
949 }