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