View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.ctv.util;
35  
36  import java.io.ByteArrayInputStream;
37  import java.io.File;
38  import java.io.FileNotFoundException;
39  import java.io.FileOutputStream;
40  import java.io.IOException;
41  import java.io.InputStream;
42  import java.io.OutputStream;
43  import java.util.Vector;
44  
45  import org.apache.commons.fileupload.FileItem;
46  import org.apache.commons.io.IOUtils;
47  
48  import com.jcraft.jsch.Channel;
49  import com.jcraft.jsch.ChannelSftp;
50  import com.jcraft.jsch.ChannelSftp.LsEntry;
51  import com.jcraft.jsch.JSch;
52  import com.jcraft.jsch.JSchException;
53  import com.jcraft.jsch.Session;
54  import com.jcraft.jsch.SftpException;
55  
56  import fr.paris.lutece.portal.service.util.AppPropertiesService;
57  
58  public class RemoteFilesUtils
59  {
60  
61      private static String HOST = AppPropertiesService.getProperty( "ctv.file.remote.host" );
62  
63      private static String USER = AppPropertiesService.getProperty( "ctv.file.remote.user" );
64  
65      private static String PASSWORD = AppPropertiesService.getProperty( "ctv.file.remote.password" );
66  
67      private static String UPLOAD_DIRECTORY = AppPropertiesService.getProperty( "ctv.file.remote.directory.upload" );
68  
69      public static void getRemoteFilesInLocalDirectory( String srcRemoteDirectory, String destLocalDirectory )
70      {
71  
72          try
73          {
74              srcRemoteDirectory = srcRemoteDirectory.replace( "\\", "/" );
75              Session session = getSession( );
76              ChannelSftp sftpChannel = getSftpChannel( session );
77              Vector<LsEntry> root = sftpChannel.ls( UPLOAD_DIRECTORY + "/" + srcRemoteDirectory );
78              for ( LsEntry entryRoot : root )
79              {
80                  if ( entryRoot.getAttrs( ).isDir( ) && !".".equals( entryRoot.getFilename( ) ) && !"..".equals( entryRoot.getFilename( ) ) )
81                  {
82                      String directoryName = UPLOAD_DIRECTORY + "/" + srcRemoteDirectory + "/" + entryRoot.getFilename( );
83                      Vector<LsEntry> directory = sftpChannel.ls( directoryName );
84                      for ( LsEntry entryDirectory : directory )
85                      {
86                          if ( !entryDirectory.getAttrs( ).isDir( ) )
87                          {
88                              InputStream srcFile = sftpChannel.get( directoryName + "/" + entryDirectory.getFilename( ) );
89                              createFileInLocal( srcFile, destLocalDirectory + "/" + entryRoot.getFilename( ) + "/" + entryDirectory.getFilename( ) );
90                          }
91                      }
92                  }
93              }
94              sftpChannel.exit( );
95              session.disconnect( );
96          }
97          catch( SftpException e )
98          {
99          }
100 
101     }
102 
103     private static void createFileInLocal( InputStream srcFile, String destFile )
104     {
105         OutputStream outputStream = null;
106         try
107         {
108             File file = new File( destFile );
109             file.getParentFile( ).mkdirs( );
110             outputStream = new FileOutputStream( file );
111             IOUtils.copy( srcFile, outputStream );
112         }
113         catch( FileNotFoundException e )
114         {
115         }
116         catch( IOException e )
117         {
118         }
119         finally
120         {
121             IOUtils.closeQuietly( outputStream );
122             IOUtils.closeQuietly( srcFile );
123         }
124     }
125 
126     public static void upload( FileItem fileToSave, String srcRemoteDirectory )
127     {
128         InputStream inputStream = new ByteArrayInputStream( fileToSave.get( ) );
129         srcRemoteDirectory = srcRemoteDirectory.replace( "\\", "/" );
130         Session session = null;
131         ChannelSftp channelSftp = null;
132         session = getSession( );
133         channelSftp = getSftpChannel( session );
134         String completeBackupPath = UPLOAD_DIRECTORY + "/" + srcRemoteDirectory;
135         try
136         {
137             String [ ] complPath = completeBackupPath.split( "/" );
138             channelSftp.cd( "/" );
139             for ( String folder : complPath )
140             {
141                 if ( folder.length( ) > 0 )
142                 {
143                     try
144                     {
145                         channelSftp.cd( folder );
146                     }
147                     catch( SftpException e2 )
148                     {
149                         channelSftp.mkdir( folder );
150                         channelSftp.cd( folder );
151                     }
152                 }
153             }
154             channelSftp.cd( "/" );
155             channelSftp.cd( completeBackupPath );
156             channelSftp.put( inputStream, fileToSave.getName( ), ChannelSftp.OVERWRITE );
157         }
158         catch( SftpException e )
159         {
160         }
161         finally
162         {
163             IOUtils.closeQuietly( inputStream );
164         }
165 
166         channelSftp.exit( );
167         session.disconnect( );
168     }
169 
170     private static ChannelSftp getSftpChannel( Session session )
171     {
172         ChannelSftp sftpChannel = null;
173         try
174         {
175             Channel channel = session.openChannel( "sftp" );
176             channel.connect( );
177             sftpChannel = (ChannelSftp) channel;
178         }
179         catch( JSchException e )
180         {
181         }
182         return sftpChannel;
183     }
184 
185     private static Session getSession( )
186     {
187         Session session = null;
188         try
189         {
190             JSch jsch = new JSch( );
191             session = jsch.getSession( USER, HOST, 22 );
192             session.setConfig( "StrictHostKeyChecking", "no" );
193             session.setPassword( PASSWORD );
194             session.connect( );
195         }
196         catch( JSchException e )
197         {
198         }
199         return session;
200     }
201 
202     public static void removeRemoteFile( String remoteFilePath )
203     {
204         try
205         {
206             remoteFilePath = remoteFilePath.replace( "\\", "/" );
207             Session session = getSession( );
208             ChannelSftp sftpChannel = getSftpChannel( session );
209             sftpChannel.rm( UPLOAD_DIRECTORY + "/" + remoteFilePath );
210             sftpChannel.exit( );
211             session.disconnect( );
212         }
213         catch( SftpException e )
214         {
215         }
216 
217     }
218 
219 }