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.model.ManagedRepositoryConfiguration;
23 import org.apache.archiva.configuration.model.RemoteRepositoryConfiguration;
24 import org.apache.archiva.configuration.model.RepositoryGroupConfiguration;
25 import org.apache.archiva.event.EventHandler;
26 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
27 import org.apache.archiva.repository.base.remote.BasicRemoteRepository;
28 import org.apache.archiva.repository.EditableManagedRepository;
29 import org.apache.archiva.repository.EditableRemoteRepository;
30 import org.apache.archiva.repository.RepositoryGroup;
31 import org.apache.archiva.repository.EditableRepositoryGroup;
32 import org.apache.archiva.repository.ManagedRepository;
33 import org.apache.archiva.repository.base.PasswordCredentials;
34 import org.apache.archiva.repository.ReleaseScheme;
35 import org.apache.archiva.repository.RemoteRepository;
36 import org.apache.archiva.repository.RepositoryCredentials;
37 import org.apache.archiva.repository.RepositoryException;
38 import org.apache.archiva.repository.RepositoryProvider;
39 import org.apache.archiva.repository.RepositoryType;
40 import org.apache.archiva.event.Event;
41 import org.apache.archiva.repository.event.RepositoryEvent;
42 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
43 import org.apache.archiva.repository.features.IndexCreationFeature;
44 import org.apache.archiva.repository.features.RemoteIndexFeature;
45 import org.apache.archiva.repository.features.StagingRepositoryFeature;
46 import org.springframework.stereotype.Service;
48 import java.io.IOException;
50 import java.nio.file.Paths;
51 import java.time.Duration;
52 import java.time.Period;
53 import java.util.HashSet;
57 * Just a simple mock class for the repository provider
59 @Service("mockRepositoryProvider")
60 public class RepositoryProviderMock implements RepositoryProvider
63 private static final Set<RepositoryType> TYPES = new HashSet<>( );
67 TYPES.add( RepositoryType.MAVEN );
68 TYPES.add( RepositoryType.NPM );
72 public Set<RepositoryType> provides( )
78 public EditableManagedRepository createManagedInstance( String id, String name )
81 return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories"));
82 } catch (IOException e) {
83 throw new RuntimeException(e);
88 public EditableRemoteRepository createRemoteInstance( String id, String name )
91 return BasicRemoteRepository.newFilesystemInstance(id, name, Paths.get("target/remotes"));
92 } catch (IOException e) {
93 throw new RuntimeException(e);
98 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
100 BasicManagedRepository managedRepository;
102 managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories"));
103 } catch (IOException e) {
104 throw new RepositoryException(e);
106 updateManagedInstance( managedRepository, configuration );
107 return managedRepository;
112 public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
116 managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
117 managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
118 managedRepository.setBaseUri( new URI( "" ) );
119 managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
120 managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
121 managedRepository.setLayout( configuration.getLayout( ) );
122 managedRepository.setScanned( configuration.isScanned( ) );
123 managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
124 if (configuration.isReleases()) {
125 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
127 if (configuration.isSnapshots()) {
128 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
130 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
131 acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
132 acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
133 acf.setRetentionCount( configuration.getRetentionCount( ) );
134 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
135 icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
136 icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
137 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
138 srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
140 catch ( Exception e )
142 throw new RepositoryException( "Error", e );
149 public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
151 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
153 BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories"));
154 updateManagedInstance(managedRepository, configuration);
155 return managedRepository;
156 } catch (IOException e) {
157 throw new RepositoryException(e);
162 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
165 BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/remotes"));
166 updateRemoteInstance(remoteRepository, configuration);
167 return remoteRepository;
168 } catch (IOException e) {
169 throw new RepositoryException(e);
174 public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
178 remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
179 remoteRepository.setBaseUri( new URI( "" ) );
180 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
181 remoteRepository.setLayout( configuration.getLayout( ) );
182 remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
183 remoteRepository.setCheckPath( configuration.getCheckPath( ) );
184 remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
185 remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
186 remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
187 char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
188 remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
189 remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
190 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
191 rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
192 rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
193 rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
194 rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
195 rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
197 catch ( Exception e )
199 throw new RepositoryException( "Error", e );
205 public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
207 ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
208 configuration.setId( managedRepository.getId( ) );
209 configuration.setName(managedRepository.getName());
210 configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
211 configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
212 configuration.setDescription( managedRepository.getDescription( ) );
213 configuration.setLayout( managedRepository.getLayout( ) );
214 configuration.setScanned( managedRepository.isScanned( ) );
215 configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
216 configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
217 configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
218 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
219 configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
220 configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
221 configuration.setRetentionCount( acf.getRetentionCount( ) );
222 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
223 configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
224 configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
225 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
226 configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
227 return configuration;
232 public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
234 RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
235 configuration.setId( remoteRepository.getId( ) );
236 configuration.setName( remoteRepository.getName( ) );
237 configuration.setDescription( remoteRepository.getDescription( ) );
238 configuration.setLayout( remoteRepository.getLayout( ) );
239 configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
240 configuration.setCheckPath( remoteRepository.getCheckPath( ) );
241 configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
242 configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
243 configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
244 RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
247 PasswordCredentials pwdCreds = (PasswordCredentials) creds;
248 configuration.setUsername( pwdCreds.getUsername( ) );
249 configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
251 configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
252 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
253 configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
254 configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
255 configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
256 configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
257 return configuration;
261 public void handle(Event event) {
266 public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
271 public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
275 public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
281 public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
285 public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )