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.BasicManagedRepository;
26 import org.apache.archiva.repository.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.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.repository.events.Event;
36 import org.apache.archiva.repository.events.RepositoryValueEvent;
37 import org.apache.archiva.repository.RepositoryException;
38 import org.apache.archiva.repository.RepositoryGroup;
39 import org.apache.archiva.repository.RepositoryProvider;
40 import org.apache.archiva.repository.RepositoryType;
41 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
42 import org.apache.archiva.repository.features.IndexCreationFeature;
43 import org.apache.archiva.repository.features.RemoteIndexFeature;
44 import org.apache.archiva.repository.features.StagingRepositoryFeature;
45 import org.springframework.stereotype.Service;
47 import java.io.IOException;
49 import java.nio.file.Paths;
50 import java.time.Duration;
51 import java.time.Period;
52 import java.util.HashSet;
56 * Just a simple mock class for the repository provider
58 @Service("mockRepositoryProvider")
59 public class RepositoryProviderMock implements RepositoryProvider
62 private static final Set<RepositoryType> TYPES = new HashSet<>( );
66 TYPES.add( RepositoryType.MAVEN );
67 TYPES.add( RepositoryType.NPM );
71 public Set<RepositoryType> provides( )
77 public EditableManagedRepository createManagedInstance( String id, String name )
80 return BasicManagedRepository.newFilesystemInstance( id, name, Paths.get("target/repositories").resolve(id) );
81 } catch (IOException e) {
82 throw new RuntimeException(e);
87 public EditableRemoteRepository createRemoteInstance( String id, String name )
90 return BasicRemoteRepository.newFilesystemInstance( id, name, Paths.get("target/remotes") );
91 } catch (IOException e) {
92 throw new RuntimeException(e);
97 public EditableRepositoryGroup createRepositoryGroup( String id, String name )
104 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
106 BasicManagedRepository managedRepository = null;
108 managedRepository = BasicManagedRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/repositories").resolve(configuration.getId()) );
109 } catch (IOException e) {
110 throw new RepositoryException(e);
112 updateManagedInstance( managedRepository, configuration );
113 return managedRepository;
118 public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
122 managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
123 managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
124 managedRepository.setBaseUri( new URI( "" ) );
125 managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
126 managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
127 managedRepository.setLayout( configuration.getLayout( ) );
128 managedRepository.setScanned( configuration.isScanned( ) );
129 managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
130 if (configuration.isReleases()) {
131 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
133 if (configuration.isSnapshots()) {
134 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
136 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
137 acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
138 acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
139 acf.setRetentionCount( configuration.getRetentionCount( ) );
140 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
141 icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
142 icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
143 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
144 srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
146 catch ( Exception e )
148 throw new RepositoryException( "Error", e );
155 public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
157 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
158 BasicManagedRepository managedRepository = null;
160 managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
161 } catch (IOException e) {
162 throw new RepositoryException(e);
164 updateManagedInstance( managedRepository, configuration );
165 return managedRepository;
169 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
171 BasicRemoteRepository remoteRepository = null;
173 remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
174 } catch (IOException e) {
175 throw new RepositoryException(e);
177 updateRemoteInstance( remoteRepository, configuration );
178 return remoteRepository;
181 @SuppressWarnings("unchecked")
183 public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
187 remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
188 remoteRepository.setBaseUri( new URI( "" ) );
189 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
190 remoteRepository.setLayout( configuration.getLayout( ) );
191 remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
192 remoteRepository.setCheckPath( configuration.getCheckPath( ) );
193 remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
194 remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
195 remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
196 char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
197 remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
198 remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
199 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
200 rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
201 rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
202 rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
203 rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
204 rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
206 catch ( Exception e )
208 throw new RepositoryException( "Error", e );
214 public RepositoryGroup createRepositoryGroup( RepositoryGroupConfiguration configuration ) throws RepositoryException
220 public void updateRepositoryGroupInstance( EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration ) throws RepositoryException
226 public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository )
228 ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
229 configuration.setId( managedRepository.getId( ) );
230 configuration.setName(managedRepository.getName());
231 configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
232 configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
233 configuration.setDescription( managedRepository.getDescription( ) );
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 configuration.setLayout( managedRepository.getLayout() );
239 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
240 configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
241 configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
242 configuration.setRetentionCount( acf.getRetentionCount( ) );
243 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
244 configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
245 configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
246 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
247 configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
248 return configuration;
252 public RepositoryGroupConfiguration getRepositoryGroupConfiguration( RepositoryGroup repositoryGroup ) throws RepositoryException
259 public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository )
261 RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
262 configuration.setId( remoteRepository.getId( ) );
263 configuration.setName( remoteRepository.getName( ) );
264 configuration.setDescription( remoteRepository.getDescription( ) );
265 configuration.setLayout( remoteRepository.getLayout( ) );
266 configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
267 configuration.setCheckPath( remoteRepository.getCheckPath( ) );
268 configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
269 configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
270 configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
271 RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
274 PasswordCredentials pwdCreds = (PasswordCredentials) creds;
275 configuration.setUsername( pwdCreds.getUsername( ) );
276 configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
278 configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
279 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
280 configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
281 configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
282 configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
283 configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
284 return configuration;
288 public void handle(Event event) {