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.kernel.exception.NoSuchLayoutException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.model.Group;
021    import com.liferay.portal.kernel.model.Layout;
022    import com.liferay.portal.kernel.model.impl.VirtualLayout;
023    import com.liferay.portal.kernel.portlet.PortletLayoutFinder;
024    import com.liferay.portal.kernel.portlet.PortletURLFactoryUtil;
025    import com.liferay.portal.kernel.security.auth.PrincipalException;
026    import com.liferay.portal.kernel.security.permission.ActionKeys;
027    import com.liferay.portal.kernel.security.permission.PermissionChecker;
028    import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
029    import com.liferay.portal.kernel.service.LayoutLocalServiceUtil;
030    import com.liferay.portal.kernel.service.permission.LayoutPermissionUtil;
031    import com.liferay.portal.kernel.theme.ThemeDisplay;
032    import com.liferay.portal.kernel.util.HttpUtil;
033    import com.liferay.portal.kernel.util.ParamUtil;
034    import com.liferay.portal.kernel.util.PortalUtil;
035    import com.liferay.portal.kernel.util.Validator;
036    import com.liferay.portal.kernel.util.WebKeys;
037    import com.liferay.sites.kernel.util.SitesUtil;
038    
039    import javax.portlet.PortletMode;
040    import javax.portlet.PortletRequest;
041    import javax.portlet.PortletURL;
042    import javax.portlet.WindowState;
043    
044    import javax.servlet.http.HttpServletRequest;
045    import javax.servlet.http.HttpServletResponse;
046    
047    /**
048     * @author Adolfo P??rez
049     */
050    public abstract class BaseFindActionHelper implements FindActionHelper {
051    
052            @Override
053            public void execute(
054                            HttpServletRequest request, HttpServletResponse response)
055                    throws Exception {
056    
057                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
058                            WebKeys.THEME_DISPLAY);
059    
060                    try {
061                            long primaryKey = ParamUtil.getLong(
062                                    request, getPrimaryKeyParameterName());
063    
064                            long groupId = ParamUtil.getLong(
065                                    request, "groupId", themeDisplay.getScopeGroupId());
066    
067                            if (primaryKey > 0) {
068                                    try {
069                                            long overrideGroupId = getGroupId(primaryKey);
070    
071                                            if (overrideGroupId > 0) {
072                                                    groupId = overrideGroupId;
073                                            }
074                                    }
075                                    catch (Exception e) {
076                                            if (_log.isDebugEnabled()) {
077                                                    _log.debug(e, e);
078                                            }
079                                    }
080                            }
081    
082                            PortletLayoutFinder portletLayoutFinder = getPortletLayoutFinder();
083    
084                            PortletLayoutFinder.Result result = portletLayoutFinder.find(
085                                    themeDisplay, groupId);
086    
087                            long plid = result.getPlid();
088    
089                            Layout layout = setTargetLayout(request, groupId, plid);
090    
091                            LayoutPermissionUtil.check(
092                                    themeDisplay.getPermissionChecker(), layout, true,
093                                    ActionKeys.VIEW);
094    
095                            String portletId = result.getPortletId();
096    
097                            PortletURL portletURL = PortletURLFactoryUtil.create(
098                                    request, portletId, plid, PortletRequest.RENDER_PHASE);
099    
100                            addRequiredParameters(request, portletId, portletURL);
101    
102                            boolean inheritRedirect = ParamUtil.getBoolean(
103                                    request, "inheritRedirect");
104    
105                            String redirect = null;
106    
107                            if (inheritRedirect) {
108                                    String noSuchEntryRedirect = ParamUtil.getString(
109                                            request, "noSuchEntryRedirect");
110    
111                                    redirect = HttpUtil.getParameter(
112                                            noSuchEntryRedirect, "redirect", false);
113    
114                                    redirect = HttpUtil.decodeURL(redirect);
115                            }
116                            else {
117                                    redirect = ParamUtil.getString(request, "redirect");
118                            }
119    
120                            if (Validator.isNotNull(redirect)) {
121                                    portletURL.setParameter("redirect", redirect);
122                            }
123    
124                            setPrimaryKeyParameter(portletURL, primaryKey);
125    
126                            portletURL.setPortletMode(PortletMode.VIEW);
127                            portletURL.setWindowState(WindowState.NORMAL);
128    
129                            portletURL = processPortletURL(request, portletURL);
130    
131                            response.sendRedirect(portletURL.toString());
132                    }
133                    catch (Exception e) {
134                            String noSuchEntryRedirect = ParamUtil.getString(
135                                    request, "noSuchEntryRedirect");
136    
137                            noSuchEntryRedirect = PortalUtil.escapeRedirect(
138                                    noSuchEntryRedirect);
139    
140                            if (Validator.isNotNull(noSuchEntryRedirect) &&
141                                    (e instanceof NoSuchLayoutException ||
142                                     e instanceof PrincipalException)) {
143    
144                                    response.sendRedirect(noSuchEntryRedirect);
145                            }
146                            else {
147                                    PortalUtil.sendError(e, request, response);
148                            }
149                    }
150            }
151    
152            @Override
153            public abstract long getGroupId(long primaryKey) throws Exception;
154    
155            @Override
156            public abstract String getPrimaryKeyParameterName();
157    
158            @Override
159            public abstract PortletURL processPortletURL(
160                            HttpServletRequest request, PortletURL portletURL)
161                    throws Exception;
162    
163            @Override
164            public abstract void setPrimaryKeyParameter(
165                            PortletURL portletURL, long primaryKey)
166                    throws Exception;
167    
168            protected static Layout setTargetLayout(
169                            HttpServletRequest request, long groupId, long plid)
170                    throws Exception {
171    
172                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
173                            WebKeys.THEME_DISPLAY);
174    
175                    PermissionChecker permissionChecker =
176                            themeDisplay.getPermissionChecker();
177    
178                    Group group = GroupLocalServiceUtil.getGroup(groupId);
179                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
180    
181                    if ((groupId == layout.getGroupId()) ||
182                            (group.getParentGroupId() == layout.getGroupId()) ||
183                            (layout.isPrivateLayout() &&
184                             !SitesUtil.isUserGroupLayoutSetViewable(
185                                     permissionChecker, layout.getGroup()))) {
186    
187                            return layout;
188                    }
189    
190                    layout = new VirtualLayout(layout, group);
191    
192                    request.setAttribute(WebKeys.LAYOUT, layout);
193    
194                    return layout;
195            }
196    
197            protected abstract void addRequiredParameters(
198                    HttpServletRequest request, String portletId, PortletURL portletURL);
199    
200            protected abstract PortletLayoutFinder getPortletLayoutFinder();
201    
202            private static final Log _log = LogFactoryUtil.getLog(
203                    BaseFindActionHelper.class);
204    
205    }