1 package org.apache.archiva.admin.mock;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.common.filelock.DefaultFileLockManager;
23 import org.apache.archiva.common.utils.PathUtil;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.indexer.ArchivaIndexManager;
26 import org.apache.archiva.indexer.ArchivaIndexingContext;
27 import org.apache.archiva.indexer.IndexCreationFailedException;
28 import org.apache.archiva.indexer.IndexUpdateFailedException;
29 import org.apache.archiva.indexer.UnsupportedBaseContextException;
30 import org.apache.archiva.proxy.ProxyRegistry;
31 import org.apache.archiva.maven.common.proxy.WagonFactory;
32 import org.apache.archiva.maven.common.proxy.WagonFactoryException;
33 import org.apache.archiva.maven.common.proxy.WagonFactoryRequest;
34 import org.apache.archiva.proxy.model.NetworkProxy;
35 import org.apache.archiva.repository.EditableRepository;
36 import org.apache.archiva.repository.ManagedRepository;
37 import org.apache.archiva.repository.base.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.storage.fs.FilesystemAsset;
43 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
44 import org.apache.archiva.repository.storage.StorageAsset;
45 import org.apache.archiva.repository.features.IndexCreationFeature;
46 import org.apache.archiva.repository.features.RemoteIndexFeature;
47 import org.apache.commons.lang3.StringUtils;
48 import org.apache.maven.index.ArtifactContext;
49 import org.apache.maven.index.ArtifactContextProducer;
50 import org.apache.maven.index.DefaultScannerListener;
51 import org.apache.maven.index.Indexer;
52 import org.apache.maven.index.IndexerEngine;
53 import org.apache.maven.index.Scanner;
54 import org.apache.maven.index.ScanningRequest;
55 import org.apache.maven.index.ScanningResult;
56 import org.apache.maven.index.context.IndexCreator;
57 import org.apache.maven.index.context.IndexingContext;
58 import org.apache.maven.index.packer.IndexPacker;
59 import org.apache.maven.index.packer.IndexPackingRequest;
60 import org.apache.maven.index.updater.IndexUpdateRequest;
61 import org.apache.maven.index.updater.ResourceFetcher;
62 import org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException;
63 import org.apache.maven.wagon.ConnectionException;
64 import org.apache.maven.wagon.ResourceDoesNotExistException;
65 import org.apache.maven.wagon.StreamWagon;
66 import org.apache.maven.wagon.TransferFailedException;
67 import org.apache.maven.wagon.Wagon;
68 import org.apache.maven.wagon.authentication.AuthenticationException;
69 import org.apache.maven.wagon.authentication.AuthenticationInfo;
70 import org.apache.maven.wagon.authorization.AuthorizationException;
71 import org.apache.maven.wagon.events.TransferEvent;
72 import org.apache.maven.wagon.events.TransferListener;
73 import org.apache.maven.wagon.proxy.ProxyInfo;
74 import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
75 import org.apache.maven.wagon.shared.http.HttpConfiguration;
76 import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
79 import org.springframework.stereotype.Service;
81 import javax.inject.Inject;
82 import java.io.FileNotFoundException;
83 import java.io.IOException;
84 import java.io.InputStream;
85 import java.net.MalformedURLException;
87 import java.nio.file.Files;
88 import java.nio.file.Path;
89 import java.nio.file.Paths;
90 import java.util.Collection;
91 import java.util.List;
93 import java.util.concurrent.ConcurrentSkipListSet;
94 import java.util.stream.Collectors;
96 @Service("archivaIndexManager#maven")
97 public class ArchivaIndexManagerMock implements ArchivaIndexManager {
99 private static final Logger log = LoggerFactory.getLogger( ArchivaIndexManagerMock.class );
102 private Indexer indexer;
105 private IndexerEngine indexerEngine;
108 private List<? extends IndexCreator> indexCreators;
111 private IndexPacker indexPacker;
114 private Scanner scanner;
117 private ArchivaConfiguration archivaConfiguration;
120 private WagonFactory wagonFactory;
123 ProxyRegistry proxyRegistry;
126 private ArtifactContextProducer artifactContextProducer;
128 private ConcurrentSkipListSet<Path> activeContexts = new ConcurrentSkipListSet<>( );
130 private static final int WAIT_TIME = 100;
131 private static final int MAX_WAIT = 10;
134 public static IndexingContext getMvnContext(ArchivaIndexingContext context ) throws UnsupportedBaseContextException
136 if ( !context.supports( IndexingContext.class ) )
138 log.error( "The provided archiva index context does not support the maven IndexingContext" );
139 throw new UnsupportedBaseContextException( "The context does not support the Maven IndexingContext" );
141 return context.getBaseContext( IndexingContext.class );
144 private Path getIndexPath( ArchivaIndexingContext ctx )
146 return ctx.getPath( ).getFilePath();
150 interface IndexUpdateConsumer
153 void accept( IndexingContext indexingContext ) throws IndexUpdateFailedException;
157 * This method is used to do some actions around the update execution code. And to make sure, that no other
158 * method is running on the same index.
160 private void executeUpdateFunction( ArchivaIndexingContext context, IndexUpdateConsumer function ) throws IndexUpdateFailedException
162 IndexingContext indexingContext = null;
165 indexingContext = getMvnContext( context );
167 catch ( UnsupportedBaseContextException e )
169 throw new IndexUpdateFailedException( "Maven index is not supported by this context", e );
171 final Path ctxPath = getIndexPath( context );
173 boolean active = false;
174 while ( loop-- > 0 && !active )
176 active = activeContexts.add( ctxPath );
179 Thread.currentThread( ).sleep( WAIT_TIME );
181 catch ( InterruptedException e )
190 function.accept( indexingContext );
194 activeContexts.remove( ctxPath );
199 throw new IndexUpdateFailedException( "Timeout while waiting for index release on context " + context.getId( ) );
204 public void pack( final ArchivaIndexingContext context ) throws IndexUpdateFailedException
206 executeUpdateFunction( context, indexingContext -> {
209 IndexPackingRequest request = new IndexPackingRequest( indexingContext,
210 indexingContext.acquireIndexSearcher( ).getIndexReader( ),
211 indexingContext.getIndexDirectoryFile( ) );
212 indexPacker.packIndex( request );
213 indexingContext.updateTimestamp( true );
215 catch ( IOException e )
217 log.error( "IOException while packing index of context " + context.getId( ) + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ) );
218 throw new IndexUpdateFailedException( "IOException during update of " + context.getId( ), e );
226 public void scan(final ArchivaIndexingContext context) throws IndexUpdateFailedException
228 executeUpdateFunction( context, indexingContext -> {
229 DefaultScannerListener listener = new DefaultScannerListener( indexingContext, indexerEngine, true, null );
230 ScanningRequest request = new ScanningRequest( indexingContext, listener );
231 ScanningResult result = scanner.scan( request );
232 if ( result.hasExceptions( ) )
234 log.error( "Exceptions occured during index scan of " + context.getId( ) );
235 result.getExceptions( ).stream( ).map( e -> e.getMessage( ) ).distinct( ).limit( 5 ).forEach(
236 s -> log.error( "Message: " + s )
244 public void update(final ArchivaIndexingContext context, final boolean fullUpdate) throws IndexUpdateFailedException
246 log.info( "start download remote index for remote repository {}", context.getRepository( ).getId( ) );
248 if ( !( context.getRepository( ) instanceof RemoteRepository) || !(context.getRepository().supportsFeature(RemoteIndexFeature.class)) )
250 throw new IndexUpdateFailedException( "The context is not associated to a remote repository with remote index " + context.getId( ) );
252 RemoteIndexFeature rif = context.getRepository().getFeature(RemoteIndexFeature.class).get();
253 remoteUpdateUri = context.getRepository().getLocation().resolve(rif.getIndexUri());
255 final RemoteRepository remoteRepository = (RemoteRepository) context.getRepository( );
257 executeUpdateFunction( context,
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 ) )
267 org.apache.archiva.common.utils.FileUtils.deleteDirectory( tempIndexDirectory );
269 Files.createDirectories( tempIndexDirectory );
270 tempIndexDirectory.toFile( ).deleteOnExit( );
271 String baseIndexUrl = indexingContext.getIndexUpdateUrl( );
273 String wagonProtocol = remoteUpdateUri.toURL( ).getProtocol( );
275 NetworkProxy networkProxy = null;
276 if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
278 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
279 if ( StringUtils.isNotBlank( rif.getProxyId( ) ) )
281 networkProxy = proxyRegistry.getNetworkProxy( rif.getProxyId( ) );
282 if ( networkProxy == null )
285 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
290 final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(
291 new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders( ) ).networkProxy(
294 int readTimeout = (int) rif.getDownloadTimeout( ).toMillis( ) * 1000;
295 wagon.setReadTimeout( readTimeout );
296 wagon.setTimeout( (int) remoteRepository.getTimeout( ).toMillis( ) * 1000 );
298 if ( wagon instanceof AbstractHttpClientWagon)
300 HttpConfiguration httpConfiguration = new HttpConfiguration( );
301 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration( );
302 httpMethodConfiguration.setUsePreemptive( true );
303 httpMethodConfiguration.setReadTimeout( readTimeout );
304 httpConfiguration.setGet( httpMethodConfiguration );
305 AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
308 wagon.addTransferListener( new DownloadListener( ) );
309 ProxyInfo proxyInfo = null;
310 if ( networkProxy != null )
312 proxyInfo = new ProxyInfo( );
313 proxyInfo.setType( networkProxy.getProtocol( ) );
314 proxyInfo.setHost( networkProxy.getHost( ) );
315 proxyInfo.setPort( networkProxy.getPort( ) );
316 proxyInfo.setUserName( networkProxy.getUsername( ) );
317 proxyInfo.setPassword(new String(networkProxy.getPassword()));
319 AuthenticationInfo authenticationInfo = null;
320 if ( remoteRepository.getLoginCredentials( ) != null && ( remoteRepository.getLoginCredentials( ) instanceof PasswordCredentials) )
322 PasswordCredentials creds = (PasswordCredentials) remoteRepository.getLoginCredentials( );
323 authenticationInfo = new AuthenticationInfo( );
324 authenticationInfo.setUserName( creds.getUsername( ) );
325 authenticationInfo.setPassword( new String( creds.getPassword( ) ) );
327 wagon.connect( new org.apache.maven.wagon.repository.Repository( remoteRepository.getId( ), baseIndexUrl ), authenticationInfo,
330 Path indexDirectory = indexingContext.getIndexDirectoryFile( ).toPath( );
331 if ( !Files.exists( indexDirectory ) )
333 Files.createDirectories( indexDirectory );
336 ResourceFetcher resourceFetcher =
337 new WagonResourceFetcher( log, tempIndexDirectory, wagon, remoteRepository );
338 IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher );
339 request.setForceFullUpdate( fullUpdate );
340 request.setLocalIndexCacheDir( indexCacheDirectory.toFile( ) );
342 // indexUpdater.fetchAndUpdateIndex( request );
344 indexingContext.updateTimestamp( true );
348 catch ( AuthenticationException e )
350 log.error( "Could not login to the remote proxy for updating index of {}", remoteRepository.getId( ), e );
351 throw new IndexUpdateFailedException( "Login in to proxy failed while updating remote repository " + remoteRepository.getId( ), e );
353 catch ( ConnectionException e )
355 log.error( "Connection error during index update for remote repository {}", remoteRepository.getId( ), e );
356 throw new IndexUpdateFailedException( "Connection error during index update for remote repository " + remoteRepository.getId( ), e );
358 catch ( MalformedURLException e )
360 log.error( "URL for remote index update of remote repository {} is not correct {}", remoteRepository.getId( ), remoteUpdateUri, e );
361 throw new IndexUpdateFailedException( "URL for remote index update of repository is not correct " + remoteUpdateUri, e );
363 catch ( IOException e )
365 log.error( "IOException during index update of remote repository {}: {}", remoteRepository.getId( ), e.getMessage( ), e );
366 throw new IndexUpdateFailedException( "IOException during index update of remote repository " + remoteRepository.getId( )
367 + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
369 catch ( WagonFactoryException e )
371 log.error( "Wagon for remote index download of {} could not be created: {}", remoteRepository.getId( ), e.getMessage( ), e );
372 throw new IndexUpdateFailedException( "Error while updating the remote index of " + remoteRepository.getId( ), e );
379 public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<URI> artifactReference ) throws IndexUpdateFailedException
381 final StorageAsset ctxUri = context.getPath();
382 executeUpdateFunction(context, indexingContext -> {
383 Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.getFilePath().toUri().resolve(r)).toFile())).collect(Collectors.toList());
385 indexer.addArtifactsToIndex(artifacts, indexingContext);
386 } catch (IOException e) {
387 log.error("IOException while adding artifact {}", e.getMessage(), e);
388 throw new IndexUpdateFailedException("Error occured while adding artifact to index of "+context.getId()
389 + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
395 public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<URI> artifactReference ) throws IndexUpdateFailedException
397 final StorageAsset ctxUri = context.getPath();
398 executeUpdateFunction(context, indexingContext -> {
399 Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.getFilePath().toUri().resolve(r)).toFile())).collect(Collectors.toList());
401 indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
402 } catch (IOException e) {
403 log.error("IOException while removing artifact {}", e.getMessage(), e);
404 throw new IndexUpdateFailedException("Error occured while removing artifact from index of "+context.getId()
405 + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
412 public boolean supportsRepository( RepositoryType type )
414 return type == RepositoryType.MAVEN;
418 public ArchivaIndexingContext createContext( Repository repository ) throws IndexCreationFailedException
420 log.debug("Creating context for repo {}, type: {}", repository.getId(), repository.getType());
421 if ( repository.getType( ) != RepositoryType.MAVEN )
423 throw new UnsupportedRepositoryTypeException( repository.getType( ) );
425 IndexingContext mvnCtx = null;
428 if ( repository instanceof RemoteRepository )
430 mvnCtx = createRemoteContext( (RemoteRepository) repository );
432 else if ( repository instanceof ManagedRepository )
434 mvnCtx = createManagedContext( (ManagedRepository) repository );
437 catch ( IOException e )
439 log.error( "IOException during context creation " + e.getMessage( ), e );
440 throw new IndexCreationFailedException( "Could not create index context for repository " + repository.getId( )
441 + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
443 MavenIndexContextMock context = new MavenIndexContextMock( repository, mvnCtx );
449 public ArchivaIndexingContext reset(ArchivaIndexingContext context) throws IndexUpdateFailedException {
450 ArchivaIndexingContext ctx;
451 executeUpdateFunction(context, indexingContext -> {
453 indexingContext.close(true);
454 } catch (IOException e) {
455 log.warn("Index close failed");
457 org.apache.archiva.repository.storage.util.StorageUtil.deleteRecursively(context.getPath());
460 Repository repo = context.getRepository();
461 ctx = createContext(context.getRepository());
462 if (repo instanceof EditableRepository) {
463 ((EditableRepository)repo).setIndexingContext(ctx);
465 } catch (IndexCreationFailedException e) {
466 throw new IndexUpdateFailedException("Could not create index");
472 public ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException {
476 if (context.supports(IndexingContext.class)) {
478 StorageAsset newPath = getIndexPath(repo);
479 IndexingContext ctx = context.getBaseContext(IndexingContext.class);
480 Path oldPath = ctx.getIndexDirectoryFile().toPath();
481 if (oldPath.equals(newPath)) {
482 // Nothing to do, if path does not change
485 if (!Files.exists(oldPath)) {
486 return createContext(repo);
487 } else if (context.isEmpty()) {
489 return createContext(repo);
491 context.close(false);
492 Files.move(oldPath, newPath.getFilePath());
493 return createContext(repo);
495 } catch (IOException e) {
496 log.error("IOException while moving index directory {}", e.getMessage(), e);
497 throw new IndexCreationFailedException("Could not recreated the index.", e);
498 } catch (UnsupportedBaseContextException e) {
499 throw new IndexCreationFailedException("The given context, is not a maven context.");
502 throw new IndexCreationFailedException("Bad context type. This is not a maven context.");
507 public void updateLocalIndexPath(Repository repo) {
508 if (repo.supportsFeature(IndexCreationFeature.class)) {
509 IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
511 icf.setLocalIndexPath(getIndexPath(repo));
512 } catch (IOException e) {
513 log.error("Could not set local index path for {}. New URI: {}", repo.getId(), icf.getIndexPath());
519 public ArchivaIndexingContext mergeContexts(Repository destinationRepo, List<ArchivaIndexingContext> contexts, boolean packIndex) throws UnsupportedOperationException, IndexCreationFailedException {
523 private StorageAsset getIndexPath( Repository repo) throws IOException {
524 IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
525 Path repoDir = repo.getRoot().getFilePath();
526 URI indexDir = icf.getIndexPath();
527 String indexPath = indexDir.getPath();
528 Path indexDirectory = null;
529 FilesystemStorage filesystemStorage = (FilesystemStorage) repo.getRoot().getStorage();
530 if ( ! StringUtils.isEmpty(indexDir.toString( ) ) )
533 indexDirectory = PathUtil.getPathFromUri( indexDir );
534 // not absolute so create it in repository directory
535 if ( indexDirectory.isAbsolute( ) )
537 indexPath = indexDirectory.getFileName().toString();
538 filesystemStorage = new FilesystemStorage(indexDirectory.getParent(), new DefaultFileLockManager());
542 indexDirectory = repoDir.resolve( indexDirectory );
547 indexDirectory = repoDir.resolve( ".index" );
548 indexPath = ".index";
551 if ( !Files.exists( indexDirectory ) )
553 Files.createDirectories( indexDirectory );
555 return new FilesystemAsset( filesystemStorage, indexPath, indexDirectory);
558 private IndexingContext createRemoteContext(RemoteRepository remoteRepository ) throws IOException
560 Path appServerBase = archivaConfiguration.getAppServerBaseDir( );
562 String contextKey = "remote-" + remoteRepository.getId( );
565 // create remote repository path
566 Path repoDir = remoteRepository.getRoot().getFilePath();
567 if ( !Files.exists( repoDir ) )
569 Files.createDirectories( repoDir );
572 StorageAsset indexDirectory = null;
574 // is there configured indexDirectory ?
575 if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
577 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
578 indexDirectory = getIndexPath(remoteRepository);
579 String remoteIndexUrl = calculateIndexRemoteUrl( remoteRepository.getLocation( ), rif );
583 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
585 catch ( IndexFormatTooOldException e )
587 // existing index with an old lucene format so we need to delete it!!!
588 // delete it first then recreate it.
589 log.warn( "the index of repository {} is too old we have to delete and recreate it", //
590 remoteRepository.getId( ) );
591 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDirectory.getFilePath() );
592 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
598 throw new IOException( "No remote index defined" );
602 private IndexingContext getIndexingContext( Repository repository, String contextKey, Path repoDir, StorageAsset indexDirectory, String indexUrl ) throws IOException
604 return indexer.createIndexingContext( contextKey, repository.getId( ), repoDir.toFile( ), indexDirectory.getFilePath().toFile( ),
605 repository.getLocation( ) == null ? null : repository.getLocation( ).toString( ),
611 private IndexingContext createManagedContext( ManagedRepository repository ) throws IOException
614 IndexingContext context;
615 // take care first about repository location as can be relative
616 Path repositoryDirectory = repository.getRoot().getFilePath();
618 if ( !Files.exists( repositoryDirectory ) )
622 Files.createDirectories( repositoryDirectory );
624 catch ( IOException e )
626 log.error( "Could not create directory {}", repositoryDirectory );
630 StorageAsset indexDirectory = null;
632 if ( repository.supportsFeature( IndexCreationFeature.class ) )
634 indexDirectory = getIndexPath(repository);
636 String indexUrl = repositoryDirectory.toUri( ).toURL( ).toExternalForm( );
639 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
640 context.setSearchable( repository.isScanned( ) );
642 catch ( IndexFormatTooOldException e )
644 // existing index with an old lucene format so we need to delete it!!!
645 // delete it first then recreate it.
646 log.warn( "the index of repository {} is too old we have to delete and recreate it", //
647 repository.getId( ) );
648 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDirectory.getFilePath() );
649 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
650 context.setSearchable( repository.isScanned( ) );
656 throw new IOException( "No repository index defined" );
660 private String calculateIndexRemoteUrl( URI baseUri, RemoteIndexFeature rif )
662 if ( rif.getIndexUri( ) == null )
664 return baseUri.resolve( ".index" ).toString( );
668 return baseUri.resolve( rif.getIndexUri( ) ).toString( );
672 private static final class DownloadListener
673 implements TransferListener
675 private Logger log = LoggerFactory.getLogger( getClass( ) );
677 private String resourceName;
679 private long startTime;
681 private int totalLength = 0;
684 public void transferInitiated( TransferEvent transferEvent )
686 startTime = System.currentTimeMillis( );
687 resourceName = transferEvent.getResource( ).getName( );
688 log.debug( "initiate transfer of {}", resourceName );
692 public void transferStarted( TransferEvent transferEvent )
694 this.totalLength = 0;
695 resourceName = transferEvent.getResource( ).getName( );
696 log.info( "start transfer of {}", transferEvent.getResource( ).getName( ) );
700 public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
702 log.debug( "transfer of {} : {}/{}", transferEvent.getResource( ).getName( ), buffer.length, length );
703 this.totalLength += length;
707 public void transferCompleted( TransferEvent transferEvent )
709 resourceName = transferEvent.getResource( ).getName( );
710 long endTime = System.currentTimeMillis( );
711 log.info( "end of transfer file {} {} kb: {}s", transferEvent.getResource( ).getName( ),
712 this.totalLength / 1024, ( endTime - startTime ) / 1000 );
716 public void transferError( TransferEvent transferEvent )
718 log.info( "error of transfer file {}: {}", transferEvent.getResource( ).getName( ),
719 transferEvent.getException( ).getMessage( ), transferEvent.getException( ) );
723 public void debug( String message )
725 log.debug( "transfer debug {}", message );
729 private static class WagonResourceFetcher
730 implements ResourceFetcher
735 Path tempIndexDirectory;
739 RemoteRepository remoteRepository;
741 private WagonResourceFetcher( Logger log, Path tempIndexDirectory, Wagon wagon,
742 RemoteRepository remoteRepository )
745 this.tempIndexDirectory = tempIndexDirectory;
747 this.remoteRepository = remoteRepository;
751 public void connect( String id, String url )
758 public void disconnect( )
765 public InputStream retrieve(String name )
766 throws IOException, FileNotFoundException
770 log.info( "index update retrieve file, name:{}", name );
771 Path file = tempIndexDirectory.resolve( name );
772 Files.deleteIfExists( file );
773 file.toFile( ).deleteOnExit( );
774 wagon.get( addParameters( name, remoteRepository ), file.toFile( ) );
775 return Files.newInputStream( file );
777 catch ( AuthorizationException | TransferFailedException e )
779 throw new IOException( e.getMessage( ), e );
781 catch ( ResourceDoesNotExistException e )
783 FileNotFoundException fnfe = new FileNotFoundException( e.getMessage( ) );
789 // FIXME remove crappy copy/paste
790 protected String addParameters( String path, RemoteRepository remoteRepository )
792 if ( remoteRepository.getExtraParameters( ).isEmpty( ) )
797 boolean question = false;
799 StringBuilder res = new StringBuilder( path == null ? "" : path );
801 for ( Map.Entry<String, String> entry : remoteRepository.getExtraParameters( ).entrySet( ) )
805 res.append( '?' ).append( entry.getKey( ) ).append( '=' ).append( entry.getValue( ) );
809 return res.toString( );