View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * Class to download zip archive files.
65   * 
66   */
67  public class DownloadArchive
68  {
69  
70  	// CONSTANTS
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  	// PROPERTIES
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  	// PARAMETERS
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  	 * Initialize a download of a zip file if the user is authorized. The autorization is based on the signature given in parameter of the request.
88  	 * @param request The request
89  	 * @param response the response
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 }