1 package org.apache.archiva.repository.mock;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
39 import java.io.IOException;
41 import java.nio.file.Paths;
42 import java.time.Duration;
43 import java.time.Period;
44 import java.util.HashSet;
46 import java.util.stream.Collectors;
49 * Just a simple mock class for the repository provider
51 @Service("mockRepositoryProvider")
52 public class RepositoryProviderMock implements RepositoryProvider
55 private static final Set<RepositoryType> TYPES = new HashSet<>( );
59 TYPES.add( RepositoryType.MAVEN );
60 TYPES.add( RepositoryType.NPM );
64 public Set<RepositoryType> provides( )
70 public EditableManagedRepository createManagedInstance( String id, String name ) throws IOException {
71 return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
75 public EditableRemoteRepository createRemoteInstance( String id, String name )
78 return BasicRemoteRepository.newFilesystemInstance( id, name , Paths.get("target/remotes"));
79 } catch (IOException e) {
80 throw new RuntimeException(e);
85 public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
88 return BasicRepositoryGroup.newFilesystemInstance( id, name, Paths.get( "target/groups" ) );
90 catch ( IOException e )
92 throw new RuntimeException( );
98 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
99 BasicManagedRepository managedRepository = null;
101 managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories").resolve(configuration.getId()));
102 } catch (IOException e) {
103 throw new RepositoryException(e);
105 updateManagedInstance( managedRepository, configuration );
106 return managedRepository;
111 public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
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 );
126 if (configuration.isSnapshots()) {
127 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
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( ) );
139 catch ( Exception e )
141 throw new RepositoryException( "Error", e );
148 public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
149 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
150 BasicManagedRepository managedRepository = null;
152 managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
153 } catch (IOException e) {
154 throw new RepositoryException(e);
156 updateManagedInstance( managedRepository, configuration );
157 managedRepository.getFeature(StagingRepositoryFeature.class).get().setStageRepoNeeded(false);
158 return managedRepository;
162 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
164 BasicRemoteRepository remoteRepository = null;
166 remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
167 } catch (IOException e) {
168 throw new RepositoryException(e);
170 updateRemoteInstance( remoteRepository, configuration );
171 return remoteRepository;
174 @SuppressWarnings( "unchecked" )
176 public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
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( ) );
199 catch ( Exception e )
201 throw new RepositoryException( "Error", e );
207 public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
208 EditableRepositoryGroup group = createRepositoryGroup( configuration.getId( ), configuration.getName( ) );
209 updateGroupInstance( group, configuration );
213 private void updateGroupInstance( EditableRepositoryGroup group, RepositoryGroupConfiguration configuration )
215 group.setMergedIndexTTL( configuration.getMergedIndexTtl() );
216 group.setSchedulingDefinition( configuration.getCronExpression() );
220 public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
225 public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
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;
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 ))
257 IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class ).get();
259 cfg.setMergedIndexPath( indexCreationFeature.getIndexPath().toString() );
261 cfg.setMergedIndexTtl(repositoryGroup.getMergedIndexTTL());
262 cfg.setRepositories(repositoryGroup.getRepositories().stream().map( Repository::getId ).collect( Collectors.toList()));
263 cfg.setCronExpression(repositoryGroup.getSchedulingDefinition());
268 public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
275 public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
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( );
290 PasswordCredentials pwdCreds = (PasswordCredentials) creds;
291 configuration.setUsername( pwdCreds.getUsername( ) );
292 configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
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;
304 public void handle(Event event) {