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