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.Configuration;
27 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
28 import org.apache.archiva.configuration.RepositoryCheckPath;
29 import org.apache.archiva.indexer.ArchivaIndexingContext;
30 import org.apache.archiva.metadata.model.facets.AuditEvent;
31 import org.apache.archiva.repository.RemoteRepository;
32 import org.apache.archiva.repository.RepositoryCredentials;
33 import org.apache.archiva.repository.RepositoryException;
34 import org.apache.archiva.repository.RepositoryRegistry;
35 import org.apache.archiva.repository.base.PasswordCredentials;
36 import org.apache.archiva.repository.features.IndexCreationFeature;
37 import org.apache.archiva.repository.features.RemoteIndexFeature;
38 import org.apache.commons.lang3.StringUtils;
39 import org.springframework.stereotype.Service;
41 import javax.annotation.PostConstruct;
42 import javax.inject.Inject;
43 import java.util.HashMap;
44 import java.util.List;
46 import java.util.stream.Collectors;
49 * @author Olivier Lamy
52 @Service("remoteRepositoryAdmin#default")
53 public class DefaultRemoteRepositoryAdmin
54 extends AbstractRepositoryAdmin
55 implements RemoteRepositoryAdmin
59 RepositoryRegistry repositoryRegistry;
63 private void initialize()
64 throws RepositoryAdminException
66 for ( org.apache.archiva.admin.model.beans.RemoteRepository remoteRepository : getRemoteRepositories() )
68 createIndexContext( remoteRepository );
73 * Conversion between the repository from the registry and the serialized DTO for the admin API
75 private org.apache.archiva.admin.model.beans.RemoteRepository convertRepo( RemoteRepository repo ) {
79 org.apache.archiva.admin.model.beans.RemoteRepository adminRepo = new org.apache.archiva.admin.model.beans.RemoteRepository( getArchivaConfiguration().getDefaultLocale() );
80 setBaseRepoAttributes( adminRepo, repo );
81 adminRepo.setUrl( convertUriToString( repo.getLocation() ));
82 adminRepo.setCronExpression( repo.getSchedulingDefinition() );
83 adminRepo.setCheckPath( repo.getCheckPath() );
84 adminRepo.setExtraHeaders( repo.getExtraHeaders() );
85 adminRepo.setExtraParameters( repo.getExtraParameters() );
86 adminRepo.setTimeout( (int) repo.getTimeout().getSeconds() );
87 RepositoryCredentials creds = repo.getLoginCredentials();
88 if (creds!=null && creds instanceof PasswordCredentials) {
89 PasswordCredentials pCreds = (PasswordCredentials) creds;
90 adminRepo.setUserName( pCreds.getUsername() );
91 adminRepo.setPassword( new String(pCreds.getPassword()!=null ? pCreds.getPassword() : new char[0]) );
93 if (repo.supportsFeature( RemoteIndexFeature.class )) {
94 RemoteIndexFeature rif = repo.getFeature( RemoteIndexFeature.class ).get();
95 adminRepo.setRemoteIndexUrl( convertUriToString( rif.getIndexUri() ) );
96 adminRepo.setDownloadRemoteIndex( rif.isDownloadRemoteIndex() );
97 adminRepo.setRemoteDownloadNetworkProxyId( rif.getProxyId() );
98 adminRepo.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup() );
99 adminRepo.setRemoteDownloadTimeout( (int) rif.getDownloadTimeout().getSeconds() );
101 if (repo.supportsFeature(IndexCreationFeature.class)) {
102 IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
103 adminRepo.setIndexDirectory(PathUtil.getPathFromUri(icf.getIndexPath()).toString());
105 adminRepo.setDescription(repo.getDescription());
109 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())) {
163 String checkUrl = remoteRepository.getUrl().toLowerCase();
164 for (RepositoryCheckPath path : getArchivaConfiguration ().getConfiguration().getArchivaDefaultConfiguration().getDefaultCheckPaths()) {
165 log.debug("Checking path for urls: {} <-> {}", checkUrl, path.getUrl());
166 if (checkUrl.startsWith(path.getUrl())) {
167 remoteRepository.setCheckPath(path.getPath());
173 Configuration configuration = getArchivaConfiguration().getConfiguration();
174 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
175 getRepositoryConfiguration( remoteRepository );
176 log.debug("Adding remote repo {}", remoteRepositoryConfiguration);
180 repositoryRegistry.putRepository( remoteRepositoryConfiguration, configuration );
182 catch ( RepositoryException e )
184 log.error("Could not add remote repository {}: {}", remoteRepositoryConfiguration.getId(), e.getMessage(), e);
185 throw new RepositoryAdminException( "Adding of remote repository failed"+(e.getMessage()==null?"":": "+e.getMessage()) );
189 saveConfiguration( configuration );
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 );
205 throw new RepositoryAdminException( "Could not delete repository "+repositoryId+". The repository does not exist." );
209 repositoryRegistry.removeRepository( repo, configuration );
211 catch ( RepositoryException e )
213 log.error("Deletion of remote repository failed {}: {}", repo.getId(), e.getMessage(), e);
214 throw new RepositoryAdminException( "Could not delete remote repository"+(e.getMessage()==null?"":": "+e.getMessage()) );
217 saveConfiguration( configuration );
223 public Boolean updateRemoteRepository( org.apache.archiva.admin.model.beans.RemoteRepository remoteRepository, AuditInformation auditInformation )
224 throws RepositoryAdminException
227 String repositoryId = remoteRepository.getId();
229 triggerAuditEvent( repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation );
231 // update means : remove and add
233 Configuration configuration = getArchivaConfiguration().getConfiguration();
235 RemoteRepositoryConfiguration remoteRepositoryConfiguration = getRepositoryConfiguration( remoteRepository );
238 repositoryRegistry.putRepository( remoteRepositoryConfiguration, configuration );
240 catch ( RepositoryException e )
242 log.error("Could not update remote repository {}: {}", remoteRepositoryConfiguration.getId(), e.getMessage(), e);
243 throw new RepositoryAdminException( "Update of remote repository failed"+(e.getMessage()==null?"":": "+e.getMessage()) );
245 saveConfiguration( configuration );
250 public Map<String, org.apache.archiva.admin.model.beans.RemoteRepository> getRemoteRepositoriesAsMap()
251 throws RepositoryAdminException
253 java.util.Map<String, org.apache.archiva.admin.model.beans.RemoteRepository> map = new HashMap<>();
255 for ( org.apache.archiva.admin.model.beans.RemoteRepository repo : getRemoteRepositories() )
257 map.put( repo.getId(), repo );
264 public ArchivaIndexingContext createIndexContext( org.apache.archiva.admin.model.beans.RemoteRepository remoteRepository )
265 throws RepositoryAdminException
267 RemoteRepository repo = repositoryRegistry.getRemoteRepository(remoteRepository.getId());
268 return repo.getIndexingContext();