001
014
015 package com.liferay.portlet.portletconfiguration.action;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.portlet.PortletConfigurationLayoutUtil;
020 import com.liferay.portal.kernel.servlet.SessionErrors;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.JavaConstants;
023 import com.liferay.portal.kernel.util.ParamUtil;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.model.Layout;
026 import com.liferay.portal.model.LayoutTypePortlet;
027 import com.liferay.portal.model.Portlet;
028 import com.liferay.portal.model.PublicRenderParameter;
029 import com.liferay.portal.security.auth.PrincipalException;
030 import com.liferay.portal.security.permission.ActionKeys;
031 import com.liferay.portal.security.permission.PermissionChecker;
032 import com.liferay.portal.service.PortletLocalServiceUtil;
033 import com.liferay.portal.service.permission.PortletPermissionUtil;
034 import com.liferay.portal.theme.ThemeDisplay;
035 import com.liferay.portal.util.PortalUtil;
036 import com.liferay.portal.util.WebKeys;
037 import com.liferay.portlet.PortletPreferencesFactoryUtil;
038 import com.liferay.portlet.portletconfiguration.util.ConfigurationActionRequest;
039 import com.liferay.portlet.portletconfiguration.util.ConfigurationRenderRequest;
040 import com.liferay.portlet.portletconfiguration.util.ConfigurationResourceRequest;
041 import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
042 import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
043 import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterIdentifierComparator;
044 import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterIdentifierConfigurationComparator;
045
046 import java.util.ArrayList;
047 import java.util.Collections;
048 import java.util.HashSet;
049 import java.util.List;
050 import java.util.Set;
051 import java.util.TreeSet;
052
053 import javax.portlet.ActionRequest;
054 import javax.portlet.PortletPreferences;
055 import javax.portlet.PortletRequest;
056 import javax.portlet.RenderRequest;
057 import javax.portlet.ResourceRequest;
058
059 import javax.servlet.ServletContext;
060 import javax.servlet.http.HttpServletRequest;
061
062
066 public class ActionUtil {
067
068 public static final String ACTION = "_ACTION_";
069
070 public static final String PRESELECTED = "_PRESELECTED_";
071
072 public static PortletPreferences getLayoutPortletSetup(
073 PortletRequest portletRequest, Portlet portlet)
074 throws SystemException {
075
076 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
077 WebKeys.THEME_DISPLAY);
078
079 Layout layout = themeDisplay.getLayout();
080
081 return PortletPreferencesFactoryUtil.getLayoutPortletSetup(
082 layout, portlet.getPortletId());
083 }
084
085 public static void getLayoutPublicRenderParameters(
086 PortletRequest portletRequest)
087 throws Exception {
088
089 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
090 WebKeys.THEME_DISPLAY);
091
092 Set<String> identifiers = new HashSet<String>();
093
094 Set<PublicRenderParameter> publicRenderParameters =
095 new TreeSet<PublicRenderParameter>(
096 new PublicRenderParameterIdentifierComparator());
097
098 LayoutTypePortlet layoutTypePortlet =
099 themeDisplay.getLayoutTypePortlet();
100
101 for (Portlet portlet : layoutTypePortlet.getAllPortlets()) {
102 for (PublicRenderParameter publicRenderParameter :
103 portlet.getPublicRenderParameters()) {
104
105 if (!identifiers.contains(
106 publicRenderParameter.getIdentifier())) {
107
108 identifiers.add(publicRenderParameter.getIdentifier());
109
110 publicRenderParameters.add(publicRenderParameter);
111 }
112 }
113 }
114
115 portletRequest.setAttribute(
116 WebKeys.PUBLIC_RENDER_PARAMETERS, publicRenderParameters);
117 }
118
119 public static void getPublicRenderParameterConfigurationList(
120 PortletRequest portletRequest, Portlet portlet)
121 throws Exception {
122
123 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
124 WebKeys.THEME_DISPLAY);
125
126 Layout layout = themeDisplay.getLayout();
127
128 PortletPreferences preferences =
129 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
130 layout, portlet.getPortletId());
131
132 List<PublicRenderParameterConfiguration>
133 publicRenderParameterConfigurations =
134 new ArrayList<PublicRenderParameterConfiguration>();
135
136 for (PublicRenderParameter publicRenderParameter :
137 portlet.getPublicRenderParameters()) {
138
139 String mappingKey =
140 PublicRenderParameterConfiguration.getMappingKey(
141 publicRenderParameter);
142 String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
143 publicRenderParameter);
144
145 String mappingValue = null;
146 boolean ignoreValue = false;
147
148 if (SessionErrors.isEmpty(portletRequest)) {
149 mappingValue = preferences.getValue(mappingKey, null);
150 ignoreValue = GetterUtil.getBoolean(
151 preferences.getValue(ignoreKey, null));
152 }
153 else {
154 mappingValue = ParamUtil.getString(portletRequest, mappingKey);
155 ignoreValue = GetterUtil.getBoolean(
156 ParamUtil.getString(portletRequest, ignoreKey));
157 }
158
159 publicRenderParameterConfigurations.add(
160 new PublicRenderParameterConfiguration(
161 publicRenderParameter, mappingValue, ignoreValue));
162 }
163
164 Collections.sort(
165 publicRenderParameterConfigurations,
166 new PublicRenderParameterIdentifierConfigurationComparator());
167
168 portletRequest.setAttribute(
169 WebKeys.PUBLIC_RENDER_PARAMETER_CONFIGURATIONS,
170 publicRenderParameterConfigurations);
171 }
172
173 public static ActionRequest getWrappedActionRequest(
174 ActionRequest actionRequest, PortletPreferences portletPreferences)
175 throws PortalException, SystemException {
176
177 HttpServletRequest request = PortalUtil.getHttpServletRequest(
178 actionRequest);
179
180 portletPreferences = getPortletPreferences(
181 request, actionRequest.getPreferences(), portletPreferences);
182
183 return new ConfigurationActionRequest(
184 actionRequest, portletPreferences);
185 }
186
187 public static RenderRequest getWrappedRenderRequest(
188 RenderRequest renderRequest, PortletPreferences portletPreferences)
189 throws PortalException, SystemException {
190
191 HttpServletRequest request = PortalUtil.getHttpServletRequest(
192 renderRequest);
193
194 portletPreferences = getPortletPreferences(
195 request, renderRequest.getPreferences(), portletPreferences);
196
197 renderRequest = new ConfigurationRenderRequest(
198 renderRequest, portletPreferences);
199
200 request.setAttribute(
201 JavaConstants.JAVAX_PORTLET_REQUEST, renderRequest);
202
203 return renderRequest;
204 }
205
206 public static ResourceRequest getWrappedResourceRequest(
207 ResourceRequest resourceRequest,
208 PortletPreferences portletPreferences)
209 throws PortalException, SystemException {
210
211 HttpServletRequest request = PortalUtil.getHttpServletRequest(
212 resourceRequest);
213
214 portletPreferences = getPortletPreferences(
215 request, resourceRequest.getPreferences(), portletPreferences);
216
217 return new ConfigurationResourceRequest(
218 resourceRequest, portletPreferences);
219 }
220
221 protected static String checkPortletConfigurationPermission(
222 PortletRequest portletRequest)
223 throws Exception {
224
225 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
226 WebKeys.THEME_DISPLAY);
227
228 PermissionChecker permissionChecker =
229 themeDisplay.getPermissionChecker();
230
231 String portletId = ParamUtil.getString(
232 portletRequest, "portletResource");
233
234 Layout layout = PortletConfigurationLayoutUtil.getLayout(themeDisplay);
235
236 if (!PortletPermissionUtil.contains(
237 permissionChecker, themeDisplay.getScopeGroupId(), layout,
238 portletId, ActionKeys.CONFIGURATION)) {
239
240 throw new PrincipalException();
241 }
242
243 return portletId;
244 }
245
246 protected static Portlet getPortlet(PortletRequest portletRequest)
247 throws Exception {
248
249 long companyId = PortalUtil.getCompanyId(portletRequest);
250
251 String portletId = checkPortletConfigurationPermission(portletRequest);
252
253 return PortletLocalServiceUtil.getPortletById(companyId, portletId);
254 }
255
256 protected static PortletPreferences getPortletPreferences(
257 HttpServletRequest request,
258 PortletPreferences portletConfigPortletPreferences,
259 PortletPreferences portletPreferences)
260 throws PortalException, SystemException {
261
262 String portletResource = ParamUtil.getString(
263 request, "portletResource");
264
265 if (Validator.isNull(portletResource)) {
266 return portletConfigPortletPreferences;
267 }
268
269 if (portletPreferences != null) {
270 return portletPreferences;
271 }
272
273 return PortletPreferencesFactoryUtil.getPortletSetup(
274 request, portletResource);
275 }
276
277 protected static String getTitle(
278 Portlet portlet, RenderRequest renderRequest)
279 throws Exception {
280
281 ServletContext servletContext =
282 (ServletContext)renderRequest.getAttribute(WebKeys.CTX);
283
284 ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
285 WebKeys.THEME_DISPLAY);
286
287 HttpServletRequest request = PortalUtil.getHttpServletRequest(
288 renderRequest);
289
290 PortletPreferences portletPreferences = getLayoutPortletSetup(
291 renderRequest, portlet);
292
293 portletPreferences = getPortletPreferences(
294 request, renderRequest.getPreferences(), portletPreferences);
295
296 String title = PortletConfigurationUtil.getPortletTitle(
297 portletPreferences, themeDisplay.getLanguageId());
298
299 if (Validator.isNull(title)) {
300 title = PortalUtil.getPortletTitle(
301 portlet, servletContext, themeDisplay.getLocale());
302 }
303
304 return title;
305 }
306
307 }