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.util.mvc.admin;
35
36 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
37 import fr.paris.lutece.portal.service.i18n.I18nService;
38 import fr.paris.lutece.portal.service.template.AppTemplateService;
39 import fr.paris.lutece.portal.service.util.AppException;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41 import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
42 import fr.paris.lutece.portal.util.mvc.utils.MVCMessage;
43 import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
44 import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
45 import fr.paris.lutece.util.ErrorMessage;
46 import fr.paris.lutece.util.beanvalidation.ValidationError;
47 import fr.paris.lutece.util.html.HtmlTemplate;
48 import fr.paris.lutece.util.url.UrlItem;
49
50 import org.apache.log4j.Logger;
51
52 import org.springframework.util.ReflectionUtils;
53
54 import java.io.IOException;
55 import java.io.OutputStream;
56 import java.io.PrintWriter;
57 import java.io.Serializable;
58
59 import java.lang.reflect.InvocationTargetException;
60 import java.lang.reflect.Method;
61
62 import java.util.ArrayList;
63 import java.util.HashMap;
64 import java.util.List;
65 import java.util.Locale;
66 import java.util.Map;
67 import java.util.Map.Entry;
68
69 import javax.servlet.http.HttpServletRequest;
70 import javax.servlet.http.HttpServletResponse;
71
72
73
74
75
76 public abstract class MVCAdminJspBean extends PluginAdminPageJspBean implements Serializable
77 {
78 private static final String MARK_ERRORS = "errors";
79 private static final String MARK_INFOS = "infos";
80 private static Logger _logger = MVCUtils.getLogger( );
81 private List<ErrorMessage> _listErrors = new ArrayList<ErrorMessage>( );
82 private List<ErrorMessage> _listInfos = new ArrayList<ErrorMessage>( );
83 private Controller _controller = getClass( ).getAnnotation( Controller.class );
84 private HttpServletResponse _response;
85
86
87
88
89
90
91
92
93 public String processController( HttpServletRequest request, HttpServletResponse response )
94 throws AccessDeniedException
95 {
96 _response = response;
97 init( request, _controller.right( ) );
98
99 Method[] methods = ReflectionUtils.getAllDeclaredMethods( getClass( ) );
100
101 try
102 {
103
104 Method m = MVCUtils.findViewAnnotedMethod( request, methods );
105
106 if ( m != null )
107 {
108 return (String) m.invoke( this, request );
109 }
110
111
112 m = MVCUtils.findActionAnnotedMethod( request, methods );
113
114 if ( m != null )
115 {
116 return (String) m.invoke( this, request );
117 }
118
119
120 m = MVCUtils.findDefaultViewMethod( methods );
121
122 return (String) m.invoke( this, request );
123 }
124 catch ( InvocationTargetException e )
125 {
126 if ( e.getTargetException( ) instanceof AccessDeniedException )
127 {
128 throw (AccessDeniedException) e.getTargetException( );
129 }
130
131 throw new AppException( "MVC Error dispaching view and action ", e );
132 }
133 catch ( IllegalAccessException e )
134 {
135 throw new AppException( "MVC Error dispaching view and action ", e );
136 }
137 }
138
139
140
141
142
143
144
145
146 protected void addError( String strMessage )
147 {
148 _listErrors.add( new MVCMessage( strMessage ) );
149 }
150
151
152
153
154
155
156 protected void addError( String strMessageKey, Locale locale )
157 {
158 _listErrors.add( new MVCMessage( I18nService.getLocalizedString( strMessageKey, locale ) ) );
159 }
160
161
162
163
164
165 protected void addInfo( String strMessage )
166 {
167 _listInfos.add( new MVCMessage( strMessage ) );
168 }
169
170
171
172
173
174
175 protected void addInfo( String strMessageKey, Locale locale )
176 {
177 _listInfos.add( new MVCMessage( I18nService.getLocalizedString( strMessageKey, locale ) ) );
178 }
179
180
181
182
183
184 protected void fillCommons( Map<String, Object> model )
185 {
186 List<ErrorMessage> listErrors = new ArrayList<ErrorMessage>( _listErrors );
187 List<ErrorMessage> listInfos = new ArrayList<ErrorMessage>( _listInfos );
188 model.put( MARK_ERRORS, listErrors );
189 model.put( MARK_INFOS, listInfos );
190 _listErrors.clear( );
191 _listInfos.clear( );
192 }
193
194
195
196
197
198 protected Map<String, Object> getModel( )
199 {
200 Map<String, Object> model = new HashMap<String, Object>( );
201 fillCommons( model );
202
203 return model;
204 }
205
206
207
208
209
210
211 protected String getPage( String strTemplate )
212 {
213 String strPageTitleProperty = _controller.pageTitleProperty( );
214
215 return getPage( strPageTitleProperty, strTemplate, getModel( ) );
216 }
217
218
219
220
221
222
223
224 protected String getPage( String strPageTitleProperty, String strTemplate )
225 {
226 return getPage( strPageTitleProperty, strTemplate, getModel( ) );
227 }
228
229
230
231
232
233
234
235
236 protected String getPage( String strPageTitleProperty, String strTemplate, Map<String, Object> model )
237 {
238 setPageTitleProperty( strPageTitleProperty );
239
240 HtmlTemplate template = AppTemplateService.getTemplate( strTemplate, getLocale( ), model );
241
242 return getAdminPage( template.getHtml( ) );
243 }
244
245
246
247
248
249
250
251
252 protected <T> boolean validateBean( T bean, String strPrefix )
253 {
254 List<ValidationError> errors = validate( bean, strPrefix );
255
256 if ( errors.isEmpty( ) )
257 {
258 return true;
259 }
260
261 for ( ValidationError errorValidation : errors )
262 {
263 MVCMessage error = new MVCMessage( );
264 error.setMessage( errorValidation.getMessage( ) );
265 _listErrors.add( error );
266 }
267
268 return false;
269 }
270
271
272
273
274
275
276
277
278 protected String getControllerJsp( )
279 {
280 return _controller.controllerJsp( );
281 }
282
283
284
285
286
287 protected String getControllerPath( )
288 {
289 return _controller.controllerPath( );
290 }
291
292
293
294
295
296
297
298
299 protected String redirect( HttpServletRequest request, String strTarget )
300 {
301 try
302 {
303 _logger.debug( "Redirect :" + strTarget );
304 _response.sendRedirect( strTarget );
305 }
306 catch ( IOException e )
307 {
308 _logger.error( "Unable to redirect : " + strTarget + " : " + e.getMessage( ), e );
309 }
310
311 return null;
312 }
313
314
315
316
317
318
319
320
321
322 protected String redirect( HttpServletRequest request, String strView, String strParameter, int nValue )
323 {
324 UrlItem url = new UrlItem( getViewUrl( strView ) );
325 url.addParameter( strParameter, nValue );
326
327 return redirect( request, url.getUrl( ) );
328 }
329
330
331
332
333
334
335
336
337
338
339
340 protected String redirect( HttpServletRequest request, String strView, String strParameter1, int nValue1,
341 String strParameter2, int nValue2 )
342 {
343 UrlItem url = new UrlItem( getViewUrl( strView ) );
344 url.addParameter( strParameter1, nValue1 );
345 url.addParameter( strParameter2, nValue2 );
346
347 return redirect( request, url.getUrl( ) );
348 }
349
350
351
352
353
354
355
356
357
358
359 protected String redirect( HttpServletRequest request, String strView, Map<String, String> additionalParameters )
360 {
361 UrlItem url = new UrlItem( getViewUrl( strView ) );
362
363 if ( additionalParameters != null )
364 {
365 for ( Entry<String, String> entry : additionalParameters.entrySet( ) )
366 {
367 url.addParameter( entry.getKey( ), entry.getValue( ) );
368 }
369 }
370
371 return redirect( request, url.getUrl( ) );
372 }
373
374
375
376
377
378
379
380
381 protected String redirectView( HttpServletRequest request, String strView )
382 {
383 return redirect( request, getViewUrl( strView ) );
384 }
385
386
387
388
389
390
391 protected String getViewUrl( String strView )
392 {
393 UrlItem url = new UrlItem( getControllerJsp( ) );
394 url.addParameter( MVCUtils.PARAMETER_VIEW, strView );
395
396 return url.getUrl( );
397 }
398
399
400
401
402
403
404 protected String getViewFullUrl( String strView )
405 {
406 return getControllerPath( ) + getViewUrl( strView );
407 }
408
409
410
411
412
413
414 protected String getActionUrl( String strAction )
415 {
416 UrlItem url = new UrlItem( getControllerPath( ) + getControllerJsp( ) );
417 url.addParameter( MVCUtils.PARAMETER_ACTION, strAction );
418
419 return url.getUrl( );
420 }
421
422
423
424
425
426
427
428 protected void download( String strData, String strFilename, String strContentType )
429 {
430 HttpServletResponse response = _response;
431 PrintWriter out = null;
432 response.setHeader( "Content-Disposition", "attachment; filename=\"" + strFilename + "\";" );
433 MVCUtils.addDownloadHeaderToResponse( response, strFilename, strContentType );
434
435 try
436 {
437 out = response.getWriter( );
438 out.print( strData );
439 }
440 catch ( IOException e )
441 {
442 AppLogService.error( e.getStackTrace( ), e );
443 }
444 finally
445 {
446 if ( out != null )
447 {
448 out.close( );
449 }
450 }
451 }
452
453
454
455
456
457
458
459 protected void download( byte[] data, String strFilename, String strContentType )
460 {
461 HttpServletResponse response = _response;
462 OutputStream os;
463 MVCUtils.addDownloadHeaderToResponse( response, strFilename, strContentType );
464
465 try
466 {
467 os = response.getOutputStream( );
468 os.write( data );
469 os.close( );
470 }
471 catch ( IOException e )
472 {
473 AppLogService.error( e.getStackTrace( ), e );
474 }
475 }
476 }