View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.servlet;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.EnumSet;
25  import java.util.List;
26  
27  import javax.servlet.DispatcherType;
28  import javax.servlet.Filter;
29  import javax.servlet.FilterConfig;
30  import javax.servlet.FilterRegistration;
31  import javax.servlet.ServletContext;
32  import javax.servlet.ServletException;
33  
34  import org.eclipse.jetty.util.TypeUtil;
35  import org.eclipse.jetty.util.log.Log;
36  import org.eclipse.jetty.util.log.Logger;
37  
38  /* --------------------------------------------------------------------- */
39  /**
40   *
41   */
42  public class FilterHolder extends Holder<Filter>
43  {
44      private static final Logger LOG = Log.getLogger(FilterHolder.class);
45  
46      /* ------------------------------------------------------------ */
47      private transient Filter _filter;
48      private transient Config _config;
49      private transient FilterRegistration.Dynamic _registration;
50  
51      /* ---------------------------------------------------------------- */
52      /** Constructor
53       */
54      public FilterHolder()
55      {
56          super (Source.EMBEDDED);
57      }
58  
59  
60      /* ---------------------------------------------------------------- */
61      /** Constructor
62       */
63      public FilterHolder(Holder.Source source)
64      {
65          super (source);
66      }
67  
68      /* ---------------------------------------------------------------- */
69      /** Constructor
70       */
71      public FilterHolder(Class<? extends Filter> filter)
72      {
73          super (Source.EMBEDDED);
74          setHeldClass(filter);
75      }
76  
77      /* ---------------------------------------------------------------- */
78      /** Constructor for existing filter.
79       */
80      public FilterHolder(Filter filter)
81      {
82          super (Source.EMBEDDED);
83          setFilter(filter);
84      }
85  
86      /* ------------------------------------------------------------ */
87      @Override
88      public void doStart()
89          throws Exception
90      {
91          super.doStart();
92  
93          if (!javax.servlet.Filter.class
94              .isAssignableFrom(_class))
95          {
96              String msg = _class+" is not a javax.servlet.Filter";
97              super.stop();
98              throw new IllegalStateException(msg);
99          }
100 
101         if (_filter==null)
102         {
103             try
104             {
105                 ServletContext context=_servletHandler.getServletContext();
106                 _filter=(context instanceof ServletContextHandler.Context)
107                     ?((ServletContextHandler.Context)context).createFilter(getHeldClass())
108                     :getHeldClass().newInstance();
109             }
110             catch (ServletException se)
111             {
112                 Throwable cause = se.getRootCause();
113                 if (cause instanceof InstantiationException)
114                     throw (InstantiationException)cause;
115                 if (cause instanceof IllegalAccessException)
116                     throw (IllegalAccessException)cause;
117                 throw se;
118             }
119         }
120 
121         _config=new Config();
122         LOG.debug("Filter.init {}",_filter);
123         _filter.init(_config);
124     }
125 
126     /* ------------------------------------------------------------ */
127     @Override
128     public void doStop()
129         throws Exception
130     {
131         if (_filter!=null)
132         {
133             try
134             {
135                 destroyInstance(_filter);
136             }
137             catch (Exception e)
138             {
139                 LOG.warn(e);
140             }
141         }
142         if (!_extInstance)
143             _filter=null;
144 
145         _config=null;
146         super.doStop();
147     }
148 
149     /* ------------------------------------------------------------ */
150     @Override
151     public void destroyInstance (Object o)
152         throws Exception
153     {
154         if (o==null)
155             return;
156         Filter f = (Filter)o;
157         f.destroy();
158         getServletHandler().destroyFilter(f);
159     }
160 
161     /* ------------------------------------------------------------ */
162     public synchronized void setFilter(Filter filter)
163     {
164         _filter=filter;
165         _extInstance=true;
166         setHeldClass(filter.getClass());
167         if (getName()==null)
168             setName(filter.getClass().getName());
169     }
170 
171     /* ------------------------------------------------------------ */
172     public Filter getFilter()
173     {
174         return _filter;
175     }
176 
177     /* ------------------------------------------------------------ */
178     @Override
179     public String toString()
180     {
181         return getName();
182     }
183 
184     /* ------------------------------------------------------------ */
185     public FilterRegistration.Dynamic getRegistration()
186     {
187         if (_registration == null)
188             _registration = new Registration();
189         return _registration;
190     }
191 
192     /* ------------------------------------------------------------ */
193     /* ------------------------------------------------------------ */
194     /* ------------------------------------------------------------ */
195     protected class Registration extends HolderRegistration implements FilterRegistration.Dynamic
196     {
197         public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames)
198         {
199             illegalStateIfContextStarted();
200             FilterMapping mapping = new FilterMapping();
201             mapping.setFilterHolder(FilterHolder.this);
202             mapping.setServletNames(servletNames);
203             mapping.setDispatcherTypes(dispatcherTypes);
204             if (isMatchAfter)
205                 _servletHandler.addFilterMapping(mapping);
206             else
207                 _servletHandler.prependFilterMapping(mapping);
208         }
209 
210         public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns)
211         {
212             illegalStateIfContextStarted();
213             FilterMapping mapping = new FilterMapping();
214             mapping.setFilterHolder(FilterHolder.this);
215             mapping.setPathSpecs(urlPatterns);
216             mapping.setDispatcherTypes(dispatcherTypes);
217             if (isMatchAfter)
218                 _servletHandler.addFilterMapping(mapping);
219             else
220                 _servletHandler.prependFilterMapping(mapping);
221         }
222 
223         public Collection<String> getServletNameMappings()
224         {
225             FilterMapping[] mappings =_servletHandler.getFilterMappings();
226             List<String> names=new ArrayList<String>();
227             for (FilterMapping mapping : mappings)
228             {
229                 if (mapping.getFilterHolder()!=FilterHolder.this)
230                     continue;
231                 String[] servlets=mapping.getServletNames();
232                 if (servlets!=null && servlets.length>0)
233                     names.addAll(Arrays.asList(servlets));
234             }
235             return names;
236         }
237 
238         public Collection<String> getUrlPatternMappings()
239         {
240             FilterMapping[] mappings =_servletHandler.getFilterMappings();
241             List<String> patterns=new ArrayList<String>();
242             for (FilterMapping mapping : mappings)
243             {
244                 if (mapping.getFilterHolder()!=FilterHolder.this)
245                     continue;
246                 String[] specs=mapping.getPathSpecs();
247                 patterns.addAll(TypeUtil.asList(specs));
248             }
249             return patterns;
250         }
251     }
252 
253     /* ------------------------------------------------------------ */
254     /* ------------------------------------------------------------ */
255     /* ------------------------------------------------------------ */
256     class Config extends HolderConfig implements FilterConfig
257     {
258         /* ------------------------------------------------------------ */
259         public String getFilterName()
260         {
261             return _name;
262         }
263     }
264 }