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.RepositoryGroup;
30 import org.apache.archiva.repository.EditableRepositoryGroup;
31 import org.apache.archiva.repository.ManagedRepository;
32 import org.apache.archiva.repository.PasswordCredentials;
33 import org.apache.archiva.repository.ReleaseScheme;
34 import org.apache.archiva.repository.RemoteRepository;
35 import org.apache.archiva.repository.RepositoryCredentials;
36 import org.apache.archiva.repository.RepositoryException;
37 import org.apache.archiva.repository.RepositoryProvider;
38 import org.apache.archiva.repository.RepositoryType;
39 import org.apache.archiva.repository.events.Event;
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 )
79 return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories"));
80 } catch (IOException e) {
81 throw new RuntimeException(e);
86 public EditableRemoteRepository createRemoteInstance( String id, String name )
89 return BasicRemoteRepository.newFilesystemInstance(id, name, Paths.get("target/remotes"));
90 } catch (IOException e) {
91 throw new RuntimeException(e);
96 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
98 BasicManagedRepository managedRepository;
100 managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories"));
101 } catch (IOException e) {
102 throw new RepositoryException(e);
104 updateManagedInstance( managedRepository, configuration );
105 return managedRepository;
110 public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
114 managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
115 managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
116 managedRepository.setBaseUri( new URI( "" ) );
117 managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
118 managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
119 managedRepository.setLayout( configuration.getLayout( ) );
120 managedRepository.setScanned( configuration.isScanned( ) );
121 managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
122 if (configuration.isReleases()) {
123 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
125 if (configuration.isSnapshots()) {
126 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
128 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
129 acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
130 acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
131 acf.setRetentionCount( configuration.getRetentionCount( ) );
132 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
133 icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
134 icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
135 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
136 srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
138 catch ( Exception e )
140 throw new RepositoryException( "Error", e );
147 public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
149 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
151 BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories"));
152 updateManagedInstance(managedRepository, configuration);
153 return managedRepository;
154 } catch (IOException e) {
155 throw new RepositoryException(e);
160 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
163 BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/remotes"));
164 updateRemoteInstance(remoteRepository, configuration);
165 return remoteRepository;
166 } catch (IOException e) {
167 throw new RepositoryException(e);
172 public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
176 remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
177 remoteRepository.setBaseUri( new URI( "" ) );
178 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
179 remoteRepository.setLayout( configuration.getLayout( ) );
180 remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
181 remoteRepository.setCheckPath( configuration.getCheckPath( ) );
182 remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
183 remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
184 remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
185 char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
186 remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
187 remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
188 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
189 rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
190 rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
191 rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
192 rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
193 rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
195 catch ( Exception e )
197 throw new RepositoryException( "Error", e );
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;
230 public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
232 RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
233 configuration.setId( remoteRepository.getId( ) );
234 configuration.setName( remoteRepository.getName( ) );
235 configuration.setDescription( remoteRepository.getDescription( ) );
236 configuration.setLayout( remoteRepository.getLayout( ) );
237 configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
238 configuration.setCheckPath( remoteRepository.getCheckPath( ) );
239 configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
240 configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
241 configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
242 RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
245 PasswordCredentials pwdCreds = (PasswordCredentials) creds;
246 configuration.setUsername( pwdCreds.getUsername( ) );
247 configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
249 configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
250 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
251 configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
252 configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
253 configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
254 configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
255 return configuration;
259 public void handle(Event event) {
264 public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
269 public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
273 public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
279 public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {