View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.pluginwizard.service;
35  
36  import fr.paris.lutece.plugins.pluginwizard.business.model.PluginModel;
37  import fr.paris.lutece.plugins.pluginwizard.service.generator.GeneratorService;
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.portal.service.plugin.PluginService;
40  import fr.paris.lutece.portal.service.util.AppLogService;
41  
42  import java.io.ByteArrayInputStream;
43  import java.io.ByteArrayOutputStream;
44  
45  import java.util.Map;
46  import java.util.zip.ZipEntry;
47  import java.util.zip.ZipOutputStream;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  /**
52   * The zip service will pack all the chosen files and create an archive
53   */
54  public class PluginWizardZipService
55  {
56      private static PluginWizardZipService _singleton;
57      private static final String PARAM_PLUGIN_ID = "plugin_id";
58      private static final String PARAM_SCHEME = "scheme";
59      private static Plugin _plugin = PluginService.getPlugin( "pluginwizard" );
60  
61      /**
62       * Gets the unique instance of the PluginWizardZipService
63       * 
64       * @return The unique instance of the PluginWizardZipService
65       */
66      public static synchronized PluginWizardZipService getInstance( )
67      {
68          if ( _singleton == null )
69          {
70              _singleton = new PluginWizardZipService( );
71          }
72  
73          return _singleton;
74      }
75  
76      /**
77       * Exports the files in a byte array
78       * 
79       * @param request
80       *            The Http request
81       * @return An array of byte which is the content of the archive
82       */
83      public byte [ ] exportZip( HttpServletRequest request )
84      {
85          ByteArrayOutputStream fos = new ByteArrayOutputStream( );
86  
87          byte [ ] buffer = new byte [ 1024];
88  
89          ZipOutputStream zipOutputStream = new ZipOutputStream( fos );
90          zipOutputStream.setLevel( 9 );
91  
92          String strPluginId = request.getParameter( PARAM_PLUGIN_ID );
93          int nPluginId = Integer.parseInt( strPluginId );
94          String strScheme = request.getParameter( PARAM_SCHEME );
95          int nScheme = Integer.parseInt( strScheme );
96          PluginModel pluginModel = ModelService.getPluginModel( nPluginId );
97          GeneratorServiced/service/generator/GeneratorService.html#GeneratorService">GeneratorService generator = new GeneratorService( );
98          Map<String, String> mapSources = generator.getGeneratedSources( _plugin, pluginModel, nScheme );
99  
100         try
101         {
102             for ( Map.Entry<String, String> sourceFile : mapSources.entrySet( ) )
103             {
104                 StringBuilder sb = new StringBuilder( sourceFile.getValue( ) );
105                 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( sb.toString( ).getBytes( "UTF-8" ) );
106                 zipOutputStream.putNextEntry( new ZipEntry( sourceFile.getKey( ) ) );
107 
108                 int nLength;
109 
110                 while ( ( nLength = byteArrayInputStream.read( buffer ) ) > 0 )
111                 {
112                     zipOutputStream.write( buffer, 0, nLength );
113                 }
114 
115                 zipOutputStream.closeEntry( );
116                 byteArrayInputStream.close( );
117             }
118 
119             zipOutputStream.finish( );
120             zipOutputStream.close( );
121         }
122         catch( Exception e )
123         {
124             AppLogService.error( e );
125         }
126 
127         return fos.toByteArray( );
128     }
129 }