001    /**
002     * Copyright (c) 2000-present 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.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.util.WebKeys;
027    import com.liferay.portal.kernel.xml.QName;
028    import com.liferay.portal.model.Layout;
029    import com.liferay.portal.model.LayoutConstants;
030    import com.liferay.portal.model.LayoutTypePortlet;
031    import com.liferay.portal.model.Portlet;
032    import com.liferay.portal.service.LayoutLocalServiceUtil;
033    
034    import java.io.IOException;
035    
036    import java.util.ArrayList;
037    import java.util.Collections;
038    import java.util.List;
039    
040    import javax.portlet.Event;
041    
042    import javax.servlet.http.HttpServletRequest;
043    import javax.servlet.http.HttpServletResponse;
044    
045    /**
046     * @author Shuyang Zhou
047     * @author Raymond Aug??
048     */
049    public class PortletContainerUtil {
050    
051            public static List<LayoutTypePortlet> getLayoutTypePortlets(Layout layout)
052                    throws PortletContainerException {
053    
054                    if (_PORTLET_EVENT_DISTRIBUTION_LAYOUT_SET) {
055                            List<Layout> layouts = null;
056    
057                            try {
058                                    layouts = LayoutLocalServiceUtil.getLayouts(
059                                            layout.getGroupId(), layout.isPrivateLayout(),
060                                            LayoutConstants.TYPE_PORTLET);
061                            }
062                            catch (SystemException se) {
063                                    throw new PortletContainerException(se);
064                            }
065    
066                            List<LayoutTypePortlet> layoutTypePortlets = new ArrayList<>(
067                                    layouts.size());
068    
069                            for (Layout curLayout : layouts) {
070                                    LayoutTypePortlet layoutTypePortlet =
071                                            (LayoutTypePortlet)curLayout.getLayoutType();
072    
073                                    layoutTypePortlets.add(layoutTypePortlet);
074                            }
075    
076                            return layoutTypePortlets;
077                    }
078    
079                    if (layout.isTypePortlet()) {
080                            List<LayoutTypePortlet> layoutTypePortlets = new ArrayList<>(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                    return setupOptionalRenderParameters(
171                            request, renderPath, columnId, columnPos, columnCount, null, null);
172            }
173    
174            public static HttpServletRequest setupOptionalRenderParameters(
175                    HttpServletRequest request, String renderPath, String columnId,
176                    Integer columnPos, Integer columnCount, Boolean boundary,
177                    Boolean decorate) {
178    
179                    if ((_LAYOUT_PARALLEL_RENDER_ENABLE && ServerDetector.isTomcat()) ||
180                            _PORTLET_CONTAINER_RESTRICT) {
181    
182                            RestrictPortletServletRequest restrictPortletServletRequest =
183                                    new RestrictPortletServletRequest(request);
184    
185                            if (renderPath != null) {
186                                    restrictPortletServletRequest.setAttribute(
187                                            WebKeys.RENDER_PATH, renderPath);
188                            }
189    
190                            if (columnId != null) {
191                                    restrictPortletServletRequest.setAttribute(
192                                            WebKeys.RENDER_PORTLET_COLUMN_ID, columnId);
193                            }
194    
195                            if (columnPos != null) {
196                                    restrictPortletServletRequest.setAttribute(
197                                            WebKeys.RENDER_PORTLET_COLUMN_POS, columnPos);
198                            }
199    
200                            if (columnCount != null) {
201                                    restrictPortletServletRequest.setAttribute(
202                                            WebKeys.RENDER_PORTLET_COLUMN_COUNT, columnCount);
203                            }
204    
205                            if (boundary != null) {
206                                    restrictPortletServletRequest.setAttribute(
207                                            WebKeys.RENDER_PORTLET_BOUNDARY, boundary);
208                            }
209    
210                            if (decorate != null) {
211                                    restrictPortletServletRequest.setAttribute(
212                                            WebKeys.PORTLET_DECORATE, decorate);
213                            }
214    
215                            return restrictPortletServletRequest;
216                    }
217    
218                    TempAttributesServletRequest tempAttributesServletRequest =
219                            new TempAttributesServletRequest(request);
220    
221                    if (renderPath != null) {
222                            tempAttributesServletRequest.setTempAttribute(
223                                    WebKeys.RENDER_PATH, renderPath);
224                    }
225    
226                    if (columnId != null) {
227                            tempAttributesServletRequest.setTempAttribute(
228                                    WebKeys.RENDER_PORTLET_COLUMN_ID, columnId);
229                    }
230    
231                    if (columnPos != null) {
232                            tempAttributesServletRequest.setTempAttribute(
233                                    WebKeys.RENDER_PORTLET_COLUMN_POS, columnPos);
234                    }
235    
236                    if (columnCount != null) {
237                            tempAttributesServletRequest.setTempAttribute(
238                                    WebKeys.RENDER_PORTLET_COLUMN_COUNT, columnCount);
239                    }
240    
241                    return tempAttributesServletRequest;
242            }
243    
244            public void setPortletContainer(PortletContainer portletContainer) {
245                    PortalRuntimePermission.checkSetBeanProperty(getClass());
246    
247                    _portletContainer = portletContainer;
248            }
249    
250            private static void _processEvents(
251                            HttpServletRequest request, HttpServletResponse response,
252                            List<Event> events)
253                    throws PortletContainerException {
254    
255                    Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
256    
257                    List<LayoutTypePortlet> layoutTypePortlets = getLayoutTypePortlets(
258                            layout);
259    
260                    for (LayoutTypePortlet layoutTypePortlet : layoutTypePortlets) {
261                            List<Portlet> portlets = null;
262    
263                            try {
264                                    portlets = layoutTypePortlet.getAllPortlets();
265                            }
266                            catch (Exception e) {
267                                    throw new PortletContainerException(e);
268                            }
269    
270                            for (Portlet portlet : portlets) {
271                                    for (Event event : events) {
272                                            javax.xml.namespace.QName qName = event.getQName();
273    
274                                            QName processingQName = portlet.getProcessingEvent(
275                                                    qName.getNamespaceURI(), qName.getLocalPart());
276    
277                                            if (processingQName == null) {
278                                                    continue;
279                                            }
280    
281                                            processEvent(
282                                                    request, response, portlet,
283                                                    layoutTypePortlet.getLayout(), event);
284                                    }
285                            }
286                    }
287            }
288    
289            private static final boolean _LAYOUT_PARALLEL_RENDER_ENABLE = false;
290    
291            private static final boolean _PORTLET_CONTAINER_RESTRICT =
292                    GetterUtil.getBoolean(
293                            PropsUtil.get(PropsKeys.PORTLET_CONTAINER_RESTRICT));
294    
295            private static final boolean _PORTLET_EVENT_DISTRIBUTION_LAYOUT_SET =
296                    !StringUtil.equalsIgnoreCase(
297                            PropsUtil.get(PropsKeys.PORTLET_EVENT_DISTRIBUTION), "layout");
298    
299            private static PortletContainer _portletContainer;
300    
301    }