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