]> source.dussan.org Git - archiva.git/blob
5a4c9f5ae459cca4013f554aabaa894a503ac762
[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
47 /**
48  * Just a simple mock class for the repository provider
49  */
50 @Service("mockRepositoryProvider")
51 public class RepositoryProviderMock implements RepositoryProvider
52 {
53
54     private static final Set<RepositoryType> TYPES = new HashSet<>( );
55
56     static
57     {
58         TYPES.add( RepositoryType.MAVEN );
59         TYPES.add( RepositoryType.NPM );
60     }
61
62     @Override
63     public Set<RepositoryType> provides( )
64     {
65         return TYPES;
66     }
67
68     @Override
69     public EditableManagedRepository createManagedInstance( String id, String name ) throws IOException {
70         return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
71     }
72
73     @Override
74     public EditableRemoteRepository createRemoteInstance( String id, String name )
75     {
76         try {
77             return BasicRemoteRepository.newFilesystemInstance( id, name , Paths.get("target/remotes"));
78         } catch (IOException e) {
79             throw new RuntimeException(e);
80         }
81     }
82
83     @Override
84     public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
85         try
86         {
87             return BasicRepositoryGroup.newFilesystemInstance( id, name, Paths.get( "target/groups" ) );
88         }
89         catch ( IOException e )
90         {
91             throw new RuntimeException( );
92         }
93
94     }
95
96     @Override
97     public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
98         BasicManagedRepository managedRepository = null;
99         try {
100             managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories").resolve(configuration.getId()));
101         } catch (IOException e) {
102             throw new RepositoryException(e);
103         }
104         updateManagedInstance( managedRepository, configuration );
105         return managedRepository;
106     }
107
108
109     @Override
110     public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
111     {
112         try
113         {
114             managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
115             managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
116             managedRepository.setBaseUri( new URI( "" ) );
117             managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
118             managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
119             managedRepository.setLayout( configuration.getLayout( ) );
120             managedRepository.setScanned( configuration.isScanned( ) );
121             managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
122             if (configuration.isReleases()) {
123                 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
124             }
125             if (configuration.isSnapshots()) {
126                 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
127             }
128             ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
129             acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
130             acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
131             acf.setRetentionCount( configuration.getRetentionCount( ) );
132             IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
133             icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
134             icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
135             StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
136             srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
137         }
138         catch ( Exception e )
139         {
140             throw new RepositoryException( "Error", e );
141         }
142
143     }
144
145
146     @Override
147     public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
148         String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
149         BasicManagedRepository managedRepository = null;
150         try {
151             managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
152         } catch (IOException e) {
153             throw new RepositoryException(e);
154         }
155         updateManagedInstance( managedRepository, configuration );
156         managedRepository.getFeature(StagingRepositoryFeature.class).get().setStageRepoNeeded(false);
157         return managedRepository;
158     }
159
160     @Override
161     public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
162     {
163         BasicRemoteRepository remoteRepository = null;
164         try {
165             remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
166         } catch (IOException e) {
167             throw new RepositoryException(e);
168         }
169         updateRemoteInstance( remoteRepository, configuration );
170         return remoteRepository;
171     }
172
173     @SuppressWarnings( "unchecked" )
174     @Override
175     public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
176     {
177         try
178         {
179             remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
180             remoteRepository.setBaseUri( new URI( "" ) );
181             remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
182             remoteRepository.setLayout( configuration.getLayout( ) );
183             remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
184             remoteRepository.setCheckPath( configuration.getCheckPath( ) );
185             remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
186             remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
187             remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
188             char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
189             remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
190             remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
191             RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
192             rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
193             rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
194             rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
195             rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
196             rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
197         }
198         catch ( Exception e )
199         {
200             throw new RepositoryException( "Error", e );
201         }
202
203     }
204
205     @Override
206     public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
207         EditableRepositoryGroup group = createRepositoryGroup( configuration.getId( ), configuration.getName( ) );
208         updateGroupInstance( group, configuration );
209         return group;
210     }
211
212     private void updateGroupInstance( EditableRepositoryGroup group, RepositoryGroupConfiguration configuration )
213     {
214         group.setMergedIndexTTL( configuration.getMergedIndexTtl() );
215         group.setSchedulingDefinition( configuration.getCronExpression() );
216     }
217
218     @Override
219     public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
220
221     }
222
223     @Override
224     public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
225     {
226         ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
227         configuration.setId( managedRepository.getId( ) );
228         configuration.setName(managedRepository.getName());
229         configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
230         configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
231         configuration.setDescription( managedRepository.getDescription( ) );
232         configuration.setLayout( managedRepository.getLayout( ) );
233         configuration.setScanned( managedRepository.isScanned( ) );
234         configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
235         configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
236         configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
237         ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
238         configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
239         configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
240         configuration.setRetentionCount( acf.getRetentionCount( ) );
241         IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
242         configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
243         configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
244         StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
245         configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
246         return configuration;
247     }
248
249     @Override
250     public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
251         return null;
252     }
253
254     @Override
255     public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
256     {
257         // do nothing
258     }
259
260
261     @Override
262     public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
263     {
264         RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
265         configuration.setId( remoteRepository.getId( ) );
266         configuration.setName( remoteRepository.getName( ) );
267         configuration.setDescription( remoteRepository.getDescription( ) );
268         configuration.setLayout( remoteRepository.getLayout( ) );
269         configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
270         configuration.setCheckPath( remoteRepository.getCheckPath( ) );
271         configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
272         configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
273         configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
274         RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
275         if (creds!=null)
276         {
277             PasswordCredentials pwdCreds = (PasswordCredentials) creds;
278             configuration.setUsername( pwdCreds.getUsername( ) );
279             configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
280         }
281         configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
282         RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
283         configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
284         configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
285         configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
286         configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
287         return configuration;
288     }
289
290     @Override
291     public void handle(Event event) {
292
293     }
294 }