View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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   * FilterService
49   */
50  public final class FilterService
51  {
52      private static FilterServicelter/FilterService.html#FilterService">FilterService _singleton = new FilterService( );
53      private static ServletContext _context;
54      private List<LuteceFilter> _listFilters = new ArrayList<>( );
55  
56      /**
57       * Private constructor
58       */
59      private FilterService( )
60      {
61      }
62  
63      /**
64       * Return the unique instance
65       * 
66       * @return The instance
67       */
68      public static FilterService getInstance( )
69      {
70          return _singleton;
71      }
72  
73      /**
74       * Register a filter
75       * 
76       * @param entry
77       *            The filter entry defined in the plugin's XML file
78       * @param plugin
79       *            The plugin
80       */
81      public void registerFilter( FilterEntry entry, Plugin plugin )
82      {
83          try
84          {
85              Filter filter = (Filter) Class.forName( entry.getFilterClass( ) ).newInstance( );
86              LuteceFilterservice/filter/LuteceFilter.html#LuteceFilter">LuteceFilter f = new LuteceFilter( entry.getName( ), filter, entry.getMappingUrlPattern( ), plugin, entry.getInitParameters( ) );
87              f.setOrder( entry.getOrder( ) );
88              _listFilters.add( f );
89              AppLogService.info( "New Filter registered : {}", entry.getName( ) );
90  
91              for ( String strKey : entry.getInitParameters( ).keySet( ) )
92              {
93                  AppLogService.info( " * init parameter - name : '{}' - value : '{}'", ( ) -> strKey, ( ) -> entry.getInitParameters( ).get( strKey ) );
94              }
95          }
96          catch( ClassNotFoundException | IllegalAccessException | InstantiationException e )
97          {
98              AppLogService.error( "Error registering a filter : {}", e.getMessage( ), e );
99          }
100     }
101 
102     /**
103      * Defines the servlet context used by the FilterConfig given to the filters
104      * 
105      * @param servletContext
106      *            The context
107      */
108     public static void setServletContext( ServletContext servletContext )
109     {
110         _context = servletContext;
111     }
112 
113     /**
114      * Initializes filters
115      * 
116      * @param context
117      *            The context
118      * @throws LuteceInitException
119      *             If an error occurs
120      */
121     public static void init( ServletContext context ) throws LuteceInitException
122     {
123         _context = context;
124         AppLogService.info( "Initialize plugins filters" );
125 
126         for ( LuteceFilter filter : FilterService.getInstance( ).getFilters( ) )
127         {
128             // Catch exception for each filter to execute all chain
129             try
130             {
131                 if ( filter.getPlugin( ).isInstalled( ) )
132                 {
133                     // Create a FilterConfig wrapper to provide init parameters to the filter
134                     LuteceFilterConfigteceFilterConfig.html#LuteceFilterConfig">LuteceFilterConfig filterConfig = new LuteceFilterConfig( filter.getName( ), _context, filter.getInitParameters( ) );
135                     filter.getFilter( ).init( filterConfig );
136                     AppLogService.info( " * filter '{}' from plugin {} initialized.", filter.getName( ), filter.getPlugin( ).getName( ) );
137                 }
138             }
139             catch( Exception e )
140             {
141                 AppLogService.error( "Error execution init() method - Filter {}", filter.getName( ), e );
142                 throw new LuteceInitException( "Error execution init() method - Filter " + filter.getName( ), e );
143             }
144         }
145 
146         sortFilters( );
147 
148         if ( AppLogService.isDebugEnabled( ) )
149         {
150             AppLogService.debug( "Displaying filters order" );
151 
152             for ( LuteceFilter filter : FilterService.getInstance( ).getFilters( ) )
153             {
154                 AppLogService.debug( filter.getName( ) + " - order = " + filter.getOrder( ) );
155             }
156         }
157     }
158 
159     /**
160      * Sort filters.
161      */
162     public static void sortFilters( )
163     {
164         // sort the filter's list
165         Collections.sort( FilterService.getInstance( ).getFilters( ) );
166     }
167 
168     /**
169      * Gives the filters list
170      * 
171      * @return The list of filters
172      */
173     public List<LuteceFilter> getFilters( )
174     {
175         return _listFilters;
176     }
177 }