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.struts;
016    
017    import com.liferay.portal.exception.NoSuchLayoutException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.model.Group;
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.PortletConstants;
031    import com.liferay.portal.model.impl.VirtualLayout;
032    import com.liferay.portal.security.auth.PrincipalException;
033    import com.liferay.portal.security.permission.ActionKeys;
034    import com.liferay.portal.security.permission.PermissionChecker;
035    import com.liferay.portal.service.GroupLocalServiceUtil;
036    import com.liferay.portal.service.LayoutLocalServiceUtil;
037    import com.liferay.portal.service.permission.LayoutPermissionUtil;
038    import com.liferay.portal.theme.ThemeDisplay;
039    import com.liferay.portal.util.PortalUtil;
040    import com.liferay.portlet.PortletURLFactoryUtil;
041    import com.liferay.sites.kernel.util.SitesUtil;
042    
043    import javax.portlet.PortletMode;
044    import javax.portlet.PortletRequest;
045    import javax.portlet.PortletURL;
046    import javax.portlet.WindowState;
047    
048    import javax.servlet.http.HttpServletRequest;
049    import javax.servlet.http.HttpServletResponse;
050    
051    /**
052     * @author Adolfo P??rez
053     */
054    public abstract class BaseFindActionHelper implements FindActionHelper {
055    
056            @Override
057            public void execute(
058                            HttpServletRequest request, HttpServletResponse response)
059                    throws Exception {
060    
061                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
062                            WebKeys.THEME_DISPLAY);
063    
064                    try {
065                            long primaryKey = ParamUtil.getLong(
066                                    request, getPrimaryKeyParameterName());
067    
068                            long groupId = ParamUtil.getLong(
069                                    request, "groupId", themeDisplay.getScopeGroupId());
070    
071                            if (primaryKey > 0) {
072                                    try {
073                                            long overrideGroupId = getGroupId(primaryKey);
074    
075                                            if (overrideGroupId > 0) {
076                                                    groupId = overrideGroupId;
077                                            }
078                                    }
079                                    catch (Exception e) {
080                                            if (_log.isDebugEnabled()) {
081                                                    _log.debug(e, e);
082                                            }
083                                    }
084                            }
085    
086                            Object[] plidAndPortletId = getPlidAndPortletId(
087                                    themeDisplay, groupId, themeDisplay.getPlid(), _portletIds);
088    
089                            long plid = (Long)plidAndPortletId[0];
090    
091                            Layout layout = setTargetLayout(request, groupId, plid);
092    
093                            LayoutPermissionUtil.check(
094                                    themeDisplay.getPermissionChecker(), layout, true,
095                                    ActionKeys.VIEW);
096    
097                            String portletId = (String)plidAndPortletId[1];
098    
099                            PortletURL portletURL = PortletURLFactoryUtil.create(
100                                    request, portletId, plid, PortletRequest.RENDER_PHASE);
101    
102                            addRequiredParameters(request, portletId, portletURL);
103    
104                            boolean inheritRedirect = ParamUtil.getBoolean(
105                                    request, "inheritRedirect");
106    
107                            String redirect = null;
108    
109                            if (inheritRedirect) {
110                                    String noSuchEntryRedirect = ParamUtil.getString(
111                                            request, "noSuchEntryRedirect");
112    
113                                    redirect = HttpUtil.getParameter(
114                                            noSuchEntryRedirect, "redirect", false);
115    
116                                    redirect = HttpUtil.decodeURL(redirect);
117                            }
118                            else {
119                                    redirect = ParamUtil.getString(request, "redirect");
120                            }
121    
122                            if (Validator.isNotNull(redirect)) {
123                                    portletURL.setParameter("redirect", redirect);
124                            }
125    
126                            setPrimaryKeyParameter(portletURL, primaryKey);
127    
128                            portletURL.setPortletMode(PortletMode.VIEW);
129                            portletURL.setWindowState(WindowState.NORMAL);
130    
131                            portletURL = processPortletURL(request, portletURL);
132    
133                            response.sendRedirect(portletURL.toString());
134                    }
135                    catch (Exception e) {
136                            String noSuchEntryRedirect = ParamUtil.getString(
137                                    request, "noSuchEntryRedirect");
138    
139                            noSuchEntryRedirect = PortalUtil.escapeRedirect(
140                                    noSuchEntryRedirect);
141    
142                            if (Validator.isNotNull(noSuchEntryRedirect) &&
143                                    (e instanceof NoSuchLayoutException ||
144                                     e instanceof PrincipalException)) {
145    
146                                    response.sendRedirect(noSuchEntryRedirect);
147                            }
148                            else {
149                                    PortalUtil.sendError(e, request, response);
150                            }
151                    }
152            }
153    
154            @Override
155            public abstract long getGroupId(long primaryKey) throws Exception;
156    
157            @Override
158            public abstract String getPrimaryKeyParameterName();
159    
160            @Override
161            public abstract String[] initPortletIds();
162    
163            @Override
164            public abstract PortletURL processPortletURL(
165                            HttpServletRequest request, PortletURL portletURL)
166                    throws Exception;
167    
168            @Override
169            public abstract void setPrimaryKeyParameter(
170                            PortletURL portletURL, long primaryKey)
171                    throws Exception;
172    
173            protected static Object[] fetchPlidAndPortletId(
174                            PermissionChecker permissionChecker, long groupId,
175                            String[] portletIds)
176                    throws Exception {
177    
178                    for (String portletId : portletIds) {
179                            long plid = PortalUtil.getPlidFromPortletId(groupId, portletId);
180    
181                            if (plid == LayoutConstants.DEFAULT_PLID) {
182                                    continue;
183                            }
184    
185                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
186    
187                            if (!LayoutPermissionUtil.contains(
188                                            permissionChecker, layout, ActionKeys.VIEW)) {
189    
190                                    continue;
191                            }
192    
193                            LayoutTypePortlet layoutTypePortlet =
194                                    (LayoutTypePortlet)layout.getLayoutType();
195    
196                            portletId = getPortletId(layoutTypePortlet, portletId);
197    
198                            return new Object[] {plid, portletId};
199                    }
200    
201                    return null;
202            }
203    
204            protected static Object[] getPlidAndPortletId(
205                            ThemeDisplay themeDisplay, long groupId, long plid,
206                            String[] portletIds)
207                    throws Exception {
208    
209                    if ((plid != LayoutConstants.DEFAULT_PLID) &&
210                            (groupId == themeDisplay.getScopeGroupId())) {
211    
212                            try {
213                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
214    
215                                    LayoutTypePortlet layoutTypePortlet =
216                                            (LayoutTypePortlet)layout.getLayoutType();
217    
218                                    for (String portletId : portletIds) {
219                                            if (!layoutTypePortlet.hasPortletId(portletId, false) ||
220                                                    !LayoutPermissionUtil.contains(
221                                                            themeDisplay.getPermissionChecker(), layout,
222                                                            ActionKeys.VIEW)) {
223    
224                                                    continue;
225                                            }
226    
227                                            portletId = getPortletId(layoutTypePortlet, portletId);
228    
229                                            return new Object[] {plid, portletId};
230                                    }
231                            }
232                            catch (NoSuchLayoutException nsle) {
233                            }
234                    }
235    
236                    Object[] plidAndPortletId = fetchPlidAndPortletId(
237                            themeDisplay.getPermissionChecker(), groupId, portletIds);
238    
239                    if ((plidAndPortletId == null) &&
240                            SitesUtil.isUserGroupLayoutSetViewable(
241                                    themeDisplay.getPermissionChecker(),
242                                    themeDisplay.getScopeGroup())) {
243    
244                            plidAndPortletId = fetchPlidAndPortletId(
245                                    themeDisplay.getPermissionChecker(),
246                                    themeDisplay.getScopeGroupId(), portletIds);
247                    }
248    
249                    if (plidAndPortletId != null) {
250                            return plidAndPortletId;
251                    }
252    
253                    StringBundler sb = new StringBundler(portletIds.length * 2 + 5);
254    
255                    sb.append("{groupId=");
256                    sb.append(groupId);
257                    sb.append(", plid=");
258                    sb.append(plid);
259    
260                    for (String portletId : portletIds) {
261                            sb.append(", portletId=");
262                            sb.append(portletId);
263                    }
264    
265                    sb.append("}");
266    
267                    throw new NoSuchLayoutException(sb.toString());
268            }
269    
270            protected static String getPortletId(
271                    LayoutTypePortlet layoutTypePortlet, String portletId) {
272    
273                    for (String curPortletId : layoutTypePortlet.getPortletIds()) {
274                            String curRootPortletId = PortletConstants.getRootPortletId(
275                                    curPortletId);
276    
277                            if (portletId.equals(curRootPortletId)) {
278                                    return curPortletId;
279                            }
280                    }
281    
282                    return portletId;
283            }
284    
285            protected static Layout setTargetLayout(
286                            HttpServletRequest request, long groupId, long plid)
287                    throws Exception {
288    
289                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
290                            WebKeys.THEME_DISPLAY);
291    
292                    PermissionChecker permissionChecker =
293                            themeDisplay.getPermissionChecker();
294    
295                    Group group = GroupLocalServiceUtil.getGroup(groupId);
296                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
297    
298                    if ((groupId == layout.getGroupId()) ||
299                            (group.getParentGroupId() == layout.getGroupId()) ||
300                            (layout.isPrivateLayout() &&
301                             !SitesUtil.isUserGroupLayoutSetViewable(
302                                     permissionChecker, layout.getGroup()))) {
303    
304                            return layout;
305                    }
306    
307                    layout = new VirtualLayout(layout, group);
308    
309                    request.setAttribute(WebKeys.LAYOUT, layout);
310    
311                    return layout;
312            }
313    
314            protected BaseFindActionHelper() {
315                    _portletIds = initPortletIds();
316    
317                    if (ArrayUtil.isEmpty(_portletIds)) {
318                            throw new RuntimeException("Portlet IDs cannot be null or empty");
319                    }
320            }
321    
322            protected abstract void addRequiredParameters(
323                    HttpServletRequest request, String portletId, PortletURL portletURL);
324    
325            private static final Log _log = LogFactoryUtil.getLog(
326                    BaseFindActionHelper.class);
327    
328            private final String[] _portletIds;
329    
330    }