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 // [MRM-520] Proxy Connectors are not deleted with the deletion of a Repository.
117 List<ProxyConnectorConfiguration> proxyConnectors = getProxyConnectors();
118 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
120 if ( StringUtils.equals( proxyConnector.getTargetRepoId(), repositoryId ) )
122 configuration.removeProxyConnector( proxyConnector );
126 saveConfiguration( configuration );
131 public Boolean updateRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
132 throws RepositoryAdminException
135 String repositoryId = remoteRepository.getId();
137 triggerAuditEvent( repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation );
139 // update means : remove and add
141 Configuration configuration = getArchivaConfiguration().getConfiguration();
143 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
144 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
145 if ( remoteRepositoryConfiguration == null )
147 throw new RepositoryAdminException(
148 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
151 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
153 remoteRepositoryConfiguration = getRemoteRepositoryConfiguration( remoteRepository );
154 configuration.addRemoteRepository( remoteRepositoryConfiguration );
155 saveConfiguration( configuration );
160 private RemoteRepositoryConfiguration getRemoteRepositoryConfiguration( RemoteRepository remoteRepository )
162 RemoteRepositoryConfiguration remoteRepositoryConfiguration = new RemoteRepositoryConfiguration();
163 remoteRepositoryConfiguration.setId( remoteRepository.getId() );
164 remoteRepositoryConfiguration.setPassword( remoteRepository.getPassword() );
165 remoteRepositoryConfiguration.setTimeout( remoteRepository.getTimeout() );
166 remoteRepositoryConfiguration.setUrl( remoteRepository.getUrl() );
167 remoteRepositoryConfiguration.setUsername( remoteRepository.getUserName() );
168 remoteRepositoryConfiguration.setLayout( remoteRepository.getLayout() );
169 remoteRepositoryConfiguration.setName( remoteRepository.getName() );
170 return remoteRepositoryConfiguration;