1 package org.apache.archiva.admin.repository.admin;
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.admin.ArchivaAdministration;
24 import org.apache.archiva.admin.model.beans.FileType;
25 import org.apache.archiva.admin.model.beans.LegacyArtifactPath;
26 import org.apache.archiva.admin.model.beans.NetworkConfiguration;
27 import org.apache.archiva.admin.model.beans.OrganisationInformation;
28 import org.apache.archiva.admin.model.beans.UiConfiguration;
29 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
30 import org.apache.archiva.configuration.Configuration;
31 import org.apache.archiva.configuration.UserInterfaceOptions;
32 import org.apache.archiva.configuration.WebappConfiguration;
33 import org.apache.archiva.metadata.model.facets.AuditEvent;
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.http.impl.conn.PoolingClientConnectionManager;
36 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
37 import org.apache.maven.wagon.providers.http.HttpWagon;
38 import org.springframework.stereotype.Service;
40 import javax.annotation.PostConstruct;
41 import javax.annotation.PreDestroy;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
47 * @author Olivier Lamy
49 @Service( "archivaAdministration#default" )
50 public class DefaultArchivaAdministration
51 extends AbstractRepositoryAdmin
52 implements ArchivaAdministration
55 private PoolingHttpClientConnectionManager poolingClientConnectionManager;
58 public void initialize()
59 throws RepositoryAdminException
61 // setup wagon on start with initial values
62 NetworkConfiguration networkConfiguration = getNetworkConfiguration();
63 setupWagon( networkConfiguration );
67 public void shutdown()
69 if ( this.poolingClientConnectionManager != null )
71 this.poolingClientConnectionManager.shutdown();
77 public List<LegacyArtifactPath> getLegacyArtifactPaths()
78 throws RepositoryAdminException
80 List<LegacyArtifactPath> legacyArtifactPaths = new ArrayList<>(
81 getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths().size() );
82 for ( org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath : getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths() )
84 legacyArtifactPaths.add(
85 getModelMapper().map( legacyArtifactPath, LegacyArtifactPath.class ) );
87 return legacyArtifactPaths;
91 public void addLegacyArtifactPath( LegacyArtifactPath legacyArtifactPath, AuditInformation auditInformation )
92 throws RepositoryAdminException
94 Configuration configuration = getArchivaConfiguration().getConfiguration();
96 configuration.addLegacyArtifactPath( getModelMapper().map( legacyArtifactPath,
97 org.apache.archiva.configuration.LegacyArtifactPath.class ) );
99 saveConfiguration( configuration );
100 triggerAuditEvent( "", "", AuditEvent.ADD_LEGACY_PATH, auditInformation );
104 public void deleteLegacyArtifactPath( String path, AuditInformation auditInformation )
105 throws RepositoryAdminException
107 Configuration configuration = getArchivaConfiguration().getConfiguration();
108 org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath =
109 new org.apache.archiva.configuration.LegacyArtifactPath();
111 legacyArtifactPath.setPath( path );
112 configuration.removeLegacyArtifactPath( legacyArtifactPath );
114 saveConfiguration( configuration );
115 triggerAuditEvent( "", "", AuditEvent.REMOVE_LEGACY_PATH, auditInformation );
120 public void addFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
121 throws RepositoryAdminException
123 Configuration configuration = getArchivaConfiguration().getConfiguration();
125 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
126 if ( fileType == null )
131 if ( fileType.getPatterns().contains( pattern ) )
133 throw new RepositoryAdminException(
134 "File type [" + fileTypeId + "] already contains pattern [" + pattern + "]" );
136 fileType.addPattern( pattern );
138 saveConfiguration( configuration );
139 triggerAuditEvent( "", "", AuditEvent.ADD_PATTERN, auditInformation );
143 public void removeFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
144 throws RepositoryAdminException
146 Configuration configuration = getArchivaConfiguration().getConfiguration();
148 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
149 if ( fileType == null )
153 fileType.removePattern( pattern );
155 saveConfiguration( configuration );
156 triggerAuditEvent( "", "", AuditEvent.REMOVE_PATTERN, auditInformation );
160 public FileType getFileType( String fileTypeId )
161 throws RepositoryAdminException
163 org.apache.archiva.configuration.FileType fileType =
164 getFileTypeById( fileTypeId, getArchivaConfiguration().getConfiguration() );
165 if ( fileType == null )
169 return getModelMapper().map( fileType, FileType.class );
173 public void addFileType( FileType fileType, AuditInformation auditInformation )
174 throws RepositoryAdminException
176 Configuration configuration = getArchivaConfiguration().getConfiguration();
177 if ( getFileTypeById( fileType.getId(), configuration ) != null )
179 throw new RepositoryAdminException(
180 "impossible to FileType with id " + fileType.getId() + " already exists" );
183 configuration.getRepositoryScanning().addFileType(
184 getModelMapper().map( fileType, org.apache.archiva.configuration.FileType.class ) );
185 saveConfiguration( configuration );
189 public void removeFileType( String fileTypeId, AuditInformation auditInformation )
190 throws RepositoryAdminException
192 Configuration configuration = getArchivaConfiguration().getConfiguration();
193 org.apache.archiva.configuration.FileType fileType = new org.apache.archiva.configuration.FileType();
194 fileType.setId( fileTypeId );
195 configuration.getRepositoryScanning().removeFileType( fileType );
196 saveConfiguration( configuration );
200 public void addKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
201 throws RepositoryAdminException
203 Configuration configuration = getArchivaConfiguration().getConfiguration();
204 if ( configuration.getRepositoryScanning().getKnownContentConsumers().contains( knownContentConsumer ) )
206 log.warn( "skip adding knownContentConsumer {} as already here", knownContentConsumer );
209 configuration.getRepositoryScanning().addKnownContentConsumer( knownContentConsumer );
210 saveConfiguration( configuration );
211 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
215 public void removeKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
216 throws RepositoryAdminException
218 Configuration configuration = getArchivaConfiguration().getConfiguration();
219 configuration.getRepositoryScanning().removeKnownContentConsumer( knownContentConsumer );
220 saveConfiguration( configuration );
221 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
225 public void addInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
226 throws RepositoryAdminException
228 Configuration configuration = getArchivaConfiguration().getConfiguration();
229 if ( configuration.getRepositoryScanning().getInvalidContentConsumers().contains( invalidContentConsumer ) )
231 log.warn( "skip adding invalidContentConsumer {} as already here", invalidContentConsumer );
234 configuration.getRepositoryScanning().addInvalidContentConsumer( invalidContentConsumer );
235 saveConfiguration( configuration );
236 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
240 public void removeInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
241 throws RepositoryAdminException
243 Configuration configuration = getArchivaConfiguration().getConfiguration();
244 configuration.getRepositoryScanning().removeInvalidContentConsumer( invalidContentConsumer );
245 saveConfiguration( configuration );
246 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
250 public void setKnownContentConsumers( List<String> knownContentConsumers, AuditInformation auditInformation )
251 throws RepositoryAdminException
253 if ( knownContentConsumers == null )
257 for ( String knowContentConsumer : knownContentConsumers )
259 addKnownContentConsumer( knowContentConsumer, auditInformation );
264 public void setInvalidContentConsumers( List<String> invalidContentConsumers, AuditInformation auditInformation )
265 throws RepositoryAdminException
267 if ( invalidContentConsumers == null )
271 for ( String invalidContentConsumer : invalidContentConsumers )
273 addKnownContentConsumer( invalidContentConsumer, auditInformation );
278 public List<FileType> getFileTypes()
279 throws RepositoryAdminException
281 List<org.apache.archiva.configuration.FileType> configFileTypes =
282 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getFileTypes();
283 if ( configFileTypes == null || configFileTypes.isEmpty() )
285 return Collections.emptyList();
287 List<FileType> fileTypes = new ArrayList<>( configFileTypes.size() );
288 for ( org.apache.archiva.configuration.FileType fileType : configFileTypes )
290 fileTypes.add( getModelMapper().map( fileType, FileType.class ) );
296 public List<String> getKnownContentConsumers()
297 throws RepositoryAdminException
299 return new ArrayList<>(
300 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getKnownContentConsumers() );
304 public List<String> getInvalidContentConsumers()
305 throws RepositoryAdminException
307 return new ArrayList<>(
308 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getInvalidContentConsumers() );
312 public OrganisationInformation getOrganisationInformation()
313 throws RepositoryAdminException
315 org.apache.archiva.configuration.OrganisationInformation organisationInformation =
316 getArchivaConfiguration().getConfiguration().getOrganisationInfo();
317 if ( organisationInformation == null )
321 return getModelMapper().map( organisationInformation, OrganisationInformation.class );
325 public void setOrganisationInformation( OrganisationInformation organisationInformation )
326 throws RepositoryAdminException
328 Configuration configuration = getArchivaConfiguration().getConfiguration();
329 if ( organisationInformation != null )
331 org.apache.archiva.configuration.OrganisationInformation organisationInformationModel =
332 getModelMapper().map( organisationInformation,
333 org.apache.archiva.configuration.OrganisationInformation.class );
334 configuration.setOrganisationInfo( organisationInformationModel );
338 configuration.setOrganisationInfo( null );
340 saveConfiguration( configuration );
344 public UiConfiguration getUiConfiguration()
345 throws RepositoryAdminException
347 WebappConfiguration webappConfiguration = getArchivaConfiguration().getConfiguration().getWebapp();
348 if ( webappConfiguration == null )
352 UserInterfaceOptions userInterfaceOptions = webappConfiguration.getUi();
353 if ( userInterfaceOptions == null )
357 return getModelMapper().map( userInterfaceOptions, UiConfiguration.class );
361 public void updateUiConfiguration( UiConfiguration uiConfiguration )
362 throws RepositoryAdminException
364 Configuration configuration = getArchivaConfiguration().getConfiguration();
365 if ( uiConfiguration != null )
368 UserInterfaceOptions userInterfaceOptions =
369 getModelMapper().map( uiConfiguration, UserInterfaceOptions.class );
370 configuration.getWebapp().setUi( userInterfaceOptions );
374 configuration.getWebapp().setUi( null );
376 saveConfiguration( configuration );
381 public NetworkConfiguration getNetworkConfiguration()
382 throws RepositoryAdminException
384 org.apache.archiva.configuration.NetworkConfiguration networkConfiguration =
385 getArchivaConfiguration().getConfiguration().getNetworkConfiguration();
387 if ( networkConfiguration == null )
391 return getModelMapper().map( networkConfiguration, NetworkConfiguration.class );
395 public void setNetworkConfiguration( NetworkConfiguration networkConfiguration )
396 throws RepositoryAdminException
398 Configuration configuration = getArchivaConfiguration().getConfiguration();
399 if ( networkConfiguration == null )
401 configuration.setNetworkConfiguration( null );
405 configuration.setNetworkConfiguration( getModelMapper().map( networkConfiguration,
406 org.apache.archiva.configuration.NetworkConfiguration.class ) );
408 setupWagon( networkConfiguration );
409 saveConfiguration( configuration );
412 protected void setupWagon( NetworkConfiguration networkConfiguration )
414 if ( networkConfiguration == null )
416 // back to default values
417 HttpWagon.setPersistentPool( true );
418 poolingClientConnectionManager = new PoolingHttpClientConnectionManager();
419 poolingClientConnectionManager.setDefaultMaxPerRoute( 30 );
420 poolingClientConnectionManager.setMaxTotal( 30 );
421 HttpWagon.setPoolingHttpClientConnectionManager( poolingClientConnectionManager );
426 HttpWagon.setPersistentPool( networkConfiguration.isUsePooling() );
427 poolingClientConnectionManager = new PoolingHttpClientConnectionManager();
428 poolingClientConnectionManager.setDefaultMaxPerRoute( networkConfiguration.getMaxTotalPerHost() );
429 poolingClientConnectionManager.setMaxTotal( networkConfiguration.getMaxTotal() );
430 HttpWagon.setPoolingHttpClientConnectionManager( poolingClientConnectionManager );
434 //-------------------------
436 //-------------------------
438 private org.apache.archiva.configuration.FileType getFileTypeById( String id, Configuration configuration )
440 for ( org.apache.archiva.configuration.FileType fileType : configuration.getRepositoryScanning().getFileTypes() )
442 if ( StringUtils.equals( id, fileType.getId() ) )