private RepositoryProvider getProvider( RepositoryType type ) throws RepositoryException { return repositoryProviders.stream( ).filter( repositoryProvider -> repositoryProvider.provides( ).contains( type ) ).findFirst( ).orElseThrow( ( ) -> new RepositoryException( "Repository type cannot be handled: " + type ) ); }
private RepositoryContentProvider getProvider(final String layout, final RepositoryType repoType) throws RepositoryException { return repositoryContentProviders.stream().filter(p->p.supports( repoType ) && p.supportsLayout( layout )). findFirst().orElseThrow( ( ) -> new RepositoryException( "Could not find content provider for repository type "+repoType+" and layout "+layout ) ); }
@SuppressWarnings( "unchecked" ) public <T extends Repository> Repository clone(T repo, String newId) throws RepositoryException { if (repo instanceof RemoteRepository ) { return this.clone((RemoteRepository)repo, newId); } else if (repo instanceof ManagedRepository) { return this.clone((ManagedRepository)repo, newId); } else { throw new RepositoryException("This repository class is not supported "+ repo.getClass().getName()); } }
private URI getURIFromString(String uriStr) throws RepositoryException { URI uri; try { if (StringUtils.isEmpty(uriStr)) { return new URI(""); } if (uriStr.startsWith("/")) { // only absolute paths are prepended with file scheme uri = new URI("file://" + uriStr); } else { uri = new URI(uriStr); } if (uri.getScheme() != null && !"file".equals(uri.getScheme())) { log.error("Bad URI scheme found: {}, URI={}", uri.getScheme(), uri); throw new RepositoryException("The uri " + uriStr + " is not valid. Only file:// URI is allowed for maven."); } } catch (URISyntaxException e) { String newCfg = "file://" + uriStr; try { uri = new URI(newCfg); } catch (URISyntaxException e1) { log.error("Could not create URI from {} -> ", uriStr, newCfg); throw new RepositoryException("The config entry " + uriStr + " cannot be converted to URI."); } } log.debug("Setting location uri: {}", uri); return uri; }
@SuppressWarnings( "unchecked" ) public void removeRepository(Repository repo) throws RepositoryException { if (repo instanceof RemoteRepository ) { removeRepository( (RemoteRepository)repo ); } else if (repo instanceof ManagedRepository) { removeRepository( (ManagedRepository)repo); } else { throw new RepositoryException( "Repository type not known: "+repo.getClass() ); } }
private ManagedRepositoryContent getManagedRepositoryContent(String id) throws RepositoryException { org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( id ); if (repo==null) { throw new RepositoryException( "Repository not found "+id ); } return repo.getContent(); }
private ManagedRepositoryContent getManagedRepositoryContent(String id) throws RepositoryException { org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( id ); if (repo==null) { throw new RepositoryException( "Could not find repository "+id ); } return repo.getContent(); }
@SuppressWarnings( "unchecked" ) @Override public <T extends RepositoryContent, V extends Repository> T createContent( Class<T> clazz, V repository ) throws RepositoryException { if (!supports( repository.getType() )) { throw new RepositoryException( "Repository type "+repository.getType()+" is not supported by this implementation." ); } if (repository instanceof ManagedRepository && ManagedRepositoryContent.class.isAssignableFrom( clazz ) ) { return (T) this.createManagedContent( (ManagedRepository) repository ); } else if (repository instanceof RemoteRepository && RemoteRepository.class.isAssignableFrom( clazz )) { return (T) this.createRemoteContent( (RemoteRepository) repository ); } else { throw new RepositoryException( "Repository flavour is not supported: "+repository.getClass().getName() ); } }
/** * Adds a remote repository, or overwrites the repository definition with the same id, if it exists already. * The modification is saved to the configuration immediately. * * @param remoteRepository the remote repository to add * @throws RepositoryException if an error occurs during configuration save */ public RemoteRepository putRepository( RemoteRepository remoteRepository ) throws RepositoryException { rwLock.writeLock( ).lock( ); try { Configuration configuration = getArchivaConfiguration().getConfiguration(); try { RemoteRepository repo = putRepository( remoteRepository, configuration ); getArchivaConfiguration().save(configuration); return repo; } catch ( RegistryException | IndeterminateConfigurationException e ) { log.error("Error while saving remote repository {}", e.getMessage(), e); throw new RepositoryException( "Could not save the configuration" + (e.getMessage( )==null?"":": "+e.getMessage()) ); } } finally { rwLock.writeLock( ).unlock( ); } }
@Override public void deleteProject( String namespace, String projectId ) throws RepositoryException, ContentNotFoundException { ArtifactReference artifactReference = new ArtifactReference(); artifactReference.setGroupId( namespace ); artifactReference.setArtifactId( projectId ); String path = toPath( artifactReference ); File directory = new File( getRepoRoot(), path ); if ( !directory.exists() ) { throw new ContentNotFoundException( "cannot found project " + namespace + ":" + projectId ); } if ( directory.isDirectory() ) { try { FileUtils.deleteDirectory( directory ); } catch ( IOException e ) { throw new RepositoryException( e.getMessage(), e ); } } else { log.warn( "project {}:{} is not a directory", namespace, projectId ); } }
throw new RepositoryException( "Could not save configuration after repository removal: "+e.getMessage() ); } finally
@Override public ManagedRepositoryContent createManagedContent( ManagedRepository repository ) throws RepositoryException { if (!supports( repository.getType() )) { throw new RepositoryException( "Repository type "+repository.getType()+" is not supported by this implementation." ); } if (!supportsLayout( repository.getLayout() )) { throw new RepositoryException( "Repository layout "+repository.getLayout()+" is not supported by this implementation." ); } ManagedDefaultRepositoryContent content = new ManagedDefaultRepositoryContent(artifactMappingProviders, filetypes); content.setRepository( repository ); return content; }
@Override public void deleteProject( String namespace, String projectId ) throws RepositoryException { ArtifactReference artifactReference = new ArtifactReference(); artifactReference.setGroupId( namespace ); artifactReference.setArtifactId( projectId ); String path = toPath( artifactReference ); Path directory = Paths.get( getRepoRoot(), path ); if ( !Files.exists(directory) ) { throw new ContentNotFoundException( "cannot found project " + namespace + ":" + projectId ); } if ( Files.isDirectory(directory) ) { try { org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory ); } catch ( IOException e ) { throw new RepositoryException( e.getMessage(), e ); } } else { log.warn( "project {}:{} is not a directory", namespace, projectId ); } }
@Override public RemoteRepositoryContent createRemoteContent( RemoteRepository repository ) throws RepositoryException { if (!supports( repository.getType() )) { throw new RepositoryException( "Repository type "+repository.getType()+" is not supported by this implementation." ); } if (!supportsLayout( repository.getLayout() )) { throw new RepositoryException( "Repository layout "+repository.getLayout()+" is not supported by this implementation." ); } RemoteDefaultRepositoryContent content = new RemoteDefaultRepositoryContent(artifactMappingProviders); content.setRepository( repository ); return content; }
/** * Creates a new repository instance with the same settings as this one. The cloned repository is not * registered or saved to the configuration. * * @param repo The origin repository * @return The cloned repository. */ public ManagedRepository clone(ManagedRepository repo, String newId) throws RepositoryException { if (managedRepositories.containsKey(newId) || remoteRepositories.containsKey(newId)) { throw new RepositoryException("The given id exists already "+newId); } RepositoryProvider provider = getProvider(repo.getType()); ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration(repo); cfg.setId(newId); ManagedRepository cloned = provider.createManagedInstance(cfg); cloned.addListener(this); return cloned; }
/** * Creates a new repository instance with the same settings as this one. The cloned repository is not * registered or saved to the configuration. * * @param repo The origin repository * @return The cloned repository. */ public RemoteRepository clone( RemoteRepository repo, String newId) throws RepositoryException { if (managedRepositories.containsKey(newId) || remoteRepositories.containsKey(newId)) { throw new RepositoryException("The given id exists already "+newId); } RepositoryProvider provider = getProvider(repo.getType()); RemoteRepositoryConfiguration cfg = provider.getRemoteConfiguration(repo); cfg.setId(newId); RemoteRepository cloned = provider.createRemoteInstance(cfg); cloned.addListener(this); return cloned; }
public ManagedRepositoryContent getManagedRepositoryContent( org.apache.archiva.repository.ManagedRepository mRepo ) throws RepositoryException { final String id = mRepo.getId(); ManagedRepositoryContent content = managedContentMap.get( id ); if ( content != null && content.getRepository()==mRepo) { return content; } RepositoryContentProvider contentProvider = getProvider( mRepo.getLayout( ), mRepo.getType( ) ); content = contentProvider.createManagedContent( mRepo ); if (content==null) { throw new RepositoryException( "Could not create repository content instance for "+mRepo.getId() ); } ManagedRepositoryContent previousContent = managedContentMap.put( id, content ); if (previousContent!=null) { previousContent.setRepository( null ); } return content; }
public RemoteRepositoryContent getRemoteRepositoryContent( RemoteRepository mRepo ) throws RepositoryException { final String id = mRepo.getId(); RemoteRepositoryContent content = remoteContentMap.get( id ); if ( content != null && content.getRepository()==mRepo) { return content; } RepositoryContentProvider contentProvider = getProvider( mRepo.getLayout( ), mRepo.getType( ) ); content = contentProvider.createRemoteContent( mRepo ); if (content==null) { throw new RepositoryException( "Could not create repository content instance for "+mRepo.getId() ); } RemoteRepositoryContent previousContent = remoteContentMap.put( id, content ); if (previousContent!=null) { previousContent.setRepository( null ); } return content; }
private void createIndexingContext(EditableRepository editableRepo) throws RepositoryException { if (editableRepo.supportsFeature(IndexCreationFeature.class)) { ArchivaIndexManager idxManager = getIndexManager(editableRepo.getType()); try { editableRepo.setIndexingContext(idxManager.createContext(editableRepo)); idxManager.updateLocalIndexPath(editableRepo); } catch (IndexCreationFailedException e) { throw new RepositoryException("Could not create index for repository "+editableRepo.getId()+": "+e.getMessage(),e); } } }
@SuppressWarnings("unchecked") @Override public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException { try { remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) ); remoteRepository.setBaseUri( new URI( "" ) ); remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) ); remoteRepository.setLayout( configuration.getLayout( ) ); remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) ); remoteRepository.setCheckPath( configuration.getCheckPath( ) ); remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) ); remoteRepository.setExtraParameters( configuration.getExtraParameters( ) ); remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) ); char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray(); remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) ); remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) ); RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( ); rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) ); rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) ); rif.setIndexUri( new URI( configuration.getIndexDir( ) ) ); rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) ); rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) ); } catch ( Exception e ) { throw new RepositoryException( "Error", e ); } }