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.SystemException;
45  import com.liferay.portal.kernel.log.Log;
46  import com.liferay.portal.kernel.log.LogFactoryUtil;
47  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
48  import com.liferay.portal.kernel.util.ContentTypes;
49  import com.liferay.portal.kernel.util.LocaleUtil;
50  import com.liferay.portal.kernel.util.StringPool;
51  import com.liferay.portal.kernel.util.Validator;
52  import com.liferay.portal.model.Layout;
53  import com.liferay.portal.model.LayoutConstants;
54  import com.liferay.portal.model.LayoutTypePortlet;
55  import com.liferay.portal.model.Portlet;
56  import com.liferay.portal.model.PortletApp;
57  import com.liferay.portal.model.PortletPreferencesIds;
58  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
59  import com.liferay.portal.theme.ThemeDisplay;
60  import com.liferay.portal.util.WebKeys;
61  import com.liferay.portlet.PortletPreferencesFactoryUtil;
62  import com.liferay.portlet.PortletPreferencesWrapper;
63  
64  import com.sun.portal.container.EntityID;
65  import com.sun.portal.container.PortletLang;
66  import com.sun.portal.container.PortletType;
67  import com.sun.portal.container.PortletWindowContext;
68  import com.sun.portal.container.PortletWindowContextException;
69  import com.sun.portal.container.service.EventHolder;
70  import com.sun.portal.container.service.PortletDescriptorHolder;
71  import com.sun.portal.container.service.PortletDescriptorHolderFactory;
72  import com.sun.portal.container.service.PublicRenderParameterHolder;
73  import com.sun.portal.container.service.policy.DistributionType;
74  
75  import java.io.UnsupportedEncodingException;
76  
77  import java.net.URLEncoder;
78  
79  import java.util.ArrayList;
80  import java.util.Collections;
81  import java.util.HashMap;
82  import java.util.List;
83  import java.util.Locale;
84  import java.util.Map;
85  import java.util.ResourceBundle;
86  import java.util.Set;
87  
88  import javax.portlet.PortletPreferences;
89  
90  import javax.servlet.http.HttpServletRequest;
91  import javax.servlet.http.HttpSession;
92  
93  /**
94   * <a href="PortletWindowContextImpl.java.html"><b><i>View Source</i></b></a>
95   *
96   * @author Deepak Gothe
97   * @author Brian Wing Shun Chan
98   * @author Manish Gupta
99   *
100  */
101 public class PortletWindowContextImpl implements PortletWindowContext {
102 
103     public PortletWindowContextImpl(
104         HttpServletRequest request, Portlet portlet, String lifecycle) {
105 
106         _request = request;
107         _portlet = portlet;
108         _lifecycle = lifecycle;
109     }
110 
111     public String encodeURL(String url) {
112         try {
113             return URLEncoder.encode(url, StringPool.UTF8);
114         }
115         catch (UnsupportedEncodingException uee) {
116             return url;
117         }
118     }
119 
120     public String getAuthenticationType() {
121         return _request.getAuthType();
122     }
123 
124     public String getConsumerID(String portletWindowName) {
125         return "dummy";
126     }
127 
128     public String getContentType() {
129         if (BrowserSnifferUtil.isWap(_request)) {
130             return ContentTypes.XHTML_MP;
131         }
132         else {
133             return ContentTypes.TEXT_HTML;
134         }
135     }
136 
137     public String getDescription(String portletName, String desiredLocale) {
138         return null;
139     }
140 
141     public String getDesktopURL(HttpServletRequest request) {
142         StringBuffer requestURL = request.getRequestURL();
143 
144         return requestURL.toString();
145     }
146 
147     public String getDesktopURL(
148         HttpServletRequest request, String query, boolean escape) {
149 
150         StringBuilder sb = new StringBuilder(getDesktopURL(request));
151 
152         if (Validator.isNotNull(query)) {
153             sb.append(StringPool.QUESTION);
154             sb.append(query);
155         }
156 
157         String url = sb.toString();
158 
159         if (escape) {
160             try {
161                 url = URLEncoder.encode(url, StringPool.UTF8);
162             }
163             catch (UnsupportedEncodingException uee) {
164             }
165         }
166 
167         return url;
168     }
169 
170     public String getDisplayName(String portletName, String desiredLocale) {
171         return null;
172     }
173 
174     public EntityID getEntityID(String portletId) {
175         return WindowInvokerUtil.getEntityID(_portlet);
176     }
177 
178     public List<String> getKeywords(String portletName, String desiredLocale) {
179         return null;
180     }
181 
182     public String getLocaleString() {
183         ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
184             WebKeys.THEME_DISPLAY);
185 
186         Locale locale = themeDisplay.getLocale();
187 
188         if (locale == null) {
189             locale = _request.getLocale();
190         }
191 
192         if (locale == null) {
193             locale = LocaleUtil.getDefault();
194         }
195 
196         return locale.toString();
197     }
198 
199     public List<String> getMarkupTypes(String portletName) {
200         return Collections.EMPTY_LIST;
201     }
202 
203     public String getPortletHandle(String portletWindowName) {
204         Portlet portlet = getPortletModel(portletWindowName);
205 
206         return portlet.getRemotePortletHandle();
207     }
208 
209     public String getPortletID(String portletWindowName) {
210         return getPortletHandle(portletWindowName);
211     }
212 
213     public PortletLang getPortletLang(String portletWindowName) {
214         return null;
215     }
216 
217     public String getPortletName(String portletWindowName) {
218         return _portlet.getPortletName();
219     }
220 
221     public List<EntityID> getPortletWindows(
222         PortletType portletType, DistributionType distributionType) {
223 
224         List<EntityID> entityIDs = new ArrayList<EntityID>();
225 
226         try {
227             List<Portlet> portlets = null;
228 
229             if (distributionType.equals(DistributionType.ALL_PORTLETS)) {
230                 portlets = getAllPortletWindows(portletType);
231             }
232             else if (distributionType.equals(
233                         DistributionType.ALL_PORTLETS_ON_PAGE)) {
234 
235                 portlets = getAvailablePortletWindows(portletType);
236             }
237             else if (distributionType.equals(
238                         DistributionType.VISIBLE_PORTLETS_ON_PAGE)) {
239 
240                 portlets = getVisiblePortletWindows(portletType);
241             }
242 
243             if (portlets != null) {
244                 for (Portlet portlet : portlets) {
245                     EntityID entityID = WindowInvokerUtil.getEntityID(portlet);
246 
247                     entityIDs.add(entityID);
248                 }
249             }
250         }
251         catch (PortletWindowContextException pre) {
252             _log.error(pre);
253         }
254 
255         return entityIDs;
256     }
257 
258     public String getPortletWindowTitle(
259         String portletWindowName, String locale) {
260 
261         return null;
262     }
263 
264     public PortletPreferences getPreferences(
265             String portletWindowName, ResourceBundle bundle, boolean readOnly)
266         throws PortletWindowContextException {
267 
268         try {
269             PortletPreferencesIds portletPreferencesIds =
270                 PortletPreferencesFactoryUtil.getPortletPreferencesIds(
271                     _request, portletWindowName);
272 
273             PortletPreferences portletPreferences =
274                 PortletPreferencesLocalServiceUtil.getPreferences(
275                     portletPreferencesIds);
276 
277             PortletPreferencesWrapper portletPreferencesWrapper =
278                 new PortletPreferencesWrapper(portletPreferences, _lifecycle);
279 
280             return portletPreferencesWrapper;
281         }
282         catch (Exception e) {
283             throw new PortletWindowContextException(e);
284         }
285     }
286 
287     public String getProducerEntityID(String portletWindowName) {
288         Portlet portlet = getPortletModel(portletWindowName);
289 
290         return portlet.getRemoteProducerEntityId();
291     }
292 
293     public Object getProperty(String name) {
294         Object value = null;
295 
296         if (_request != null) {
297             HttpSession session = _request.getSession(false);
298 
299             if (session != null) {
300                 value = session.getAttribute(name);
301             }
302         }
303 
304         return value;
305     }
306 
307     public Map<String, String> getRoleMap(String portletWindowName) {
308         return Collections.EMPTY_MAP;
309     }
310 
311     public List<String> getRoles() {
312         return Collections.EMPTY_LIST;
313     }
314 
315     public String getShortTitle(String portletName, String desiredLocale) {
316         return null;
317     }
318 
319     public List<EventHolder> getSupportedProcessingEventHolders(
320         EntityID entityID) {
321 
322         PortletDescriptorHolder portletDescriptorHolder =
323             getPortletDescriptorHolder();
324 
325         if (portletDescriptorHolder == null) {
326             return null;
327         }
328 
329         return portletDescriptorHolder.getSupportedProcessingEventHolders(
330             entityID);
331     }
332 
333     public List<PublicRenderParameterHolder>
334         getSupportedPublicRenderParameterHolders(
335             EntityID entityID, Map<String, String[]> renderParameters) {
336 
337         PortletDescriptorHolder portletDescriptorHolder =
338             getPortletDescriptorHolder();
339 
340         if (portletDescriptorHolder == null) {
341             return Collections.EMPTY_LIST;
342         }
343 
344         return portletDescriptorHolder.getSupportedPublicRenderParameterHolders(
345             entityID, renderParameters);
346     }
347 
348     public List<EventHolder> getSupportedPublishingEventHolders(
349         EntityID entityID) {
350 
351         PortletDescriptorHolder portletDescriptorHolder =
352             getPortletDescriptorHolder();
353 
354         if (portletDescriptorHolder == null) {
355             return null;
356         }
357 
358         return portletDescriptorHolder.getSupportedPublishingEventHolders(
359             entityID);
360     }
361 
362     public String getTitle(String portletName, String desiredLocale) {
363         return null;
364     }
365 
366     public Map<String, String> getUserInfo() {
367         return Collections.EMPTY_MAP;
368     }
369 
370     public Map<String, String> getUserInfoMap(String portletWindowName) {
371         Map<String, String> userInfo = new HashMap<String, String>();
372 
373         PortletApp portletApp = _portlet.getPortletApp();
374 
375         Set<String> userAttributes = portletApp.getUserAttributes();
376 
377         for (String userAttribute : userAttributes) {
378             userInfo.put(userAttribute, userAttribute);
379         }
380 
381         return userInfo;
382     }
383 
384     public String getUserRepresentation() {
385         return null;
386     }
387 
388     public void init(HttpServletRequest request) {
389         _request = request;
390     }
391 
392     public boolean isAuthless(HttpServletRequest request) {
393         return false;
394     }
395 
396     public void setPortletHandle(
397         String portletWindowName, String portletHandle) {
398     }
399 
400     public void setProperty(String name, Object value) {
401         if (_request != null) {
402             HttpSession session = _request.getSession();
403 
404             session.setAttribute(name, value);
405         }
406     }
407 
408     public void store() {
409     }
410 
411     public EventHolder verifySupportedProcessingEvent(
412         EntityID entityID, EventHolder eventHolder) {
413 
414         PortletDescriptorHolder portletDescriptorHolder =
415             getPortletDescriptorHolder();
416 
417         if (portletDescriptorHolder == null) {
418             return null;
419         }
420 
421         return portletDescriptorHolder.verifySupportedProcessingEvent(
422             entityID, eventHolder);
423     }
424 
425     public Map<String, String> verifySupportedPublicRenderParameters(
426         EntityID entityID,
427         List<PublicRenderParameterHolder> publicRenderParameterHolders) {
428 
429         PortletDescriptorHolder portletDescriptorHolder =
430             getPortletDescriptorHolder();
431 
432         if (portletDescriptorHolder == null) {
433             return Collections.EMPTY_MAP;
434         }
435 
436         return portletDescriptorHolder.verifySupportedPublicRenderParameters(
437             entityID, publicRenderParameterHolders);
438     }
439 
440     public EventHolder verifySupportedPublishingEvent(
441         EntityID entityID, EventHolder eventHolder) {
442 
443         PortletDescriptorHolder portletDescriptorHolder =
444             getPortletDescriptorHolder();
445 
446         if (portletDescriptorHolder == null) {
447             return null;
448         }
449 
450         return portletDescriptorHolder.verifySupportedPublishingEvent(
451             entityID, eventHolder);
452     }
453 
454     protected List<Portlet> getAllPortletWindows(PortletType portletType)
455         throws PortletWindowContextException {
456 
457         List<Portlet> portlets = new ArrayList<Portlet>();
458 
459         try {
460             ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
461                 WebKeys.THEME_DISPLAY);
462 
463             List<Layout> layouts = themeDisplay.getLayouts();
464 
465             for (Layout layout : layouts) {
466                 LayoutTypePortlet layoutTypePortlet =
467                     (LayoutTypePortlet)layout.getLayoutType();
468 
469                 for (Portlet portlet : layoutTypePortlet.getAllPortlets()) {
470                     PortletApp portletApp = portlet.getPortletApp();
471 
472                     if (portletApp.isWARFile() || portlet.isRemote()) {
473                         portlets.add(portlet);
474                     }
475                 }
476             }
477         }
478         catch (SystemException se) {
479             throw new PortletWindowContextException(se);
480         }
481 
482         return portlets;
483     }
484 
485     protected List<Portlet> getAvailablePortletWindows(PortletType portletType)
486         throws PortletWindowContextException {
487 
488         return getVisiblePortletWindows(portletType);
489     }
490 
491     protected PortletDescriptorHolder getPortletDescriptorHolder() {
492         PortletDescriptorHolder portletDescriptorHolder = null;
493 
494         try {
495             portletDescriptorHolder =
496                 PortletDescriptorHolderFactory.getPortletDescriptorHolder();
497         }
498         catch (Exception e) {
499             _log.error(e);
500         }
501 
502         return portletDescriptorHolder;
503     }
504 
505     protected Portlet getPortletModel(String portletWindowName) {
506         if (portletWindowName.equalsIgnoreCase(_portlet.getPortletId())) {
507             return _portlet;
508         }
509 
510         try{
511             List<Portlet> remotePortlets = getAllPortletWindows(
512                 PortletType.REMOTE);
513 
514             for (Portlet portlet : remotePortlets) {
515                 if (portlet.isRemote() &&
516                     portletWindowName.equals(portlet.getPortletId())) {
517 
518                     return portlet;
519                 }
520             }
521         }
522         catch (Exception e) {
523             _log.error(e, e);
524         }
525 
526         return null;
527     }
528 
529     protected List<Portlet> getVisiblePortletWindows(PortletType portletType)
530         throws PortletWindowContextException {
531 
532         List<Portlet> portlets = null;
533 
534         Layout layout = (Layout)_request.getAttribute(WebKeys.LAYOUT);
535 
536         if (layout.getType().equals(LayoutConstants.TYPE_PORTLET)) {
537             LayoutTypePortlet layoutTypePortlet =
538                 (LayoutTypePortlet)layout.getLayoutType();
539 
540             try {
541                 portlets = layoutTypePortlet.getPortlets();
542             }
543             catch (SystemException se) {
544                 throw new PortletWindowContextException(se);
545             }
546         }
547 
548         return portlets;
549     }
550 
551     private static Log _log =
552         LogFactoryUtil.getLog(PortletWindowContextImpl.class);
553 
554     private HttpServletRequest _request;
555     private Portlet _portlet;
556     private String _lifecycle;
557 
558 }