001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.protocol.http.servlet;
018
019import java.util.Iterator;
020import java.util.List;
021
022import javax.servlet.FilterConfig;
023
024import org.apache.wicket.protocol.http.WebApplication;
025import org.apache.wicket.util.lang.Generics;
026
027/**
028 * A very simple manager for web filter factories.
029 * 
030 * @author Juergen Donnerstag
031 */
032public class FilterFactoryManager implements Iterable<AbstractRequestWrapperFactory>
033{
034        private List<AbstractRequestWrapperFactory> filters;
035
036        /**
037         * Construct.
038         */
039        public FilterFactoryManager()
040        {
041        }
042
043        /**
044         * Add a filter factory
045         * 
046         * @param wrapperFactory
047         * @return this
048         */
049        public final FilterFactoryManager add(final AbstractRequestWrapperFactory wrapperFactory)
050        {
051                if (wrapperFactory != null)
052                {
053                        if (filters == null)
054                        {
055                                filters = Generics.newArrayList(2);
056                        }
057
058                        filters.add(wrapperFactory);
059                }
060
061                return this;
062        }
063
064        /**
065         * Add a X-Forwarded web filter factory
066         * 
067         * @param config
068         *            If null, <code>WebApplication.get().getWicketFilter().getFilterConfig()</code>
069         *            will be called to retrieve the config.
070         * @return this
071         */
072        public final FilterFactoryManager addXForwardedRequestWrapperFactory(FilterConfig config)
073        {
074                if (config == null)
075                {
076                        config = WebApplication.get().getWicketFilter().getFilterConfig();
077                }
078
079                XForwardedRequestWrapperFactory factory = new XForwardedRequestWrapperFactory();
080                factory.init(config);
081
082                return add(factory);
083        }
084
085        /**
086         * Add a Secure remote address web filter factory
087         * 
088         * @param config
089         *            If null, <code>WebApplication.get().getWicketFilter().getFilterConfig()</code>
090         *            will be called to retrieve the config.
091         * @return this
092         */
093        public final FilterFactoryManager addSecuredRemoteAddressRequestWrapperFactory(
094                FilterConfig config)
095        {
096                if (config == null)
097                {
098                        config = WebApplication.get().getWicketFilter().getFilterConfig();
099                }
100
101                SecuredRemoteAddressRequestWrapperFactory factory = new SecuredRemoteAddressRequestWrapperFactory();
102                factory.init(config);
103
104                return add(factory);
105        }
106
107        /**
108         * @see java.lang.Iterable#iterator()
109         */
110        @Override
111        public Iterator<AbstractRequestWrapperFactory> iterator()
112        {
113                return filters.iterator();
114        }
115}