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