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.configuration.Configuration;
27 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
28 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
29 import org.apache.archiva.configuration.RepositoryCheckPath;
30 import org.apache.archiva.metadata.model.facets.AuditEvent;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.maven.index.NexusIndexer;
33 import org.apache.maven.index.context.IndexCreator;
34 import org.apache.maven.index.context.IndexingContext;
35 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
36 import org.springframework.stereotype.Service;
38 import javax.annotation.PostConstruct;
39 import javax.annotation.PreDestroy;
40 import javax.inject.Inject;
42 import java.io.IOException;
43 import java.net.MalformedURLException;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
50 * @author Olivier Lamy
53 @Service("remoteRepositoryAdmin#default")
54 public class DefaultRemoteRepositoryAdmin
55 extends AbstractRepositoryAdmin
56 implements RemoteRepositoryAdmin
60 private List<? extends IndexCreator> indexCreators;
63 private NexusIndexer indexer;
66 private void initialize()
67 throws RepositoryAdminException
69 for ( RemoteRepository remoteRepository : getRemoteRepositories() )
71 createIndexContext( remoteRepository );
76 private void shutdown()
77 throws RepositoryAdminException
81 List<RemoteRepository> remoteRepositories = getRemoteRepositories();
82 // close index on shutdown
83 for ( RemoteRepository remoteRepository : remoteRepositories )
85 IndexingContext context = indexer.getIndexingContexts().get( remoteRepository.getId() );
86 if ( context != null )
88 indexer.removeIndexingContext( context, false );
92 catch ( IOException e )
94 throw new RepositoryAdminException( e.getMessage(), e );
100 public List<RemoteRepository> getRemoteRepositories()
101 throws RepositoryAdminException
103 List<RemoteRepository> remoteRepositories =
104 new ArrayList<>( getArchivaConfiguration().getConfiguration().getRemoteRepositories().size() );
105 for ( RemoteRepositoryConfiguration repositoryConfiguration : getArchivaConfiguration().getConfiguration().getRemoteRepositories() )
107 RemoteRepository remoteRepository =
108 new RemoteRepository( repositoryConfiguration.getId(), repositoryConfiguration.getName(),
109 repositoryConfiguration.getUrl(), repositoryConfiguration.getLayout(),
110 repositoryConfiguration.getUsername(), repositoryConfiguration.getPassword(),
111 repositoryConfiguration.getTimeout() );
112 remoteRepository.setDownloadRemoteIndex( repositoryConfiguration.isDownloadRemoteIndex() );
113 remoteRepository.setRemoteIndexUrl( repositoryConfiguration.getRemoteIndexUrl() );
114 remoteRepository.setCronExpression( repositoryConfiguration.getRefreshCronExpression() );
115 remoteRepository.setIndexDirectory( repositoryConfiguration.getIndexDir() );
116 remoteRepository.setRemoteDownloadNetworkProxyId(
117 repositoryConfiguration.getRemoteDownloadNetworkProxyId() );
118 remoteRepository.setRemoteDownloadTimeout( repositoryConfiguration.getRemoteDownloadTimeout() );
119 remoteRepository.setDownloadRemoteIndexOnStartup(
120 repositoryConfiguration.isDownloadRemoteIndexOnStartup() );
121 remoteRepository.setDescription( repositoryConfiguration.getDescription() );
122 remoteRepository.setExtraHeaders( repositoryConfiguration.getExtraHeaders() );
123 remoteRepository.setExtraParameters( repositoryConfiguration.getExtraParameters() );
124 remoteRepository.setCheckPath(repositoryConfiguration.getCheckPath());
125 remoteRepositories.add( remoteRepository );
127 return remoteRepositories;
131 public RemoteRepository getRemoteRepository( String repositoryId )
132 throws RepositoryAdminException
134 for ( RemoteRepository remoteRepository : getRemoteRepositories() )
136 if ( StringUtils.equals( repositoryId, remoteRepository.getId() ) )
138 return remoteRepository;
145 public Boolean addRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
146 throws RepositoryAdminException
148 triggerAuditEvent( remoteRepository.getId(), null, AuditEvent.ADD_REMOTE_REPO, auditInformation );
149 getRepositoryCommonValidator().basicValidation( remoteRepository, false );
151 //TODO we can validate it's a good uri/url
152 if ( StringUtils.isEmpty( remoteRepository.getUrl() ) )
154 throw new RepositoryAdminException( "url cannot be null" );
157 //MRM-752 - url needs trimming
158 //MRM-1940 - URL should not end with a slash
159 remoteRepository.setUrl( StringUtils.stripEnd(StringUtils.trim( remoteRepository.getUrl() ), "/"));
161 if (StringUtils.isEmpty(remoteRepository.getCheckPath())) {
162 String checkUrl = remoteRepository.getUrl().toLowerCase();
163 for (RepositoryCheckPath path : getArchivaConfiguration ().getConfiguration().getArchivaDefaultConfiguration().getDefaultCheckPaths()) {
164 log.debug("Checking path for urls: {} <-> {}", checkUrl, path.getUrl());
165 if (checkUrl.startsWith(path.getUrl())) {
166 remoteRepository.setCheckPath(path.getPath());
172 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
173 getRemoteRepositoryConfiguration( remoteRepository );
175 Configuration configuration = getArchivaConfiguration().getConfiguration();
176 configuration.addRemoteRepository( remoteRepositoryConfiguration );
177 saveConfiguration( configuration );
183 public Boolean deleteRemoteRepository( String repositoryId, AuditInformation auditInformation )
184 throws RepositoryAdminException
187 triggerAuditEvent( repositoryId, null, AuditEvent.DELETE_REMOTE_REPO, auditInformation );
189 Configuration configuration = getArchivaConfiguration().getConfiguration();
191 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
192 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
193 if ( remoteRepositoryConfiguration == null )
195 throw new RepositoryAdminException(
196 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
199 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
201 // TODO use ProxyConnectorAdmin interface ?
202 // [MRM-520] Proxy Connectors are not deleted with the deletion of a Repository.
203 List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>( configuration.getProxyConnectors() );
204 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
206 if ( StringUtils.equals( proxyConnector.getTargetRepoId(), repositoryId ) )
208 configuration.removeProxyConnector( proxyConnector );
212 saveConfiguration( configuration );
218 public Boolean updateRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
219 throws RepositoryAdminException
222 String repositoryId = remoteRepository.getId();
224 triggerAuditEvent( repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation );
226 // update means : remove and add
228 Configuration configuration = getArchivaConfiguration().getConfiguration();
230 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
231 configuration.getRemoteRepositoriesAsMap().get( repositoryId );
232 if ( remoteRepositoryConfiguration == null )
234 throw new RepositoryAdminException(
235 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
238 configuration.removeRemoteRepository( remoteRepositoryConfiguration );
240 remoteRepositoryConfiguration = getRemoteRepositoryConfiguration( remoteRepository );
241 configuration.addRemoteRepository( remoteRepositoryConfiguration );
242 saveConfiguration( configuration );
248 public Map<String, RemoteRepository> getRemoteRepositoriesAsMap()
249 throws RepositoryAdminException
251 java.util.Map<String, RemoteRepository> map = new HashMap<>();
253 for ( RemoteRepository repo : getRemoteRepositories() )
255 map.put( repo.getId(), repo );
262 public IndexingContext createIndexContext( RemoteRepository remoteRepository )
263 throws RepositoryAdminException
267 String appServerBase = getRegistry().getString( "appserver.base" );
269 String contextKey = "remote-" + remoteRepository.getId();
270 IndexingContext indexingContext = indexer.getIndexingContexts().get( contextKey );
271 if ( indexingContext != null )
273 return indexingContext;
275 // create remote repository path
276 File repoDir = new File( appServerBase, "data/remotes/" + remoteRepository.getId() );
277 if ( !repoDir.exists() )
282 File indexDirectory = null;
284 // is there configured indexDirectory ?
285 String indexDirectoryPath = remoteRepository.getIndexDirectory();
287 if ( StringUtils.isNotBlank( indexDirectoryPath ) )
289 if ( new File( indexDirectoryPath ).isAbsolute() )
291 indexDirectory = new File( indexDirectoryPath );
295 indexDirectory = new File( repoDir, indexDirectoryPath );
298 // if not configured use a default value
299 if ( indexDirectory == null )
301 indexDirectory = new File( repoDir, ".index" );
303 if ( !indexDirectory.exists() )
305 indexDirectory.mkdirs();
307 return indexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
308 remoteRepository.getUrl(), calculateIndexRemoteUrl( remoteRepository ),
311 catch ( MalformedURLException e )
313 throw new RepositoryAdminException( e.getMessage(), e );
315 catch ( IOException e )
317 throw new RepositoryAdminException( e.getMessage(), e );
319 catch ( UnsupportedExistingLuceneIndexException e )
321 throw new RepositoryAdminException( e.getMessage(), e );
326 protected String calculateIndexRemoteUrl( RemoteRepository remoteRepository )
328 if ( StringUtils.startsWith( remoteRepository.getRemoteIndexUrl(), "http" ) )
330 String baseUrl = remoteRepository.getRemoteIndexUrl();
331 return baseUrl.endsWith( "/" ) ? StringUtils.substringBeforeLast( baseUrl, "/" ) : baseUrl;
333 String baseUrl = StringUtils.endsWith( remoteRepository.getUrl(), "/" ) ? StringUtils.substringBeforeLast(
334 remoteRepository.getUrl(), "/" ) : remoteRepository.getUrl();
336 baseUrl = StringUtils.isEmpty( remoteRepository.getRemoteIndexUrl() )
337 ? baseUrl + "/.index"
338 : baseUrl + "/" + remoteRepository.getRemoteIndexUrl();
343 private RemoteRepositoryConfiguration getRemoteRepositoryConfiguration( RemoteRepository remoteRepository )
345 RemoteRepositoryConfiguration remoteRepositoryConfiguration = new RemoteRepositoryConfiguration();
346 remoteRepositoryConfiguration.setId( remoteRepository.getId() );
347 remoteRepositoryConfiguration.setPassword( remoteRepository.getPassword() );
348 remoteRepositoryConfiguration.setTimeout( remoteRepository.getTimeout() );
349 remoteRepositoryConfiguration.setUrl( remoteRepository.getUrl() );
350 remoteRepositoryConfiguration.setUsername( remoteRepository.getUserName() );
351 remoteRepositoryConfiguration.setLayout( remoteRepository.getLayout() );
352 remoteRepositoryConfiguration.setName( remoteRepository.getName() );
353 remoteRepositoryConfiguration.setDownloadRemoteIndex( remoteRepository.isDownloadRemoteIndex() );
354 remoteRepositoryConfiguration.setRemoteIndexUrl( remoteRepository.getRemoteIndexUrl() );
355 remoteRepositoryConfiguration.setRefreshCronExpression( remoteRepository.getCronExpression() );
356 remoteRepositoryConfiguration.setIndexDir( remoteRepository.getIndexDirectory() );
357 remoteRepositoryConfiguration.setRemoteDownloadNetworkProxyId(
358 remoteRepository.getRemoteDownloadNetworkProxyId() );
359 remoteRepositoryConfiguration.setRemoteDownloadTimeout( remoteRepository.getRemoteDownloadTimeout() );
360 remoteRepositoryConfiguration.setDownloadRemoteIndexOnStartup(
361 remoteRepository.isDownloadRemoteIndexOnStartup() );
362 remoteRepositoryConfiguration.setDescription( remoteRepository.getDescription() );
363 remoteRepositoryConfiguration.setExtraHeaders( remoteRepository.getExtraHeaders() );
364 remoteRepositoryConfiguration.setExtraParameters( remoteRepository.getExtraParameters() );
365 remoteRepositoryConfiguration.setCheckPath(remoteRepository.getCheckPath());
366 return remoteRepositoryConfiguration;