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.codec.net.URLCodec;
35 import org.apache.commons.lang.StringEscapeUtils;
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.http.impl.conn.PoolingClientConnectionManager;
38 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
39 import org.apache.maven.wagon.providers.http.HttpWagon;
40 import org.springframework.stereotype.Service;
41 import org.springframework.util.ResourceUtils;
43 import javax.annotation.PostConstruct;
44 import javax.annotation.PreDestroy;
45 import java.io.UnsupportedEncodingException;
46 import java.net.MalformedURLException;
48 import java.net.URLEncoder;
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.List;
54 * @author Olivier Lamy
56 @Service( "archivaAdministration#default" )
57 public class DefaultArchivaAdministration
58 extends AbstractRepositoryAdmin
59 implements ArchivaAdministration
62 private PoolingHttpClientConnectionManager poolingClientConnectionManager;
65 public void initialize()
66 throws RepositoryAdminException
68 // setup wagon on start with initial values
69 NetworkConfiguration networkConfiguration = getNetworkConfiguration();
70 setupWagon( networkConfiguration );
74 public void shutdown()
76 if ( this.poolingClientConnectionManager != null )
78 this.poolingClientConnectionManager.shutdown();
84 public List<LegacyArtifactPath> getLegacyArtifactPaths()
85 throws RepositoryAdminException
87 List<LegacyArtifactPath> legacyArtifactPaths = new ArrayList<>(
88 getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths().size() );
89 for ( org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath : getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths() )
91 legacyArtifactPaths.add(
92 getModelMapper().map( legacyArtifactPath, LegacyArtifactPath.class ) );
94 return legacyArtifactPaths;
98 public void addLegacyArtifactPath( LegacyArtifactPath legacyArtifactPath, AuditInformation auditInformation )
99 throws RepositoryAdminException
101 Configuration configuration = getArchivaConfiguration().getConfiguration();
103 configuration.addLegacyArtifactPath( getModelMapper().map( legacyArtifactPath,
104 org.apache.archiva.configuration.LegacyArtifactPath.class ) );
106 saveConfiguration( configuration );
107 triggerAuditEvent( "", "", AuditEvent.ADD_LEGACY_PATH, auditInformation );
111 public void deleteLegacyArtifactPath( String path, AuditInformation auditInformation )
112 throws RepositoryAdminException
114 Configuration configuration = getArchivaConfiguration().getConfiguration();
115 org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath =
116 new org.apache.archiva.configuration.LegacyArtifactPath();
118 legacyArtifactPath.setPath( path );
119 configuration.removeLegacyArtifactPath( legacyArtifactPath );
121 saveConfiguration( configuration );
122 triggerAuditEvent( "", "", AuditEvent.REMOVE_LEGACY_PATH, auditInformation );
127 public void addFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
128 throws RepositoryAdminException
130 Configuration configuration = getArchivaConfiguration().getConfiguration();
132 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
133 if ( fileType == null )
138 if ( fileType.getPatterns().contains( pattern ) )
140 throw new RepositoryAdminException(
141 "File type [" + fileTypeId + "] already contains pattern [" + pattern + "]" );
143 fileType.addPattern( pattern );
145 saveConfiguration( configuration );
146 triggerAuditEvent( "", "", AuditEvent.ADD_PATTERN, auditInformation );
150 public void removeFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
151 throws RepositoryAdminException
153 Configuration configuration = getArchivaConfiguration().getConfiguration();
155 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
156 if ( fileType == null )
160 fileType.removePattern( pattern );
162 saveConfiguration( configuration );
163 triggerAuditEvent( "", "", AuditEvent.REMOVE_PATTERN, auditInformation );
167 public FileType getFileType( String fileTypeId )
168 throws RepositoryAdminException
170 org.apache.archiva.configuration.FileType fileType =
171 getFileTypeById( fileTypeId, getArchivaConfiguration().getConfiguration() );
172 if ( fileType == null )
176 return getModelMapper().map( fileType, FileType.class );
180 public void addFileType( FileType fileType, AuditInformation auditInformation )
181 throws RepositoryAdminException
183 Configuration configuration = getArchivaConfiguration().getConfiguration();
184 if ( getFileTypeById( fileType.getId(), configuration ) != null )
186 throw new RepositoryAdminException(
187 "impossible to FileType with id " + fileType.getId() + " already exists" );
190 configuration.getRepositoryScanning().addFileType(
191 getModelMapper().map( fileType, org.apache.archiva.configuration.FileType.class ) );
192 saveConfiguration( configuration );
196 public void removeFileType( String fileTypeId, AuditInformation auditInformation )
197 throws RepositoryAdminException
199 Configuration configuration = getArchivaConfiguration().getConfiguration();
200 org.apache.archiva.configuration.FileType fileType = new org.apache.archiva.configuration.FileType();
201 fileType.setId( fileTypeId );
202 configuration.getRepositoryScanning().removeFileType( fileType );
203 saveConfiguration( configuration );
207 public void addKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
208 throws RepositoryAdminException
210 Configuration configuration = getArchivaConfiguration().getConfiguration();
211 if ( configuration.getRepositoryScanning().getKnownContentConsumers().contains( knownContentConsumer ) )
213 log.warn( "skip adding knownContentConsumer {} as already here", knownContentConsumer );
216 configuration.getRepositoryScanning().addKnownContentConsumer( knownContentConsumer );
217 saveConfiguration( configuration );
218 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
222 public void removeKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
223 throws RepositoryAdminException
225 Configuration configuration = getArchivaConfiguration().getConfiguration();
226 configuration.getRepositoryScanning().removeKnownContentConsumer( knownContentConsumer );
227 saveConfiguration( configuration );
228 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
232 public void addInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
233 throws RepositoryAdminException
235 Configuration configuration = getArchivaConfiguration().getConfiguration();
236 if ( configuration.getRepositoryScanning().getInvalidContentConsumers().contains( invalidContentConsumer ) )
238 log.warn( "skip adding invalidContentConsumer {} as already here", invalidContentConsumer );
241 configuration.getRepositoryScanning().addInvalidContentConsumer( invalidContentConsumer );
242 saveConfiguration( configuration );
243 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
247 public void removeInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
248 throws RepositoryAdminException
250 Configuration configuration = getArchivaConfiguration().getConfiguration();
251 configuration.getRepositoryScanning().removeInvalidContentConsumer( invalidContentConsumer );
252 saveConfiguration( configuration );
253 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
257 public void setKnownContentConsumers( List<String> knownContentConsumers, AuditInformation auditInformation )
258 throws RepositoryAdminException
260 if ( knownContentConsumers == null )
264 for ( String knowContentConsumer : knownContentConsumers )
266 addKnownContentConsumer( knowContentConsumer, auditInformation );
271 public void setInvalidContentConsumers( List<String> invalidContentConsumers, AuditInformation auditInformation )
272 throws RepositoryAdminException
274 if ( invalidContentConsumers == null )
278 for ( String invalidContentConsumer : invalidContentConsumers )
280 addKnownContentConsumer( invalidContentConsumer, auditInformation );
285 public List<FileType> getFileTypes()
286 throws RepositoryAdminException
288 List<org.apache.archiva.configuration.FileType> configFileTypes =
289 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getFileTypes();
290 if ( configFileTypes == null || configFileTypes.isEmpty() )
292 return Collections.emptyList();
294 List<FileType> fileTypes = new ArrayList<>( configFileTypes.size() );
295 for ( org.apache.archiva.configuration.FileType fileType : configFileTypes )
297 fileTypes.add( getModelMapper().map( fileType, FileType.class ) );
303 public List<String> getKnownContentConsumers()
304 throws RepositoryAdminException
306 return new ArrayList<>(
307 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getKnownContentConsumers() );
311 public List<String> getInvalidContentConsumers()
312 throws RepositoryAdminException
314 return new ArrayList<>(
315 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getInvalidContentConsumers() );
319 public OrganisationInformation getOrganisationInformation()
320 throws RepositoryAdminException
322 org.apache.archiva.configuration.OrganisationInformation organisationInformation =
323 getArchivaConfiguration().getConfiguration().getOrganisationInfo();
324 if ( organisationInformation == null )
328 return getModelMapper().map( organisationInformation, OrganisationInformation.class );
331 private void checkUrl(String url, String propertyName) throws RepositoryAdminException {
332 if ( StringUtils.isNotEmpty( url ) )
334 if ( !ResourceUtils.isUrl( url ) )
336 throw new RepositoryAdminException( "Bad URL in " + propertyName + ": " + url );
342 private String convertName(String name) {
343 return StringEscapeUtils.escapeHtml( StringUtils.trimToEmpty( name ) );
347 public void setOrganisationInformation( OrganisationInformation organisationInformation )
348 throws RepositoryAdminException
350 checkUrl(organisationInformation.getUrl(), "url");
351 checkUrl( organisationInformation.getLogoLocation(), "logoLocation" );
352 Configuration configuration = getArchivaConfiguration( ).getConfiguration( );
353 if ( organisationInformation != null )
355 organisationInformation.setName( convertName( organisationInformation.getName() ));
356 org.apache.archiva.configuration.OrganisationInformation organisationInformationModel =
357 getModelMapper( ).map( organisationInformation,
358 org.apache.archiva.configuration.OrganisationInformation.class );
359 configuration.setOrganisationInfo( organisationInformationModel );
363 configuration.setOrganisationInfo( null );
365 saveConfiguration( configuration );
369 public UiConfiguration getUiConfiguration()
370 throws RepositoryAdminException
372 WebappConfiguration webappConfiguration = getArchivaConfiguration().getConfiguration().getWebapp();
373 if ( webappConfiguration == null )
377 UserInterfaceOptions userInterfaceOptions = webappConfiguration.getUi();
378 if ( userInterfaceOptions == null )
382 return getModelMapper().map( userInterfaceOptions, UiConfiguration.class );
386 public void updateUiConfiguration( UiConfiguration uiConfiguration )
387 throws RepositoryAdminException
389 Configuration configuration = getArchivaConfiguration().getConfiguration();
390 if ( uiConfiguration != null )
393 UserInterfaceOptions userInterfaceOptions =
394 getModelMapper().map( uiConfiguration, UserInterfaceOptions.class );
395 configuration.getWebapp().setUi( userInterfaceOptions );
399 configuration.getWebapp().setUi( null );
401 saveConfiguration( configuration );
406 public NetworkConfiguration getNetworkConfiguration()
407 throws RepositoryAdminException
409 org.apache.archiva.configuration.NetworkConfiguration networkConfiguration =
410 getArchivaConfiguration().getConfiguration().getNetworkConfiguration();
412 if ( networkConfiguration == null )
416 return getModelMapper().map( networkConfiguration, NetworkConfiguration.class );
420 public void setNetworkConfiguration( NetworkConfiguration networkConfiguration )
421 throws RepositoryAdminException
423 Configuration configuration = getArchivaConfiguration().getConfiguration();
424 if ( networkConfiguration == null )
426 configuration.setNetworkConfiguration( null );
430 configuration.setNetworkConfiguration( getModelMapper().map( networkConfiguration,
431 org.apache.archiva.configuration.NetworkConfiguration.class ) );
433 setupWagon( networkConfiguration );
434 saveConfiguration( configuration );
437 protected void setupWagon( NetworkConfiguration networkConfiguration )
439 if ( networkConfiguration == null )
441 // back to default values
442 HttpWagon.setPersistentPool( true );
443 poolingClientConnectionManager = new PoolingHttpClientConnectionManager();
444 poolingClientConnectionManager.setDefaultMaxPerRoute( 30 );
445 poolingClientConnectionManager.setMaxTotal( 30 );
446 HttpWagon.setPoolingHttpClientConnectionManager( poolingClientConnectionManager );
451 HttpWagon.setPersistentPool( networkConfiguration.isUsePooling() );
452 poolingClientConnectionManager = new PoolingHttpClientConnectionManager();
453 poolingClientConnectionManager.setDefaultMaxPerRoute( networkConfiguration.getMaxTotalPerHost() );
454 poolingClientConnectionManager.setMaxTotal( networkConfiguration.getMaxTotal() );
455 HttpWagon.setPoolingHttpClientConnectionManager( poolingClientConnectionManager );
459 //-------------------------
461 //-------------------------
463 private org.apache.archiva.configuration.FileType getFileTypeById( String id, Configuration configuration )
465 for ( org.apache.archiva.configuration.FileType fileType : configuration.getRepositoryScanning().getFileTypes() )
467 if ( StringUtils.equals( id, fileType.getId() ) )