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.event.RepositoryEvent;
29 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
30 import org.apache.archiva.repository.features.IndexCreationFeature;
31 import org.apache.archiva.repository.features.RemoteIndexFeature;
32 import org.apache.archiva.repository.features.StagingRepositoryFeature;
33 import org.apache.archiva.repository.base.BasicManagedRepository;
34 import org.apache.archiva.repository.base.BasicRemoteRepository;
35 import org.apache.archiva.repository.base.PasswordCredentials;
36 import org.springframework.stereotype.Service;
38 import java.io.IOException;
40 import java.nio.file.Paths;
41 import java.time.Duration;
42 import java.time.Period;
43 import java.util.HashSet;
47 * Just a simple mock class for the repository provider
49 @Service("mockRepositoryProvider")
50 public class RepositoryProviderMock implements RepositoryProvider
53 private static final Set<RepositoryType> TYPES = new HashSet<>( );
57 TYPES.add( RepositoryType.MAVEN );
58 TYPES.add( RepositoryType.NPM );
62 public Set<RepositoryType> provides( )
68 public EditableManagedRepository createManagedInstance( String id, String name ) throws IOException {
69 return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
73 public EditableRemoteRepository createRemoteInstance( String id, String name )
76 return BasicRemoteRepository.newFilesystemInstance( id, name , Paths.get("target/remotes"));
77 } catch (IOException e) {
78 throw new RuntimeException(e);
83 public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
88 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
89 BasicManagedRepository managedRepository = null;
91 managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories").resolve(configuration.getId()));
92 } catch (IOException e) {
93 throw new RepositoryException(e);
95 updateManagedInstance( managedRepository, configuration );
96 return managedRepository;
101 public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
105 managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
106 managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
107 managedRepository.setBaseUri( new URI( "" ) );
108 managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
109 managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
110 managedRepository.setLayout( configuration.getLayout( ) );
111 managedRepository.setScanned( configuration.isScanned( ) );
112 managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
113 if (configuration.isReleases()) {
114 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
116 if (configuration.isSnapshots()) {
117 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
119 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
120 acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
121 acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
122 acf.setRetentionCount( configuration.getRetentionCount( ) );
123 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
124 icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
125 icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
126 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
127 srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
129 catch ( Exception e )
131 throw new RepositoryException( "Error", e );
138 public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
139 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
140 BasicManagedRepository managedRepository = null;
142 managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
143 } catch (IOException e) {
144 throw new RepositoryException(e);
146 updateManagedInstance( managedRepository, configuration );
147 managedRepository.getFeature(StagingRepositoryFeature.class).get().setStageRepoNeeded(false);
148 return managedRepository;
152 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
154 BasicRemoteRepository remoteRepository = null;
156 remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
157 } catch (IOException e) {
158 throw new RepositoryException(e);
160 updateRemoteInstance( remoteRepository, configuration );
161 return remoteRepository;
164 @SuppressWarnings( "unchecked" )
166 public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
170 remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
171 remoteRepository.setBaseUri( new URI( "" ) );
172 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
173 remoteRepository.setLayout( configuration.getLayout( ) );
174 remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
175 remoteRepository.setCheckPath( configuration.getCheckPath( ) );
176 remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
177 remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
178 remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
179 char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
180 remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
181 remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
182 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
183 rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
184 rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
185 rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
186 rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
187 rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
189 catch ( Exception e )
191 throw new RepositoryException( "Error", e );
197 public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
202 public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
207 public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
209 ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
210 configuration.setId( managedRepository.getId( ) );
211 configuration.setName(managedRepository.getName());
212 configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
213 configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
214 configuration.setDescription( managedRepository.getDescription( ) );
215 configuration.setLayout( managedRepository.getLayout( ) );
216 configuration.setScanned( managedRepository.isScanned( ) );
217 configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
218 configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
219 configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
220 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
221 configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
222 configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
223 configuration.setRetentionCount( acf.getRetentionCount( ) );
224 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
225 configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
226 configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
227 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
228 configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
229 return configuration;
233 public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
238 public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
245 public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
247 RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
248 configuration.setId( remoteRepository.getId( ) );
249 configuration.setName( remoteRepository.getName( ) );
250 configuration.setDescription( remoteRepository.getDescription( ) );
251 configuration.setLayout( remoteRepository.getLayout( ) );
252 configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
253 configuration.setCheckPath( remoteRepository.getCheckPath( ) );
254 configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
255 configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
256 configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
257 RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
260 PasswordCredentials pwdCreds = (PasswordCredentials) creds;
261 configuration.setUsername( pwdCreds.getUsername( ) );
262 configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
264 configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
265 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
266 configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
267 configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
268 configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
269 configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
270 return configuration;
274 public void handle(Event event) {