]> source.dussan.org Git - archiva.git/blob
5a3a4736d82c07d74f5f79ad1064e714c89691d4
[archiva.git] /
1 package org.apache.archiva.repository.mock;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
23 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
24 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
25 import org.apache.archiva.event.EventHandler;
26 import org.apache.archiva.repository.*;
27 import org.apache.archiva.event.Event;
28 import org.apache.archiva.repository.base.group.BasicRepositoryGroup;
29 import org.apache.archiva.repository.event.RepositoryEvent;
30 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
31 import org.apache.archiva.repository.features.IndexCreationFeature;
32 import org.apache.archiva.repository.features.RemoteIndexFeature;
33 import org.apache.archiva.repository.features.StagingRepositoryFeature;
34 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
35 import org.apache.archiva.repository.base.remote.BasicRemoteRepository;
36 import org.apache.archiva.repository.base.PasswordCredentials;
37 import org.springframework.stereotype.Service;
38
39 import java.io.IOException;
40 import java.net.URI;
41 import java.nio.file.Paths;
42 import java.time.Duration;
43 import java.time.Period;
44 import java.util.HashSet;
45 import java.util.Set;
46 import java.util.stream.Collectors;
47
48 /**
49  * Just a simple mock class for the repository provider
50  */
51 @Service("mockRepositoryProvider")
52 public class RepositoryProviderMock implements RepositoryProvider
53 {
54
55     private static final Set<RepositoryType> TYPES = new HashSet<>( );
56
57     static
58     {
59         TYPES.add( RepositoryType.MAVEN );
60         TYPES.add( RepositoryType.NPM );
61     }
62
63     @Override
64     public Set<RepositoryType> provides( )
65     {
66         return TYPES;
67     }
68
69     @Override
70     public EditableManagedRepository createManagedInstance( String id, String name ) throws IOException {
71         return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
72     }
73
74     @Override
75     public EditableRemoteRepository createRemoteInstance( String id, String name )
76     {
77         try {
78             return BasicRemoteRepository.newFilesystemInstance( id, name , Paths.get("target/remotes"));
79         } catch (IOException e) {
80             throw new RuntimeException(e);
81         }
82     }
83
84     @Override
85     public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
86         try
87         {
88             return BasicRepositoryGroup.newFilesystemInstance( id, name, Paths.get( "target/groups" ) );
89         }
90         catch ( IOException e )
91         {
92             throw new RuntimeException( );
93         }
94
95     }
96
97     @Override
98     public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
99         BasicManagedRepository managedRepository = null;
100         try {
101             managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories").resolve(configuration.getId()));
102         } catch (IOException e) {
103             throw new RepositoryException(e);
104         }
105         updateManagedInstance( managedRepository, configuration );
106         return managedRepository;
107     }
108
109
110     @Override
111     public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
112     {
113         try
114         {
115             managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
116             managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
117             managedRepository.setBaseUri( new URI( "" ) );
118             managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
119             managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
120             managedRepository.setLayout( configuration.getLayout( ) );
121             managedRepository.setScanned( configuration.isScanned( ) );
122             managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
123             if (configuration.isReleases()) {
124                 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
125             }
126             if (configuration.isSnapshots()) {
127                 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
128             }
129             ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
130             acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
131             acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
132             acf.setRetentionCount( configuration.getRetentionCount( ) );
133             IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
134             icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
135             icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
136             StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
137             srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
138         }
139         catch ( Exception e )
140         {
141             throw new RepositoryException( "Error", e );
142         }
143
144     }
145
146
147     @Override
148     public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
149         String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
150         BasicManagedRepository managedRepository = null;
151         try {
152             managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
153         } catch (IOException e) {
154             throw new RepositoryException(e);
155         }
156         updateManagedInstance( managedRepository, configuration );
157         managedRepository.getFeature(StagingRepositoryFeature.class).get().setStageRepoNeeded(false);
158         return managedRepository;
159     }
160
161     @Override
162     public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
163     {
164         BasicRemoteRepository remoteRepository = null;
165         try {
166             remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
167         } catch (IOException e) {
168             throw new RepositoryException(e);
169         }
170         updateRemoteInstance( remoteRepository, configuration );
171         return remoteRepository;
172     }
173
174     @SuppressWarnings( "unchecked" )
175     @Override
176     public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
177     {
178         try
179         {
180             remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
181             remoteRepository.setBaseUri( new URI( "" ) );
182             remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
183             remoteRepository.setLayout( configuration.getLayout( ) );
184             remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
185             remoteRepository.setCheckPath( configuration.getCheckPath( ) );
186             remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
187             remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
188             remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
189             char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
190             remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
191             remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
192             RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
193             rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
194             rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
195             rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
196             rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
197             rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
198         }
199         catch ( Exception e )
200         {
201             throw new RepositoryException( "Error", e );
202         }
203
204     }
205
206     @Override
207     public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
208         EditableRepositoryGroup group = createRepositoryGroup( configuration.getId( ), configuration.getName( ) );
209         updateGroupInstance( group, configuration );
210         return group;
211     }
212
213     private void updateGroupInstance( EditableRepositoryGroup group, RepositoryGroupConfiguration configuration )
214     {
215         group.setMergedIndexTTL( configuration.getMergedIndexTtl() );
216         group.setSchedulingDefinition( configuration.getCronExpression() );
217     }
218
219     @Override
220     public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
221
222     }
223
224     @Override
225     public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
226     {
227         ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
228         configuration.setId( managedRepository.getId( ) );
229         configuration.setName(managedRepository.getName());
230         configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
231         configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
232         configuration.setDescription( managedRepository.getDescription( ) );
233         configuration.setLayout( managedRepository.getLayout( ) );
234         configuration.setScanned( managedRepository.isScanned( ) );
235         configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
236         configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
237         configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
238         ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
239         configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
240         configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
241         configuration.setRetentionCount( acf.getRetentionCount( ) );
242         IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
243         configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
244         configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
245         StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
246         configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
247         return configuration;
248     }
249
250     @Override
251     public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
252         RepositoryGroupConfiguration cfg = new RepositoryGroupConfiguration();
253         cfg.setId(repositoryGroup.getId());
254         cfg.setName(repositoryGroup.getName());
255         if (repositoryGroup.supportsFeature( IndexCreationFeature.class ))
256         {
257             IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class ).get();
258
259             cfg.setMergedIndexPath( indexCreationFeature.getIndexPath().toString() );
260         }
261         cfg.setMergedIndexTtl(repositoryGroup.getMergedIndexTTL());
262         cfg.setRepositories(repositoryGroup.getRepositories().stream().map( Repository::getId ).collect( Collectors.toList()));
263         cfg.setCronExpression(repositoryGroup.getSchedulingDefinition());
264         return cfg;
265     }
266
267     @Override
268     public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
269     {
270         // do nothing
271     }
272
273
274     @Override
275     public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
276     {
277         RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
278         configuration.setId( remoteRepository.getId( ) );
279         configuration.setName( remoteRepository.getName( ) );
280         configuration.setDescription( remoteRepository.getDescription( ) );
281         configuration.setLayout( remoteRepository.getLayout( ) );
282         configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
283         configuration.setCheckPath( remoteRepository.getCheckPath( ) );
284         configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
285         configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
286         configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
287         RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
288         if (creds!=null)
289         {
290             PasswordCredentials pwdCreds = (PasswordCredentials) creds;
291             configuration.setUsername( pwdCreds.getUsername( ) );
292             configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
293         }
294         configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
295         RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
296         configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
297         configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
298         configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
299         configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
300         return configuration;
301     }
302
303     @Override
304     public void handle(Event event) {
305
306     }
307 }