View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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.ServletException;
32  
33  import org.eclipse.jetty.servlet.Holder.Source;
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                 _filter=((ServletContextHandler.Context)_servletHandler.getServletContext()).createFilter(getHeldClass());
106             }
107             catch (ServletException se)
108             {
109                 Throwable cause = se.getRootCause();
110                 if (cause instanceof InstantiationException)
111                     throw (InstantiationException)cause;
112                 if (cause instanceof IllegalAccessException)
113                     throw (IllegalAccessException)cause;
114                 throw se;
115             }
116         }
117         
118         _config=new Config();
119         _filter.init(_config);
120     }
121 
122     /* ------------------------------------------------------------ */
123     @Override
124     public void doStop()
125         throws Exception
126     {      
127         if (_filter!=null)
128         {
129             try
130             {
131                 destroyInstance(_filter);
132             }
133             catch (Exception e)
134             {
135                 LOG.warn(e);
136             }
137         }
138         if (!_extInstance)
139             _filter=null;
140         
141         _config=null;
142         super.doStop();   
143     }
144 
145     /* ------------------------------------------------------------ */
146     @Override
147     public void destroyInstance (Object o)
148         throws Exception
149     {
150         if (o==null)
151             return;
152         Filter f = (Filter)o;
153         f.destroy();
154         getServletHandler().destroyFilter(f);
155     }
156 
157     /* ------------------------------------------------------------ */
158     public synchronized void setFilter(Filter filter)
159     {
160         _filter=filter;
161         _extInstance=true;
162         setHeldClass(filter.getClass());
163         if (getName()==null)
164             setName(filter.getClass().getName());
165     }
166     
167     /* ------------------------------------------------------------ */
168     public Filter getFilter()
169     {
170         return _filter;
171     }
172 
173     /* ------------------------------------------------------------ */
174     @Override
175     public String toString()
176     {
177         return getName();
178     }
179     
180     /* ------------------------------------------------------------ */
181     public FilterRegistration.Dynamic getRegistration()
182     {
183         if (_registration == null)
184             _registration = new Registration();
185         return _registration;
186     }
187     
188     /* ------------------------------------------------------------ */
189     /* ------------------------------------------------------------ */
190     /* ------------------------------------------------------------ */
191     protected class Registration extends HolderRegistration implements FilterRegistration.Dynamic
192     {
193         public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames)
194         {
195             illegalStateIfContextStarted();
196             FilterMapping mapping = new FilterMapping();
197             mapping.setFilterHolder(FilterHolder.this);
198             mapping.setServletNames(servletNames);
199             mapping.setDispatcherTypes(dispatcherTypes);
200             if (isMatchAfter)
201                 _servletHandler.addFilterMapping(mapping);
202             else
203                 _servletHandler.prependFilterMapping(mapping);
204         }
205 
206         public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns)
207         {
208             illegalStateIfContextStarted();
209             FilterMapping mapping = new FilterMapping();
210             mapping.setFilterHolder(FilterHolder.this);
211             mapping.setPathSpecs(urlPatterns);
212             mapping.setDispatcherTypes(dispatcherTypes);
213             if (isMatchAfter)
214                 _servletHandler.addFilterMapping(mapping);
215             else
216                 _servletHandler.prependFilterMapping(mapping);
217         }
218 
219         public Collection<String> getServletNameMappings()
220         {
221             FilterMapping[] mappings =_servletHandler.getFilterMappings();
222             List<String> names=new ArrayList<String>();
223             for (FilterMapping mapping : mappings)
224             {
225                 if (mapping.getFilterHolder()!=FilterHolder.this)
226                     continue;
227                 String[] servlets=mapping.getServletNames();
228                 if (servlets!=null && servlets.length>0)
229                     names.addAll(Arrays.asList(servlets));
230             }
231             return names;
232         }
233 
234         public Collection<String> getUrlPatternMappings()
235         {
236             FilterMapping[] mappings =_servletHandler.getFilterMappings();
237             List<String> patterns=new ArrayList<String>();
238             for (FilterMapping mapping : mappings)
239             {
240                 if (mapping.getFilterHolder()!=FilterHolder.this)
241                     continue;
242                 String[] specs=mapping.getPathSpecs();
243                 patterns.addAll(TypeUtil.asList(specs));
244             }
245             return patterns;
246         }
247     }
248 
249     /* ------------------------------------------------------------ */
250     /* ------------------------------------------------------------ */
251     /* ------------------------------------------------------------ */
252     class Config extends HolderConfig implements FilterConfig
253     {
254         /* ------------------------------------------------------------ */
255         public String getFilterName()
256         {
257             return _name;
258         }
259     }
260 }