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