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.workflow.modules.archive.web;
35
36 import fr.paris.lutece.plugins.archive.util.ZipGenerateUtil;
37 import fr.paris.lutece.plugins.directory.business.Directory;
38 import fr.paris.lutece.plugins.directory.business.DirectoryHome;
39 import fr.paris.lutece.plugins.directory.modules.pdfproducer.business.producerconfig.ConfigProducerHome;
40 import fr.paris.lutece.plugins.directory.modules.pdfproducer.business.producerconfig.IConfigProducer;
41 import fr.paris.lutece.plugins.directory.modules.pdfproducer.utils.PDFUtils;
42 import fr.paris.lutece.plugins.workflow.modules.archive.service.WorkflowArchivePlugin;
43 import fr.paris.lutece.portal.service.plugin.Plugin;
44 import fr.paris.lutece.portal.service.plugin.PluginService;
45 import fr.paris.lutece.portal.service.spring.SpringContextService;
46 import fr.paris.lutece.portal.service.util.AppLogService;
47 import fr.paris.lutece.portal.service.util.AppPathService;
48 import fr.paris.lutece.portal.service.util.AppPropertiesService;
49 import fr.paris.lutece.util.signrequest.AbstractAuthenticator;
50 import fr.paris.lutece.util.string.StringUtil;
51
52 import java.io.File;
53 import java.io.FileInputStream;
54 import java.io.IOException;
55 import java.io.OutputStream;
56
57 import javax.servlet.http.HttpServletRequest;
58 import javax.servlet.http.HttpServletResponse;
59
60 import org.apache.commons.lang.StringUtils;
61
62
63
64
65
66
67 public class DownloadArchive
68 {
69
70
71 public static final String EXTENSION_FILE_ZIP = ".zip";
72 public static final String CONSTANT_SLASH = "/";
73 public static final String CONSTANT_UNDERSCORE = "_";
74
75
76 private static final String PROPERTY_ZIP_FOLDER_PATH = "module.workflow.archive.zip_folder_path";
77 private static final String BEAN_REQUEST_AUTHENTICATOR_FOR_URL = "workflow-archive.requestAuthenticatorForUrl";
78
79
80 private static final String PARAM_ID_RECORD = "id_record";
81 private static final String PARAM_ID_DIRECTORY = "id_directory";
82 private static final String PARAM_ID_CONFIG_PRODUCER = "id_config_producer";
83
84 private AbstractAuthenticator _authenticator = SpringContextService.getBean( BEAN_REQUEST_AUTHENTICATOR_FOR_URL );
85
86
87
88
89
90
91 public void doDownloadFile( HttpServletRequest request, HttpServletResponse response )
92 {
93 String strIdRecord = request.getParameter( PARAM_ID_RECORD );
94 String strIdDirectory = request.getParameter( PARAM_ID_DIRECTORY );
95 String strIdConfigProducer = request.getParameter( PARAM_ID_CONFIG_PRODUCER );
96 Plugin plugin = PluginService.getPlugin( WorkflowArchivePlugin.PLUGIN_NAME );
97
98 if ( _authenticator.isRequestAuthenticated( request ) && StringUtils.isNotBlank( strIdRecord ) )
99 {
100 Directory directory = DirectoryHome.findByPrimaryKey( Integer.parseInt( strIdDirectory ), plugin );
101
102 String strZipFolderPath = AppPathService.getAbsolutePathFromRelativePath( AppPropertiesService.getProperty( PROPERTY_ZIP_FOLDER_PATH ) );
103 String strZIPFileName = strIdDirectory + CONSTANT_UNDERSCORE + strIdRecord + EXTENSION_FILE_ZIP;
104
105 String strDownloadFileName = null;
106 IConfigProducer configProducer = ConfigProducerHome.loadConfig( plugin, Integer.parseInt( strIdConfigProducer ) );
107 if ( configProducer != null )
108 {
109 strDownloadFileName = PDFUtils.getFileNameFromConfig( directory, configProducer, Integer.parseInt( strIdRecord ), request.getLocale( ) );
110 }
111 else
112 {
113 strDownloadFileName = StringUtil.replaceAccent( directory.getTitle( ) ) + CONSTANT_UNDERSCORE + strIdRecord + EXTENSION_FILE_ZIP;
114 }
115
116 response.setHeader( "Content-Disposition", "attachment ;filename=\"" + strDownloadFileName + "\"" );
117 response.setHeader( "Pragma", "public" );
118 response.setHeader( "Expires", "0" );
119 response.setHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
120
121 response.setContentType( ZipGenerateUtil.ARCHIVE_MIME_TYPE_ZIP );
122 try
123 {
124 OutputStream os = response.getOutputStream( );
125
126 File zipFile = new File( strZipFolderPath + CONSTANT_SLASH + strZIPFileName );
127 byte[] buffer = new byte[( int ) zipFile.length( )];
128 FileInputStream is = new FileInputStream( zipFile );
129 is.read( buffer );
130 is.close( );
131 os.write( buffer );
132 os.flush( );
133 os.close( );
134 }
135 catch ( IOException e )
136 {
137 AppLogService.error( e.getMessage( ), e );
138 }
139 }
140 }
141 }