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.repository.*;
26 import org.apache.archiva.repository.events.Event;
27 import org.apache.archiva.repository.events.RepositoryValueEvent;
28 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
29 import org.apache.archiva.repository.features.IndexCreationFeature;
30 import org.apache.archiva.repository.features.RemoteIndexFeature;
31 import org.apache.archiva.repository.features.StagingRepositoryFeature;
32 import org.springframework.stereotype.Service;
34 import java.io.IOException;
36 import java.nio.file.Paths;
37 import java.time.Duration;
38 import java.time.Period;
39 import java.util.HashSet;
43 * Just a simple mock class for the repository provider
45 @Service("mockRepositoryProvider")
46 public class RepositoryProviderMock implements RepositoryProvider
49 private static final Set<RepositoryType> TYPES = new HashSet<>( );
53 TYPES.add( RepositoryType.MAVEN );
54 TYPES.add( RepositoryType.NPM );
58 public Set<RepositoryType> provides( )
64 public EditableManagedRepository createManagedInstance( String id, String name ) throws IOException {
65 return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
69 public EditableRemoteRepository createRemoteInstance( String id, String name )
72 return BasicRemoteRepository.newFilesystemInstance( id, name , Paths.get("target/remotes"));
73 } catch (IOException e) {
74 throw new RuntimeException(e);
79 public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
84 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
85 BasicManagedRepository managedRepository = null;
87 managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories").resolve(configuration.getId()));
88 } catch (IOException e) {
89 throw new RepositoryException(e);
91 updateManagedInstance( managedRepository, configuration );
92 return managedRepository;
97 public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
101 managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
102 managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
103 managedRepository.setBaseUri( new URI( "" ) );
104 managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
105 managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
106 managedRepository.setLayout( configuration.getLayout( ) );
107 managedRepository.setScanned( configuration.isScanned( ) );
108 managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
109 if (configuration.isReleases()) {
110 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
112 if (configuration.isSnapshots()) {
113 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
115 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
116 acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
117 acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
118 acf.setRetentionCount( configuration.getRetentionCount( ) );
119 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
120 icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
121 icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
122 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
123 srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
125 catch ( Exception e )
127 throw new RepositoryException( "Error", e );
134 public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
135 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
136 BasicManagedRepository managedRepository = null;
138 managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
139 } catch (IOException e) {
140 throw new RepositoryException(e);
142 updateManagedInstance( managedRepository, configuration );
143 managedRepository.getFeature(StagingRepositoryFeature.class).get().setStageRepoNeeded(false);
144 return managedRepository;
148 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
150 BasicRemoteRepository remoteRepository = null;
152 remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
153 } catch (IOException e) {
154 throw new RepositoryException(e);
156 updateRemoteInstance( remoteRepository, configuration );
157 return remoteRepository;
160 @SuppressWarnings( "unchecked" )
162 public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
166 remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
167 remoteRepository.setBaseUri( new URI( "" ) );
168 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
169 remoteRepository.setLayout( configuration.getLayout( ) );
170 remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
171 remoteRepository.setCheckPath( configuration.getCheckPath( ) );
172 remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
173 remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
174 remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
175 char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
176 remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
177 remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
178 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
179 rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
180 rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
181 rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
182 rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
183 rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
185 catch ( Exception e )
187 throw new RepositoryException( "Error", e );
193 public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
198 public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
203 public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
205 ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
206 configuration.setId( managedRepository.getId( ) );
207 configuration.setName(managedRepository.getName());
208 configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
209 configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
210 configuration.setDescription( managedRepository.getDescription( ) );
211 configuration.setLayout( managedRepository.getLayout( ) );
212 configuration.setScanned( managedRepository.isScanned( ) );
213 configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
214 configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
215 configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
216 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
217 configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
218 configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
219 configuration.setRetentionCount( acf.getRetentionCount( ) );
220 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
221 configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
222 configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
223 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
224 configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
225 return configuration;
229 public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
235 public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
237 RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
238 configuration.setId( remoteRepository.getId( ) );
239 configuration.setName( remoteRepository.getName( ) );
240 configuration.setDescription( remoteRepository.getDescription( ) );
241 configuration.setLayout( remoteRepository.getLayout( ) );
242 configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
243 configuration.setCheckPath( remoteRepository.getCheckPath( ) );
244 configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
245 configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
246 configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
247 RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
250 PasswordCredentials pwdCreds = (PasswordCredentials) creds;
251 configuration.setUsername( pwdCreds.getUsername( ) );
252 configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
254 configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
255 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
256 configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
257 configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
258 configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
259 configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
260 return configuration;
264 public void handle(Event event) {