View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.portal.service.filter;
35  
36  import fr.paris.lutece.portal.service.init.LuteceInitException;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.List;
43  
44  import javax.servlet.Filter;
45  import javax.servlet.ServletContext;
46  
47  
48  /**
49   * FilterService
50   */
51  public final class FilterService
52  {
53      private static FilterService _singleton = new FilterService(  );
54      private static ServletContext _context;
55      private List<LuteceFilter> _listFilters = new ArrayList<LuteceFilter>(  );
56  
57      /**
58       * Private constructor
59       */
60      private FilterService(  )
61      {
62      }
63  
64      /**
65       * Return the unique instance
66       * @return The instance
67       */
68      public static FilterService getInstance(  )
69      {
70          return _singleton;
71      }
72  
73      /**
74       * Register a filter
75       * @param entry The filter entry defined in the plugin's XML file
76       * @param plugin The plugin
77       */
78      public void registerFilter( FilterEntry entry, Plugin plugin )
79      {
80          try
81          {
82              Filter filter = (Filter) Class.forName( entry.getFilterClass(  ) ).newInstance(  );
83              LuteceFilter f = new LuteceFilter( entry.getName(  ), filter, entry.getMappingUrlPattern(  ), plugin,
84                      entry.getInitParameters(  ) );
85              f.setOrder( entry.getOrder(  ) );
86              _listFilters.add( f );
87              AppLogService.info( "New Filter registered : " + entry.getName(  ) );
88  
89              for ( String strKey : entry.getInitParameters(  ).keySet(  ) )
90              {
91                  AppLogService.info( " * init parameter - name : '" + strKey + "' - value : '" +
92                      entry.getInitParameters(  ).get( strKey ) + "'" );
93              }
94          }
95          catch ( InstantiationException e )
96          {
97              AppLogService.error( "Error registering a filter : " + e.getMessage(  ), e );
98          }
99          catch ( IllegalAccessException e )
100         {
101             AppLogService.error( "Error registering a filter : " + e.getMessage(  ), e );
102         }
103         catch ( ClassNotFoundException e )
104         {
105             AppLogService.error( "Error registering a filter : " + e.getMessage(  ), e );
106         }
107     }
108 
109     /**
110      * Defines the servlet context used by the FilterConfig given to the filters
111      * @param servletContext The context
112      */
113     public static void setServletContext( ServletContext servletContext )
114     {
115         _context = servletContext;
116     }
117 
118     /**
119      * Initializes filters
120      * @param context The context
121      * @throws LuteceInitException If an error occurs
122      */
123     public static void init( ServletContext context ) throws LuteceInitException
124     {
125         _context = context;
126         AppLogService.info( "Initialize plugins filters" );
127 
128         for ( LuteceFilter filter : FilterService.getInstance(  ).getFilters(  ) )
129         {
130             // Catch exception for each filter to execute all chain
131             try
132             {
133                 if ( filter.getPlugin(  ).isInstalled(  ) )
134                 {
135                     // Create a FilterConfig wrapper to provide init parameters to the filter
136                     LuteceFilterConfig filterConfig = new LuteceFilterConfig( filter.getName(  ), _context,
137                             filter.getInitParameters(  ) );
138                     filter.getFilter(  ).init( filterConfig );
139                     AppLogService.info( " * filter '" + filter.getName(  ) + "' from plugin " +
140                         filter.getPlugin(  ).getName(  ) + " initialized." );
141                 }
142             }
143             catch ( Exception e )
144             {
145                 AppLogService.error( "Error execution init() method - Filter " + filter.getName(  ), e );
146                 throw new LuteceInitException( "Error execution init() method - Filter " + filter.getName(  ), e );
147             }
148         }
149 
150         sortFilters(  );
151 
152         if ( AppLogService.isDebugEnabled(  ) )
153         {
154             AppLogService.debug( "Displaying filters order" );
155 
156             for ( LuteceFilter filter : FilterService.getInstance(  ).getFilters(  ) )
157             {
158                 AppLogService.debug( filter.getName(  ) + " - order = " + filter.getOrder(  ) );
159             }
160         }
161     }
162 
163     /**
164      * Sort filters.
165      */
166     public static void sortFilters(  )
167     {
168         // sort the filter's list
169         Collections.sort( FilterService.getInstance(  ).getFilters(  ) );
170     }
171 
172     /**
173      * Gives the filters list
174      * @return The list of filters
175      */
176     public List<LuteceFilter> getFilters(  )
177     {
178         return _listFilters;
179     }
180 }