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.AuditInformation;
22 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
23 import org.apache.archiva.admin.repository.RepositoryAdminException;
24 import org.apache.archiva.audit.AuditEvent;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.maven.archiva.configuration.Configuration;
27 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
28 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
29 import org.springframework.stereotype.Service;
31 import java.util.ArrayList;
32 import java.util.List;
35 * @author Olivier Lamy
38 @Service( "remoteRepositoryAdmin#default" )
39 public class DefaultRemoteRepositoryAdmin
40 extends AbstractRepositoryAdmin
41 implements RemoteRepositoryAdmin
45 public List<RemoteRepository> getRemoteRepositories()
46 throws RepositoryAdminException
48 List<RemoteRepository> remoteRepositories = new ArrayList<RemoteRepository>();
49 for ( RemoteRepositoryConfiguration repositoryConfiguration : getArchivaConfiguration().getConfiguration().getRemoteRepositories() )
51 remoteRepositories.add(
52 new RemoteRepository( repositoryConfiguration.getId(), repositoryConfiguration.getName(),
53 repositoryConfiguration.getUrl(), repositoryConfiguration.getLayout(),
54 repositoryConfiguration.getUsername(), repositoryConfiguration.getPassword(),
55 repositoryConfiguration.getTimeout() ) );
57 return remoteRepositories;
60 public RemoteRepository getRemoteRepository( String repositoryId )
61 throws RepositoryAdminException
63 for ( RemoteRepository remoteRepository : getRemoteRepositories() )
65 if ( StringUtils.equals( repositoryId, remoteRepository.getId() ) )
67 return remoteRepository;
73 public Boolean addRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
74 throws RepositoryAdminException
76 triggerAuditEvent( remoteRepository.getId(), null, AuditEvent.ADD_REMOTE_REPO, auditInformation );
77 getRepositoryCommonValidator().basicValidation( remoteRepository, false );
79 //TODO we can validate it's a good uri/url
80 if ( StringUtils.isEmpty( remoteRepository.getUrl() ) )
82 throw new RepositoryAdminException( "url cannot be null" );
85 //MRM-752 - url needs trimming
86 remoteRepository.setUrl( StringUtils.trim( remoteRepository.getUrl() ) );
88 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
89 getRemoteRepositoryConfiguration( remoteRepository );
91 Configuration configuration = getArchivaConfiguration().getConfiguration();
92 configuration.addRemoteRepository( remoteRepositoryConfiguration );
93 saveConfiguration( configuration );
98 public Boolean deleteRemoteRepository( String repositoryId, AuditInformation auditInformation )
99 throws RepositoryAdminException
102 triggerAuditEvent( repositoryId, null, AuditEvent.DELETE_REMOTE_REPO, auditInformation );
104 Configuration configuration = getArchivaConfiguration().getConfiguration();
106 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
107 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
108 if ( remoteRepositoryConfiguration == null )
110 throw new RepositoryAdminException(
111 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
114 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
116 // TODO use ProxyConnectorAdmin interface ?
117 // [MRM-520] Proxy Connectors are not deleted with the deletion of a Repository.
118 List<ProxyConnectorConfiguration> proxyConnectors =
119 new ArrayList<ProxyConnectorConfiguration>( configuration.getProxyConnectors() );
120 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
122 if ( StringUtils.equals( proxyConnector.getTargetRepoId(), repositoryId ) )
124 configuration.removeProxyConnector( proxyConnector );
128 saveConfiguration( configuration );
133 public Boolean updateRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
134 throws RepositoryAdminException
137 String repositoryId = remoteRepository.getId();
139 triggerAuditEvent( repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation );
141 // update means : remove and add
143 Configuration configuration = getArchivaConfiguration().getConfiguration();
145 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
146 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
147 if ( remoteRepositoryConfiguration == null )
149 throw new RepositoryAdminException(
150 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
153 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
155 remoteRepositoryConfiguration = getRemoteRepositoryConfiguration( remoteRepository );
156 configuration.addRemoteRepository( remoteRepositoryConfiguration );
157 saveConfiguration( configuration );
162 private RemoteRepositoryConfiguration getRemoteRepositoryConfiguration( RemoteRepository remoteRepository )
164 RemoteRepositoryConfiguration remoteRepositoryConfiguration = new RemoteRepositoryConfiguration();
165 remoteRepositoryConfiguration.setId( remoteRepository.getId() );
166 remoteRepositoryConfiguration.setPassword( remoteRepository.getPassword() );
167 remoteRepositoryConfiguration.setTimeout( remoteRepository.getTimeout() );
168 remoteRepositoryConfiguration.setUrl( remoteRepository.getUrl() );
169 remoteRepositoryConfiguration.setUsername( remoteRepository.getUserName() );
170 remoteRepositoryConfiguration.setLayout( remoteRepository.getLayout() );
171 remoteRepositoryConfiguration.setName( remoteRepository.getName() );
172 return remoteRepositoryConfiguration;