View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.appcenter.modules.fastdeployapplication.service;
35  
36  import java.io.FileInputStream;
37  import java.io.FileNotFoundException;
38  import java.io.InputStream;
39  import java.io.OutputStream;
40  import java.util.ArrayList;
41  import java.util.List;
42  import java.util.Vector;
43  import java.util.regex.Pattern;
44  
45  import com.jcraft.jsch.ChannelSftp;
46  import com.jcraft.jsch.JSchException;
47  import com.jcraft.jsch.SftpException;
48  
49  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
50  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FtpInfo;
51  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
52  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.SFTPUtils;
53  
54  public class SFTPService implements IFtpService
55  {
56      
57      
58      
59      
60      @Override
61      public String uploadFile( String fileName, String pathLocalFile, FtpInfo ftpInfo, String remoteDirectoryPath, CommandResult commandResult,
62              boolean bBinaryFile )
63      {
64        	InputStream inputStream = null;
65  
66          try
67          {
68              inputStream = new FileInputStream( pathLocalFile );
69          }
70          catch( FileNotFoundException e )
71          {
72              // TODO Auto-generated catch block
73              DeploymentUtils.addTechnicalError( commandResult, "probleme avec le chemin d'upload du fichier " + pathLocalFile + ":" + e.getMessage( ), e );
74  
75          }
76          return uploadFile(fileName, inputStream, ftpInfo, remoteDirectoryPath, commandResult, bBinaryFile);     
77    
78      }
79      @Override
80      public String uploadFile( String fileName, InputStream inputStream, FtpInfo ftpInfo, String remoteDirectoryPath, CommandResult commandResult,
81              boolean bBinaryFile )
82      {
83  
84  
85               // login
86          	 ChannelSftp  channel=null;
87               try
88               {
89               	channel=SFTPUtils.setupJsch(ftpInfo);
90  
91                   if ( channel==null )
92                   {
93                       DeploymentUtils.addTechnicalError( commandResult, "Probleme de connexion SFTP,le compte SFTP n'est pas reconnu pour le compte "+ftpInfo.getUserLogin()  );
94                   }
95               }catch (JSchException e) {
96                   DeploymentUtils.addTechnicalError( commandResult, "Probleme de connexion SFTP,le compte SFTP n'est pas reconnu pour le compte "+ftpInfo.getUserLogin() ,e );    
97  			}
98               try
99               {
100 
101                  if ( channel!=null )
102                  {
103                 	 
104                    SFTPUtils.upload(channel,inputStream,remoteDirectoryPath,fileName);
105                  }
106              }catch (SftpException e) {
107                  DeploymentUtils.addTechnicalError( commandResult, "Probleme de lors de l'upload du fichier "+fileName ,e );    
108 			}
109                 
110              // upload File
111          return null;
112     }
113     @Override
114     public void getFile(String fileName, OutputStream outputStream, FtpInfo ftpInfo,  String remoteDirectoryPath, CommandResult commandResult )
115     {
116         // login
117    	 ChannelSftp  channel=null;
118         try
119         {
120         	channel=SFTPUtils.setupJsch(ftpInfo);
121 
122             if ( channel==null )
123             {
124                 DeploymentUtils.addTechnicalError( commandResult, "Probleme de connexion SFTP,le compte SFTP n'est pas reconnu pour le compte "+ftpInfo.getUserLogin()  );
125             }
126         }catch (JSchException e) {
127             DeploymentUtils.addTechnicalError( commandResult, "Probleme de connexion SFTP,le compte SFTP n'est pas reconnu pour le compte "+ftpInfo.getUserLogin() ,e );    
128 		}
129         try
130         {
131 
132             if ( channel!=null )
133             {
134            	 
135               SFTPUtils.download(channel, outputStream, remoteDirectoryPath, fileName);
136             }
137         }catch (SftpException e) {
138             DeploymentUtils.addTechnicalError( commandResult, "Probleme de lors de l'upload du fichier "+fileName ,e );    
139 		}
140            
141         }
142     @Override
143     public List<String> getListDirectoryFiles(FtpInfo ftpInfo,String fileSearch,  String remoteDirectoryPath, CommandResult commandResult)
144     {
145         
146        Pattern pattern= Pattern.compile(fileSearch);
147         List<String> listFile=new ArrayList<String>();
148         ChannelSftp  channel=null;
149         try
150         {
151             channel=SFTPUtils.setupJsch(ftpInfo);
152 
153             if ( channel==null )
154             {
155                 DeploymentUtils.addTechnicalError( commandResult, "Probleme de connexion SFTP,le compte SFTP n'est pas reconnu pour le compte "+ftpInfo.getUserLogin()  );
156             }
157         }catch (JSchException e) {
158             DeploymentUtils.addTechnicalError( commandResult, "Probleme de connexion SFTP,le compte SFTP n'est pas reconnu pour le compte "+ftpInfo.getUserLogin() ,e );    
159         }
160         try
161         {
162 
163             if ( channel!=null )
164             {
165                 Vector<ChannelSftp.LsEntry> list = channel.ls(remoteDirectoryPath);
166                 for(ChannelSftp.LsEntry entry : list) {
167                     if(pattern.matcher(entry.getFilename( )).find())
168                     {
169                     
170                         listFile.add( entry.getFilename()); 
171                     }
172                 }
173               
174             }
175         }catch (SftpException e) {
176             DeploymentUtils.addTechnicalError( commandResult, "Probleme lors du listing du repertoire "+remoteDirectoryPath ,e );    
177         }
178            
179         return listFile;
180     }
181         
182     
183     
184     
185     
186     
187 }