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.audit.AuditEvent;
31 import org.apache.archiva.configuration.Configuration;
32 import org.apache.archiva.configuration.UserInterfaceOptions;
33 import org.apache.archiva.configuration.WebappConfiguration;
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.http.impl.conn.PoolingClientConnectionManager;
36 import org.apache.maven.wagon.providers.http.HttpWagon;
37 import org.springframework.stereotype.Service;
39 import javax.annotation.PostConstruct;
40 import javax.annotation.PreDestroy;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.List;
46 * @author Olivier Lamy
48 @Service( "archivaAdministration#default" )
49 public class DefaultArchivaAdministration
50 extends AbstractRepositoryAdmin
51 implements ArchivaAdministration
54 private PoolingClientConnectionManager poolingClientConnectionManager;
57 public void initialize()
58 throws RepositoryAdminException
60 // setup wagon on start with initial values
61 NetworkConfiguration networkConfiguration = getNetworkConfiguration();
62 setupWagon( networkConfiguration );
66 public void shutdown()
68 if ( this.poolingClientConnectionManager != null )
70 this.poolingClientConnectionManager.shutdown();
76 public List<LegacyArtifactPath> getLegacyArtifactPaths()
77 throws RepositoryAdminException
79 List<LegacyArtifactPath> legacyArtifactPaths = new ArrayList<>(
80 getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths().size() );
81 for ( org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath : getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths() )
83 legacyArtifactPaths.add(
84 getModelMapper().map( legacyArtifactPath, LegacyArtifactPath.class ) );
86 return legacyArtifactPaths;
90 public void addLegacyArtifactPath( LegacyArtifactPath legacyArtifactPath, AuditInformation auditInformation )
91 throws RepositoryAdminException
93 Configuration configuration = getArchivaConfiguration().getConfiguration();
95 configuration.addLegacyArtifactPath( getModelMapper().map( legacyArtifactPath,
96 org.apache.archiva.configuration.LegacyArtifactPath.class ) );
98 saveConfiguration( configuration );
99 triggerAuditEvent( "", "", AuditEvent.ADD_LEGACY_PATH, auditInformation );
103 public void deleteLegacyArtifactPath( String path, AuditInformation auditInformation )
104 throws RepositoryAdminException
106 Configuration configuration = getArchivaConfiguration().getConfiguration();
107 org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath =
108 new org.apache.archiva.configuration.LegacyArtifactPath();
110 legacyArtifactPath.setPath( path );
111 configuration.removeLegacyArtifactPath( legacyArtifactPath );
113 saveConfiguration( configuration );
114 triggerAuditEvent( "", "", AuditEvent.REMOVE_LEGACY_PATH, auditInformation );
119 public void addFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
120 throws RepositoryAdminException
122 Configuration configuration = getArchivaConfiguration().getConfiguration();
124 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
125 if ( fileType == null )
130 if ( fileType.getPatterns().contains( pattern ) )
132 throw new RepositoryAdminException(
133 "File type [" + fileTypeId + "] already contains pattern [" + pattern + "]" );
135 fileType.addPattern( pattern );
137 saveConfiguration( configuration );
138 triggerAuditEvent( "", "", AuditEvent.ADD_PATTERN, auditInformation );
142 public void removeFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
143 throws RepositoryAdminException
145 Configuration configuration = getArchivaConfiguration().getConfiguration();
147 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
148 if ( fileType == null )
152 fileType.removePattern( pattern );
154 saveConfiguration( configuration );
155 triggerAuditEvent( "", "", AuditEvent.REMOVE_PATTERN, auditInformation );
159 public FileType getFileType( String fileTypeId )
160 throws RepositoryAdminException
162 org.apache.archiva.configuration.FileType fileType =
163 getFileTypeById( fileTypeId, getArchivaConfiguration().getConfiguration() );
164 if ( fileType == null )
168 return getModelMapper().map( fileType, FileType.class );
172 public void addFileType( FileType fileType, AuditInformation auditInformation )
173 throws RepositoryAdminException
175 Configuration configuration = getArchivaConfiguration().getConfiguration();
176 if ( getFileTypeById( fileType.getId(), configuration ) != null )
178 throw new RepositoryAdminException(
179 "impossible to FileType with id " + fileType.getId() + " already exists" );
182 configuration.getRepositoryScanning().addFileType(
183 getModelMapper().map( fileType, org.apache.archiva.configuration.FileType.class ) );
184 saveConfiguration( configuration );
188 public void removeFileType( String fileTypeId, AuditInformation auditInformation )
189 throws RepositoryAdminException
191 Configuration configuration = getArchivaConfiguration().getConfiguration();
192 org.apache.archiva.configuration.FileType fileType = new org.apache.archiva.configuration.FileType();
193 fileType.setId( fileTypeId );
194 configuration.getRepositoryScanning().removeFileType( fileType );
195 saveConfiguration( configuration );
199 public void addKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
200 throws RepositoryAdminException
202 Configuration configuration = getArchivaConfiguration().getConfiguration();
203 if ( configuration.getRepositoryScanning().getKnownContentConsumers().contains( knownContentConsumer ) )
205 log.warn( "skip adding knownContentConsumer {} as already here", knownContentConsumer );
208 configuration.getRepositoryScanning().addKnownContentConsumer( knownContentConsumer );
209 saveConfiguration( configuration );
210 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
214 public void removeKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
215 throws RepositoryAdminException
217 Configuration configuration = getArchivaConfiguration().getConfiguration();
218 configuration.getRepositoryScanning().removeKnownContentConsumer( knownContentConsumer );
219 saveConfiguration( configuration );
220 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
224 public void addInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
225 throws RepositoryAdminException
227 Configuration configuration = getArchivaConfiguration().getConfiguration();
228 if ( configuration.getRepositoryScanning().getInvalidContentConsumers().contains( invalidContentConsumer ) )
230 log.warn( "skip adding invalidContentConsumer {} as already here", invalidContentConsumer );
233 configuration.getRepositoryScanning().addInvalidContentConsumer( invalidContentConsumer );
234 saveConfiguration( configuration );
235 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
239 public void removeInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
240 throws RepositoryAdminException
242 Configuration configuration = getArchivaConfiguration().getConfiguration();
243 configuration.getRepositoryScanning().removeInvalidContentConsumer( invalidContentConsumer );
244 saveConfiguration( configuration );
245 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
249 public void setKnownContentConsumers( List<String> knownContentConsumers, AuditInformation auditInformation )
250 throws RepositoryAdminException
252 if ( knownContentConsumers == null )
256 for ( String knowContentConsumer : knownContentConsumers )
258 addKnownContentConsumer( knowContentConsumer, auditInformation );
263 public void setInvalidContentConsumers( List<String> invalidContentConsumers, AuditInformation auditInformation )
264 throws RepositoryAdminException
266 if ( invalidContentConsumers == null )
270 for ( String invalidContentConsumer : invalidContentConsumers )
272 addKnownContentConsumer( invalidContentConsumer, auditInformation );
277 public List<FileType> getFileTypes()
278 throws RepositoryAdminException
280 List<org.apache.archiva.configuration.FileType> configFileTypes =
281 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getFileTypes();
282 if ( configFileTypes == null || configFileTypes.isEmpty() )
284 return Collections.emptyList();
286 List<FileType> fileTypes = new ArrayList<>( configFileTypes.size() );
287 for ( org.apache.archiva.configuration.FileType fileType : configFileTypes )
289 fileTypes.add( getModelMapper().map( fileType, FileType.class ) );
295 public List<String> getKnownContentConsumers()
296 throws RepositoryAdminException
298 return new ArrayList<>(
299 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getKnownContentConsumers() );
303 public List<String> getInvalidContentConsumers()
304 throws RepositoryAdminException
306 return new ArrayList<>(
307 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getInvalidContentConsumers() );
311 public OrganisationInformation getOrganisationInformation()
312 throws RepositoryAdminException
314 org.apache.archiva.configuration.OrganisationInformation organisationInformation =
315 getArchivaConfiguration().getConfiguration().getOrganisationInfo();
316 if ( organisationInformation == null )
320 return getModelMapper().map( organisationInformation, OrganisationInformation.class );
324 public void setOrganisationInformation( OrganisationInformation organisationInformation )
325 throws RepositoryAdminException
327 Configuration configuration = getArchivaConfiguration().getConfiguration();
328 if ( organisationInformation != null )
330 org.apache.archiva.configuration.OrganisationInformation organisationInformationModel =
331 getModelMapper().map( organisationInformation,
332 org.apache.archiva.configuration.OrganisationInformation.class );
333 configuration.setOrganisationInfo( organisationInformationModel );
337 configuration.setOrganisationInfo( null );
339 saveConfiguration( configuration );
343 public UiConfiguration getUiConfiguration()
344 throws RepositoryAdminException
346 WebappConfiguration webappConfiguration = getArchivaConfiguration().getConfiguration().getWebapp();
347 if ( webappConfiguration == null )
351 UserInterfaceOptions userInterfaceOptions = webappConfiguration.getUi();
352 if ( userInterfaceOptions == null )
356 return getModelMapper().map( userInterfaceOptions, UiConfiguration.class );
360 public void updateUiConfiguration( UiConfiguration uiConfiguration )
361 throws RepositoryAdminException
363 Configuration configuration = getArchivaConfiguration().getConfiguration();
364 if ( uiConfiguration != null )
367 UserInterfaceOptions userInterfaceOptions =
368 getModelMapper().map( uiConfiguration, UserInterfaceOptions.class );
369 configuration.getWebapp().setUi( userInterfaceOptions );
373 configuration.getWebapp().setUi( null );
375 saveConfiguration( configuration );
380 public NetworkConfiguration getNetworkConfiguration()
381 throws RepositoryAdminException
383 org.apache.archiva.configuration.NetworkConfiguration networkConfiguration =
384 getArchivaConfiguration().getConfiguration().getNetworkConfiguration();
386 if ( networkConfiguration == null )
390 return getModelMapper().map( networkConfiguration, NetworkConfiguration.class );
394 public void setNetworkConfiguration( NetworkConfiguration networkConfiguration )
395 throws RepositoryAdminException
397 Configuration configuration = getArchivaConfiguration().getConfiguration();
398 if ( networkConfiguration == null )
400 configuration.setNetworkConfiguration( null );
404 configuration.setNetworkConfiguration( getModelMapper().map( networkConfiguration,
405 org.apache.archiva.configuration.NetworkConfiguration.class ) );
407 setupWagon( networkConfiguration );
408 saveConfiguration( configuration );
411 protected void setupWagon( NetworkConfiguration networkConfiguration )
413 if ( networkConfiguration == null )
415 // back to default values
416 HttpWagon.setUseClientManagerPooled( true );
417 poolingClientConnectionManager = new PoolingClientConnectionManager();
418 poolingClientConnectionManager.setDefaultMaxPerRoute( 30 );
419 poolingClientConnectionManager.setMaxTotal( 30 );
420 HttpWagon.setConnectionManagerPooled( poolingClientConnectionManager );
425 HttpWagon.setUseClientManagerPooled( networkConfiguration.isUsePooling() );
426 poolingClientConnectionManager = new PoolingClientConnectionManager();
427 poolingClientConnectionManager.setDefaultMaxPerRoute( networkConfiguration.getMaxTotalPerHost() );
428 poolingClientConnectionManager.setMaxTotal( networkConfiguration.getMaxTotal() );
429 HttpWagon.setConnectionManagerPooled( poolingClientConnectionManager );
433 //-------------------------
435 //-------------------------
437 private org.apache.archiva.configuration.FileType getFileTypeById( String id, Configuration configuration )
439 for ( org.apache.archiva.configuration.FileType fileType : configuration.getRepositoryScanning().getFileTypes() )
441 if ( StringUtils.equals( id, fileType.getId() ) )