1 package org.apache.archiva.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.utils.FileUtils;
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.proxy.maven.WagonFactory;
32 import org.apache.archiva.proxy.maven.WagonFactoryException;
33 import org.apache.archiva.proxy.maven.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.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.content.FilesystemAsset;
43 import org.apache.archiva.repository.content.StorageAsset;
44 import org.apache.archiva.repository.features.IndexCreationFeature;
45 import org.apache.archiva.repository.features.RemoteIndexFeature;
46 import org.apache.commons.lang.StringUtils;
47 import org.apache.maven.index.ArtifactContext;
48 import org.apache.maven.index.ArtifactContextProducer;
49 import org.apache.maven.index.DefaultScannerListener;
50 import org.apache.maven.index.Indexer;
51 import org.apache.maven.index.IndexerEngine;
52 import org.apache.maven.index.Scanner;
53 import org.apache.maven.index.ScanningRequest;
54 import org.apache.maven.index.ScanningResult;
55 import org.apache.maven.index.context.IndexCreator;
56 import org.apache.maven.index.context.IndexingContext;
57 import org.apache.maven.index.packer.IndexPacker;
58 import org.apache.maven.index.packer.IndexPackingRequest;
59 import org.apache.maven.index.updater.IndexUpdateRequest;
60 import org.apache.maven.index.updater.ResourceFetcher;
61 import org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException;
62 import org.apache.maven.wagon.ConnectionException;
63 import org.apache.maven.wagon.ResourceDoesNotExistException;
64 import org.apache.maven.wagon.StreamWagon;
65 import org.apache.maven.wagon.TransferFailedException;
66 import org.apache.maven.wagon.Wagon;
67 import org.apache.maven.wagon.authentication.AuthenticationException;
68 import org.apache.maven.wagon.authentication.AuthenticationInfo;
69 import org.apache.maven.wagon.authorization.AuthorizationException;
70 import org.apache.maven.wagon.events.TransferEvent;
71 import org.apache.maven.wagon.events.TransferListener;
72 import org.apache.maven.wagon.proxy.ProxyInfo;
73 import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
74 import org.apache.maven.wagon.shared.http.HttpConfiguration;
75 import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78 import org.springframework.stereotype.Service;
80 import javax.inject.Inject;
81 import java.io.FileNotFoundException;
82 import java.io.IOException;
83 import java.io.InputStream;
84 import java.net.MalformedURLException;
86 import java.nio.file.Files;
87 import java.nio.file.Path;
88 import java.nio.file.Paths;
89 import java.util.Collection;
90 import java.util.List;
92 import java.util.concurrent.ConcurrentSkipListSet;
93 import java.util.stream.Collectors;
95 @Service("archivaIndexManager#maven")
96 public class ArchivaIndexManagerMock implements ArchivaIndexManager {
98 private static final Logger log = LoggerFactory.getLogger( ArchivaIndexManagerMock.class );
101 private Indexer indexer;
104 private IndexerEngine indexerEngine;
107 private List<? extends IndexCreator> indexCreators;
110 private IndexPacker indexPacker;
113 private Scanner scanner;
116 private ArchivaConfiguration archivaConfiguration;
119 private WagonFactory wagonFactory;
123 private ArtifactContextProducer artifactContextProducer;
125 private ConcurrentSkipListSet<Path> activeContexts = new ConcurrentSkipListSet<>( );
127 private static final int WAIT_TIME = 100;
128 private static final int MAX_WAIT = 10;
131 public static IndexingContext getMvnContext(ArchivaIndexingContext context ) throws UnsupportedBaseContextException
133 if ( !context.supports( IndexingContext.class ) )
135 log.error( "The provided archiva index context does not support the maven IndexingContext" );
136 throw new UnsupportedBaseContextException( "The context does not support the Maven IndexingContext" );
138 return context.getBaseContext( IndexingContext.class );
141 private Path getIndexPath( ArchivaIndexingContext ctx )
143 return PathUtil.getPathFromUri( ctx.getPath( ) );
147 interface IndexUpdateConsumer
150 void accept( IndexingContext indexingContext ) throws IndexUpdateFailedException;
154 * This method is used to do some actions around the update execution code. And to make sure, that no other
155 * method is running on the same index.
157 private void executeUpdateFunction( ArchivaIndexingContext context, IndexUpdateConsumer function ) throws IndexUpdateFailedException
159 IndexingContext indexingContext = null;
162 indexingContext = getMvnContext( context );
164 catch ( UnsupportedBaseContextException e )
166 throw new IndexUpdateFailedException( "Maven index is not supported by this context", e );
168 final Path ctxPath = getIndexPath( context );
170 boolean active = false;
171 while ( loop-- > 0 && !active )
173 active = activeContexts.add( ctxPath );
176 Thread.currentThread( ).sleep( WAIT_TIME );
178 catch ( InterruptedException e )
187 function.accept( indexingContext );
191 activeContexts.remove( ctxPath );
196 throw new IndexUpdateFailedException( "Timeout while waiting for index release on context " + context.getId( ) );
201 public void pack( final ArchivaIndexingContext context ) throws IndexUpdateFailedException
203 executeUpdateFunction( context, indexingContext -> {
206 IndexPackingRequest request = new IndexPackingRequest( indexingContext,
207 indexingContext.acquireIndexSearcher( ).getIndexReader( ),
208 indexingContext.getIndexDirectoryFile( ) );
209 indexPacker.packIndex( request );
210 indexingContext.updateTimestamp( true );
212 catch ( IOException e )
214 log.error( "IOException while packing index of context " + context.getId( ) + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ) );
215 throw new IndexUpdateFailedException( "IOException during update of " + context.getId( ), e );
223 public void scan(final ArchivaIndexingContext context) throws IndexUpdateFailedException
225 executeUpdateFunction( context, indexingContext -> {
226 DefaultScannerListener listener = new DefaultScannerListener( indexingContext, indexerEngine, true, null );
227 ScanningRequest request = new ScanningRequest( indexingContext, listener );
228 ScanningResult result = scanner.scan( request );
229 if ( result.hasExceptions( ) )
231 log.error( "Exceptions occured during index scan of " + context.getId( ) );
232 result.getExceptions( ).stream( ).map( e -> e.getMessage( ) ).distinct( ).limit( 5 ).forEach(
233 s -> log.error( "Message: " + s )
241 public void update(final ArchivaIndexingContext context, final boolean fullUpdate) throws IndexUpdateFailedException
243 log.info( "start download remote index for remote repository {}", context.getRepository( ).getId( ) );
245 if ( !( context.getRepository( ) instanceof RemoteRepository) || !(context.getRepository().supportsFeature(RemoteIndexFeature.class)) )
247 throw new IndexUpdateFailedException( "The context is not associated to a remote repository with remote index " + context.getId( ) );
249 RemoteIndexFeature rif = context.getRepository().getFeature(RemoteIndexFeature.class).get();
250 remoteUpdateUri = context.getRepository().getLocation().resolve(rif.getIndexUri());
252 final RemoteRepository remoteRepository = (RemoteRepository) context.getRepository( );
254 executeUpdateFunction( context,
258 // create a temp directory to download files
259 Path tempIndexDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".tmpIndex" );
260 Path indexCacheDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".indexCache" );
261 Files.createDirectories( indexCacheDirectory );
262 if ( Files.exists( tempIndexDirectory ) )
264 FileUtils.deleteDirectory( tempIndexDirectory );
266 Files.createDirectories( tempIndexDirectory );
267 tempIndexDirectory.toFile( ).deleteOnExit( );
268 String baseIndexUrl = indexingContext.getIndexUpdateUrl( );
270 String wagonProtocol = remoteUpdateUri.toURL( ).getProtocol( );
272 NetworkProxy networkProxy = null;
273 if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
275 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
277 final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(
278 new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders( ) ).networkProxy(
281 int readTimeout = (int) rif.getDownloadTimeout( ).toMillis( ) * 1000;
282 wagon.setReadTimeout( readTimeout );
283 wagon.setTimeout( (int) remoteRepository.getTimeout( ).toMillis( ) * 1000 );
285 if ( wagon instanceof AbstractHttpClientWagon)
287 HttpConfiguration httpConfiguration = new HttpConfiguration( );
288 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration( );
289 httpMethodConfiguration.setUsePreemptive( true );
290 httpMethodConfiguration.setReadTimeout( readTimeout );
291 httpConfiguration.setGet( httpMethodConfiguration );
292 AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
295 wagon.addTransferListener( new DownloadListener( ) );
296 ProxyInfo proxyInfo = null;
297 if ( networkProxy != null )
299 proxyInfo = new ProxyInfo( );
300 proxyInfo.setType( networkProxy.getProtocol( ) );
301 proxyInfo.setHost( networkProxy.getHost( ) );
302 proxyInfo.setPort( networkProxy.getPort( ) );
303 proxyInfo.setUserName( networkProxy.getUsername( ) );
304 proxyInfo.setPassword( networkProxy.getPassword( ) );
306 AuthenticationInfo authenticationInfo = null;
307 if ( remoteRepository.getLoginCredentials( ) != null && ( remoteRepository.getLoginCredentials( ) instanceof PasswordCredentials) )
309 PasswordCredentials creds = (PasswordCredentials) remoteRepository.getLoginCredentials( );
310 authenticationInfo = new AuthenticationInfo( );
311 authenticationInfo.setUserName( creds.getUsername( ) );
312 authenticationInfo.setPassword( new String( creds.getPassword( ) ) );
314 wagon.connect( new org.apache.maven.wagon.repository.Repository( remoteRepository.getId( ), baseIndexUrl ), authenticationInfo,
317 Path indexDirectory = indexingContext.getIndexDirectoryFile( ).toPath( );
318 if ( !Files.exists( indexDirectory ) )
320 Files.createDirectories( indexDirectory );
323 ResourceFetcher resourceFetcher =
324 new WagonResourceFetcher( log, tempIndexDirectory, wagon, remoteRepository );
325 IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher );
326 request.setForceFullUpdate( fullUpdate );
327 request.setLocalIndexCacheDir( indexCacheDirectory.toFile( ) );
329 // indexUpdater.fetchAndUpdateIndex( request );
331 indexingContext.updateTimestamp( true );
335 catch ( AuthenticationException e )
337 log.error( "Could not login to the remote proxy for updating index of {}", remoteRepository.getId( ), e );
338 throw new IndexUpdateFailedException( "Login in to proxy failed while updating remote repository " + remoteRepository.getId( ), e );
340 catch ( ConnectionException e )
342 log.error( "Connection error during index update for remote repository {}", remoteRepository.getId( ), e );
343 throw new IndexUpdateFailedException( "Connection error during index update for remote repository " + remoteRepository.getId( ), e );
345 catch ( MalformedURLException e )
347 log.error( "URL for remote index update of remote repository {} is not correct {}", remoteRepository.getId( ), remoteUpdateUri, e );
348 throw new IndexUpdateFailedException( "URL for remote index update of repository is not correct " + remoteUpdateUri, e );
350 catch ( IOException e )
352 log.error( "IOException during index update of remote repository {}: {}", remoteRepository.getId( ), e.getMessage( ), e );
353 throw new IndexUpdateFailedException( "IOException during index update of remote repository " + remoteRepository.getId( )
354 + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
356 catch ( WagonFactoryException e )
358 log.error( "Wagon for remote index download of {} could not be created: {}", remoteRepository.getId( ), e.getMessage( ), e );
359 throw new IndexUpdateFailedException( "Error while updating the remote index of " + remoteRepository.getId( ), e );
366 public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<URI> artifactReference ) throws IndexUpdateFailedException
368 final URI ctxUri = context.getPath();
369 executeUpdateFunction(context, indexingContext -> {
370 Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(r)).toFile())).collect(Collectors.toList());
372 indexer.addArtifactsToIndex(artifacts, indexingContext);
373 } catch (IOException e) {
374 log.error("IOException while adding artifact {}", e.getMessage(), e);
375 throw new IndexUpdateFailedException("Error occured while adding artifact to index of "+context.getId()
376 + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
382 public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<URI> artifactReference ) throws IndexUpdateFailedException
384 final URI ctxUri = context.getPath();
385 executeUpdateFunction(context, indexingContext -> {
386 Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(r)).toFile())).collect(Collectors.toList());
388 indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
389 } catch (IOException e) {
390 log.error("IOException while removing artifact {}", e.getMessage(), e);
391 throw new IndexUpdateFailedException("Error occured while removing artifact from index of "+context.getId()
392 + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
399 public boolean supportsRepository( RepositoryType type )
401 return type == RepositoryType.MAVEN;
405 public ArchivaIndexingContext createContext( Repository repository ) throws IndexCreationFailedException
407 log.debug("Creating context for repo {}, type: {}", repository.getId(), repository.getType());
408 if ( repository.getType( ) != RepositoryType.MAVEN )
410 throw new UnsupportedRepositoryTypeException( repository.getType( ) );
412 IndexingContext mvnCtx = null;
415 if ( repository instanceof RemoteRepository )
417 mvnCtx = createRemoteContext( (RemoteRepository) repository );
419 else if ( repository instanceof ManagedRepository )
421 mvnCtx = createManagedContext( (ManagedRepository) repository );
424 catch ( IOException e )
426 log.error( "IOException during context creation " + e.getMessage( ), e );
427 throw new IndexCreationFailedException( "Could not create index context for repository " + repository.getId( )
428 + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
430 MavenIndexContextMock context = new MavenIndexContextMock( repository, mvnCtx );
436 public ArchivaIndexingContext reset(ArchivaIndexingContext context) throws IndexUpdateFailedException {
437 ArchivaIndexingContext ctx;
438 executeUpdateFunction(context, indexingContext -> {
440 indexingContext.close(true);
441 } catch (IOException e) {
442 log.warn("Index close failed");
445 FileUtils.deleteDirectory(Paths.get(context.getPath()));
446 } catch (IOException e) {
447 throw new IndexUpdateFailedException("Could not delete index files");
451 Repository repo = context.getRepository();
452 ctx = createContext(context.getRepository());
453 if (repo instanceof EditableRepository) {
454 ((EditableRepository)repo).setIndexingContext(ctx);
456 } catch (IndexCreationFailedException e) {
457 throw new IndexUpdateFailedException("Could not create index");
463 public ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException {
467 if (context.supports(IndexingContext.class)) {
469 StorageAsset newPath = getIndexPath(repo);
470 IndexingContext ctx = context.getBaseContext(IndexingContext.class);
471 Path oldPath = ctx.getIndexDirectoryFile().toPath();
472 if (oldPath.equals(newPath)) {
473 // Nothing to do, if path does not change
476 if (!Files.exists(oldPath)) {
477 return createContext(repo);
478 } else if (context.isEmpty()) {
480 return createContext(repo);
482 context.close(false);
483 Files.move(oldPath, newPath.getFilePath());
484 return createContext(repo);
486 } catch (IOException e) {
487 log.error("IOException while moving index directory {}", e.getMessage(), e);
488 throw new IndexCreationFailedException("Could not recreated the index.", e);
489 } catch (UnsupportedBaseContextException e) {
490 throw new IndexCreationFailedException("The given context, is not a maven context.");
493 throw new IndexCreationFailedException("Bad context type. This is not a maven context.");
498 public void updateLocalIndexPath(Repository repo) {
499 if (repo.supportsFeature(IndexCreationFeature.class)) {
500 IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
502 icf.setLocalIndexPath(getIndexPath(repo));
503 } catch (IOException e) {
504 log.error("Could not set local index path for {}. New URI: {}", repo.getId(), icf.getIndexPath());
510 public ArchivaIndexingContext mergeContexts(Repository destinationRepo, List<ArchivaIndexingContext> contexts, boolean packIndex) throws UnsupportedOperationException, IndexCreationFailedException {
514 private StorageAsset getIndexPath( Repository repo) throws IOException {
515 IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
516 Path repoDir = repo.getAsset("").getFilePath();
517 URI indexDir = icf.getIndexPath();
518 String indexPath = indexDir.getPath();
519 Path indexDirectory = null;
520 if ( ! StringUtils.isEmpty(indexDir.toString( ) ) )
523 indexDirectory = PathUtil.getPathFromUri( indexDir );
524 // not absolute so create it in repository directory
525 if ( indexDirectory.isAbsolute( ) )
527 indexPath = indexDirectory.getFileName().toString();
531 indexDirectory = repoDir.resolve( indexDirectory );
536 indexDirectory = repoDir.resolve( ".index" );
537 indexPath = ".index";
540 if ( !Files.exists( indexDirectory ) )
542 Files.createDirectories( indexDirectory );
544 return new FilesystemAsset( indexPath, indexDirectory);
547 private IndexingContext createRemoteContext(RemoteRepository remoteRepository ) throws IOException
549 Path appServerBase = archivaConfiguration.getAppServerBaseDir( );
551 String contextKey = "remote-" + remoteRepository.getId( );
554 // create remote repository path
555 Path repoDir = remoteRepository.getAsset("").getFilePath();
556 if ( !Files.exists( repoDir ) )
558 Files.createDirectories( repoDir );
561 StorageAsset indexDirectory = null;
563 // is there configured indexDirectory ?
564 if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
566 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
567 indexDirectory = getIndexPath(remoteRepository);
568 String remoteIndexUrl = calculateIndexRemoteUrl( remoteRepository.getLocation( ), rif );
572 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
574 catch ( IndexFormatTooOldException e )
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 remoteRepository.getId( ) );
580 FileUtils.deleteDirectory( indexDirectory.getFilePath() );
581 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
587 throw new IOException( "No remote index defined" );
591 private IndexingContext getIndexingContext( Repository repository, String contextKey, Path repoDir, StorageAsset indexDirectory, String indexUrl ) throws IOException
593 return indexer.createIndexingContext( contextKey, repository.getId( ), repoDir.toFile( ), indexDirectory.getFilePath().toFile( ),
594 repository.getLocation( ) == null ? null : repository.getLocation( ).toString( ),
600 private IndexingContext createManagedContext( ManagedRepository repository ) throws IOException
603 IndexingContext context;
604 // take care first about repository location as can be relative
605 Path repositoryDirectory = repository.getAsset("").getFilePath();
607 if ( !Files.exists( repositoryDirectory ) )
611 Files.createDirectories( repositoryDirectory );
613 catch ( IOException e )
615 log.error( "Could not create directory {}", repositoryDirectory );
619 StorageAsset indexDirectory = null;
621 if ( repository.supportsFeature( IndexCreationFeature.class ) )
623 indexDirectory = getIndexPath(repository);
625 String indexUrl = repositoryDirectory.toUri( ).toURL( ).toExternalForm( );
628 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
629 context.setSearchable( repository.isScanned( ) );
631 catch ( IndexFormatTooOldException e )
633 // existing index with an old lucene format so we need to delete it!!!
634 // delete it first then recreate it.
635 log.warn( "the index of repository {} is too old we have to delete and recreate it", //
636 repository.getId( ) );
637 FileUtils.deleteDirectory( indexDirectory.getFilePath() );
638 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
639 context.setSearchable( repository.isScanned( ) );
645 throw new IOException( "No repository index defined" );
649 private String calculateIndexRemoteUrl( URI baseUri, RemoteIndexFeature rif )
651 if ( rif.getIndexUri( ) == null )
653 return baseUri.resolve( ".index" ).toString( );
657 return baseUri.resolve( rif.getIndexUri( ) ).toString( );
661 private static final class DownloadListener
662 implements TransferListener
664 private Logger log = LoggerFactory.getLogger( getClass( ) );
666 private String resourceName;
668 private long startTime;
670 private int totalLength = 0;
673 public void transferInitiated( TransferEvent transferEvent )
675 startTime = System.currentTimeMillis( );
676 resourceName = transferEvent.getResource( ).getName( );
677 log.debug( "initiate transfer of {}", resourceName );
681 public void transferStarted( TransferEvent transferEvent )
683 this.totalLength = 0;
684 resourceName = transferEvent.getResource( ).getName( );
685 log.info( "start transfer of {}", transferEvent.getResource( ).getName( ) );
689 public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
691 log.debug( "transfer of {} : {}/{}", transferEvent.getResource( ).getName( ), buffer.length, length );
692 this.totalLength += length;
696 public void transferCompleted( TransferEvent transferEvent )
698 resourceName = transferEvent.getResource( ).getName( );
699 long endTime = System.currentTimeMillis( );
700 log.info( "end of transfer file {} {} kb: {}s", transferEvent.getResource( ).getName( ),
701 this.totalLength / 1024, ( endTime - startTime ) / 1000 );
705 public void transferError( TransferEvent transferEvent )
707 log.info( "error of transfer file {}: {}", transferEvent.getResource( ).getName( ),
708 transferEvent.getException( ).getMessage( ), transferEvent.getException( ) );
712 public void debug( String message )
714 log.debug( "transfer debug {}", message );
718 private static class WagonResourceFetcher
719 implements ResourceFetcher
724 Path tempIndexDirectory;
728 RemoteRepository remoteRepository;
730 private WagonResourceFetcher( Logger log, Path tempIndexDirectory, Wagon wagon,
731 RemoteRepository remoteRepository )
734 this.tempIndexDirectory = tempIndexDirectory;
736 this.remoteRepository = remoteRepository;
740 public void connect( String id, String url )
747 public void disconnect( )
754 public InputStream retrieve(String name )
755 throws IOException, FileNotFoundException
759 log.info( "index update retrieve file, name:{}", name );
760 Path file = tempIndexDirectory.resolve( name );
761 Files.deleteIfExists( file );
762 file.toFile( ).deleteOnExit( );
763 wagon.get( addParameters( name, remoteRepository ), file.toFile( ) );
764 return Files.newInputStream( file );
766 catch ( AuthorizationException | TransferFailedException e )
768 throw new IOException( e.getMessage( ), e );
770 catch ( ResourceDoesNotExistException e )
772 FileNotFoundException fnfe = new FileNotFoundException( e.getMessage( ) );
778 // FIXME remove crappy copy/paste
779 protected String addParameters( String path, RemoteRepository remoteRepository )
781 if ( remoteRepository.getExtraParameters( ).isEmpty( ) )
786 boolean question = false;
788 StringBuilder res = new StringBuilder( path == null ? "" : path );
790 for ( Map.Entry<String, String> entry : remoteRepository.getExtraParameters( ).entrySet( ) )
794 res.append( '?' ).append( entry.getKey( ) ).append( '=' ).append( entry.getValue( ) );
798 return res.toString( );