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;
016    
017    import com.liferay.portal.kernel.portlet.DynamicActionRequest;
018    import com.liferay.portal.kernel.portlet.DynamicResourceRequest;
019    import com.liferay.portal.kernel.util.SetUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.io.IOException;
024    
025    import java.util.Objects;
026    import java.util.Set;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    import javax.portlet.PortletException;
031    import javax.portlet.ResourceRequest;
032    import javax.portlet.ResourceResponse;
033    import javax.portlet.filter.ActionFilter;
034    import javax.portlet.filter.FilterChain;
035    import javax.portlet.filter.FilterConfig;
036    import javax.portlet.filter.ResourceFilter;
037    
038    /**
039     * @author Julio Camarero
040     */
041    public class CheckboxParametersPortletFilter
042            implements ActionFilter, ResourceFilter {
043    
044            @Override
045            public void destroy() {
046            }
047    
048            @Override
049            public void doFilter(
050                            ActionRequest actionRequest, ActionResponse actionResponse,
051                            FilterChain filterChain)
052                    throws IOException, PortletException {
053    
054                    String checkboxNames = actionRequest.getParameter("checkboxNames");
055    
056                    if (Validator.isNull(checkboxNames)) {
057                            filterChain.doFilter(actionRequest, actionResponse);
058    
059                            return;
060                    }
061    
062                    DynamicActionRequest dynamicActionRequest = new DynamicActionRequest(
063                            actionRequest);
064    
065                    Set<String> parameterNames = SetUtil.fromEnumeration(
066                            actionRequest.getParameterNames());
067    
068                    for (String checkboxName : StringUtil.split(checkboxNames)) {
069                            if (!parameterNames.contains(checkboxName)) {
070                                    dynamicActionRequest.setParameter(
071                                            checkboxName, Boolean.FALSE.toString());
072                            }
073                            else {
074                                    String value = dynamicActionRequest.getParameter(checkboxName);
075    
076                                    if (Objects.equals(value, "on")) {
077                                            dynamicActionRequest.setParameter(
078                                                    checkboxName, Boolean.TRUE.toString());
079                                    }
080                            }
081                    }
082    
083                    filterChain.doFilter(dynamicActionRequest, actionResponse);
084            }
085    
086            @Override
087            public void doFilter(
088                            ResourceRequest resourceRequest, ResourceResponse resourceResponse,
089                            FilterChain filterChain)
090                    throws IOException, PortletException {
091    
092                    String checkboxNames = resourceRequest.getParameter("checkboxNames");
093    
094                    if (Validator.isNull(checkboxNames)) {
095                            filterChain.doFilter(resourceRequest, resourceResponse);
096    
097                            return;
098                    }
099    
100                    DynamicResourceRequest dynamicResourceRequest =
101                            new DynamicResourceRequest(resourceRequest);
102    
103                    Set<String> parameterNames = SetUtil.fromEnumeration(
104                            resourceRequest.getParameterNames());
105    
106                    for (String checkboxName : StringUtil.split(checkboxNames)) {
107                            if (!parameterNames.contains(checkboxName)) {
108                                    dynamicResourceRequest.setParameter(
109                                            checkboxName, Boolean.FALSE.toString());
110                            }
111                            else {
112                                    String value = dynamicResourceRequest.getParameter(
113                                            checkboxName);
114    
115                                    if (Objects.equals(value, "on")) {
116                                            dynamicResourceRequest.setParameter(
117                                                    checkboxName, Boolean.TRUE.toString());
118                                    }
119                            }
120                    }
121    
122                    filterChain.doFilter(dynamicResourceRequest, resourceResponse);
123            }
124    
125            @Override
126            public void init(FilterConfig filterConfig) {
127            }
128    
129    }