1 package org.apache.archiva.admin.repository.remote;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
24 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
25 import org.apache.archiva.common.utils.PathUtil;
26 import org.apache.archiva.configuration.model.Configuration;
27 import org.apache.archiva.configuration.model.RemoteRepositoryConfiguration;
28 import org.apache.archiva.configuration.model.RepositoryCheckPath;
29 import org.apache.archiva.metadata.model.facets.AuditEvent;
30 import org.apache.archiva.repository.RemoteRepository;
31 import org.apache.archiva.repository.RepositoryCredentials;
32 import org.apache.archiva.repository.RepositoryException;
33 import org.apache.archiva.repository.RepositoryRegistry;
34 import org.apache.archiva.repository.base.PasswordCredentials;
35 import org.apache.archiva.repository.features.IndexCreationFeature;
36 import org.apache.archiva.repository.features.RemoteIndexFeature;
37 import org.apache.commons.lang3.StringUtils;
38 import org.springframework.stereotype.Service;
40 import javax.annotation.PostConstruct;
41 import javax.inject.Inject;
42 import java.util.List;
44 import java.util.stream.Collectors;
47 * @author Olivier Lamy
50 @Service( "remoteRepositoryAdmin#default" )
51 public class DefaultRemoteRepositoryAdmin
52 extends AbstractRepositoryAdmin
53 implements RemoteRepositoryAdmin
57 RepositoryRegistry repositoryRegistry;
61 private void initialize( )
62 throws RepositoryAdminException
67 * Conversion between the repository from the registry and the serialized DTO for the admin API
69 private org.apache.archiva.admin.model.beans.RemoteRepository convertRepo( RemoteRepository repo )
75 org.apache.archiva.admin.model.beans.RemoteRepository adminRepo = new org.apache.archiva.admin.model.beans.RemoteRepository( getArchivaConfiguration( ).getDefaultLocale( ) );
76 setBaseRepoAttributes( adminRepo, repo );
77 adminRepo.setUrl( convertUriToString( repo.getLocation( ) ) );
78 adminRepo.setCronExpression( repo.getSchedulingDefinition( ) );
79 adminRepo.setCheckPath( repo.getCheckPath( ) );
80 adminRepo.setExtraHeaders( repo.getExtraHeaders( ) );
81 adminRepo.setExtraParameters( repo.getExtraParameters( ) );
82 adminRepo.setTimeout( (int) repo.getTimeout( ).getSeconds( ) );
83 RepositoryCredentials creds = repo.getLoginCredentials( );
84 if ( creds != null && creds instanceof PasswordCredentials )
86 PasswordCredentials pCreds = (PasswordCredentials) creds;
87 adminRepo.setUserName( pCreds.getUsername( ) );
88 adminRepo.setPassword( new String( pCreds.getPassword( ) != null ? pCreds.getPassword( ) : new char[0] ) );
90 if ( repo.supportsFeature( RemoteIndexFeature.class ) )
92 RemoteIndexFeature rif = repo.getFeature( RemoteIndexFeature.class ).get( );
93 adminRepo.setRemoteIndexUrl( convertUriToString( rif.getIndexUri( ) ) );
94 adminRepo.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
95 adminRepo.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
96 adminRepo.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
97 adminRepo.setRemoteDownloadTimeout( (int) rif.getDownloadTimeout( ).getSeconds( ) );
99 if ( repo.supportsFeature( IndexCreationFeature.class ) )
101 IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get( );
102 adminRepo.setIndexDirectory( PathUtil.getPathFromUri( icf.getIndexPath( ) ).toString( ) );
104 adminRepo.setDescription( repo.getDescription( ) );
108 private RemoteRepositoryConfiguration getRepositoryConfiguration( org.apache.archiva.admin.model.beans.RemoteRepository repo )
110 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration( );
111 setBaseRepoAttributes( repoConfig, repo );
112 repoConfig.setUrl( getRepositoryCommonValidator( ).removeExpressions( repo.getUrl( ) ) );
113 repoConfig.setRefreshCronExpression( repo.getCronExpression( ) );
114 repoConfig.setCheckPath( repo.getCheckPath( ) );
115 repoConfig.setExtraHeaders( repo.getExtraHeaders( ) );
116 repoConfig.setExtraParameters( repo.getExtraParameters( ) );
117 repoConfig.setUsername( repo.getUserName( ) );
118 repoConfig.setPassword( repo.getPassword( ) );
119 repoConfig.setTimeout( repo.getTimeout( ) );
120 repoConfig.setRemoteIndexUrl( repo.getRemoteIndexUrl( ) );
121 repoConfig.setDownloadRemoteIndex( repo.isDownloadRemoteIndex( ) );
122 repoConfig.setRemoteDownloadNetworkProxyId( repo.getRemoteDownloadNetworkProxyId( ) );
123 repoConfig.setDownloadRemoteIndexOnStartup( repo.isDownloadRemoteIndexOnStartup( ) );
124 repoConfig.setRemoteDownloadTimeout( repo.getRemoteDownloadTimeout( ) );
125 repoConfig.setDescription( repo.getDescription( ) );
126 repoConfig.setIndexDir( repo.getIndexDirectory( ) );
131 public List<org.apache.archiva.admin.model.beans.RemoteRepository> getRemoteRepositories( )
132 throws RepositoryAdminException
135 return repositoryRegistry.getRemoteRepositories( ).stream( ).map( repo -> convertRepo( repo ) ).collect( Collectors.toList( ) );
139 public org.apache.archiva.admin.model.beans.RemoteRepository getRemoteRepository( String repositoryId )
140 throws RepositoryAdminException
142 return convertRepo( repositoryRegistry.getRemoteRepository( repositoryId ) );
146 public Boolean addRemoteRepository( org.apache.archiva.admin.model.beans.RemoteRepository remoteRepository, AuditInformation auditInformation )
147 throws RepositoryAdminException
149 triggerAuditEvent( remoteRepository.getId( ), null, AuditEvent.ADD_REMOTE_REPO, auditInformation );
150 getRepositoryCommonValidator( ).basicValidation( remoteRepository, false );
152 //TODO we can validate it's a good uri/url
153 if ( StringUtils.isEmpty( remoteRepository.getUrl( ) ) )
155 throw new RepositoryAdminException( "url cannot be null" );
158 //MRM-752 - url needs trimming
159 //MRM-1940 - URL should not end with a slash
160 remoteRepository.setUrl( StringUtils.stripEnd( StringUtils.trim( remoteRepository.getUrl( ) ), "/" ) );
162 if ( StringUtils.isEmpty( remoteRepository.getCheckPath( ) ) )
164 String checkUrl = remoteRepository.getUrl( ).toLowerCase( );
165 for ( RepositoryCheckPath path : getArchivaConfiguration( ).getConfiguration( ).getArchivaDefaultConfiguration( ).getDefaultCheckPaths( ) )
167 log.debug( "Checking path for urls: {} <-> {}", checkUrl, path.getUrl( ) );
168 if ( checkUrl.startsWith( path.getUrl( ) ) )
170 remoteRepository.setCheckPath( path.getPath( ) );
176 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
177 getRepositoryConfiguration( remoteRepository );
178 log.debug( "Adding remote repo {}", remoteRepositoryConfiguration );
182 repositoryRegistry.putRepository( remoteRepositoryConfiguration );
184 catch ( RepositoryException e )
186 log.error( "Could not add remote repository {}: {}", remoteRepositoryConfiguration.getId( ), e.getMessage( ), e );
187 throw new RepositoryAdminException( "Adding of remote repository failed" + ( e.getMessage( ) == null ? "" : ": " + e.getMessage( ) ) );
195 public Boolean deleteRemoteRepository( String repositoryId, AuditInformation auditInformation )
196 throws RepositoryAdminException
199 triggerAuditEvent( repositoryId, null, AuditEvent.DELETE_REMOTE_REPO, auditInformation );
201 Configuration configuration = getArchivaConfiguration( ).getConfiguration( );
203 RemoteRepository repo = repositoryRegistry.getRemoteRepository( repositoryId );
206 throw new RepositoryAdminException( "Could not delete repository " + repositoryId + ". The repository does not exist." );
210 repositoryRegistry.removeRepository( repo );
212 catch ( RepositoryException e )
214 log.error( "Deletion of remote repository failed {}: {}", repo.getId( ), e.getMessage( ), e );
215 throw new RepositoryAdminException( "Could not delete remote repository" + ( e.getMessage( ) == null ? "" : ": " + e.getMessage( ) ) );
222 public Boolean updateRemoteRepository( org.apache.archiva.admin.model.beans.RemoteRepository remoteRepository, AuditInformation auditInformation )
223 throws RepositoryAdminException
226 String repositoryId = remoteRepository.getId( );
228 triggerAuditEvent( repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation );
230 // update means : remove and add
232 Configuration configuration = getArchivaConfiguration( ).getConfiguration( );
234 RemoteRepositoryConfiguration remoteRepositoryConfiguration = getRepositoryConfiguration( remoteRepository );
237 repositoryRegistry.putRepository( remoteRepositoryConfiguration );
239 catch ( RepositoryException e )
241 log.error( "Could not update remote repository {}: {}", remoteRepositoryConfiguration.getId( ), e.getMessage( ), e );
242 throw new RepositoryAdminException( "Update of remote repository failed" + ( e.getMessage( ) == null ? "" : ": " + e.getMessage( ) ) );
248 public Map<String, org.apache.archiva.admin.model.beans.RemoteRepository> getRemoteRepositoriesAsMap( )
249 throws RepositoryAdminException
251 return repositoryRegistry.getRemoteRepositories( ).stream( )
252 .map( r -> convertRepo( r ) )
253 .collect( Collectors.toMap(