]> source.dussan.org Git - archiva.git/blob
60c2fdd3c7c673e9013b4a1c7a4eead6637bd807
[archiva.git] /
1 package org.apache.archiva.indexer.maven;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.NetworkProxy;
24 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
25 import org.apache.archiva.common.utils.PathUtil;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.indexer.ArchivaIndexManager;
28 import org.apache.archiva.indexer.ArchivaIndexingContext;
29 import org.apache.archiva.indexer.IndexCreationFailedException;
30 import org.apache.archiva.indexer.IndexUpdateFailedException;
31 import org.apache.archiva.indexer.UnsupportedBaseContextException;
32 import org.apache.archiva.model.ArtifactReference;
33 import org.apache.archiva.proxy.common.WagonFactory;
34 import org.apache.archiva.proxy.common.WagonFactoryException;
35 import org.apache.archiva.proxy.common.WagonFactoryRequest;
36 import org.apache.archiva.repository.ManagedRepository;
37 import org.apache.archiva.repository.PasswordCredentials;
38 import org.apache.archiva.repository.RemoteRepository;
39 import org.apache.archiva.repository.Repository;
40 import org.apache.archiva.repository.RepositoryType;
41 import org.apache.archiva.repository.UnsupportedRepositoryTypeException;
42 import org.apache.archiva.repository.features.IndexCreationFeature;
43 import org.apache.archiva.repository.features.RemoteIndexFeature;
44 import org.apache.commons.lang.StringUtils;
45 import org.apache.maven.index.*;
46 import org.apache.maven.index.context.IndexCreator;
47 import org.apache.maven.index.context.IndexingContext;
48 import org.apache.maven.index.packer.IndexPacker;
49 import org.apache.maven.index.packer.IndexPackingRequest;
50 import org.apache.maven.index.updater.IndexUpdateRequest;
51 import org.apache.maven.index.updater.IndexUpdater;
52 import org.apache.maven.index.updater.ResourceFetcher;
53 import org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException;
54 import org.apache.maven.wagon.ConnectionException;
55 import org.apache.maven.wagon.ResourceDoesNotExistException;
56 import org.apache.maven.wagon.StreamWagon;
57 import org.apache.maven.wagon.TransferFailedException;
58 import org.apache.maven.wagon.Wagon;
59 import org.apache.maven.wagon.authentication.AuthenticationException;
60 import org.apache.maven.wagon.authentication.AuthenticationInfo;
61 import org.apache.maven.wagon.authorization.AuthorizationException;
62 import org.apache.maven.wagon.events.TransferEvent;
63 import org.apache.maven.wagon.events.TransferListener;
64 import org.apache.maven.wagon.proxy.ProxyInfo;
65 import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
66 import org.apache.maven.wagon.shared.http.HttpConfiguration;
67 import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70 import org.springframework.stereotype.Service;
71
72 import javax.inject.Inject;
73 import java.io.FileNotFoundException;
74 import java.io.IOException;
75 import java.io.InputStream;
76 import java.net.MalformedURLException;
77 import java.net.URI;
78 import java.nio.file.Files;
79 import java.nio.file.Path;
80 import java.nio.file.Paths;
81 import java.util.ArrayList;
82 import java.util.Collection;
83 import java.util.List;
84 import java.util.Map;
85 import java.util.concurrent.ConcurrentSkipListSet;
86 import java.util.stream.Collectors;
87
88 /**
89  * Maven implementation of index manager.
90  * The index manager is a singleton, so we try to make sure, that index operations are not running
91  * parallel by synchronizing on the index path.
92  * A update operation waits for parallel running methods to finish before starting, but after a certain
93  * time of retries a IndexUpdateFailedException is thrown.
94  */
95 @Service( "archivaIndexManager#maven" )
96 public class MavenIndexManager implements ArchivaIndexManager
97 {
98
99     private static final Logger log = LoggerFactory.getLogger( MavenIndexManager.class );
100
101     @Inject
102     private Indexer indexer;
103
104     @Inject
105     private IndexerEngine indexerEngine;
106
107     @Inject
108     private List<? extends IndexCreator> indexCreators;
109
110     @Inject
111     private IndexPacker indexPacker;
112
113     @Inject
114     private Scanner scanner;
115
116     @Inject
117     private ArchivaConfiguration archivaConfiguration;
118
119     @Inject
120     private WagonFactory wagonFactory;
121
122     @Inject
123     private NetworkProxyAdmin networkProxyAdmin;
124
125     @Inject
126     private IndexUpdater indexUpdater;
127
128     @Inject
129     private ArtifactContextProducer artifactContextProducer;
130
131     private ConcurrentSkipListSet<Path> activeContexts = new ConcurrentSkipListSet<>( );
132
133     private static final int WAIT_TIME = 100;
134     private static final int MAX_WAIT = 10;
135
136
137     IndexingContext getMvnContext( ArchivaIndexingContext context ) throws UnsupportedBaseContextException
138     {
139         if ( !context.supports( IndexingContext.class ) )
140         {
141             log.error( "The provided archiva index context does not support the maven IndexingContext" );
142             throw new UnsupportedBaseContextException( "The context does not support the Maven IndexingContext" );
143         }
144         return context.getBaseContext( IndexingContext.class );
145     }
146
147     private Path getIndexPath( ArchivaIndexingContext ctx )
148     {
149         return PathUtil.getPathFromUri( ctx.getPath( ) );
150     }
151
152     @FunctionalInterface
153     interface IndexUpdateConsumer
154     {
155
156         void accept( IndexingContext indexingContext ) throws IndexUpdateFailedException;
157     }
158
159     /*
160      * This method is used to do some actions around the update execution code. And to make sure, that no other
161      * method is running on the same index.
162      */
163     private void executeUpdateFunction( ArchivaIndexingContext context, IndexUpdateConsumer function ) throws IndexUpdateFailedException
164     {
165         IndexingContext indexingContext = null;
166         try
167         {
168             indexingContext = getMvnContext( context );
169         }
170         catch ( UnsupportedBaseContextException e )
171         {
172             throw new IndexUpdateFailedException( "Maven index is not supported by this context", e );
173         }
174         final Path ctxPath = getIndexPath( context );
175         int loop = MAX_WAIT;
176         boolean active = false;
177         while ( loop-- > 0 && !active )
178         {
179             active = activeContexts.add( ctxPath );
180             try
181             {
182                 Thread.currentThread( ).sleep( WAIT_TIME );
183             }
184             catch ( InterruptedException e )
185             {
186                 // Ignore this
187             }
188         }
189         if ( active )
190         {
191             try
192             {
193                 function.accept( indexingContext );
194             }
195             finally
196             {
197                 activeContexts.remove( ctxPath );
198             }
199         }
200         else
201         {
202             throw new IndexUpdateFailedException( "Timeout while waiting for index release on context " + context.getId( ) );
203         }
204     }
205
206     @Override
207     public void pack( final ArchivaIndexingContext context ) throws IndexUpdateFailedException
208     {
209         executeUpdateFunction( context, indexingContext -> {
210                 try
211                 {
212                     IndexPackingRequest request = new IndexPackingRequest( indexingContext,
213                         indexingContext.acquireIndexSearcher( ).getIndexReader( ),
214                         indexingContext.getIndexDirectoryFile( ) );
215                     indexPacker.packIndex( request );
216                     indexingContext.updateTimestamp( true );
217                 }
218                 catch ( IOException e )
219                 {
220                     log.error( "IOException while packing index of context " + context.getId( ) + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ) );
221                     throw new IndexUpdateFailedException( "IOException during update of " + context.getId( ), e );
222                 }
223             }
224         );
225
226     }
227
228     @Override
229     public void scan( final ArchivaIndexingContext context, final boolean update ) throws IndexUpdateFailedException
230     {
231         executeUpdateFunction( context, indexingContext -> {
232             DefaultScannerListener listener = new DefaultScannerListener( indexingContext, indexerEngine, update, null );
233             ScanningRequest request = new ScanningRequest( indexingContext, listener );
234             ScanningResult result = scanner.scan( request );
235             if ( result.hasExceptions( ) )
236             {
237                 log.error( "Exceptions occured during index scan of " + context.getId( ) );
238                 result.getExceptions( ).stream( ).map( e -> e.getMessage( ) ).distinct( ).limit( 5 ).forEach(
239                     s -> log.error( "Message: " + s )
240                 );
241             }
242
243         } );
244     }
245
246     @Override
247     public void update( final ArchivaIndexingContext context, final URI remoteUpdateUri, final boolean fullUpdate ) throws IndexUpdateFailedException
248     {
249         log.info( "start download remote index for remote repository {}", context.getRepository( ).getId( ) );
250
251         if ( !( context.getRepository( ) instanceof RemoteRepository ) )
252         {
253             throw new IndexUpdateFailedException( "The context is not associated to a remote repository " + context.getId( ) );
254         }
255         final RemoteRepository remoteRepository = (RemoteRepository) context.getRepository( );
256
257         executeUpdateFunction( context,
258             indexingContext -> {
259                 try
260                 {
261                     // create a temp directory to download files
262                     Path tempIndexDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".tmpIndex" );
263                     Path indexCacheDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".indexCache" );
264                     Files.createDirectories( indexCacheDirectory );
265                     if ( Files.exists( tempIndexDirectory ) )
266                     {
267                         org.apache.archiva.common.utils.FileUtils.deleteDirectory( tempIndexDirectory );
268                     }
269                     Files.createDirectories( tempIndexDirectory );
270                     tempIndexDirectory.toFile( ).deleteOnExit( );
271                     String baseIndexUrl = indexingContext.getIndexUpdateUrl( );
272
273                     String wagonProtocol = remoteUpdateUri.toURL( ).getProtocol( );
274
275                     NetworkProxy networkProxy = null;
276                     if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
277                     {
278                         RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
279                         if ( StringUtils.isNotBlank( rif.getProxyId( ) ) )
280                         {
281                             try
282                             {
283                                 networkProxy = networkProxyAdmin.getNetworkProxy( rif.getProxyId( ) );
284                             }
285                             catch ( RepositoryAdminException e )
286                             {
287                                 log.error( "Error occured while retrieving proxy {}", e.getMessage( ) );
288                             }
289                             if ( networkProxy == null )
290                             {
291                                 log.warn(
292                                     "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
293                                     rif.getProxyId( ) );
294                             }
295                         }
296
297                         final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(
298                             new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders( ) ).networkProxy(
299                                 networkProxy )
300                         );
301                         int readTimeout = (int) rif.getDownloadTimeout( ).toMillis( ) * 1000;
302                         wagon.setReadTimeout( readTimeout );
303                         wagon.setTimeout( (int) remoteRepository.getTimeout( ).toMillis( ) * 1000 );
304
305                         if ( wagon instanceof AbstractHttpClientWagon )
306                         {
307                             HttpConfiguration httpConfiguration = new HttpConfiguration( );
308                             HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration( );
309                             httpMethodConfiguration.setUsePreemptive( true );
310                             httpMethodConfiguration.setReadTimeout( readTimeout );
311                             httpConfiguration.setGet( httpMethodConfiguration );
312                             AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
313                         }
314
315                         wagon.addTransferListener( new DownloadListener( ) );
316                         ProxyInfo proxyInfo = null;
317                         if ( networkProxy != null )
318                         {
319                             proxyInfo = new ProxyInfo( );
320                             proxyInfo.setType( networkProxy.getProtocol( ) );
321                             proxyInfo.setHost( networkProxy.getHost( ) );
322                             proxyInfo.setPort( networkProxy.getPort( ) );
323                             proxyInfo.setUserName( networkProxy.getUsername( ) );
324                             proxyInfo.setPassword( networkProxy.getPassword( ) );
325                         }
326                         AuthenticationInfo authenticationInfo = null;
327                         if ( remoteRepository.getLoginCredentials( ) != null && ( remoteRepository.getLoginCredentials( ) instanceof PasswordCredentials ) )
328                         {
329                             PasswordCredentials creds = (PasswordCredentials) remoteRepository.getLoginCredentials( );
330                             authenticationInfo = new AuthenticationInfo( );
331                             authenticationInfo.setUserName( creds.getUsername( ) );
332                             authenticationInfo.setPassword( new String( creds.getPassword( ) ) );
333                         }
334                         wagon.connect( new org.apache.maven.wagon.repository.Repository( remoteRepository.getId( ), baseIndexUrl ), authenticationInfo,
335                             proxyInfo );
336
337                         Path indexDirectory = indexingContext.getIndexDirectoryFile( ).toPath( );
338                         if ( !Files.exists( indexDirectory ) )
339                         {
340                             Files.createDirectories( indexDirectory );
341                         }
342
343                         ResourceFetcher resourceFetcher =
344                             new WagonResourceFetcher( log, tempIndexDirectory, wagon, remoteRepository );
345                         IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher );
346                         request.setForceFullUpdate( fullUpdate );
347                         request.setLocalIndexCacheDir( indexCacheDirectory.toFile( ) );
348
349                         indexUpdater.fetchAndUpdateIndex( request );
350
351                         indexingContext.updateTimestamp( true );
352                     }
353
354                 }
355                 catch ( AuthenticationException e )
356                 {
357                     log.error( "Could not login to the remote proxy for updating index of {}", remoteRepository.getId( ), e );
358                     throw new IndexUpdateFailedException( "Login in to proxy failed while updating remote repository " + remoteRepository.getId( ), e );
359                 }
360                 catch ( ConnectionException e )
361                 {
362                     log.error( "Connection error during index update for remote repository {}", remoteRepository.getId( ), e );
363                     throw new IndexUpdateFailedException( "Connection error during index update for remote repository " + remoteRepository.getId( ), e );
364                 }
365                 catch ( MalformedURLException e )
366                 {
367                     log.error( "URL for remote index update of remote repository {} is not correct {}", remoteRepository.getId( ), remoteUpdateUri, e );
368                     throw new IndexUpdateFailedException( "URL for remote index update of repository is not correct " + remoteUpdateUri, e );
369                 }
370                 catch ( IOException e )
371                 {
372                     log.error( "IOException during index update of remote repository {}: {}", remoteRepository.getId( ), e.getMessage( ), e );
373                     throw new IndexUpdateFailedException( "IOException during index update of remote repository " + remoteRepository.getId( )
374                         + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
375                 }
376                 catch ( WagonFactoryException e )
377                 {
378                     log.error( "Wagon for remote index download of {} could not be created: {}", remoteRepository.getId( ), e.getMessage( ), e );
379                     throw new IndexUpdateFailedException( "Error while updating the remote index of " + remoteRepository.getId( ), e );
380                 }
381             } );
382
383     }
384
385     @Override
386     public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<Path> artifactReference ) throws IndexUpdateFailedException
387     {
388         executeUpdateFunction(context, indexingContext -> {
389             Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList());
390             try {
391                 indexer.addArtifactsToIndex(artifacts, indexingContext);
392             } catch (IOException e) {
393                 log.error("IOException while adding artifact {}", e.getMessage(), e);
394                 throw new IndexUpdateFailedException("Error occured while adding artifact to index of "+context.getId()
395                 + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
396             }
397         });
398     }
399
400     @Override
401     public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<Path> artifactReference ) throws IndexUpdateFailedException
402     {
403         executeUpdateFunction(context, indexingContext -> {
404             Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList());
405             try {
406                 indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
407             } catch (IOException e) {
408                 log.error("IOException while removing artifact {}", e.getMessage(), e);
409                 throw new IndexUpdateFailedException("Error occured while removing artifact from index of "+context.getId()
410                         + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
411             }
412         });
413
414     }
415
416     @Override
417     public boolean supportsRepository( RepositoryType type )
418     {
419         return type == RepositoryType.MAVEN;
420     }
421
422     @Override
423     public ArchivaIndexingContext createContext( Repository repository ) throws IndexCreationFailedException
424     {
425
426         if ( repository.getType( ) != RepositoryType.MAVEN )
427         {
428             throw new UnsupportedRepositoryTypeException( repository.getType( ) );
429         }
430         IndexingContext mvnCtx = null;
431         try
432         {
433             if ( repository instanceof RemoteRepository )
434             {
435                 mvnCtx = createRemoteContext( (RemoteRepository) repository );
436             }
437             else if ( repository instanceof ManagedRepository )
438             {
439                 mvnCtx = createManagedContext( (ManagedRepository) repository );
440             }
441         }
442         catch ( IOException e )
443         {
444             log.error( "IOException during context creation " + e.getMessage( ), e );
445             throw new IndexCreationFailedException( "Could not create index context for repository " + repository.getId( )
446                 + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
447         }
448         MavenIndexContext context = new MavenIndexContext( repository, mvnCtx );
449         return context;
450     }
451
452     private IndexingContext createRemoteContext( RemoteRepository remoteRepository ) throws IOException
453     {
454         Path appServerBase = archivaConfiguration.getAppServerBaseDir( );
455
456         String contextKey = "remote-" + remoteRepository.getId( );
457
458         // create remote repository path
459         Path repoDir = appServerBase.resolve( "data" ).resolve( "remotes" ).resolve( remoteRepository.getId( ) );
460         if ( !Files.exists( repoDir ) )
461         {
462             Files.createDirectories( repoDir );
463         }
464
465         Path indexDirectory;
466
467         // is there configured indexDirectory ?
468         if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
469         {
470             RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
471             indexDirectory = PathUtil.getPathFromUri( rif.getIndexUri( ) );
472             if ( !indexDirectory.isAbsolute( ) )
473             {
474                 indexDirectory = repoDir.resolve( indexDirectory );
475             }
476
477             // if not configured use a default value
478             if ( indexDirectory == null )
479             {
480                 indexDirectory = repoDir.resolve( ".index" );
481             }
482             if ( !Files.exists( indexDirectory ) )
483             {
484                 Files.createDirectories( indexDirectory );
485             }
486
487             String remoteIndexUrl = calculateIndexRemoteUrl( remoteRepository.getLocation( ), rif );
488             try
489             {
490
491                 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
492             }
493             catch ( IndexFormatTooOldException e )
494             {
495                 // existing index with an old lucene format so we need to delete it!!!
496                 // delete it first then recreate it.
497                 log.warn( "the index of repository {} is too old we have to delete and recreate it", //
498                     remoteRepository.getId( ) );
499                 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDirectory );
500                 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
501
502             }
503         }
504         else
505         {
506             throw new IOException( "No remote index defined" );
507         }
508     }
509
510     private IndexingContext getIndexingContext( Repository repository, String contextKey, Path repoDir, Path indexDirectory, String indexUrl ) throws IOException
511     {
512         return indexer.createIndexingContext( contextKey, repository.getId( ), repoDir.toFile( ), indexDirectory.toFile( ),
513             repository.getLocation( ) == null ? null : repository.getLocation( ).toString( ),
514             indexUrl,
515             true, false,
516             indexCreators );
517     }
518
519     private IndexingContext createManagedContext( ManagedRepository repository ) throws IOException
520     {
521
522         IndexingContext context;
523         // take care first about repository location as can be relative
524         Path repositoryDirectory = repository.getLocalPath();
525
526         if ( !Files.exists( repositoryDirectory ) )
527         {
528             try
529             {
530                 Files.createDirectories( repositoryDirectory );
531             }
532             catch ( IOException e )
533             {
534                 log.error( "Could not create directory {}", repositoryDirectory );
535             }
536         }
537
538
539         if ( repository.supportsFeature( IndexCreationFeature.class ) )
540         {
541             IndexCreationFeature icf = repository.getFeature( IndexCreationFeature.class ).get( );
542             URI indexDir = icf.getIndexPath( );
543             //File managedRepository = new File( repository.getLocation() );
544
545             Path indexDirectory = null;
546             if ( indexDir != null && !"".equals( indexDir.toString( ) ) )
547             {
548
549                 indexDirectory = PathUtil.getPathFromUri( indexDir );
550                 // not absolute so create it in repository directory
551                 if ( !indexDirectory.isAbsolute( ) )
552                 {
553                     indexDirectory = repositoryDirectory.resolve( indexDirectory );
554                 }
555                 icf.setIndexPath( indexDirectory.normalize( ).toUri( ) );
556             }
557             else
558             {
559                 indexDirectory = repositoryDirectory.resolve( ".indexer" );
560                 icf.setIndexPath( indexDirectory.toUri( ) );
561             }
562
563             if ( !Files.exists( indexDirectory ) )
564             {
565                 Files.createDirectories( indexDirectory );
566             }
567
568             String indexUrl = repositoryDirectory.toUri( ).toURL( ).toExternalForm( );
569             try
570             {
571                 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
572                 context.setSearchable( repository.isScanned( ) );
573             }
574             catch ( IndexFormatTooOldException e )
575             {
576                 // existing index with an old lucene format so we need to delete it!!!
577                 // delete it first then recreate it.
578                 log.warn( "the index of repository {} is too old we have to delete and recreate it", //
579                     repository.getId( ) );
580                 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDirectory );
581                 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
582                 context.setSearchable( repository.isScanned( ) );
583             }
584             return context;
585         }
586         else
587         {
588             throw new IOException( "No repository index defined" );
589         }
590     }
591
592     private String calculateIndexRemoteUrl( URI baseUri, RemoteIndexFeature rif )
593     {
594         if ( rif.getIndexUri( ) == null )
595         {
596             return baseUri.resolve( ".index" ).toString( );
597         }
598         else
599         {
600             return baseUri.resolve( rif.getIndexUri( ) ).toString( );
601         }
602     }
603
604     private static final class DownloadListener
605         implements TransferListener
606     {
607         private Logger log = LoggerFactory.getLogger( getClass( ) );
608
609         private String resourceName;
610
611         private long startTime;
612
613         private int totalLength = 0;
614
615         @Override
616         public void transferInitiated( TransferEvent transferEvent )
617         {
618             startTime = System.currentTimeMillis( );
619             resourceName = transferEvent.getResource( ).getName( );
620             log.debug( "initiate transfer of {}", resourceName );
621         }
622
623         @Override
624         public void transferStarted( TransferEvent transferEvent )
625         {
626             this.totalLength = 0;
627             resourceName = transferEvent.getResource( ).getName( );
628             log.info( "start transfer of {}", transferEvent.getResource( ).getName( ) );
629         }
630
631         @Override
632         public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
633         {
634             log.debug( "transfer of {} : {}/{}", transferEvent.getResource( ).getName( ), buffer.length, length );
635             this.totalLength += length;
636         }
637
638         @Override
639         public void transferCompleted( TransferEvent transferEvent )
640         {
641             resourceName = transferEvent.getResource( ).getName( );
642             long endTime = System.currentTimeMillis( );
643             log.info( "end of transfer file {} {} kb: {}s", transferEvent.getResource( ).getName( ),
644                 this.totalLength / 1024, ( endTime - startTime ) / 1000 );
645         }
646
647         @Override
648         public void transferError( TransferEvent transferEvent )
649         {
650             log.info( "error of transfer file {}: {}", transferEvent.getResource( ).getName( ),
651                 transferEvent.getException( ).getMessage( ), transferEvent.getException( ) );
652         }
653
654         @Override
655         public void debug( String message )
656         {
657             log.debug( "transfer debug {}", message );
658         }
659     }
660
661     private static class WagonResourceFetcher
662         implements ResourceFetcher
663     {
664
665         Logger log;
666
667         Path tempIndexDirectory;
668
669         Wagon wagon;
670
671         RemoteRepository remoteRepository;
672
673         private WagonResourceFetcher( Logger log, Path tempIndexDirectory, Wagon wagon,
674                                       RemoteRepository remoteRepository )
675         {
676             this.log = log;
677             this.tempIndexDirectory = tempIndexDirectory;
678             this.wagon = wagon;
679             this.remoteRepository = remoteRepository;
680         }
681
682         @Override
683         public void connect( String id, String url )
684             throws IOException
685         {
686             //no op
687         }
688
689         @Override
690         public void disconnect( )
691             throws IOException
692         {
693             // no op
694         }
695
696         @Override
697         public InputStream retrieve( String name )
698             throws IOException, FileNotFoundException
699         {
700             try
701             {
702                 log.info( "index update retrieve file, name:{}", name );
703                 Path file = tempIndexDirectory.resolve( name );
704                 Files.deleteIfExists( file );
705                 file.toFile( ).deleteOnExit( );
706                 wagon.get( addParameters( name, remoteRepository ), file.toFile( ) );
707                 return Files.newInputStream( file );
708             }
709             catch ( AuthorizationException | TransferFailedException e )
710             {
711                 throw new IOException( e.getMessage( ), e );
712             }
713             catch ( ResourceDoesNotExistException e )
714             {
715                 FileNotFoundException fnfe = new FileNotFoundException( e.getMessage( ) );
716                 fnfe.initCause( e );
717                 throw fnfe;
718             }
719         }
720
721         // FIXME remove crappy copy/paste
722         protected String addParameters( String path, RemoteRepository remoteRepository )
723         {
724             if ( remoteRepository.getExtraParameters( ).isEmpty( ) )
725             {
726                 return path;
727             }
728
729             boolean question = false;
730
731             StringBuilder res = new StringBuilder( path == null ? "" : path );
732
733             for ( Map.Entry<String, String> entry : remoteRepository.getExtraParameters( ).entrySet( ) )
734             {
735                 if ( !question )
736                 {
737                     res.append( '?' ).append( entry.getKey( ) ).append( '=' ).append( entry.getValue( ) );
738                 }
739             }
740
741             return res.toString( );
742         }
743
744     }
745 }