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;
39 import org.springframework.util.ResourceUtils;
41 import javax.annotation.PostConstruct;
42 import javax.annotation.PreDestroy;
43 import java.io.UnsupportedEncodingException;
44 import java.net.MalformedURLException;
46 import java.net.URLEncoder;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.List;
52 * @author Olivier Lamy
54 @Service( "archivaAdministration#default" )
55 public class DefaultArchivaAdministration
56 extends AbstractRepositoryAdmin
57 implements ArchivaAdministration
60 private PoolingHttpClientConnectionManager poolingClientConnectionManager;
63 public void initialize()
64 throws RepositoryAdminException
66 // setup wagon on start with initial values
67 NetworkConfiguration networkConfiguration = getNetworkConfiguration();
68 setupWagon( networkConfiguration );
72 public void shutdown()
74 if ( this.poolingClientConnectionManager != null )
76 this.poolingClientConnectionManager.shutdown();
82 public List<LegacyArtifactPath> getLegacyArtifactPaths()
83 throws RepositoryAdminException
85 List<LegacyArtifactPath> legacyArtifactPaths = new ArrayList<>(
86 getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths().size() );
87 for ( org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath : getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths() )
89 legacyArtifactPaths.add(
90 getModelMapper().map( legacyArtifactPath, LegacyArtifactPath.class ) );
92 return legacyArtifactPaths;
96 public void addLegacyArtifactPath( LegacyArtifactPath legacyArtifactPath, AuditInformation auditInformation )
97 throws RepositoryAdminException
99 Configuration configuration = getArchivaConfiguration().getConfiguration();
101 configuration.addLegacyArtifactPath( getModelMapper().map( legacyArtifactPath,
102 org.apache.archiva.configuration.LegacyArtifactPath.class ) );
104 saveConfiguration( configuration );
105 triggerAuditEvent( "", "", AuditEvent.ADD_LEGACY_PATH, auditInformation );
109 public void deleteLegacyArtifactPath( String path, AuditInformation auditInformation )
110 throws RepositoryAdminException
112 Configuration configuration = getArchivaConfiguration().getConfiguration();
113 org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath =
114 new org.apache.archiva.configuration.LegacyArtifactPath();
116 legacyArtifactPath.setPath( path );
117 configuration.removeLegacyArtifactPath( legacyArtifactPath );
119 saveConfiguration( configuration );
120 triggerAuditEvent( "", "", AuditEvent.REMOVE_LEGACY_PATH, auditInformation );
125 public void addFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
126 throws RepositoryAdminException
128 Configuration configuration = getArchivaConfiguration().getConfiguration();
130 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
131 if ( fileType == null )
136 if ( fileType.getPatterns().contains( pattern ) )
138 throw new RepositoryAdminException(
139 "File type [" + fileTypeId + "] already contains pattern [" + pattern + "]" );
141 fileType.addPattern( pattern );
143 saveConfiguration( configuration );
144 triggerAuditEvent( "", "", AuditEvent.ADD_PATTERN, auditInformation );
148 public void removeFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
149 throws RepositoryAdminException
151 Configuration configuration = getArchivaConfiguration().getConfiguration();
153 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
154 if ( fileType == null )
158 fileType.removePattern( pattern );
160 saveConfiguration( configuration );
161 triggerAuditEvent( "", "", AuditEvent.REMOVE_PATTERN, auditInformation );
165 public FileType getFileType( String fileTypeId )
166 throws RepositoryAdminException
168 org.apache.archiva.configuration.FileType fileType =
169 getFileTypeById( fileTypeId, getArchivaConfiguration().getConfiguration() );
170 if ( fileType == null )
174 return getModelMapper().map( fileType, FileType.class );
178 public void addFileType( FileType fileType, AuditInformation auditInformation )
179 throws RepositoryAdminException
181 Configuration configuration = getArchivaConfiguration().getConfiguration();
182 if ( getFileTypeById( fileType.getId(), configuration ) != null )
184 throw new RepositoryAdminException(
185 "impossible to FileType with id " + fileType.getId() + " already exists" );
188 configuration.getRepositoryScanning().addFileType(
189 getModelMapper().map( fileType, org.apache.archiva.configuration.FileType.class ) );
190 saveConfiguration( configuration );
194 public void removeFileType( String fileTypeId, AuditInformation auditInformation )
195 throws RepositoryAdminException
197 Configuration configuration = getArchivaConfiguration().getConfiguration();
198 org.apache.archiva.configuration.FileType fileType = new org.apache.archiva.configuration.FileType();
199 fileType.setId( fileTypeId );
200 configuration.getRepositoryScanning().removeFileType( fileType );
201 saveConfiguration( configuration );
205 public void addKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
206 throws RepositoryAdminException
208 Configuration configuration = getArchivaConfiguration().getConfiguration();
209 if ( configuration.getRepositoryScanning().getKnownContentConsumers().contains( knownContentConsumer ) )
211 log.warn( "skip adding knownContentConsumer {} as already here", knownContentConsumer );
214 configuration.getRepositoryScanning().addKnownContentConsumer( knownContentConsumer );
215 saveConfiguration( configuration );
216 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
220 public void removeKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
221 throws RepositoryAdminException
223 Configuration configuration = getArchivaConfiguration().getConfiguration();
224 configuration.getRepositoryScanning().removeKnownContentConsumer( knownContentConsumer );
225 saveConfiguration( configuration );
226 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
230 public void addInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
231 throws RepositoryAdminException
233 Configuration configuration = getArchivaConfiguration().getConfiguration();
234 if ( configuration.getRepositoryScanning().getInvalidContentConsumers().contains( invalidContentConsumer ) )
236 log.warn( "skip adding invalidContentConsumer {} as already here", invalidContentConsumer );
239 configuration.getRepositoryScanning().addInvalidContentConsumer( invalidContentConsumer );
240 saveConfiguration( configuration );
241 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
245 public void removeInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
246 throws RepositoryAdminException
248 Configuration configuration = getArchivaConfiguration().getConfiguration();
249 configuration.getRepositoryScanning().removeInvalidContentConsumer( invalidContentConsumer );
250 saveConfiguration( configuration );
251 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
255 public void setKnownContentConsumers( List<String> knownContentConsumers, AuditInformation auditInformation )
256 throws RepositoryAdminException
258 if ( knownContentConsumers == null )
262 for ( String knowContentConsumer : knownContentConsumers )
264 addKnownContentConsumer( knowContentConsumer, auditInformation );
269 public void setInvalidContentConsumers( List<String> invalidContentConsumers, AuditInformation auditInformation )
270 throws RepositoryAdminException
272 if ( invalidContentConsumers == null )
276 for ( String invalidContentConsumer : invalidContentConsumers )
278 addKnownContentConsumer( invalidContentConsumer, auditInformation );
283 public List<FileType> getFileTypes()
284 throws RepositoryAdminException
286 List<org.apache.archiva.configuration.FileType> configFileTypes =
287 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getFileTypes();
288 if ( configFileTypes == null || configFileTypes.isEmpty() )
290 return Collections.emptyList();
292 List<FileType> fileTypes = new ArrayList<>( configFileTypes.size() );
293 for ( org.apache.archiva.configuration.FileType fileType : configFileTypes )
295 fileTypes.add( getModelMapper().map( fileType, FileType.class ) );
301 public List<String> getKnownContentConsumers()
302 throws RepositoryAdminException
304 return new ArrayList<>(
305 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getKnownContentConsumers() );
309 public List<String> getInvalidContentConsumers()
310 throws RepositoryAdminException
312 return new ArrayList<>(
313 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getInvalidContentConsumers() );
317 public OrganisationInformation getOrganisationInformation()
318 throws RepositoryAdminException
320 org.apache.archiva.configuration.OrganisationInformation organisationInformation =
321 getArchivaConfiguration().getConfiguration().getOrganisationInfo();
322 if ( organisationInformation == null )
326 return getModelMapper().map( organisationInformation, OrganisationInformation.class );
329 private void checkUrl(String url, String propertyName) throws RepositoryAdminException {
330 if ( StringUtils.isNotEmpty( url ) )
332 if ( !ResourceUtils.isUrl( url ) )
334 throw new RepositoryAdminException( "Bad URL in " + propertyName + ": " + url );
341 public void setOrganisationInformation( OrganisationInformation organisationInformation )
342 throws RepositoryAdminException
344 checkUrl(organisationInformation.getUrl(), "url");
345 checkUrl( organisationInformation.getLogoLocation(), "logoLocation" );
346 Configuration configuration = getArchivaConfiguration( ).getConfiguration( );
347 if ( organisationInformation != null )
349 org.apache.archiva.configuration.OrganisationInformation organisationInformationModel =
350 getModelMapper( ).map( organisationInformation,
351 org.apache.archiva.configuration.OrganisationInformation.class );
352 configuration.setOrganisationInfo( organisationInformationModel );
356 configuration.setOrganisationInfo( null );
358 saveConfiguration( configuration );
362 public UiConfiguration getUiConfiguration()
363 throws RepositoryAdminException
365 WebappConfiguration webappConfiguration = getArchivaConfiguration().getConfiguration().getWebapp();
366 if ( webappConfiguration == null )
370 UserInterfaceOptions userInterfaceOptions = webappConfiguration.getUi();
371 if ( userInterfaceOptions == null )
375 return getModelMapper().map( userInterfaceOptions, UiConfiguration.class );
379 public void updateUiConfiguration( UiConfiguration uiConfiguration )
380 throws RepositoryAdminException
382 Configuration configuration = getArchivaConfiguration().getConfiguration();
383 if ( uiConfiguration != null )
386 UserInterfaceOptions userInterfaceOptions =
387 getModelMapper().map( uiConfiguration, UserInterfaceOptions.class );
388 configuration.getWebapp().setUi( userInterfaceOptions );
392 configuration.getWebapp().setUi( null );
394 saveConfiguration( configuration );
399 public NetworkConfiguration getNetworkConfiguration()
400 throws RepositoryAdminException
402 org.apache.archiva.configuration.NetworkConfiguration networkConfiguration =
403 getArchivaConfiguration().getConfiguration().getNetworkConfiguration();
405 if ( networkConfiguration == null )
409 return getModelMapper().map( networkConfiguration, NetworkConfiguration.class );
413 public void setNetworkConfiguration( NetworkConfiguration networkConfiguration )
414 throws RepositoryAdminException
416 Configuration configuration = getArchivaConfiguration().getConfiguration();
417 if ( networkConfiguration == null )
419 configuration.setNetworkConfiguration( null );
423 configuration.setNetworkConfiguration( getModelMapper().map( networkConfiguration,
424 org.apache.archiva.configuration.NetworkConfiguration.class ) );
426 setupWagon( networkConfiguration );
427 saveConfiguration( configuration );
430 protected void setupWagon( NetworkConfiguration networkConfiguration )
432 if ( networkConfiguration == null )
434 // back to default values
435 HttpWagon.setPersistentPool( true );
436 poolingClientConnectionManager = new PoolingHttpClientConnectionManager();
437 poolingClientConnectionManager.setDefaultMaxPerRoute( 30 );
438 poolingClientConnectionManager.setMaxTotal( 30 );
439 HttpWagon.setPoolingHttpClientConnectionManager( poolingClientConnectionManager );
444 HttpWagon.setPersistentPool( networkConfiguration.isUsePooling() );
445 poolingClientConnectionManager = new PoolingHttpClientConnectionManager();
446 poolingClientConnectionManager.setDefaultMaxPerRoute( networkConfiguration.getMaxTotalPerHost() );
447 poolingClientConnectionManager.setMaxTotal( networkConfiguration.getMaxTotal() );
448 HttpWagon.setPoolingHttpClientConnectionManager( poolingClientConnectionManager );
452 //-------------------------
454 //-------------------------
456 private org.apache.archiva.configuration.FileType getFileTypeById( String id, Configuration configuration )
458 for ( org.apache.archiva.configuration.FileType fileType : configuration.getRepositoryScanning().getFileTypes() )
460 if ( StringUtils.equals( id, fileType.getId() ) )