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