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