001
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
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 =
067 new ArrayList<LayoutTypePortlet>(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 =
081 new ArrayList<LayoutTypePortlet>(1);
082
083 LayoutTypePortlet layoutTypePortlet =
084 (LayoutTypePortlet)layout.getLayoutType();
085
086 layoutTypePortlets.add(layoutTypePortlet);
087
088 return layoutTypePortlets;
089 }
090
091 return Collections.emptyList();
092 }
093
094 public static PortletContainer getPortletContainer() {
095 PortalRuntimePermission.checkGetBeanProperty(
096 PortletContainerUtil.class);
097
098 return _portletContainer;
099 }
100
101 public static void preparePortlet(
102 HttpServletRequest request, Portlet portlet)
103 throws PortletContainerException {
104
105 getPortletContainer().preparePortlet(request, portlet);
106 }
107
108 public static void processAction(
109 HttpServletRequest request, HttpServletResponse response,
110 Portlet portlet)
111 throws PortletContainerException {
112
113 PortletContainer portletContainer = getPortletContainer();
114
115 ActionResult actionResult = portletContainer.processAction(
116 request, response, portlet);
117
118 List<Event> events = actionResult.getEvents();
119
120 if (!events.isEmpty()) {
121 _processEvents(request, response, events);
122 }
123
124 String location = actionResult.getLocation();
125
126 if (Validator.isNotNull(location)) {
127 try {
128 response.sendRedirect(location);
129 }
130 catch (IOException ioe) {
131 throw new PortletContainerException(ioe);
132 }
133 }
134 }
135
136 public static void processEvent(
137 HttpServletRequest request, HttpServletResponse response,
138 Portlet portlet, Layout layout, Event event)
139 throws PortletContainerException {
140
141 PortletContainer portletContainer = getPortletContainer();
142
143 List<Event> events = portletContainer.processEvent(
144 request, response, portlet, layout, event);
145
146 if (!events.isEmpty()) {
147 _processEvents(request, response, events);
148 }
149 }
150
151 public static void render(
152 HttpServletRequest request, HttpServletResponse response,
153 Portlet portlet)
154 throws PortletContainerException {
155
156 getPortletContainer().render(request, response, portlet);
157 }
158
159 public static void serveResource(
160 HttpServletRequest request, HttpServletResponse response,
161 Portlet portlet)
162 throws PortletContainerException {
163
164 getPortletContainer().serveResource(request, response, portlet);
165 }
166
167 public static HttpServletRequest setupOptionalRenderParameters(
168 HttpServletRequest request, String renderPath, String columnId,
169 Integer columnPos, Integer columnCount) {
170
171 if ((_LAYOUT_PARALLEL_RENDER_ENABLE && ServerDetector.isTomcat()) ||
172 _PORTLET_CONTAINER_RESTRICT) {
173
174 RestrictPortletServletRequest restrictPortletServletRequest =
175 new RestrictPortletServletRequest(request);
176
177 if (renderPath != null) {
178 restrictPortletServletRequest.setAttribute(
179 WebKeys.RENDER_PATH, renderPath);
180 }
181
182 if (columnId != null) {
183 restrictPortletServletRequest.setAttribute(
184 WebKeys.RENDER_PORTLET_COLUMN_ID, columnId);
185 }
186
187 if (columnPos != null) {
188 restrictPortletServletRequest.setAttribute(
189 WebKeys.RENDER_PORTLET_COLUMN_POS, columnPos);
190 }
191
192 if (columnCount != null) {
193 restrictPortletServletRequest.setAttribute(
194 WebKeys.RENDER_PORTLET_COLUMN_COUNT, columnCount);
195 }
196
197 return restrictPortletServletRequest;
198 }
199
200 TempAttributesServletRequest tempAttributesServletRequest =
201 new TempAttributesServletRequest(request);
202
203 if (renderPath != null) {
204 tempAttributesServletRequest.setTempAttribute(
205 WebKeys.RENDER_PATH, renderPath);
206 }
207
208 if (columnId != null) {
209 tempAttributesServletRequest.setTempAttribute(
210 WebKeys.RENDER_PORTLET_COLUMN_ID, columnId);
211 }
212
213 if (columnPos != null) {
214 tempAttributesServletRequest.setTempAttribute(
215 WebKeys.RENDER_PORTLET_COLUMN_POS, columnPos);
216 }
217
218 if (columnCount != null) {
219 tempAttributesServletRequest.setTempAttribute(
220 WebKeys.RENDER_PORTLET_COLUMN_COUNT, columnCount);
221 }
222
223 return tempAttributesServletRequest;
224 }
225
226 public void setPortletContainer(PortletContainer portletContainer) {
227 PortalRuntimePermission.checkSetBeanProperty(getClass());
228
229 _portletContainer = portletContainer;
230 }
231
232 private static void _processEvents(
233 HttpServletRequest request, HttpServletResponse response,
234 List<Event> events)
235 throws PortletContainerException {
236
237 Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
238
239 List<LayoutTypePortlet> layoutTypePortlets = getLayoutTypePortlets(
240 layout);
241
242 for (LayoutTypePortlet layoutTypePortlet : layoutTypePortlets) {
243 List<Portlet> portlets = null;
244
245 try {
246 portlets = layoutTypePortlet.getAllPortlets();
247 }
248 catch (Exception e) {
249 throw new PortletContainerException(e);
250 }
251
252 for (Portlet portlet : portlets) {
253 for (Event event : events) {
254 javax.xml.namespace.QName qName = event.getQName();
255
256 QName processingQName = portlet.getProcessingEvent(
257 qName.getNamespaceURI(), qName.getLocalPart());
258
259 if (processingQName == null) {
260 continue;
261 }
262
263 processEvent(
264 request, response, portlet,
265 layoutTypePortlet.getLayout(), event);
266 }
267 }
268 }
269 }
270
271 private static final boolean _LAYOUT_PARALLEL_RENDER_ENABLE =
272 GetterUtil.getBoolean(
273 PropsUtil.get(PropsKeys.LAYOUT_PARALLEL_RENDER_ENABLE));
274
275 private static final boolean _PORTLET_CONTAINER_RESTRICT =
276 GetterUtil.getBoolean(
277 PropsUtil.get(PropsKeys.PORTLET_CONTAINER_RESTRICT));
278
279 private static final boolean _PORTLET_EVENT_DISTRIBUTION_LAYOUT_SET =
280 !StringUtil.equalsIgnoreCase(
281 PropsUtil.get(PropsKeys.PORTLET_EVENT_DISTRIBUTION), "layout");
282
283 private static PortletContainer _portletContainer;
284
285 }