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