1 package fr.paris.lutece.portal.service.securityheader;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.Comparator;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Locale;
10 import java.util.Map;
11 import java.util.stream.Collectors;
12
13 import org.apache.logging.log4j.LogManager;
14 import org.apache.logging.log4j.Logger;
15
16 import fr.paris.lutece.portal.business.securityheader.SecurityHeader;
17 import fr.paris.lutece.portal.business.securityheader.SecurityHeaderHome;
18 import fr.paris.lutece.portal.business.securityheader.SecurityHeaderPageCategory;
19 import fr.paris.lutece.portal.business.securityheader.SecurityHeaderType;
20 import fr.paris.lutece.util.ReferenceList;
21
22
23
24
25 public class SecurityHeaderService
26 {
27
28
29
30
31 private Map<String, SecurityHeader> _mapSecurityHeaders = new HashMap<String, SecurityHeader>( );
32
33 Map<String, Map<String, List<SecurityHeader>>> _mapActiveSecurityHeadersForFilters = new HashMap<String, Map<String, List<SecurityHeader>>>( );
34
35
36
37 private boolean _bMapRefreshDone = false;
38
39 private Logger _logger = LogManager.getLogger( "lutece.securityHeader" );
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public List<SecurityHeader> find( String strName, String strType, String pageCategory )
54 {
55 ArrayList<SecurityHeader> securityHeadersResultList = new ArrayList<SecurityHeader>( );
56
57 for( SecurityHeader securityHeader : findAll( ) )
58 {
59 if( securityHeader.getName( ).equals( strName ) && securityHeader.getType( ).equals( strType ) )
60 {
61 if( pageCategory == null || securityHeader.getPageCategory( ).equals( pageCategory ) )
62 {
63 securityHeadersResultList.add( securityHeader );
64 }
65 }
66 }
67
68 return securityHeadersResultList;
69 }
70
71
72
73
74
75
76
77
78
79 public Collection<SecurityHeader> findActive( String strType, String strPageCategory )
80 {
81 if( !_bMapRefreshDone )
82 {
83 refreshSecurityHeadersMap( SecurityHeaderHome.findAll() );
84 }
85 if( _mapActiveSecurityHeadersForFilters.get(strType) != null )
86 {
87 return _mapActiveSecurityHeadersForFilters.get( strType ).get( strPageCategory );
88 }
89 return null;
90 }
91
92
93
94
95
96
97
98 public Collection<SecurityHeader> findAll( )
99 {
100 Collection<SecurityHeader> securityHeadersList = null;
101
102 if( _bMapRefreshDone )
103 {
104 securityHeadersList = _mapSecurityHeaders.values( );
105 }
106 else
107 {
108 securityHeadersList = SecurityHeaderHome.findAll( );
109 refreshSecurityHeadersMap( securityHeadersList );
110 }
111
112 return securityHeadersList;
113 }
114
115
116
117
118
119
120
121
122 public List<SecurityHeader> findAllSorted( Locale locale )
123 {
124 List<SecurityHeader> securityHeadersList = findAll( ).stream().collect( Collectors.toList( ) );
125 Collections.sort( securityHeadersList, Comparator.comparing( SecurityHeader::getType )
126 .thenComparing( SecurityHeader::getPageCategory, Comparator.nullsLast( Comparator.naturalOrder( ) ) )
127 .thenComparing( SecurityHeader::getName ) );
128
129 return securityHeadersList;
130 }
131
132
133
134
135
136
137 public ReferenceList getTypeList( )
138 {
139 ReferenceList listTypes = new ReferenceList( );
140
141 for (SecurityHeaderType type : SecurityHeaderType.values()) {
142 listTypes.addItem( type.getCode(), type.getCode() );
143 }
144
145 return listTypes;
146 }
147
148
149
150
151
152
153 public ReferenceList getPageCategoryList( )
154 {
155 ReferenceList listPageCategory = new ReferenceList( );
156
157 for (SecurityHeaderPageCategory pageCategory : SecurityHeaderPageCategory.values()) {
158 listPageCategory.addItem( pageCategory.getCode(), pageCategory.getCode() );
159 }
160
161 return listPageCategory;
162 }
163
164
165
166
167
168
169
170 public void create( SecurityHeader securityHeader )
171 {
172 _logger.debug( "Security header to create : name : {}, value : {}, type : {}, page category : {}", securityHeader.getName( ), securityHeader.getValue( ), securityHeader.getType( ), securityHeader.getPageCategory( ) );
173 SecurityHeaderHome.create( securityHeader );
174 clearMapSecurityHeaders( );
175 _logger.debug( "Security header created" );
176 }
177
178
179
180
181
182
183
184 public void update( SecurityHeader securityHeader )
185 {
186 _logger.debug( "Security header to update : id : {}, name : {}, value : {}, type : {}, page category : {}", securityHeader.getId( ), securityHeader.getName( ), securityHeader.getValue( ), securityHeader.getType( ), securityHeader.getPageCategory( ) );
187 SecurityHeaderHome.update( securityHeader );
188 clearMapSecurityHeaders( );
189 _logger.debug( "Security header updated" );
190 }
191
192
193
194
195
196
197
198 public void remove( int nSecurityHeaderId )
199 {
200 SecurityHeader securityHeader = _mapSecurityHeaders.get( String.valueOf( nSecurityHeaderId ) );
201 _logger.debug( "Security header to delete : id : {}, name : {}, value : {}, type : {}, page category : {}", securityHeader.getId( ), securityHeader.getName( ), securityHeader.getValue( ), securityHeader.getType( ), securityHeader.getPageCategory( ) );
202 SecurityHeaderHome.remove( nSecurityHeaderId );
203 clearMapSecurityHeaders( );
204 _logger.debug( "Security header deleted" );
205 }
206
207
208
209
210
211
212
213 public void enable( int nSecurityHeaderId )
214 {
215 SecurityHeader securityHeader = _mapSecurityHeaders.get( String.valueOf( nSecurityHeaderId ) );
216 _logger.debug( "Security header to enable : id : {}, name : {}, value : {}, type : {}, page category : {}", securityHeader.getId( ), securityHeader.getName( ), securityHeader.getValue( ), securityHeader.getType( ), securityHeader.getPageCategory( ) );
217 SecurityHeaderHome.updateIsActive( nSecurityHeaderId, true );
218 clearMapSecurityHeaders( );
219 _logger.debug( "Security header enabled" );
220 }
221
222
223
224
225
226
227
228 public void disable( int nSecurityHeaderId )
229 {
230 SecurityHeader securityHeader = _mapSecurityHeaders.get( String.valueOf( nSecurityHeaderId ) );
231 _logger.debug( "Security header to disable : id : {}, name : {}, value : {}, type : {}, page category : {}", securityHeader.getId( ), securityHeader.getName( ), securityHeader.getValue( ), securityHeader.getType( ), securityHeader.getPageCategory( ) );
232 SecurityHeaderHome.updateIsActive( nSecurityHeaderId, false );
233 clearMapSecurityHeaders( );
234 _logger.debug( "Security header disabled" );
235 }
236
237
238
239
240
241 private void clearMapSecurityHeaders( )
242 {
243 _mapSecurityHeaders.clear( );
244 _mapActiveSecurityHeadersForFilters.clear( );
245 _bMapRefreshDone = false;
246 _logger.debug( "Security header maps cleared" );
247 }
248
249
250
251
252
253
254
255 private void refreshSecurityHeadersMap( Collection<SecurityHeader> securityHeadersList )
256 {
257 for( SecurityHeader securityHeader : securityHeadersList )
258 {
259 _mapSecurityHeaders.put( String.valueOf( securityHeader.getId( ) ), securityHeader );
260
261 if( securityHeader.isActive() )
262 {
263 _mapActiveSecurityHeadersForFilters.put( securityHeader.getType( ), addHeaderToTypeMap( securityHeader ) );
264 }
265 }
266 _bMapRefreshDone = true;
267 _logger.debug( "Security header map refreshed" );
268 }
269
270
271
272
273
274
275
276 private Map<String, List<SecurityHeader>> addHeaderToTypeMap( SecurityHeader securityHeader )
277 {
278
279
280
281
282 String firstKey = securityHeader.getType( );
283 Map<String, List<SecurityHeader>> mapHeadersForType = _mapActiveSecurityHeadersForFilters.get( firstKey );
284 List<SecurityHeader> headersListToUpdate = null;
285
286 String secondKey = null;
287 if( securityHeader.getType( ).equals( SecurityHeaderType.PAGE.getCode( ) ) )
288 {
289 secondKey = securityHeader.getPageCategory( );
290 }
291
292 if( mapHeadersForType == null )
293 {
294 mapHeadersForType = new HashMap<String, List<SecurityHeader>>( );
295 headersListToUpdate = new ArrayList<SecurityHeader>( );
296 }
297 else
298 {
299 headersListToUpdate = mapHeadersForType.get( secondKey );
300 if( headersListToUpdate == null )
301 {
302 headersListToUpdate = new ArrayList<SecurityHeader>( );
303 }
304 }
305 headersListToUpdate.add( securityHeader );
306 mapHeadersForType.put( secondKey, headersListToUpdate );
307
308 return mapHeadersForType;
309 }
310 }