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.*;
25 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
26 import org.apache.archiva.configuration.Configuration;
27 import org.apache.archiva.configuration.UserInterfaceOptions;
28 import org.apache.archiva.configuration.WebappConfiguration;
29 import org.apache.archiva.metadata.model.facets.AuditEvent;
30 import org.apache.commons.lang3.StringEscapeUtils;
31 import org.apache.commons.lang3.StringUtils;
32 import org.springframework.stereotype.Service;
33 import org.springframework.util.ResourceUtils;
36 import java.net.URISyntaxException;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
42 * @author Olivier Lamy
44 @Service( "archivaAdministration#default" )
45 public class DefaultArchivaAdministration
46 extends AbstractRepositoryAdmin
47 implements ArchivaAdministration
52 public List<LegacyArtifactPath> getLegacyArtifactPaths()
53 throws RepositoryAdminException
55 List<LegacyArtifactPath> legacyArtifactPaths = new ArrayList<>(
56 getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths().size() );
57 for ( org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath : getArchivaConfiguration().getConfiguration().getLegacyArtifactPaths() )
59 legacyArtifactPaths.add(
60 getModelMapper().map( legacyArtifactPath, LegacyArtifactPath.class ) );
62 return legacyArtifactPaths;
66 public void addLegacyArtifactPath( LegacyArtifactPath legacyArtifactPath, AuditInformation auditInformation )
67 throws RepositoryAdminException
69 Configuration configuration = getArchivaConfiguration().getConfiguration();
71 configuration.addLegacyArtifactPath( getModelMapper().map( legacyArtifactPath,
72 org.apache.archiva.configuration.LegacyArtifactPath.class ) );
74 saveConfiguration( configuration );
75 triggerAuditEvent( "", "", AuditEvent.ADD_LEGACY_PATH, auditInformation );
79 public void deleteLegacyArtifactPath( String path, AuditInformation auditInformation )
80 throws RepositoryAdminException
82 Configuration configuration = getArchivaConfiguration().getConfiguration();
83 org.apache.archiva.configuration.LegacyArtifactPath legacyArtifactPath =
84 new org.apache.archiva.configuration.LegacyArtifactPath();
86 legacyArtifactPath.setPath( path );
87 configuration.removeLegacyArtifactPath( legacyArtifactPath );
89 saveConfiguration( configuration );
90 triggerAuditEvent( "", "", AuditEvent.REMOVE_LEGACY_PATH, auditInformation );
95 public void addFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
96 throws RepositoryAdminException
98 Configuration configuration = getArchivaConfiguration().getConfiguration();
100 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
101 if ( fileType == null )
106 if ( fileType.getPatterns().contains( pattern ) )
108 throw new RepositoryAdminException(
109 "File type [" + fileTypeId + "] already contains pattern [" + pattern + "]" );
111 fileType.addPattern( pattern );
113 saveConfiguration( configuration );
114 triggerAuditEvent( "", "", AuditEvent.ADD_PATTERN, auditInformation );
118 public void removeFileTypePattern( String fileTypeId, String pattern, AuditInformation auditInformation )
119 throws RepositoryAdminException
121 Configuration configuration = getArchivaConfiguration().getConfiguration();
123 org.apache.archiva.configuration.FileType fileType = getFileTypeById( fileTypeId, configuration );
124 if ( fileType == null )
128 fileType.removePattern( pattern );
130 saveConfiguration( configuration );
131 triggerAuditEvent( "", "", AuditEvent.REMOVE_PATTERN, auditInformation );
135 public FileType getFileType( String fileTypeId )
136 throws RepositoryAdminException
138 org.apache.archiva.configuration.FileType fileType =
139 getFileTypeById( fileTypeId, getArchivaConfiguration().getConfiguration() );
140 if ( fileType == null )
144 return getModelMapper().map( fileType, FileType.class );
148 public void addFileType( FileType fileType, AuditInformation auditInformation )
149 throws RepositoryAdminException
151 Configuration configuration = getArchivaConfiguration().getConfiguration();
152 if ( getFileTypeById( fileType.getId(), configuration ) != null )
154 throw new RepositoryAdminException(
155 "impossible to FileType with id " + fileType.getId() + " already exists" );
158 configuration.getRepositoryScanning().addFileType(
159 getModelMapper().map( fileType, org.apache.archiva.configuration.FileType.class ) );
160 saveConfiguration( configuration );
164 public void removeFileType( String fileTypeId, AuditInformation auditInformation )
165 throws RepositoryAdminException
167 Configuration configuration = getArchivaConfiguration().getConfiguration();
168 org.apache.archiva.configuration.FileType fileType = new org.apache.archiva.configuration.FileType();
169 fileType.setId( fileTypeId );
170 configuration.getRepositoryScanning().removeFileType( fileType );
171 saveConfiguration( configuration );
175 public void addKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
176 throws RepositoryAdminException
178 Configuration configuration = getArchivaConfiguration().getConfiguration();
179 if ( configuration.getRepositoryScanning().getKnownContentConsumers().contains( knownContentConsumer ) )
181 log.warn( "skip adding knownContentConsumer {} as already here", knownContentConsumer );
184 configuration.getRepositoryScanning().addKnownContentConsumer( knownContentConsumer );
185 saveConfiguration( configuration );
186 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
190 public void removeKnownContentConsumer( String knownContentConsumer, AuditInformation auditInformation )
191 throws RepositoryAdminException
193 Configuration configuration = getArchivaConfiguration().getConfiguration();
194 configuration.getRepositoryScanning().removeKnownContentConsumer( knownContentConsumer );
195 saveConfiguration( configuration );
196 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
200 public void addInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
201 throws RepositoryAdminException
203 Configuration configuration = getArchivaConfiguration().getConfiguration();
204 if ( configuration.getRepositoryScanning().getInvalidContentConsumers().contains( invalidContentConsumer ) )
206 log.warn( "skip adding invalidContentConsumer {} as already here", invalidContentConsumer );
209 configuration.getRepositoryScanning().addInvalidContentConsumer( invalidContentConsumer );
210 saveConfiguration( configuration );
211 triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
215 public void removeInvalidContentConsumer( String invalidContentConsumer, AuditInformation auditInformation )
216 throws RepositoryAdminException
218 Configuration configuration = getArchivaConfiguration().getConfiguration();
219 configuration.getRepositoryScanning().removeInvalidContentConsumer( invalidContentConsumer );
220 saveConfiguration( configuration );
221 triggerAuditEvent( "", "", AuditEvent.DISABLE_REPO_CONSUMER, auditInformation );
225 public void setKnownContentConsumers( List<String> knownContentConsumers, AuditInformation auditInformation )
226 throws RepositoryAdminException
228 if ( knownContentConsumers == null )
232 for ( String knowContentConsumer : knownContentConsumers )
234 addKnownContentConsumer( knowContentConsumer, auditInformation );
239 public void setInvalidContentConsumers( List<String> invalidContentConsumers, AuditInformation auditInformation )
240 throws RepositoryAdminException
242 if ( invalidContentConsumers == null )
246 for ( String invalidContentConsumer : invalidContentConsumers )
248 addKnownContentConsumer( invalidContentConsumer, auditInformation );
253 public List<FileType> getFileTypes()
254 throws RepositoryAdminException
256 List<org.apache.archiva.configuration.FileType> configFileTypes =
257 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getFileTypes();
258 if ( configFileTypes == null || configFileTypes.isEmpty() )
260 return Collections.emptyList();
262 List<FileType> fileTypes = new ArrayList<>( configFileTypes.size() );
263 for ( org.apache.archiva.configuration.FileType fileType : configFileTypes )
265 fileTypes.add( getModelMapper().map( fileType, FileType.class ) );
271 public List<String> getKnownContentConsumers()
272 throws RepositoryAdminException
274 return new ArrayList<>(
275 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getKnownContentConsumers() );
279 public List<String> getInvalidContentConsumers()
280 throws RepositoryAdminException
282 return new ArrayList<>(
283 getArchivaConfiguration().getConfiguration().getRepositoryScanning().getInvalidContentConsumers() );
287 public OrganisationInformation getOrganisationInformation()
288 throws RepositoryAdminException
290 org.apache.archiva.configuration.OrganisationInformation organisationInformation =
291 getArchivaConfiguration().getConfiguration().getOrganisationInfo();
292 if ( organisationInformation == null )
296 return getModelMapper().map( organisationInformation, OrganisationInformation.class );
299 private String fixUrl(String url, String propertyName) throws RepositoryAdminException {
300 if ( StringUtils.isNotEmpty( url ) )
302 if ( !ResourceUtils.isUrl( url ) )
304 throw new RepositoryAdminException( "Bad URL in " + propertyName + ": " + url );
307 URI urlToCheck = new URI(url);
308 return urlToCheck.toString();
309 } catch (URISyntaxException e) {
310 throw new RepositoryAdminException( "Bad URL in " + propertyName + ": " + url );
317 private String convertName(String name) {
318 return StringEscapeUtils.escapeHtml4( StringUtils.trimToEmpty( name ) );
322 public void setOrganisationInformation( OrganisationInformation organisationInformation )
323 throws RepositoryAdminException
326 organisationInformation.setUrl(fixUrl(organisationInformation.getUrl(), "url"));
327 organisationInformation.setLogoLocation(fixUrl( organisationInformation.getLogoLocation(), "logoLocation" ));
328 Configuration configuration = getArchivaConfiguration( ).getConfiguration( );
329 if ( organisationInformation != null )
331 organisationInformation.setName( convertName( organisationInformation.getName() ));
332 org.apache.archiva.configuration.OrganisationInformation organisationInformationModel =
333 getModelMapper( ).map( organisationInformation,
334 org.apache.archiva.configuration.OrganisationInformation.class );
335 configuration.setOrganisationInfo( organisationInformationModel );
339 configuration.setOrganisationInfo( null );
341 saveConfiguration( configuration );
345 public UiConfiguration getUiConfiguration()
346 throws RepositoryAdminException
348 WebappConfiguration webappConfiguration = getArchivaConfiguration().getConfiguration().getWebapp();
349 if ( webappConfiguration == null )
353 UserInterfaceOptions userInterfaceOptions = webappConfiguration.getUi();
354 if ( userInterfaceOptions == null )
358 return getModelMapper().map( userInterfaceOptions, UiConfiguration.class );
362 public void updateUiConfiguration( UiConfiguration uiConfiguration )
363 throws RepositoryAdminException
365 Configuration configuration = getArchivaConfiguration().getConfiguration();
366 if ( uiConfiguration != null )
369 UserInterfaceOptions userInterfaceOptions =
370 getModelMapper().map( uiConfiguration, UserInterfaceOptions.class );
371 configuration.getWebapp().setUi( userInterfaceOptions );
375 configuration.getWebapp().setUi( null );
377 saveConfiguration( configuration );
382 public NetworkConfiguration getNetworkConfiguration()
383 throws RepositoryAdminException
385 org.apache.archiva.configuration.NetworkConfiguration networkConfiguration =
386 getArchivaConfiguration().getConfiguration().getNetworkConfiguration();
388 if ( networkConfiguration == null )
392 return getModelMapper().map( networkConfiguration, NetworkConfiguration.class );
396 public void setNetworkConfiguration( NetworkConfiguration networkConfiguration )
397 throws RepositoryAdminException
399 Configuration configuration = getArchivaConfiguration().getConfiguration();
400 if ( networkConfiguration == null )
402 configuration.setNetworkConfiguration( null );
406 configuration.setNetworkConfiguration( getModelMapper().map( networkConfiguration,
407 org.apache.archiva.configuration.NetworkConfiguration.class ) );
409 // setupWagon( networkConfiguration );
410 saveConfiguration( configuration );
414 //-------------------------
416 //-------------------------
418 private org.apache.archiva.configuration.FileType getFileTypeById( String id, Configuration configuration )
420 for ( org.apache.archiva.configuration.FileType fileType : configuration.getRepositoryScanning().getFileTypes() )
422 if ( StringUtils.equals( id, fileType.getId() ) )