View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.appcenter.modules.fastdeployapplication.util.vcs;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.io.PrintWriter;
39  import java.io.StringWriter;
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.Comparator;
43  import java.util.List;
44  
45  import org.tmatesoft.svn.core.ISVNDirEntryHandler;
46  import org.tmatesoft.svn.core.SVNAuthenticationException;
47  import org.tmatesoft.svn.core.SVNCancelException;
48  import org.tmatesoft.svn.core.SVNDirEntry;
49  import org.tmatesoft.svn.core.SVNException;
50  import org.tmatesoft.svn.core.SVNNodeKind;
51  import org.tmatesoft.svn.core.SVNURL;
52  import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
53  import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
54  import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
55  import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
56  import org.tmatesoft.svn.core.io.SVNRepository;
57  import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
58  import org.tmatesoft.svn.core.wc.ISVNEventHandler;
59  import org.tmatesoft.svn.core.wc.SVNClientManager;
60  import org.tmatesoft.svn.core.wc.SVNEvent;
61  import org.tmatesoft.svn.core.wc.SVNEventAction;
62  import org.tmatesoft.svn.core.wc.SVNRevision;
63  
64  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
65  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
66  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.FileUtil;
67  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ReleaseSVNCheckoutClient;
68  import fr.paris.lutece.util.ReferenceItem;
69  import fr.paris.lutece.util.ReferenceList;
70  
71  public final class SVNUtils
72  {
73      private static final Comparator<SVNDirEntry> _compareSvnEntries = new Comparator<SVNDirEntry>( )
74      {
75          @Override
76          public int compare( SVNDirEntry o1, SVNDirEntry o2 )
77          {
78              return -1 * o1.getDate( ).compareTo( o2.getDate( ) );
79          }
80      };
81  
82      /**
83       * Constructeur vide
84       */
85      private SVNUtils( )
86      {
87          // nothing
88      }
89  
90      /**
91       * Initialise les diff�rentes factory pour le svn
92       */
93      public static void init( )
94      {
95          /*
96           * For using over http:// and https:
97           */
98          DAVRepositoryFactory.setup( );
99          /*
100          * For using over svn:// and svn+xxx:
101          */
102         SVNRepositoryFactoryImpl.setup( );
103 
104         /*
105          * For using over file:/
106          */
107         FSRepositoryFactory.setup( );
108 
109     }
110 
111     public static String doSvnCheckoutSite( String strSiteName, String strUrl, String strCheckoutBaseSitePath, ReleaseSVNCheckoutClient updateClient,
112             CommandResult result ) throws SVNException
113     {
114         SVNURL url = SVNURL.parseURIEncoded( strUrl );
115         File file = new File( strCheckoutBaseSitePath );
116 
117         if ( file.exists( ) )
118         {
119             if ( !FileUtil.delete( file, result.getLog( ) ) )
120             {
121                 result.setError( result.getLog( ).toString( ) );
122 
123                 return result.getLog( ).toString( );
124             }
125         }
126 
127         SVNRepository repository = SVNRepositoryFactory.create( url, null );
128         final StringBuffer logBuffer = result.getLog( );
129 
130         try
131         {
132             updateClient.setEventHandler( new ISVNEventHandler( )
133             {
134                 public void checkCancelled( ) throws SVNCancelException
135                 {
136                     // Do nothing
137                 }
138 
139                 public void handleEvent( SVNEvent event, double progress ) throws SVNException
140                 {
141                     logBuffer.append( ( ( event.getAction( ) == SVNEventAction.UPDATE_ADD ) ? "ADDED " : event.getAction( ) ) + " " + event.getFile( ) + "\n" );
142                 }
143             } );
144 
145             // SVNDepth.INFINITY + dernier param�tre � FALSE pour la version 1.3.2
146             updateClient.doCheckout( repository.getLocation( ), file, SVNRevision.HEAD, SVNRevision.HEAD, true );
147         }
148         catch( SVNAuthenticationException e )
149         {
150             // _result.getLog( ).append( CONSTANTE_NO_LOGIN_PASSWORD );
151             // _result.setStatus( ICommandThread.STATUS_EXCEPTION );
152             // _result.setRunning( false );
153 
154             DeploymentUtils.addTechnicalError( result, "Une erreur est survenue lors de la tentative d'authentification avec le svn" + e, e );
155 
156             StringWriter sw = new StringWriter( );
157             PrintWriter pw = new PrintWriter( sw );
158             e.printStackTrace( pw );
159 
160             String errorLog = sw.toString( );
161             pw.flush( );
162             pw.close( );
163 
164             try
165             {
166                 sw.flush( );
167                 sw.close( );
168             }
169             catch( IOException e1 )
170             {
171                 // do nothing
172                 // _logger.error( e1 );
173             }
174 
175             // _result.setLog( _result.getLog( ).append( errorLog ) );
176             // _logger.error( e );
177 
178             // _result.setIdError( ReleaseLogger.logError( _result.getLog( ).toString( ), e ) );
179         }
180         catch( Exception e )
181         {
182             // _result.setStatus( ICommandThread.STATUS_EXCEPTION );
183             // _result.setRunning( false );
184             StringWriter sw = new StringWriter( );
185             PrintWriter pw = new PrintWriter( sw );
186             e.printStackTrace( pw );
187 
188             String errorLog = sw.toString( );
189             pw.flush( );
190             pw.close( );
191 
192             try
193             {
194                 sw.flush( );
195                 sw.close( );
196             }
197             catch( IOException e1 )
198             {
199                 // do nothing
200                 // _logger.error( e1 );
201             }
202 
203             DeploymentUtils.addTechnicalError( result, "Une erreur svn est survenue:" + e, e );
204 
205         }
206 
207         return null;
208     }
209 
210     public static ReferenceList getSvnDirChildren( String strUrlSite, SVNClientManager clientManager ) throws SVNException
211     {
212         final ReferenceList listSites = new ReferenceList( );
213         final SVNURL url;
214 
215         final List<SVNDirEntry> listSvnEntries = new ArrayList<SVNDirEntry>( );
216         url = SVNURL.parseURIEncoded( strUrlSite );
217 
218         SVNRepository repository = SVNRepositoryFactory.create( url, null );
219 
220         clientManager.getLogClient( ).doList( repository.getLocation( ), SVNRevision.HEAD, SVNRevision.HEAD, false, false, new ISVNDirEntryHandler( )
221         {
222             public void handleDirEntry( SVNDirEntry entry ) throws SVNException
223             {
224                 ReferenceItem referenceItem;
225 
226                 if ( !url.equals( entry.getURL( ) ) )
227                 {
228                     if ( entry.getKind( ) == SVNNodeKind.DIR )
229                     {
230                         listSvnEntries.add( entry );
231                     }
232                 }
233             }
234         } );
235 
236         Collections.sort( listSvnEntries, _compareSvnEntries );
237 
238         for ( SVNDirEntry svnEntry : listSvnEntries )
239         {
240             listSites.addItem( svnEntry.getName( ), svnEntry.getName( ) );
241         }
242 
243         return listSites;
244     }
245 
246     public static boolean checkAuthentication( ISVNAuthenticationManager authManager, String strUrl )
247     {
248         try
249         {
250             SVNURL url = SVNURL.parseURIEncoded( strUrl );
251             SVNRepository repository = SVNRepositoryFactory.create( url, null );
252             repository.setAuthenticationManager( authManager );
253             repository.testConnection( );
254         }
255         catch( SVNException e )
256         {
257             return false;
258         }
259         return true;
260 
261     }
262 }