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.base.BasicManagedRepository;
26 import org.apache.archiva.repository.base.BasicRemoteRepository;
27 import org.apache.archiva.repository.EditableManagedRepository;
28 import org.apache.archiva.repository.EditableRemoteRepository;
29 import org.apache.archiva.repository.EditableRepositoryGroup;
30 import org.apache.archiva.repository.ManagedRepository;
31 import org.apache.archiva.repository.base.PasswordCredentials;
32 import org.apache.archiva.repository.ReleaseScheme;
33 import org.apache.archiva.repository.RemoteRepository;
34 import org.apache.archiva.repository.RepositoryCredentials;
35 import org.apache.archiva.event.Event;
36 import org.apache.archiva.repository.RepositoryException;
37 import org.apache.archiva.repository.RepositoryGroup;
38 import org.apache.archiva.repository.RepositoryProvider;
39 import org.apache.archiva.repository.RepositoryType;
40 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
41 import org.apache.archiva.repository.features.IndexCreationFeature;
42 import org.apache.archiva.repository.features.RemoteIndexFeature;
43 import org.apache.archiva.repository.features.StagingRepositoryFeature;
44 import org.springframework.stereotype.Service;
46 import java.io.IOException;
48 import java.nio.file.Paths;
49 import java.time.Duration;
50 import java.time.Period;
51 import java.util.HashSet;
55 * Just a simple mock class for the repository provider
57 @Service("mockRepositoryProvider")
58 public class RepositoryProviderMock implements RepositoryProvider
61 private static final Set<RepositoryType> TYPES = new HashSet<>( );
65 TYPES.add( RepositoryType.MAVEN );
66 TYPES.add( RepositoryType.NPM );
70 public Set<RepositoryType> provides( )
76 public EditableManagedRepository createManagedInstance( String id, String name ) throws IOException {
77 return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
81 public EditableRemoteRepository createRemoteInstance( String id, String name )
84 return BasicRemoteRepository.newFilesystemInstance( id, name, Paths.get("target/remotes") );
85 } catch (IOException e) {
86 throw new RuntimeException(e);
91 public EditableRepositoryGroup createRepositoryGroup( String id, String name )
97 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
150 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
151 BasicManagedRepository managedRepository = null;
153 managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
154 } catch (IOException e) {
155 throw new RepositoryException(e);
157 updateManagedInstance( managedRepository, configuration );
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( ) );
198 IndexCreationFeature icf = remoteRepository.getFeature(IndexCreationFeature.class).get();
199 icf.setIndexPath(new URI(".index" ));
201 catch ( Exception e )
203 throw new RepositoryException( "Error while updating remote instance: "+e.getMessage(), e );
209 public RepositoryGroup createRepositoryGroup( RepositoryGroupConfiguration configuration ) throws RepositoryException
215 public void updateRepositoryGroupInstance( EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration ) throws RepositoryException
221 public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
223 ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
224 configuration.setId( managedRepository.getId( ) );
225 configuration.setName(managedRepository.getName());
226 configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
227 configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
228 configuration.setDescription( managedRepository.getDescription( ) );
229 configuration.setLayout( managedRepository.getLayout( ) );
230 configuration.setScanned( managedRepository.isScanned( ) );
231 configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
232 configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
233 configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
234 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
235 configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
236 configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
237 configuration.setRetentionCount( acf.getRetentionCount( ) );
238 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
239 configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
240 configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
241 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
242 configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
243 return configuration;
247 public RepositoryGroupConfiguration getRepositoryGroupConfiguration( RepositoryGroup repositoryGroup ) throws RepositoryException
254 public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
256 RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
257 configuration.setId( remoteRepository.getId( ) );
258 configuration.setName( remoteRepository.getName( ) );
259 configuration.setDescription( remoteRepository.getDescription( ) );
260 configuration.setLayout( remoteRepository.getLayout( ) );
261 configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
262 configuration.setCheckPath( remoteRepository.getCheckPath( ) );
263 configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
264 configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
265 configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
266 RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
269 PasswordCredentials pwdCreds = (PasswordCredentials) creds;
270 configuration.setUsername( pwdCreds.getUsername( ) );
271 configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
273 configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
274 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
275 configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
276 configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
277 configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
278 configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
279 return configuration;
283 public void handle(Event event) {