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.plugins.directory.business;
35
36 import fr.paris.lutece.plugins.blobstore.service.BlobStoreClientException;
37 import fr.paris.lutece.plugins.directory.service.upload.DirectoryAsynchronousUploadHandler;
38 import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
39 import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
40 import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
41 import fr.paris.lutece.portal.service.fileupload.FileUploadService;
42 import fr.paris.lutece.portal.service.i18n.I18nService;
43 import fr.paris.lutece.portal.service.message.AdminMessage;
44 import fr.paris.lutece.portal.service.message.AdminMessageService;
45 import fr.paris.lutece.portal.service.plugin.Plugin;
46 import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
47 import fr.paris.lutece.portal.service.util.AppLogService;
48 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
49 import fr.paris.lutece.portal.web.util.LocalizedPaginator;
50 import fr.paris.lutece.util.ReferenceList;
51 import fr.paris.lutece.util.filesystem.FileSystemUtil;
52 import fr.paris.lutece.util.html.Paginator;
53 import fr.paris.lutece.util.xml.XmlUtil;
54
55 import org.apache.commons.fileupload.FileItem;
56 import org.apache.commons.lang.StringUtils;
57
58 import java.util.HashMap;
59 import java.util.List;
60 import java.util.Locale;
61 import java.util.Map;
62
63 import javax.servlet.http.HttpServletRequest;
64
65
66
67
68
69
70 public class EntryTypeDownloadUrl extends AbstractEntryTypeUpload
71 {
72
73 public static final String CONSTANT_WS_REST_URL = "ws_rest_url";
74 public static final String CONSTANT_BLOBSTORE = "blobstore";
75 public static final String CONSTANT_OPTION = "option";
76
77
78 private static final String PARAMETER_WS_REST_URL = "ws_rest_url";
79 private static final String PARAMETER_BLOBSTORE = "blobstore";
80
81
82 private static final String TAG_A = "a";
83 private static final String TAG_IMG = "img";
84
85
86 private static final String ATTRIBUTE_HREF = "href";
87 private static final String ATTRIBUTE_SRC = "src";
88 private static final String ATTRIBUTE_TITLE = "title";
89 private static final String ATTRIBUTE_ALT = "alt";
90
91
92 private static final String FIELD_WS_REST_URL = "directory.create_entry.label_ws_rest_url";
93 private static final String FIELD_BLOBSTORE = "directory.create_entry.label_blobstore";
94
95
96 private static final String URL_IMG_DOWNLOAD = "images/local/skin/plugins/directory/download.png";
97
98
99 private static final String PROPERTY_LABEL_DOWNLOAD = "directory.viewing_directory_record.download";
100 private static final String MESSAGE_ENTRY_NOT_WELL_CONFIGURED = "directory.message.error.entry_not_well_configured";
101
102
103 private final String _template_create = "admin/plugins/directory/entrytypedownloadurl/create_entry_type_download_url.html";
104 private final String _template_modify = "admin/plugins/directory/entrytypedownloadurl/modify_entry_type_download_url.html";
105 private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypedownloadurl/html_code_form_entry_type_download_url.html";
106 private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypedownloadurl/html_code_form_search_entry_type_download_url.html";
107 private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypedownloadurl/html_code_entry_value_type_download_url.html";
108 private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypedownloadurl/html_code_form_entry_type_download_url.html";
109 private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypedownloadurl/html_code_form_search_entry_type_download_url.html";
110 private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypedownloadurl/html_code_entry_value_type_download_url.html";
111
112
113
114
115 @Override
116 public String getTemplateHtmlFormEntry( boolean isDisplayFront )
117 {
118 if ( isDisplayFront )
119 {
120 return _template_html_front_code_form_entry;
121 }
122
123 return _template_html_code_form_entry;
124 }
125
126
127
128
129 @Override
130 public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
131 {
132 if ( isDisplayFront )
133 {
134 return _template_html_front_code_entry_value;
135 }
136
137 return _template_html_code_entry_value;
138 }
139
140
141
142
143 @Override
144 public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
145 {
146 if ( isDisplayFront )
147 {
148 return _template_html_front_code_form_search_entry;
149 }
150
151 return _template_html_code_form_search_entry;
152 }
153
154
155
156
157 @Override
158 public String getEntryData( HttpServletRequest request, Locale locale )
159 {
160 String strTitle = request.getParameter( PARAMETER_TITLE );
161 String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
162 String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
163 .trim( ) : null;
164 String strComment = request.getParameter( PARAMETER_COMMENT );
165 String strMandatory = request.getParameter( PARAMETER_MANDATORY );
166 String strIndexed = request.getParameter( PARAMETER_INDEXED );
167 String strDocumentTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
168 String strDocumentSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
169 String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
170 String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
171 String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
172 String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
173 String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
174 String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
175
176 String strFieldError = checkEntryData( request, locale );
177
178 if ( StringUtils.isNotBlank( strFieldError ) )
179 {
180 return strFieldError;
181 }
182
183 this.setTitle( strTitle );
184 this.setHelpMessage( strHelpMessage );
185 this.setHelpMessageSearch( strHelpMessageSearch );
186 this.setComment( strComment );
187
188 this.setFields( request );
189
190 this.setMandatory( strMandatory != null );
191 this.setIndexed( strIndexed != null );
192 this.setIndexedAsTitle( strDocumentTitle != null );
193 this.setIndexedAsSummary( strDocumentSummary != null );
194 this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
195 this.setShownInResultList( strShowInResultList != null );
196 this.setShownInResultRecord( strShowInResultRecord != null );
197 this.setShownInHistory( strShowInHistory != null );
198 this.setShownInExport( strShowInExport != null );
199 this.setShownInCompleteness( strShowInCompleteness != null );
200
201 return null;
202 }
203
204
205
206
207 @Override
208 public String getTemplateCreate( )
209 {
210 return _template_create;
211 }
212
213
214
215
216 @Override
217 public String getTemplateModify( )
218 {
219 return _template_modify;
220 }
221
222
223
224
225 @Override
226 public Paginator<RegularExpression> getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex )
227 {
228 return new Paginator<RegularExpression>( this.getFields( ).get( 0 ).getRegularExpressionList( ), nItemPerPage, strBaseUrl, strPageIndexParameterName,
229 strPageIndex );
230 }
231
232
233
234
235 @Override
236 public ReferenceList getReferenceListRegularExpression( IEntry entry, Plugin plugin )
237 {
238 ReferenceList refListRegularExpression = null;
239
240 if ( RegularExpressionService.getInstance( ).isAvailable( ) )
241 {
242 refListRegularExpression = new ReferenceList( );
243
244 List<RegularExpression> listRegularExpression = RegularExpressionService.getInstance( ).getAllRegularExpression( );
245
246 for ( RegularExpression regularExpression : listRegularExpression )
247 {
248 if ( !entry.getFields( ).get( 0 ).getRegularExpressionList( ).contains( regularExpression ) )
249 {
250 refListRegularExpression.addItem( regularExpression.getIdExpression( ), regularExpression.getTitle( ) );
251 }
252 }
253 }
254
255 return refListRegularExpression;
256 }
257
258
259
260
261 @Override
262 public void getRecordFieldData( Record record, HttpServletRequest request, boolean bTestDirectoryError, boolean bAddNewValue,
263 List<RecordField> listRecordField, Locale locale ) throws DirectoryErrorException
264 {
265 if ( request instanceof MultipartHttpServletRequest )
266 {
267
268 DirectoryAsynchronousUploadHandler handler = DirectoryAsynchronousUploadHandler.getHandler( );
269
270 if ( !handler.isBlobStoreClientServiceAvailable( ) )
271 {
272 String strErrorMessage = I18nService.getLocalizedString( MESSAGE_BLOBSTORE_CLIENT_SERVICE_UNAVAILABLE, locale );
273 throw new DirectoryErrorException( this.getTitle( ), strErrorMessage );
274 }
275
276
277 Field fieldWSRestUrl = DirectoryUtils.findFieldByTitleInTheList( CONSTANT_WS_REST_URL, getFields( ) );
278 Field fieldBlobStore = DirectoryUtils.findFieldByTitleInTheList( CONSTANT_BLOBSTORE, getFields( ) );
279 String strWSRestUrl = fieldWSRestUrl.getValue( );
280 String strBlobStore = fieldBlobStore.getValue( );
281
282 if ( bTestDirectoryError && ( StringUtils.isBlank( strWSRestUrl ) || StringUtils.isBlank( strBlobStore ) ) )
283 {
284 String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ENTRY_NOT_WELL_CONFIGURED, locale );
285 throw new DirectoryErrorException( this.getTitle( ), strErrorMessage );
286 }
287
288
289
290
291
292
293
294
295 List<FileItem> asynchronousFileItems = getFileSources( request );
296
297 if ( ( asynchronousFileItems != null ) && !asynchronousFileItems.isEmpty( ) )
298 {
299
300 String strUploadAction = DirectoryAsynchronousUploadHandler.getHandler( ).getUploadAction( request );
301
302 if ( StringUtils.isNotBlank( strUploadAction ) )
303 {
304
305
306
307 for ( FileItem fileItem : asynchronousFileItems )
308 {
309 RecordField recordField = new RecordField( );
310 recordField.setEntry( this );
311 recordField.setFileName( fileItem.getName( ) );
312 recordField.setFileExtension( fileItem.getContentType( ) );
313 listRecordField.add( recordField );
314 }
315 }
316 else
317 {
318
319
320 if ( bTestDirectoryError )
321 {
322 this.checkRecordFieldData( asynchronousFileItems, locale );
323 }
324
325
326 try
327 {
328 handler.doRemoveFile( record, this, strWSRestUrl );
329 }
330 catch( BlobStoreClientException e )
331 {
332 AppLogService.debug( e );
333 }
334
335
336 for ( FileItem fileItem : asynchronousFileItems )
337 {
338 String strDownloadFileUrl = StringUtils.EMPTY;
339
340 try
341 {
342 if ( fileItem != null )
343 {
344
345 String strBlobKey = handler.doUploadFile( strWSRestUrl, fileItem, strBlobStore );
346 strDownloadFileUrl = handler.getFileUrl( strWSRestUrl, strBlobStore, strBlobKey );
347 }
348 }
349 catch( Exception e )
350 {
351 throw new DirectoryErrorException( this.getTitle( ), e.getMessage( ) );
352 }
353
354
355 RecordField recordField = new RecordField( );
356 recordField.setEntry( this );
357 recordField.setValue( strDownloadFileUrl );
358 listRecordField.add( recordField );
359 }
360 }
361 }
362 else
363 {
364
365 if ( bTestDirectoryError && this.isMandatory( ) )
366 {
367 throw new DirectoryErrorException( this.getTitle( ) );
368 }
369
370
371 try
372 {
373 handler.doRemoveFile( record, this, strWSRestUrl );
374 }
375 catch( BlobStoreClientException e )
376 {
377 AppLogService.debug( e );
378 }
379 }
380 }
381 else
382 {
383
384 String [ ] listDownloadFileUrls = request.getParameterValues( Integer.toString( getIdEntry( ) ) );
385
386 if ( ( listDownloadFileUrls != null ) && ( listDownloadFileUrls.length > 0 ) )
387 {
388 for ( String strDownloadFileUrl : listDownloadFileUrls )
389 {
390 RecordField recordField = new RecordField( );
391 recordField.setEntry( this );
392 recordField.setValue( StringUtils.isNotBlank( strDownloadFileUrl ) ? strDownloadFileUrl : StringUtils.EMPTY );
393 listRecordField.add( recordField );
394 }
395 }
396 }
397 }
398
399
400
401
402 @Override
403 public String convertRecordFieldTitleToString( RecordField recordField, Locale locale, boolean bDisplayFront )
404 {
405 String strTitle = super.convertRecordFieldTitleToString( recordField, locale, bDisplayFront );
406
407 if ( StringUtils.isNotBlank( strTitle ) && bDisplayFront )
408 {
409
410 StringBuffer sbHtml = new StringBuffer( );
411
412 Map<String, String> mapParamTagA = new HashMap<String, String>( );
413 mapParamTagA.put( ATTRIBUTE_HREF, strTitle );
414
415 String strAlt = I18nService.getLocalizedString( PROPERTY_LABEL_DOWNLOAD, locale );
416 Map<String, String> mapParamTagImg = new HashMap<String, String>( );
417 mapParamTagImg.put( ATTRIBUTE_SRC, URL_IMG_DOWNLOAD );
418 mapParamTagImg.put( ATTRIBUTE_TITLE, strAlt );
419 mapParamTagImg.put( ATTRIBUTE_ALT, strAlt );
420
421 XmlUtil.beginElement( sbHtml, TAG_A, mapParamTagA );
422 XmlUtil.addEmptyElement( sbHtml, TAG_IMG, mapParamTagImg );
423 XmlUtil.endElement( sbHtml, TAG_A );
424
425 strTitle = sbHtml.toString( );
426 }
427
428 return strTitle;
429 }
430
431
432
433
434
435 @Override
436 public boolean isSortable( )
437 {
438 return true;
439 }
440
441
442
443
444 @Override
445 public LocalizedPaginator<RegularExpression> getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex,
446 Locale locale )
447 {
448 return new LocalizedPaginator<RegularExpression>( this.getFields( ).get( 0 ).getRegularExpressionList( ), nItemPerPage, strBaseUrl,
449 strPageIndexParameterName, strPageIndex, locale );
450 }
451
452
453
454
455 @Override
456 protected void setFields( HttpServletRequest request, List<Field> listFields )
457 {
458 listFields.add( buildFieldOption( request ) );
459 listFields.add( buildFieldWSRestUrl( request ) );
460 listFields.add( buildFieldBlobStore( request ) );
461 }
462
463
464
465
466 @Override
467 protected String checkEntryData( HttpServletRequest request, Locale locale )
468 {
469 String strError = super.checkEntryData( request, locale );
470
471 if ( StringUtils.isBlank( strError ) )
472 {
473 DirectoryAsynchronousUploadHandler handler = DirectoryAsynchronousUploadHandler.getHandler( );
474
475 if ( !handler.isBlobStoreClientServiceAvailable( ) )
476 {
477 return AdminMessageService.getMessageUrl( request, MESSAGE_BLOBSTORE_CLIENT_SERVICE_UNAVAILABLE, AdminMessage.TYPE_STOP );
478 }
479
480 String strFieldError = StringUtils.EMPTY;
481 String strWSRestUrl = request.getParameter( PARAMETER_WS_REST_URL );
482 String strBlobStore = request.getParameter( PARAMETER_BLOBSTORE );
483
484 if ( StringUtils.isBlank( strWSRestUrl ) )
485 {
486 strFieldError = FIELD_WS_REST_URL;
487 }
488
489 if ( StringUtils.isBlank( strBlobStore ) )
490 {
491 strFieldError = FIELD_BLOBSTORE;
492 }
493
494 if ( StringUtils.isNotBlank( strFieldError ) )
495 {
496 Object [ ] tabRequiredFields = {
497 I18nService.getLocalizedString( strFieldError, locale )
498 };
499
500 return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
501 }
502 }
503
504 return strError;
505 }
506
507
508
509
510 @Override
511 protected void checkRecordFieldData( FileItem fileItem, Locale locale ) throws DirectoryErrorException
512 {
513
514 DirectoryAsynchronousUploadHandler handler = DirectoryAsynchronousUploadHandler.getHandler( );
515
516 if ( !handler.isBlobStoreClientServiceAvailable( ) )
517 {
518 String strErrorMessage = I18nService.getLocalizedString( MESSAGE_BLOBSTORE_CLIENT_SERVICE_UNAVAILABLE, locale );
519 throw new DirectoryErrorException( this.getTitle( ), strErrorMessage );
520 }
521
522 String strFilename = ( fileItem != null ) ? FileUploadService.getFileNameOnly( fileItem ) : StringUtils.EMPTY;
523 String strMimeType = FileSystemUtil.getMIMEType( strFilename );
524
525
526 Field fieldOption = DirectoryUtils.findFieldByTitleInTheList( CONSTANT_OPTION, getFields( ) );
527
528 if ( ( fieldOption == null ) || StringUtils.isBlank( fieldOption.getValue( ) ) )
529 {
530 throw new DirectoryErrorException( getTitle( ) );
531 }
532
533 if ( StringUtils.isNotBlank( strFilename ) && StringUtils.isNotBlank( strMimeType ) && StringUtils.isNotBlank( fieldOption.getValue( ) )
534 && !ALL.equals( fieldOption.getValue( ) ) )
535 {
536 String [ ] listAuthorizedFileExt = fieldOption.getValue( ).split( COMMA );
537
538 if ( ( listAuthorizedFileExt != null ) && ( listAuthorizedFileExt.length > 0 ) )
539 {
540 boolean bIsAuthorized = false;
541
542 for ( String strAuthorizedFileExt : listAuthorizedFileExt )
543 {
544 String strAuthorizedMimeType = FileSystemUtil.getMIMEType( DirectoryUtils.CONSTANT_DOT + strAuthorizedFileExt );
545
546 if ( StringUtils.isNotBlank( strAuthorizedMimeType ) && strAuthorizedMimeType.equals( strMimeType ) )
547 {
548 bIsAuthorized = true;
549
550 break;
551 }
552 }
553
554 if ( !bIsAuthorized )
555 {
556 Object [ ] param = {
557 fieldOption.getValue( )
558 };
559 String strErrorMessage = I18nService.getLocalizedString( DirectoryUtils.MESSAGE_DIRECTORY_ERROR_MIME_TYPE, param, locale );
560 throw new DirectoryErrorException( getTitle( ), strErrorMessage );
561 }
562 }
563 }
564 }
565
566
567
568
569
570
571
572
573
574
575 private Field buildFieldOption( HttpServletRequest request )
576 {
577 String strValue = request.getParameter( PARAMETER_VALUE );
578 String strWidth = request.getParameter( PARAMETER_WIDTH );
579
580
581
582
583 int nWidth = DirectoryUtils.convertStringToInt( strWidth );
584
585
586 Field fieldOption = DirectoryUtils.findFieldByTitleInTheList( CONSTANT_OPTION, getFields( ) );
587
588 if ( fieldOption == null )
589 {
590 fieldOption = new Field( );
591 }
592
593 fieldOption.setEntry( this );
594 fieldOption.setTitle( CONSTANT_OPTION );
595 fieldOption.setValue( strValue );
596 fieldOption.setWidth( nWidth );
597
598 return fieldOption;
599 }
600
601
602
603
604
605
606
607
608 private Field buildFieldWSRestUrl( HttpServletRequest request )
609 {
610 String strWSRestUrl = request.getParameter( PARAMETER_WS_REST_URL );
611 Field fieldWSRestUrl = DirectoryUtils.findFieldByTitleInTheList( CONSTANT_WS_REST_URL, getFields( ) );
612
613 if ( fieldWSRestUrl == null )
614 {
615 fieldWSRestUrl = new Field( );
616 }
617
618 fieldWSRestUrl.setEntry( this );
619 fieldWSRestUrl.setTitle( CONSTANT_WS_REST_URL );
620 fieldWSRestUrl.setValue( strWSRestUrl );
621
622 return fieldWSRestUrl;
623 }
624
625
626
627
628
629
630
631
632 private Field buildFieldBlobStore( HttpServletRequest request )
633 {
634 String strBlobStore = request.getParameter( PARAMETER_BLOBSTORE );
635 Field fieldBlobStore = DirectoryUtils.findFieldByTitleInTheList( CONSTANT_BLOBSTORE, getFields( ) );
636
637 if ( fieldBlobStore == null )
638 {
639 fieldBlobStore = new Field( );
640 }
641
642 fieldBlobStore.setEntry( this );
643 fieldBlobStore.setTitle( CONSTANT_BLOBSTORE );
644 fieldBlobStore.setValue( strBlobStore );
645
646 return fieldBlobStore;
647 }
648 }