This project has retired. For details please refer to its Attic page.
DefaultRemoteRepositoryAdmin xref
View Javadoc
1   package org.apache.archiva.admin.repository.remote;
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.remote.RemoteRepositoryAdmin;
24  import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
25  import org.apache.archiva.common.utils.PathUtil;
26  import org.apache.archiva.configuration.Configuration;
27  import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
28  import org.apache.archiva.configuration.RepositoryCheckPath;
29  import org.apache.archiva.indexer.UnsupportedBaseContextException;
30  import org.apache.archiva.metadata.model.facets.AuditEvent;
31  import org.apache.archiva.repository.base.PasswordCredentials;
32  import org.apache.archiva.repository.RemoteRepository;
33  import org.apache.archiva.repository.RepositoryCredentials;
34  import org.apache.archiva.repository.RepositoryException;
35  import org.apache.archiva.repository.RepositoryRegistry;
36  import org.apache.archiva.repository.features.IndexCreationFeature;
37  import org.apache.archiva.repository.features.RemoteIndexFeature;
38  import org.apache.commons.lang3.StringUtils;
39  import org.apache.maven.index.context.IndexingContext;
40  import org.springframework.stereotype.Service;
41  
42  import javax.annotation.PostConstruct;
43  import javax.inject.Inject;
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Map;
47  import java.util.stream.Collectors;
48  
49  /**
50   * @author Olivier Lamy
51   * @since 1.4-M1
52   */
53  @Service("remoteRepositoryAdmin#default")
54  public class DefaultRemoteRepositoryAdmin
55      extends AbstractRepositoryAdmin
56      implements RemoteRepositoryAdmin
57  {
58  
59      @Inject
60      RepositoryRegistry repositoryRegistry;
61  
62  
63      @PostConstruct
64      private void initialize()
65          throws RepositoryAdminException
66      {
67          for ( org.apache.archiva.admin.model.beans.RemoteRepository remoteRepository : getRemoteRepositories() )
68          {
69              createIndexContext( remoteRepository );
70          }
71      }
72  
73      /*
74   * Conversion between the repository from the registry and the serialized DTO for the admin API
75   */
76      private org.apache.archiva.admin.model.beans.RemoteRepository convertRepo( RemoteRepository repo ) {
77          if (repo==null) {
78              return null;
79          }
80          org.apache.archiva.admin.model.beans.RemoteRepository adminRepo = new org.apache.archiva.admin.model.beans.RemoteRepository( getArchivaConfiguration().getDefaultLocale() );
81          setBaseRepoAttributes( adminRepo, repo );
82          adminRepo.setUrl( convertUriToString( repo.getLocation() ));
83          adminRepo.setCronExpression( repo.getSchedulingDefinition() );
84          adminRepo.setCheckPath( repo.getCheckPath() );
85          adminRepo.setExtraHeaders( repo.getExtraHeaders() );
86          adminRepo.setExtraParameters( repo.getExtraParameters() );
87          adminRepo.setTimeout( (int) repo.getTimeout().getSeconds() );
88          RepositoryCredentials creds = repo.getLoginCredentials();
89          if (creds!=null && creds instanceof PasswordCredentials) {
90              PasswordCredentials./../org/apache/archiva/repository/base/PasswordCredentials.html#PasswordCredentials">PasswordCredentials pCreds = (PasswordCredentials) creds;
91              adminRepo.setUserName( pCreds.getUsername() );
92              adminRepo.setPassword( new String(pCreds.getPassword()!=null ? pCreds.getPassword() : new char[0]) );
93          }
94          if (repo.supportsFeature( RemoteIndexFeature.class )) {
95              RemoteIndexFeature rif = repo.getFeature( RemoteIndexFeature.class ).get();
96              adminRepo.setRemoteIndexUrl( convertUriToString( rif.getIndexUri() ) );
97              adminRepo.setDownloadRemoteIndex( rif.isDownloadRemoteIndex() );
98              adminRepo.setRemoteDownloadNetworkProxyId( rif.getProxyId() );
99              adminRepo.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup() );
100             adminRepo.setRemoteDownloadTimeout( (int) rif.getDownloadTimeout().getSeconds() );
101         }
102         if (repo.supportsFeature(IndexCreationFeature.class)) {
103             IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
104             adminRepo.setIndexDirectory(PathUtil.getPathFromUri(icf.getIndexPath()).toString());
105         }
106         adminRepo.setDescription(repo.getDescription());
107         return adminRepo;
108     }
109 
110     private RemoteRepositoryConfiguration getRepositoryConfiguration( org.apache.archiva.admin.model.beans.RemoteRepository repo) {
111         RemoteRepositoryConfigurationConfiguration.html#RemoteRepositoryConfiguration">RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
112         setBaseRepoAttributes( repoConfig, repo );
113         repoConfig.setUrl( getRepositoryCommonValidator().removeExpressions( repo.getUrl() ) );
114         repoConfig.setRefreshCronExpression( repo.getCronExpression() );
115         repoConfig.setCheckPath( repo.getCheckPath() );
116         repoConfig.setExtraHeaders( repo.getExtraHeaders() );
117         repoConfig.setExtraParameters( repo.getExtraParameters() );
118         repoConfig.setUsername( repo.getUserName() );
119         repoConfig.setPassword( repo.getPassword() );
120         repoConfig.setTimeout( repo.getTimeout() );
121         repoConfig.setRemoteIndexUrl( repo.getRemoteIndexUrl() );
122         repoConfig.setDownloadRemoteIndex( repo.isDownloadRemoteIndex() );
123         repoConfig.setRemoteDownloadNetworkProxyId( repo.getRemoteDownloadNetworkProxyId() );
124         repoConfig.setDownloadRemoteIndexOnStartup( repo.isDownloadRemoteIndexOnStartup() );
125         repoConfig.setRemoteDownloadTimeout( repo.getRemoteDownloadTimeout() );
126         repoConfig.setDescription(repo.getDescription());
127         repoConfig.setIndexDir(repo.getIndexDirectory());
128         return repoConfig;
129     }
130 
131     @Override
132     public List<org.apache.archiva.admin.model.beans.RemoteRepository> getRemoteRepositories()
133         throws RepositoryAdminException
134     {
135 
136         return repositoryRegistry.getRemoteRepositories().stream().map( repo -> convertRepo( repo ) ).collect( Collectors.toList());
137     }
138 
139     @Override
140     public org.apache.archiva.admin.model.beans.RemoteRepository getRemoteRepository( String repositoryId )
141         throws RepositoryAdminException
142     {
143         return convertRepo( repositoryRegistry.getRemoteRepository( repositoryId ));
144     }
145 
146     @Override
147     public Boolean addRemoteRepository( org.apache.archiva.admin.model.beans.RemoteRepository remoteRepository, AuditInformation auditInformation )
148         throws RepositoryAdminException
149     {
150         triggerAuditEvent( remoteRepository.getId(), null, AuditEvent.ADD_REMOTE_REPO, auditInformation );
151         getRepositoryCommonValidator().basicValidation( remoteRepository, false );
152 
153         //TODO we can validate it's a good uri/url
154         if ( StringUtils.isEmpty( remoteRepository.getUrl() ) )
155         {
156             throw new RepositoryAdminException( "url cannot be null" );
157         }
158 
159         //MRM-752 - url needs trimming
160         //MRM-1940 - URL should not end with a slash
161         remoteRepository.setUrl( StringUtils.stripEnd(StringUtils.trim( remoteRepository.getUrl() ), "/"));
162 
163         if (StringUtils.isEmpty(remoteRepository.getCheckPath())) {
164             String checkUrl = remoteRepository.getUrl().toLowerCase();
165             for (RepositoryCheckPath path : getArchivaConfiguration ().getConfiguration().getArchivaDefaultConfiguration().getDefaultCheckPaths()) {
166                 log.debug("Checking path for urls: {} <-> {}", checkUrl, path.getUrl());
167                 if (checkUrl.startsWith(path.getUrl())) {
168                     remoteRepository.setCheckPath(path.getPath());
169                     break;
170                 }
171             }
172         }
173 
174         Configuration configuration = getArchivaConfiguration().getConfiguration();
175         RemoteRepositoryConfiguration remoteRepositoryConfiguration =
176             getRepositoryConfiguration( remoteRepository );
177         log.debug("Adding remote repo {}", remoteRepositoryConfiguration);
178 
179         try
180         {
181             repositoryRegistry.putRepository( remoteRepositoryConfiguration, configuration );
182         }
183         catch ( RepositoryException e )
184         {
185             log.error("Could not add remote repository {}: {}", remoteRepositoryConfiguration.getId(), e.getMessage(), e);
186             throw new RepositoryAdminException( "Adding of remote repository failed"+(e.getMessage()==null?"":": "+e.getMessage()) );
187 
188         }
189 
190         saveConfiguration( configuration );
191 
192         return Boolean.TRUE;
193     }
194 
195     @Override
196     public Boolean deleteRemoteRepository( String repositoryId, AuditInformation auditInformation )
197         throws RepositoryAdminException
198     {
199 
200         triggerAuditEvent( repositoryId, null, AuditEvent.DELETE_REMOTE_REPO, auditInformation );
201 
202         Configuration configuration = getArchivaConfiguration().getConfiguration();
203 
204         RemoteRepository repo = repositoryRegistry.getRemoteRepository( repositoryId );
205         if (repo==null) {
206             throw new RepositoryAdminException( "Could not delete repository "+repositoryId+". The repository does not exist." );
207         }
208         try
209         {
210             repositoryRegistry.removeRepository( repo, configuration );
211         }
212         catch ( RepositoryException e )
213         {
214             log.error("Deletion of remote repository failed {}: {}", repo.getId(), e.getMessage(), e);
215             throw new RepositoryAdminException( "Could not delete remote repository"+(e.getMessage()==null?"":": "+e.getMessage()) );
216         }
217 
218         saveConfiguration( configuration );
219 
220         return Boolean.TRUE;
221     }
222 
223     @Override
224     public Boolean updateRemoteRepository( org.apache.archiva.admin.model.beans.RemoteRepository remoteRepository, AuditInformation auditInformation )
225         throws RepositoryAdminException
226     {
227 
228         String repositoryId = remoteRepository.getId();
229 
230         triggerAuditEvent( repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation );
231 
232         // update means : remove and add
233 
234         Configuration configuration = getArchivaConfiguration().getConfiguration();
235 
236         RemoteRepositoryConfiguration remoteRepositoryConfiguration = getRepositoryConfiguration( remoteRepository );
237         try
238         {
239             repositoryRegistry.putRepository( remoteRepositoryConfiguration, configuration );
240         }
241         catch ( RepositoryException e )
242         {
243             log.error("Could not update remote repository {}: {}", remoteRepositoryConfiguration.getId(), e.getMessage(), e);
244             throw new RepositoryAdminException( "Update of remote repository failed"+(e.getMessage()==null?"":": "+e.getMessage()) );
245         }
246         saveConfiguration( configuration );
247         return Boolean.TRUE;
248     }
249 
250     @Override
251     public Map<String, org.apache.archiva.admin.model.beans.RemoteRepository> getRemoteRepositoriesAsMap()
252         throws RepositoryAdminException
253     {
254         java.util.Map<String, org.apache.archiva.admin.model.beans.RemoteRepository> map = new HashMap<>();
255 
256         for ( org.apache.archiva.admin.model.beans.RemoteRepository repo : getRemoteRepositories() )
257         {
258             map.put( repo.getId(), repo );
259         }
260 
261         return map;
262     }
263 
264     @Override
265     public IndexingContext createIndexContext( org.apache.archiva.admin.model.beans.RemoteRepository remoteRepository )
266         throws RepositoryAdminException
267     {
268         try
269         {
270             RemoteRepository repo = repositoryRegistry.getRemoteRepository(remoteRepository.getId());
271             return repo.getIndexingContext().getBaseContext(IndexingContext.class);
272         } catch (UnsupportedBaseContextException e) {
273             throw new RepositoryAdminException( e.getMessage(), e);
274         }
275 
276     }
277 
278 
279 
280 
281 }