You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractRepositoryAdmin.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package org.apache.archiva.admin.repository;
  2. /*
  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
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  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
  18. * under the License.
  19. */
  20. import org.apache.archiva.admin.model.AuditInformation;
  21. import org.apache.archiva.admin.model.RepositoryAdminException;
  22. import org.apache.archiva.admin.model.RepositoryCommonValidator;
  23. import org.apache.archiva.admin.model.beans.AbstractRepository;
  24. import org.apache.archiva.configuration.AbstractRepositoryConfiguration;
  25. import org.apache.archiva.configuration.ArchivaConfiguration;
  26. import org.apache.archiva.configuration.Configuration;
  27. import org.apache.archiva.configuration.IndeterminateConfigurationException;
  28. import org.apache.archiva.metadata.model.facets.AuditEvent;
  29. import org.apache.archiva.redback.components.registry.Registry;
  30. import org.apache.archiva.redback.users.User;
  31. import org.apache.archiva.repository.Repository;
  32. import org.apache.archiva.repository.events.AuditListener;
  33. import org.apache.archiva.repository.features.IndexCreationFeature;
  34. import org.apache.commons.lang.StringUtils;
  35. import org.modelmapper.ModelMapper;
  36. import org.modelmapper.convention.MatchingStrategies;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import javax.inject.Inject;
  41. import javax.inject.Named;
  42. import java.net.URI;
  43. import java.nio.file.Paths;
  44. import java.util.ArrayList;
  45. import java.util.List;
  46. /**
  47. * @author Olivier Lamy
  48. * @since 1.4-M1
  49. */
  50. public abstract class AbstractRepositoryAdmin
  51. {
  52. protected Logger log = LoggerFactory.getLogger( getClass() );
  53. @Inject
  54. @Autowired(required = false)
  55. private List<AuditListener> auditListeners = new ArrayList<>();
  56. @Inject
  57. private RepositoryCommonValidator repositoryCommonValidator;
  58. @Inject
  59. private ArchivaConfiguration archivaConfiguration;
  60. @Inject
  61. @Named(value = "commons-configuration")
  62. private Registry registry;
  63. protected void triggerAuditEvent( String repositoryId, String resource, String action,
  64. AuditInformation auditInformation )
  65. {
  66. User user = auditInformation == null ? null : auditInformation.getUser();
  67. AuditEvent event = new AuditEvent( repositoryId, user == null ? "null" : user.getUsername(), resource, action );
  68. event.setRemoteIP( auditInformation == null ? "null" : auditInformation.getRemoteAddr() );
  69. for ( AuditListener listener : getAuditListeners() )
  70. {
  71. listener.auditEvent( event );
  72. }
  73. }
  74. protected void saveConfiguration( Configuration config )
  75. throws RepositoryAdminException
  76. {
  77. try
  78. {
  79. getArchivaConfiguration().save( config );
  80. }
  81. catch ( org.apache.archiva.redback.components.registry.RegistryException e )
  82. {
  83. throw new RepositoryAdminException( "Error occurred in the registry: " + e.getLocalizedMessage(), e );
  84. }
  85. catch ( IndeterminateConfigurationException e )
  86. {
  87. throw new RepositoryAdminException(
  88. "Error occurred while saving the configuration: " + e.getLocalizedMessage(), e );
  89. }
  90. }
  91. protected String convertUriToString( URI uri ) {
  92. if (uri==null) {
  93. return "";
  94. }
  95. String result;
  96. if (uri.getScheme()==null) {
  97. result = uri.getPath();
  98. } else if ("file".equals(uri.getScheme())) {
  99. result = Paths.get(uri).normalize().toString();
  100. } else {
  101. result = uri.toString();
  102. }
  103. log.debug("Converted uri {} -> {}", uri, result);
  104. return result;
  105. }
  106. protected void setBaseRepoAttributes( AbstractRepository adminRepo, Repository repo){
  107. adminRepo.setId(repo.getId());
  108. adminRepo.setName( repo.getName() );
  109. adminRepo.setLayout( repo.getLayout( ) );
  110. adminRepo.setDescription( repo.getDescription() );
  111. adminRepo.setType(repo.getType()==null?"MAVEN": repo.getType().name());
  112. if (repo.supportsFeature( IndexCreationFeature.class )) {
  113. IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
  114. adminRepo.setIndexDirectory( convertUriToString( icf.getIndexPath() ) );
  115. adminRepo.setPackedIndexDirectory(convertUriToString(icf.getPackedIndexPath()));
  116. }
  117. }
  118. protected void setBaseRepoAttributes( AbstractRepositoryConfiguration repoConfig, AbstractRepository repo) {
  119. repoConfig.setId( repo.getId() );
  120. repoConfig.setName( repo.getName() );
  121. repoConfig.setLayout( repo.getLayout() );
  122. repoConfig.setDescription( repo.getDescription() );
  123. repoConfig.setIndexDir( repo.getIndexDirectory() );
  124. repoConfig.setPackedIndexDir(repo.getPackedIndexDirectory());
  125. repoConfig.setType( StringUtils.isEmpty( repo.getType() ) ? "MAVEN" : repo.getType() );
  126. }
  127. private static class ModelMapperHolder
  128. {
  129. private static ModelMapper MODEL_MAPPER = new ModelMapper();
  130. static
  131. {
  132. MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
  133. }
  134. }
  135. protected ModelMapper getModelMapper()
  136. {
  137. return ModelMapperHolder.MODEL_MAPPER;
  138. }
  139. public List<AuditListener> getAuditListeners()
  140. {
  141. return auditListeners;
  142. }
  143. public void setAuditListeners( List<AuditListener> auditListeners )
  144. {
  145. this.auditListeners = auditListeners;
  146. }
  147. public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
  148. {
  149. this.archivaConfiguration = archivaConfiguration;
  150. }
  151. public ArchivaConfiguration getArchivaConfiguration()
  152. {
  153. return archivaConfiguration;
  154. }
  155. public RepositoryCommonValidator getRepositoryCommonValidator()
  156. {
  157. return repositoryCommonValidator;
  158. }
  159. public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator )
  160. {
  161. this.repositoryCommonValidator = repositoryCommonValidator;
  162. }
  163. public Registry getRegistry()
  164. {
  165. return registry;
  166. }
  167. public void setRegistry( org.apache.archiva.redback.components.registry.Registry registry )
  168. {
  169. this.registry = registry;
  170. }
  171. }