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.util.servlet.filters;
016    
017    import java.util.Collections;
018    import java.util.Enumeration;
019    import java.util.LinkedHashMap;
020    import java.util.Map;
021    
022    import javax.servlet.FilterConfig;
023    import javax.servlet.ServletContext;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Bruno Farache
028     */
029    public class DynamicFilterConfig implements FilterConfig {
030    
031            public DynamicFilterConfig(FilterConfig filterConfig) {
032                    this(null, null);
033    
034                    Enumeration<String> enu = filterConfig.getInitParameterNames();
035    
036                    while (enu.hasMoreElements()) {
037                            String name = enu.nextElement();
038    
039                            addInitParameter(name, filterConfig.getInitParameter(name));
040                    }
041            }
042    
043            public DynamicFilterConfig(
044                    String filterName, ServletContext servletContext) {
045    
046                    _filterName = filterName;
047                    _servletContext = servletContext;
048            }
049    
050            public void addInitParameter(String name, String value) {
051                    _parameters.put(name, value);
052            }
053    
054            @Override
055            public String getFilterName() {
056                    return _filterName;
057            }
058    
059            @Override
060            public String getInitParameter(String name) {
061                    return _parameters.get(name);
062            }
063    
064            @Override
065            public Enumeration<String> getInitParameterNames() {
066                    return Collections.enumeration(_parameters.keySet());
067            }
068    
069            @Override
070            public ServletContext getServletContext() {
071                    return _servletContext;
072            }
073    
074            private final String _filterName;
075            private final Map<String, String> _parameters = new LinkedHashMap<>();
076            private final ServletContext _servletContext;
077    
078    }