View Javadoc

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