]> source.dussan.org Git - archiva.git/blob
d54f868a0ed14f005988f1b7d3597757498b56cb
[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.proxy.common.WagonFactory;
33 import org.apache.archiva.proxy.common.WagonFactoryException;
34 import org.apache.archiva.proxy.common.WagonFactoryRequest;
35 import org.apache.archiva.repository.ManagedRepository;
36 import org.apache.archiva.repository.PasswordCredentials;
37 import org.apache.archiva.repository.RemoteRepository;
38 import org.apache.archiva.repository.Repository;
39 import org.apache.archiva.repository.RepositoryType;
40 import org.apache.archiva.repository.UnsupportedRepositoryTypeException;
41 import org.apache.archiva.repository.features.IndexCreationFeature;
42 import org.apache.archiva.repository.features.RemoteIndexFeature;
43 import org.apache.commons.lang.StringUtils;
44 import org.apache.maven.index.*;
45 import org.apache.maven.index.context.IndexCreator;
46 import org.apache.maven.index.context.IndexingContext;
47 import org.apache.maven.index.packer.IndexPacker;
48 import org.apache.maven.index.packer.IndexPackingRequest;
49 import org.apache.maven.index.updater.IndexUpdateRequest;
50 import org.apache.maven.index.updater.IndexUpdater;
51 import org.apache.maven.index.updater.ResourceFetcher;
52 import org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException;
53 import org.apache.maven.wagon.ConnectionException;
54 import org.apache.maven.wagon.ResourceDoesNotExistException;
55 import org.apache.maven.wagon.StreamWagon;
56 import org.apache.maven.wagon.TransferFailedException;
57 import org.apache.maven.wagon.Wagon;
58 import org.apache.maven.wagon.authentication.AuthenticationException;
59 import org.apache.maven.wagon.authentication.AuthenticationInfo;
60 import org.apache.maven.wagon.authorization.AuthorizationException;
61 import org.apache.maven.wagon.events.TransferEvent;
62 import org.apache.maven.wagon.events.TransferListener;
63 import org.apache.maven.wagon.proxy.ProxyInfo;
64 import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
65 import org.apache.maven.wagon.shared.http.HttpConfiguration;
66 import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
69 import org.springframework.stereotype.Service;
70
71 import javax.inject.Inject;
72 import java.io.FileNotFoundException;
73 import java.io.IOException;
74 import java.io.InputStream;
75 import java.net.MalformedURLException;
76 import java.net.URI;
77 import java.nio.file.Files;
78 import java.nio.file.Path;
79 import java.nio.file.Paths;
80 import java.util.Collection;
81 import java.util.List;
82 import java.util.Map;
83 import java.util.concurrent.ConcurrentSkipListSet;
84 import java.util.stream.Collectors;
85
86 /**
87  * Maven implementation of index manager.
88  * The index manager is a singleton, so we try to make sure, that index operations are not running
89  * parallel by synchronizing on the index path.
90  * A update operation waits for parallel running methods to finish before starting, but after a certain
91  * time of retries a IndexUpdateFailedException is thrown.
92  */
93 @Service( "archivaIndexManager#maven" )
94 public class MavenIndexManager implements ArchivaIndexManager
95 {
96
97     private static final Logger log = LoggerFactory.getLogger( MavenIndexManager.class );
98
99     @Inject
100     private Indexer indexer;
101
102     @Inject
103     private IndexerEngine indexerEngine;
104
105     @Inject
106     private List<? extends IndexCreator> indexCreators;
107
108     @Inject
109     private IndexPacker indexPacker;
110
111     @Inject
112     private Scanner scanner;
113
114     @Inject
115     private ArchivaConfiguration archivaConfiguration;
116
117     @Inject
118     private WagonFactory wagonFactory;
119
120     @Inject
121     private NetworkProxyAdmin networkProxyAdmin;
122
123     @Inject
124     private IndexUpdater indexUpdater;
125
126     @Inject
127     private ArtifactContextProducer artifactContextProducer;
128
129     private ConcurrentSkipListSet<Path> activeContexts = new ConcurrentSkipListSet<>( );
130
131     private static final int WAIT_TIME = 100;
132     private static final int MAX_WAIT = 10;
133
134
135     public static IndexingContext getMvnContext( ArchivaIndexingContext context ) throws UnsupportedBaseContextException
136     {
137         if ( !context.supports( IndexingContext.class ) )
138         {
139             log.error( "The provided archiva index context does not support the maven IndexingContext" );
140             throw new UnsupportedBaseContextException( "The context does not support the Maven IndexingContext" );
141         }
142         return context.getBaseContext( IndexingContext.class );
143     }
144
145     private Path getIndexPath( ArchivaIndexingContext ctx )
146     {
147         return PathUtil.getPathFromUri( ctx.getPath( ) );
148     }
149
150     @FunctionalInterface
151     interface IndexUpdateConsumer
152     {
153
154         void accept( IndexingContext indexingContext ) throws IndexUpdateFailedException;
155     }
156
157     /*
158      * This method is used to do some actions around the update execution code. And to make sure, that no other
159      * method is running on the same index.
160      */
161     private void executeUpdateFunction( ArchivaIndexingContext context, IndexUpdateConsumer function ) throws IndexUpdateFailedException
162     {
163         IndexingContext indexingContext = null;
164         try
165         {
166             indexingContext = getMvnContext( context );
167         }
168         catch ( UnsupportedBaseContextException e )
169         {
170             throw new IndexUpdateFailedException( "Maven index is not supported by this context", e );
171         }
172         final Path ctxPath = getIndexPath( context );
173         int loop = MAX_WAIT;
174         boolean active = false;
175         while ( loop-- > 0 && !active )
176         {
177             active = activeContexts.add( ctxPath );
178             try
179             {
180                 Thread.currentThread( ).sleep( WAIT_TIME );
181             }
182             catch ( InterruptedException e )
183             {
184                 // Ignore this
185             }
186         }
187         if ( active )
188         {
189             try
190             {
191                 function.accept( indexingContext );
192             }
193             finally
194             {
195                 activeContexts.remove( ctxPath );
196             }
197         }
198         else
199         {
200             throw new IndexUpdateFailedException( "Timeout while waiting for index release on context " + context.getId( ) );
201         }
202     }
203
204     @Override
205     public void pack( final ArchivaIndexingContext context ) throws IndexUpdateFailedException
206     {
207         executeUpdateFunction( context, indexingContext -> {
208                 try
209                 {
210                     IndexPackingRequest request = new IndexPackingRequest( indexingContext,
211                         indexingContext.acquireIndexSearcher( ).getIndexReader( ),
212                         indexingContext.getIndexDirectoryFile( ) );
213                     indexPacker.packIndex( request );
214                     indexingContext.updateTimestamp( true );
215                 }
216                 catch ( IOException e )
217                 {
218                     log.error( "IOException while packing index of context " + context.getId( ) + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ) );
219                     throw new IndexUpdateFailedException( "IOException during update of " + context.getId( ), e );
220                 }
221             }
222         );
223
224     }
225
226     @Override
227     public void scan(final ArchivaIndexingContext context) throws IndexUpdateFailedException
228     {
229         executeUpdateFunction( context, indexingContext -> {
230             DefaultScannerListener listener = new DefaultScannerListener( indexingContext, indexerEngine, true, null );
231             ScanningRequest request = new ScanningRequest( indexingContext, listener );
232             ScanningResult result = scanner.scan( request );
233             if ( result.hasExceptions( ) )
234             {
235                 log.error( "Exceptions occured during index scan of " + context.getId( ) );
236                 result.getExceptions( ).stream( ).map( e -> e.getMessage( ) ).distinct( ).limit( 5 ).forEach(
237                     s -> log.error( "Message: " + s )
238                 );
239             }
240
241         } );
242     }
243
244     @Override
245     public void update( final ArchivaIndexingContext context, final URI remoteUpdateUri, final boolean fullUpdate ) throws IndexUpdateFailedException
246     {
247         log.info( "start download remote index for remote repository {}", context.getRepository( ).getId( ) );
248
249         if ( !( context.getRepository( ) instanceof RemoteRepository ) )
250         {
251             throw new IndexUpdateFailedException( "The context is not associated to a remote repository " + context.getId( ) );
252         }
253         final RemoteRepository remoteRepository = (RemoteRepository) context.getRepository( );
254
255         executeUpdateFunction( context,
256             indexingContext -> {
257                 try
258                 {
259                     // create a temp directory to download files
260                     Path tempIndexDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".tmpIndex" );
261                     Path indexCacheDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".indexCache" );
262                     Files.createDirectories( indexCacheDirectory );
263                     if ( Files.exists( tempIndexDirectory ) )
264                     {
265                         org.apache.archiva.common.utils.FileUtils.deleteDirectory( tempIndexDirectory );
266                     }
267                     Files.createDirectories( tempIndexDirectory );
268                     tempIndexDirectory.toFile( ).deleteOnExit( );
269                     String baseIndexUrl = indexingContext.getIndexUpdateUrl( );
270
271                     String wagonProtocol = remoteUpdateUri.toURL( ).getProtocol( );
272
273                     NetworkProxy networkProxy = null;
274                     if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
275                     {
276                         RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
277                         if ( StringUtils.isNotBlank( rif.getProxyId( ) ) )
278                         {
279                             try
280                             {
281                                 networkProxy = networkProxyAdmin.getNetworkProxy( rif.getProxyId( ) );
282                             }
283                             catch ( RepositoryAdminException e )
284                             {
285                                 log.error( "Error occured while retrieving proxy {}", e.getMessage( ) );
286                             }
287                             if ( networkProxy == null )
288                             {
289                                 log.warn(
290                                     "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
291                                     rif.getProxyId( ) );
292                             }
293                         }
294
295                         final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(
296                             new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders( ) ).networkProxy(
297                                 networkProxy )
298                         );
299                         int readTimeout = (int) rif.getDownloadTimeout( ).toMillis( ) * 1000;
300                         wagon.setReadTimeout( readTimeout );
301                         wagon.setTimeout( (int) remoteRepository.getTimeout( ).toMillis( ) * 1000 );
302
303                         if ( wagon instanceof AbstractHttpClientWagon )
304                         {
305                             HttpConfiguration httpConfiguration = new HttpConfiguration( );
306                             HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration( );
307                             httpMethodConfiguration.setUsePreemptive( true );
308                             httpMethodConfiguration.setReadTimeout( readTimeout );
309                             httpConfiguration.setGet( httpMethodConfiguration );
310                             AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
311                         }
312
313                         wagon.addTransferListener( new DownloadListener( ) );
314                         ProxyInfo proxyInfo = null;
315                         if ( networkProxy != null )
316                         {
317                             proxyInfo = new ProxyInfo( );
318                             proxyInfo.setType( networkProxy.getProtocol( ) );
319                             proxyInfo.setHost( networkProxy.getHost( ) );
320                             proxyInfo.setPort( networkProxy.getPort( ) );
321                             proxyInfo.setUserName( networkProxy.getUsername( ) );
322                             proxyInfo.setPassword( networkProxy.getPassword( ) );
323                         }
324                         AuthenticationInfo authenticationInfo = null;
325                         if ( remoteRepository.getLoginCredentials( ) != null && ( remoteRepository.getLoginCredentials( ) instanceof PasswordCredentials ) )
326                         {
327                             PasswordCredentials creds = (PasswordCredentials) remoteRepository.getLoginCredentials( );
328                             authenticationInfo = new AuthenticationInfo( );
329                             authenticationInfo.setUserName( creds.getUsername( ) );
330                             authenticationInfo.setPassword( new String( creds.getPassword( ) ) );
331                         }
332                         wagon.connect( new org.apache.maven.wagon.repository.Repository( remoteRepository.getId( ), baseIndexUrl ), authenticationInfo,
333                             proxyInfo );
334
335                         Path indexDirectory = indexingContext.getIndexDirectoryFile( ).toPath( );
336                         if ( !Files.exists( indexDirectory ) )
337                         {
338                             Files.createDirectories( indexDirectory );
339                         }
340
341                         ResourceFetcher resourceFetcher =
342                             new WagonResourceFetcher( log, tempIndexDirectory, wagon, remoteRepository );
343                         IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher );
344                         request.setForceFullUpdate( fullUpdate );
345                         request.setLocalIndexCacheDir( indexCacheDirectory.toFile( ) );
346
347                         indexUpdater.fetchAndUpdateIndex( request );
348
349                         indexingContext.updateTimestamp( true );
350                     }
351
352                 }
353                 catch ( AuthenticationException e )
354                 {
355                     log.error( "Could not login to the remote proxy for updating index of {}", remoteRepository.getId( ), e );
356                     throw new IndexUpdateFailedException( "Login in to proxy failed while updating remote repository " + remoteRepository.getId( ), e );
357                 }
358                 catch ( ConnectionException e )
359                 {
360                     log.error( "Connection error during index update for remote repository {}", remoteRepository.getId( ), e );
361                     throw new IndexUpdateFailedException( "Connection error during index update for remote repository " + remoteRepository.getId( ), e );
362                 }
363                 catch ( MalformedURLException e )
364                 {
365                     log.error( "URL for remote index update of remote repository {} is not correct {}", remoteRepository.getId( ), remoteUpdateUri, e );
366                     throw new IndexUpdateFailedException( "URL for remote index update of repository is not correct " + remoteUpdateUri, e );
367                 }
368                 catch ( IOException e )
369                 {
370                     log.error( "IOException during index update of remote repository {}: {}", remoteRepository.getId( ), e.getMessage( ), e );
371                     throw new IndexUpdateFailedException( "IOException during index update of remote repository " + remoteRepository.getId( )
372                         + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
373                 }
374                 catch ( WagonFactoryException e )
375                 {
376                     log.error( "Wagon for remote index download of {} could not be created: {}", remoteRepository.getId( ), e.getMessage( ), e );
377                     throw new IndexUpdateFailedException( "Error while updating the remote index of " + remoteRepository.getId( ), e );
378                 }
379             } );
380
381     }
382
383     @Override
384     public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<URI> artifactReference ) throws IndexUpdateFailedException
385     {
386         final URI ctxUri = context.getPath();
387         executeUpdateFunction(context, indexingContext -> {
388             Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(r)).toFile())).collect(Collectors.toList());
389             try {
390                 indexer.addArtifactsToIndex(artifacts, indexingContext);
391             } catch (IOException e) {
392                 log.error("IOException while adding artifact {}", e.getMessage(), e);
393                 throw new IndexUpdateFailedException("Error occured while adding artifact to index of "+context.getId()
394                 + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
395             }
396         });
397     }
398
399     @Override
400     public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<URI> artifactReference ) throws IndexUpdateFailedException
401     {
402         final URI ctxUri = context.getPath();
403         executeUpdateFunction(context, indexingContext -> {
404             Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(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 }