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