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