001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.portlet;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
019    import com.liferay.portal.kernel.servlet.TempAttributesServletRequest;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.ServerDetector;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.kernel.xml.QName;
027    import com.liferay.portal.model.Layout;
028    import com.liferay.portal.model.LayoutConstants;
029    import com.liferay.portal.model.LayoutTypePortlet;
030    import com.liferay.portal.model.Portlet;
031    import com.liferay.portal.service.LayoutLocalServiceUtil;
032    
033    import java.io.IOException;
034    
035    import java.util.ArrayList;
036    import java.util.Collections;
037    import java.util.List;
038    
039    import javax.portlet.Event;
040    
041    import javax.servlet.http.HttpServletRequest;
042    import javax.servlet.http.HttpServletResponse;
043    
044    /**
045     * @author Shuyang Zhou
046     * @author Raymond Aug??
047     */
048    public class PortletContainerUtil {
049    
050            public static List<LayoutTypePortlet> getLayoutTypePortlets(Layout layout)
051                    throws PortletContainerException {
052    
053                    if (_PORTLET_EVENT_DISTRIBUTION_LAYOUT_SET) {
054                            List<Layout> layouts = null;
055    
056                            try {
057                                    layouts = LayoutLocalServiceUtil.getLayouts(
058                                            layout.getGroupId(), layout.isPrivateLayout(),
059                                            LayoutConstants.TYPE_PORTLET);
060                            }
061                            catch (SystemException se) {
062                                    throw new PortletContainerException(se);
063                            }
064    
065                            List<LayoutTypePortlet> layoutTypePortlets =
066                                    new ArrayList<LayoutTypePortlet>(layouts.size());
067    
068                            for (Layout curLayout : layouts) {
069                                    LayoutTypePortlet layoutTypePortlet =
070                                            (LayoutTypePortlet)curLayout.getLayoutType();
071    
072                                    layoutTypePortlets.add(layoutTypePortlet);
073                            }
074    
075                            return layoutTypePortlets;
076                    }
077    
078                    if (layout.isTypePortlet()) {
079                            List<LayoutTypePortlet> layoutTypePortlets =
080                                    new ArrayList<LayoutTypePortlet>(1);
081    
082                            LayoutTypePortlet layoutTypePortlet =
083                                    (LayoutTypePortlet)layout.getLayoutType();
084    
085                            layoutTypePortlets.add(layoutTypePortlet);
086    
087                            return layoutTypePortlets;
088                    }
089    
090                    return Collections.emptyList();
091            }
092    
093            public static PortletContainer getPortletContainer() {
094                    PortalRuntimePermission.checkGetBeanProperty(
095                            PortletContainerUtil.class);
096    
097                    return _portletContainer;
098            }
099    
100            public static void preparePortlet(
101                            HttpServletRequest request, Portlet portlet)
102                    throws PortletContainerException {
103    
104                    getPortletContainer().preparePortlet(request, portlet);
105            }
106    
107            public static void processAction(
108                            HttpServletRequest request, HttpServletResponse response,
109                            Portlet portlet)
110                    throws PortletContainerException {
111    
112                    PortletContainer portletContainer = getPortletContainer();
113    
114                    ActionResult actionResult = portletContainer.processAction(
115                            request, response, portlet);
116    
117                    List<Event> events = actionResult.getEvents();
118    
119                    if (!events.isEmpty()) {
120                            _processEvents(request, response, events);
121                    }
122    
123                    String location = actionResult.getLocation();
124    
125                    if (Validator.isNotNull(location)) {
126                            try {
127                                    response.sendRedirect(location);
128                            }
129                            catch (IOException ioe) {
130                                    throw new PortletContainerException(ioe);
131                            }
132                    }
133            }
134    
135            public static void processEvent(
136                            HttpServletRequest request, HttpServletResponse response,
137                            Portlet portlet, Layout layout, Event event)
138                    throws PortletContainerException {
139    
140                    PortletContainer portletContainer = getPortletContainer();
141    
142                    List<Event> events = portletContainer.processEvent(
143                            request, response, portlet, layout, event);
144    
145                    if (!events.isEmpty()) {
146                            _processEvents(request, response, events);
147                    }
148            }
149    
150            public static void render(
151                            HttpServletRequest request, HttpServletResponse response,
152                            Portlet portlet)
153                    throws PortletContainerException {
154    
155                    getPortletContainer().render(request, response, portlet);
156            }
157    
158            public static void serveResource(
159                            HttpServletRequest request, HttpServletResponse response,
160                            Portlet portlet)
161                    throws PortletContainerException {
162    
163                    getPortletContainer().serveResource(request, response, portlet);
164            }
165    
166            public static HttpServletRequest setupOptionalRenderParameters(
167                    HttpServletRequest request, String renderPath, String columnId,
168                    Integer columnPos, Integer columnCount) {
169    
170                    if ((_LAYOUT_PARALLEL_RENDER_ENABLE && ServerDetector.isTomcat()) ||
171                            _PORTLET_CONTAINER_RESTRICT) {
172    
173                            RestrictPortletServletRequest restrictPortletServletRequest =
174                                    new RestrictPortletServletRequest(request);
175    
176                            if (renderPath != null) {
177                                    restrictPortletServletRequest.setAttribute(
178                                            WebKeys.RENDER_PATH, renderPath);
179                            }
180    
181                            if (columnId != null) {
182                                    restrictPortletServletRequest.setAttribute(
183                                            WebKeys.RENDER_PORTLET_COLUMN_ID, columnId);
184                            }
185    
186                            if (columnPos != null) {
187                                    restrictPortletServletRequest.setAttribute(
188                                            WebKeys.RENDER_PORTLET_COLUMN_POS, columnPos);
189                            }
190    
191                            if (columnCount != null) {
192                                    restrictPortletServletRequest.setAttribute(
193                                            WebKeys.RENDER_PORTLET_COLUMN_COUNT, columnCount);
194                            }
195    
196                            return restrictPortletServletRequest;
197                    }
198    
199                    TempAttributesServletRequest tempAttributesServletRequest =
200                            new TempAttributesServletRequest(request);
201    
202                    if (renderPath != null) {
203                            tempAttributesServletRequest.setTempAttribute(
204                                    WebKeys.RENDER_PATH, renderPath);
205                    }
206    
207                    if (columnId != null) {
208                            tempAttributesServletRequest.setTempAttribute(
209                                    WebKeys.RENDER_PORTLET_COLUMN_ID, columnId);
210                    }
211    
212                    if (columnPos != null) {
213                            tempAttributesServletRequest.setTempAttribute(
214                                    WebKeys.RENDER_PORTLET_COLUMN_POS, columnPos);
215                    }
216    
217                    if (columnCount != null) {
218                            tempAttributesServletRequest.setTempAttribute(
219                                    WebKeys.RENDER_PORTLET_COLUMN_COUNT, columnCount);
220                    }
221    
222                    return tempAttributesServletRequest;
223            }
224    
225            public void setPortletContainer(PortletContainer portletContainer) {
226                    PortalRuntimePermission.checkSetBeanProperty(getClass());
227    
228                    _portletContainer = portletContainer;
229            }
230    
231            private static void _processEvents(
232                            HttpServletRequest request, HttpServletResponse response,
233                            List<Event> events)
234                    throws PortletContainerException {
235    
236                    Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
237    
238                    List<LayoutTypePortlet> layoutTypePortlets = getLayoutTypePortlets(
239                            layout);
240    
241                    for (LayoutTypePortlet layoutTypePortlet : layoutTypePortlets) {
242                            List<Portlet> portlets = null;
243    
244                            try {
245                                    portlets = layoutTypePortlet.getAllPortlets();
246                            }
247                            catch (Exception e) {
248                                    throw new PortletContainerException(e);
249                            }
250    
251                            for (Portlet portlet : portlets) {
252                                    for (Event event : events) {
253                                            javax.xml.namespace.QName qName = event.getQName();
254    
255                                            QName processingQName = portlet.getProcessingEvent(
256                                                    qName.getNamespaceURI(), qName.getLocalPart());
257    
258                                            if (processingQName == null) {
259                                                    continue;
260                                            }
261    
262                                            processEvent(
263                                                    request, response, portlet,
264                                                    layoutTypePortlet.getLayout(), event);
265                                    }
266                            }
267                    }
268            }
269    
270            private static final boolean _LAYOUT_PARALLEL_RENDER_ENABLE =
271                    GetterUtil.getBoolean(
272                            PropsUtil.get(PropsKeys.LAYOUT_PARALLEL_RENDER_ENABLE));
273    
274            private static final boolean _PORTLET_CONTAINER_RESTRICT =
275                    GetterUtil.getBoolean(
276                            PropsUtil.get(PropsKeys.PORTLET_CONTAINER_RESTRICT));
277    
278            private static final boolean _PORTLET_EVENT_DISTRIBUTION_LAYOUT_SET =
279                    !PropsUtil.get(PropsKeys.PORTLET_EVENT_DISTRIBUTION).equalsIgnoreCase(
280                            "layout");
281    
282            private static PortletContainer _portletContainer;
283    
284    }