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 java.io.IOException;
37
38 import javax.servlet.Filter;
39 import javax.servlet.FilterChain;
40 import javax.servlet.FilterConfig;
41 import javax.servlet.ServletException;
42 import javax.servlet.ServletRequest;
43 import javax.servlet.ServletResponse;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46
47 import fr.paris.lutece.portal.service.util.AppLogService;
48 import fr.paris.lutece.util.sql.TransactionManager;
49
50
51
52
53 public class MainFilter implements Filter
54 {
55
56
57
58 public void init( FilterConfig config ) throws ServletException
59 {
60 FilterService.setServletContext( config.getServletContext( ) );
61 }
62
63
64
65
66 public void doFilter( ServletRequest requestServlet, ServletResponse responseServlet, FilterChain chain ) throws IOException, ServletException
67 {
68 AppLogService.debug( "MainFilter : doFilter()" );
69
70 HttpServletRequest request = (HttpServletRequest) requestServlet;
71 HttpServletResponse response = (HttpServletResponse) responseServlet;
72 LuteceFilterChainlterChain.html#LuteceFilterChain">LuteceFilterChain chainPluginsFilters = new LuteceFilterChain( );
73
74 for ( LuteceFilter filter : FilterService.getInstance( ).getFilters( ) )
75 {
76 AppLogService.debug( "PluginFilter : {} - url pattern : {}", filter.getName( ), filter.getMappingUrlPattern( ) );
77
78
79 try
80 {
81
82 if ( matchMapping( filter, request ) && filter.getPlugin( ).isInstalled( ) )
83 {
84 chainPluginsFilters.setFollowChain( false );
85 filter.getFilter( ).doFilter( request, response, chainPluginsFilters );
86
87 if ( !chainPluginsFilters.shouldFollowChain( ) )
88 {
89
90 return;
91 }
92
93
94
95 request = (HttpServletRequest) chainPluginsFilters.getRequest( );
96 response = (HttpServletResponse) chainPluginsFilters.getResponse( );
97 }
98 }
99 catch( Exception e )
100 {
101 AppLogService.error( "Error execution doFilter method - Filter {}", filter.getName( ), e );
102 }
103 }
104
105
106 chain.doFilter( request, response );
107
108 TransactionManager.rollBackEveryTransaction( );
109 }
110
111
112
113
114 public void destroy( )
115 {
116 for ( LuteceFilter filter : FilterService.getInstance( ).getFilters( ) )
117 {
118
119 try
120 {
121
122 if ( filter.getPlugin( ).isInstalled( ) )
123 {
124 filter.getFilter( ).destroy( );
125 }
126 }
127 catch( Exception e )
128 {
129 AppLogService.error( "Error execution destroy() method - Filter {} ", filter.getName( ), e );
130 }
131 }
132 }
133
134
135
136
137
138
139
140
141
142
143 boolean matchMapping( LuteceFilter filter, HttpServletRequest request )
144 {
145 return matchFilterUrl( filter.getMappingUrlPattern( ), request.getServletPath( ) );
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159 boolean matchFilterUrl( String strUrlPattern, String strRequestUrl )
160 {
161 if ( strUrlPattern == null )
162 {
163 return ( false );
164 }
165
166 if ( strUrlPattern.equals( strRequestUrl ) )
167 {
168 return ( true );
169 }
170
171 if ( strUrlPattern.equals( "/*" ) )
172 {
173 return ( true );
174 }
175
176 if ( strUrlPattern.endsWith( "/*" ) )
177 {
178 if ( strUrlPattern.regionMatches( 0, strRequestUrl, 0, strUrlPattern.length( ) - 2 )
179 && ( strRequestUrl.length( ) == ( strUrlPattern.length( ) - 2 ) || '/' == strRequestUrl.charAt( strUrlPattern.length( ) - 2 ) ) )
180 {
181 return ( true );
182 }
183
184 return ( false );
185 }
186
187
188 if ( strUrlPattern.startsWith( "*." ) )
189 {
190 int slash = strRequestUrl.lastIndexOf( '/' );
191 int period = strRequestUrl.lastIndexOf( '.' );
192
193 if ( ( slash >= 0 ) && ( period > slash ) && ( period != ( strRequestUrl.length( ) - 1 ) )
194 && ( ( strRequestUrl.length( ) - period ) == ( strUrlPattern.length( ) - 1 ) ) )
195 {
196 return ( strUrlPattern.regionMatches( 2, strRequestUrl, period + 1, strUrlPattern.length( ) - 2 ) );
197 }
198 }
199
200
201 return ( false );
202 }
203 }