001    /**
002     * Copyright (c) 2000-present 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.portlet.portletconfiguration.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.model.Layout;
019    import com.liferay.portal.kernel.model.LayoutTypePortlet;
020    import com.liferay.portal.kernel.model.Portlet;
021    import com.liferay.portal.kernel.model.PublicRenderParameter;
022    import com.liferay.portal.kernel.portlet.PortletConfigurationLayoutUtil;
023    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
024    import com.liferay.portal.kernel.security.auth.PrincipalException;
025    import com.liferay.portal.kernel.security.permission.ActionKeys;
026    import com.liferay.portal.kernel.security.permission.PermissionChecker;
027    import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
028    import com.liferay.portal.kernel.service.permission.PortletPermissionUtil;
029    import com.liferay.portal.kernel.servlet.SessionErrors;
030    import com.liferay.portal.kernel.theme.ThemeDisplay;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.JavaConstants;
033    import com.liferay.portal.kernel.util.ParamUtil;
034    import com.liferay.portal.kernel.util.PortalUtil;
035    import com.liferay.portal.kernel.util.Validator;
036    import com.liferay.portal.kernel.util.WebKeys;
037    import com.liferay.portlet.configuration.kernel.util.PortletConfigurationUtil;
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.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    /**
062     * @author Jorge Ferrer
063     * @author Raymond Aug??
064     */
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.MustHavePermission(
135                                    permissionChecker, Portlet.class.getName(), portletId,
136                                    ActionKeys.CONFIGURATION);
137                    }
138    
139                    return PortletLocalServiceUtil.getPortletById(
140                            themeDisplay.getCompanyId(), portletId);
141            }
142    
143            public static void getPublicRenderParameterConfigurationList(
144                            PortletRequest portletRequest, Portlet portlet)
145                    throws Exception {
146    
147                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
148                            WebKeys.THEME_DISPLAY);
149    
150                    Layout layout = themeDisplay.getLayout();
151    
152                    PortletPreferences preferences =
153                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
154                                    layout, portlet.getPortletId());
155    
156                    List<PublicRenderParameterConfiguration>
157                            publicRenderParameterConfigurations = new ArrayList<>();
158    
159                    for (PublicRenderParameter publicRenderParameter :
160                                    portlet.getPublicRenderParameters()) {
161    
162                            String mappingKey =
163                                    PublicRenderParameterConfiguration.getMappingKey(
164                                            publicRenderParameter);
165                            String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
166                                    publicRenderParameter);
167    
168                            String mappingValue = null;
169                            boolean ignoreValue = false;
170    
171                            if (SessionErrors.isEmpty(portletRequest)) {
172                                    mappingValue = preferences.getValue(mappingKey, null);
173                                    ignoreValue = GetterUtil.getBoolean(
174                                            preferences.getValue(ignoreKey, null));
175                            }
176                            else {
177                                    mappingValue = ParamUtil.getString(portletRequest, mappingKey);
178                                    ignoreValue = GetterUtil.getBoolean(
179                                            ParamUtil.getString(portletRequest, ignoreKey));
180                            }
181    
182                            publicRenderParameterConfigurations.add(
183                                    new PublicRenderParameterConfiguration(
184                                            publicRenderParameter, mappingValue, ignoreValue));
185                    }
186    
187                    Collections.sort(
188                            publicRenderParameterConfigurations,
189                            new PublicRenderParameterIdentifierConfigurationComparator());
190    
191                    portletRequest.setAttribute(
192                            WebKeys.PUBLIC_RENDER_PARAMETER_CONFIGURATIONS,
193                            publicRenderParameterConfigurations);
194            }
195    
196            public static String getTitle(Portlet portlet, RenderRequest renderRequest)
197                    throws Exception {
198    
199                    ServletContext servletContext =
200                            (ServletContext)renderRequest.getAttribute(WebKeys.CTX);
201    
202                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
203                            WebKeys.THEME_DISPLAY);
204    
205                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
206                            renderRequest);
207    
208                    PortletPreferences portletSetup = getLayoutPortletSetup(
209                            renderRequest, portlet);
210    
211                    portletSetup = getPortletSetup(
212                            request, renderRequest.getPreferences(), portletSetup);
213    
214                    String title = PortletConfigurationUtil.getPortletTitle(
215                            portletSetup, themeDisplay.getLanguageId());
216    
217                    if (Validator.isNull(title)) {
218                            title = PortalUtil.getPortletTitle(
219                                    portlet, servletContext, themeDisplay.getLocale());
220                    }
221    
222                    return title;
223            }
224    
225            public static ActionRequest getWrappedActionRequest(
226                            ActionRequest actionRequest, PortletPreferences portletPreferences)
227                    throws PortalException {
228    
229                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
230                            actionRequest);
231    
232                    portletPreferences = getPortletPreferences(
233                            request, actionRequest.getPreferences(), portletPreferences);
234    
235                    return new ConfigurationActionRequest(
236                            actionRequest, portletPreferences);
237            }
238    
239            public static RenderRequest getWrappedRenderRequest(
240                            RenderRequest renderRequest, PortletPreferences portletPreferences)
241                    throws PortalException {
242    
243                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
244                            renderRequest);
245    
246                    portletPreferences = getPortletPreferences(
247                            request, renderRequest.getPreferences(), portletPreferences);
248    
249                    renderRequest = new ConfigurationRenderRequest(
250                            renderRequest, portletPreferences);
251    
252                    request.setAttribute(
253                            JavaConstants.JAVAX_PORTLET_REQUEST, renderRequest);
254    
255                    return renderRequest;
256            }
257    
258            public static ResourceRequest getWrappedResourceRequest(
259                            ResourceRequest resourceRequest,
260                            PortletPreferences portletPreferences)
261                    throws PortalException {
262    
263                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
264                            resourceRequest);
265    
266                    portletPreferences = getPortletPreferences(
267                            request, resourceRequest.getPreferences(), portletPreferences);
268    
269                    return new ConfigurationResourceRequest(
270                            resourceRequest, portletPreferences);
271            }
272    
273            protected static PortletPreferences getPortletPreferences(
274                            HttpServletRequest request,
275                            PortletPreferences portletConfigPortletPreferences,
276                            PortletPreferences portletPreferences)
277                    throws PortalException {
278    
279                    String portletResource = ParamUtil.getString(
280                            request, "portletResource");
281    
282                    if (Validator.isNull(portletResource)) {
283                            return portletConfigPortletPreferences;
284                    }
285    
286                    if (portletPreferences != null) {
287                            return portletPreferences;
288                    }
289    
290                    return PortletPreferencesFactoryUtil.getPortletPreferences(
291                            request, portletResource);
292            }
293    
294            protected static PortletPreferences getPortletSetup(
295                            HttpServletRequest request,
296                            PortletPreferences portletConfigPortletSetup,
297                            PortletPreferences portletSetup)
298                    throws PortalException {
299    
300                    String portletResource = ParamUtil.getString(
301                            request, "portletResource");
302    
303                    if (Validator.isNull(portletResource)) {
304                            return portletConfigPortletSetup;
305                    }
306    
307                    if (portletSetup != null) {
308                            return portletSetup;
309                    }
310    
311                    return PortletPreferencesFactoryUtil.getPortletSetup(
312                            request, portletResource);
313            }
314    
315    }