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