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