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.beans.RemoteRepository;
24 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
25 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
26 import org.apache.archiva.audit.AuditEvent;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
30 import org.apache.commons.lang.StringUtils;
31 import org.springframework.stereotype.Service;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
39 * @author Olivier Lamy
42 @Service( "remoteRepositoryAdmin#default" )
43 public class DefaultRemoteRepositoryAdmin
44 extends AbstractRepositoryAdmin
45 implements RemoteRepositoryAdmin
49 public List<RemoteRepository> getRemoteRepositories()
50 throws RepositoryAdminException
52 List<RemoteRepository> remoteRepositories = new ArrayList<RemoteRepository>();
53 for ( RemoteRepositoryConfiguration repositoryConfiguration : getArchivaConfiguration().getConfiguration().getRemoteRepositories() )
55 RemoteRepository remoteRepository =
56 new RemoteRepository( repositoryConfiguration.getId(), repositoryConfiguration.getName(),
57 repositoryConfiguration.getUrl(), repositoryConfiguration.getLayout(),
58 repositoryConfiguration.getUsername(), repositoryConfiguration.getPassword(),
59 repositoryConfiguration.getTimeout() );
60 remoteRepository.setDownloadRemoteIndex( repositoryConfiguration.isDownloadRemoteIndex() );
61 remoteRepository.setRemoteIndexUrl( repositoryConfiguration.getRemoteIndexUrl() );
62 remoteRepository.setCronExpression( repositoryConfiguration.getRefreshCronExpression() );
63 remoteRepository.setIndexDirectory( repositoryConfiguration.getIndexDir() );
64 remoteRepository.setRemoteDownloadNetworkProxyId(
65 repositoryConfiguration.getRemoteDownloadNetworkProxyId() );
66 remoteRepository.setRemoteDownloadTimeout( repositoryConfiguration.getRemoteDownloadTimeout() );
67 remoteRepositories.add( remoteRepository );
69 return remoteRepositories;
72 public RemoteRepository getRemoteRepository( String repositoryId )
73 throws RepositoryAdminException
75 for ( RemoteRepository remoteRepository : getRemoteRepositories() )
77 if ( StringUtils.equals( repositoryId, remoteRepository.getId() ) )
79 return remoteRepository;
85 public Boolean addRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
86 throws RepositoryAdminException
88 triggerAuditEvent( remoteRepository.getId(), null, AuditEvent.ADD_REMOTE_REPO, auditInformation );
89 getRepositoryCommonValidator().basicValidation( remoteRepository, false );
91 //TODO we can validate it's a good uri/url
92 if ( StringUtils.isEmpty( remoteRepository.getUrl() ) )
94 throw new RepositoryAdminException( "url cannot be null" );
97 //MRM-752 - url needs trimming
98 remoteRepository.setUrl( StringUtils.trim( remoteRepository.getUrl() ) );
100 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
101 getRemoteRepositoryConfiguration( remoteRepository );
103 Configuration configuration = getArchivaConfiguration().getConfiguration();
104 configuration.addRemoteRepository( remoteRepositoryConfiguration );
105 saveConfiguration( configuration );
110 public Boolean deleteRemoteRepository( String repositoryId, AuditInformation auditInformation )
111 throws RepositoryAdminException
114 triggerAuditEvent( repositoryId, null, AuditEvent.DELETE_REMOTE_REPO, auditInformation );
116 Configuration configuration = getArchivaConfiguration().getConfiguration();
118 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
119 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
120 if ( remoteRepositoryConfiguration == null )
122 throw new RepositoryAdminException(
123 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
126 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
128 // TODO use ProxyConnectorAdmin interface ?
129 // [MRM-520] Proxy Connectors are not deleted with the deletion of a Repository.
130 List<ProxyConnectorConfiguration> proxyConnectors =
131 new ArrayList<ProxyConnectorConfiguration>( configuration.getProxyConnectors() );
132 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
134 if ( StringUtils.equals( proxyConnector.getTargetRepoId(), repositoryId ) )
136 configuration.removeProxyConnector( proxyConnector );
140 saveConfiguration( configuration );
145 public Boolean updateRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
146 throws RepositoryAdminException
149 String repositoryId = remoteRepository.getId();
151 triggerAuditEvent( repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation );
153 // update means : remove and add
155 Configuration configuration = getArchivaConfiguration().getConfiguration();
157 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
158 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
159 if ( remoteRepositoryConfiguration == null )
161 throw new RepositoryAdminException(
162 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
165 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
167 remoteRepositoryConfiguration = getRemoteRepositoryConfiguration( remoteRepository );
168 configuration.addRemoteRepository( remoteRepositoryConfiguration );
169 saveConfiguration( configuration );
174 public Map<String, RemoteRepository> getRemoteRepositoriesAsMap()
175 throws RepositoryAdminException
177 java.util.Map<String, RemoteRepository> map = new HashMap<String, RemoteRepository>();
179 for ( RemoteRepository repo : getRemoteRepositories() )
181 map.put( repo.getId(), repo );
187 private RemoteRepositoryConfiguration getRemoteRepositoryConfiguration( RemoteRepository remoteRepository )
189 RemoteRepositoryConfiguration remoteRepositoryConfiguration = new RemoteRepositoryConfiguration();
190 remoteRepositoryConfiguration.setId( remoteRepository.getId() );
191 remoteRepositoryConfiguration.setPassword( remoteRepository.getPassword() );
192 remoteRepositoryConfiguration.setTimeout( remoteRepository.getTimeout() );
193 remoteRepositoryConfiguration.setUrl( remoteRepository.getUrl() );
194 remoteRepositoryConfiguration.setUsername( remoteRepository.getUserName() );
195 remoteRepositoryConfiguration.setLayout( remoteRepository.getLayout() );
196 remoteRepositoryConfiguration.setName( remoteRepository.getName() );
197 remoteRepositoryConfiguration.setDownloadRemoteIndex( remoteRepository.isDownloadRemoteIndex() );
198 remoteRepositoryConfiguration.setRemoteIndexUrl( remoteRepository.getRemoteIndexUrl() );
199 remoteRepositoryConfiguration.setRefreshCronExpression( remoteRepository.getCronExpression() );
200 remoteRepositoryConfiguration.setIndexDir( remoteRepository.getIndexDirectory() );
201 remoteRepositoryConfiguration.setRemoteDownloadNetworkProxyId(
202 remoteRepository.getRemoteDownloadNetworkProxyId() );
203 remoteRepositoryConfiguration.setRemoteDownloadTimeout( remoteRepository.getRemoteDownloadTimeout() );
204 return remoteRepositoryConfiguration;