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