1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
58
59 private FilterService( )
60 {
61 }
62
63
64
65
66
67
68 public static FilterService getInstance( )
69 {
70 return _singleton;
71 }
72
73
74
75
76
77
78
79
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
104
105
106
107
108 public static void setServletContext( ServletContext servletContext )
109 {
110 _context = servletContext;
111 }
112
113
114
115
116
117
118
119
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
129 try
130 {
131 if ( filter.getPlugin( ).isInstalled( ) )
132 {
133
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
161
162 public static void sortFilters( )
163 {
164
165 Collections.sort( FilterService.getInstance( ).getFilters( ) );
166 }
167
168
169
170
171
172
173 public List<LuteceFilter> getFilters( )
174 {
175 return _listFilters;
176 }
177 }