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.event.EventType;
27 import org.apache.archiva.repository.base.BasicManagedRepository;
28 import org.apache.archiva.repository.base.BasicRemoteRepository;
29 import org.apache.archiva.repository.EditableManagedRepository;
30 import org.apache.archiva.repository.EditableRemoteRepository;
31 import org.apache.archiva.repository.RepositoryGroup;
32 import org.apache.archiva.repository.EditableRepositoryGroup;
33 import org.apache.archiva.repository.ManagedRepository;
34 import org.apache.archiva.repository.base.PasswordCredentials;
35 import org.apache.archiva.repository.ReleaseScheme;
36 import org.apache.archiva.repository.RemoteRepository;
37 import org.apache.archiva.repository.RepositoryCredentials;
38 import org.apache.archiva.repository.RepositoryException;
39 import org.apache.archiva.repository.RepositoryProvider;
40 import org.apache.archiva.repository.RepositoryType;
41 import org.apache.archiva.event.Event;
42 import org.apache.archiva.repository.event.RepositoryEvent;
43 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
44 import org.apache.archiva.repository.features.IndexCreationFeature;
45 import org.apache.archiva.repository.features.RemoteIndexFeature;
46 import org.apache.archiva.repository.features.StagingRepositoryFeature;
47 import org.springframework.stereotype.Service;
49 import java.io.IOException;
51 import java.nio.file.Paths;
52 import java.time.Duration;
53 import java.time.Period;
54 import java.util.HashSet;
58 * Just a simple mock class for the repository provider
60 @Service("mockRepositoryProvider")
61 public class RepositoryProviderMock implements RepositoryProvider
64 private static final Set<RepositoryType> TYPES = new HashSet<>( );
68 TYPES.add( RepositoryType.MAVEN );
69 TYPES.add( RepositoryType.NPM );
73 public Set<RepositoryType> provides( )
79 public EditableManagedRepository createManagedInstance( String id, String name )
82 return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories"));
83 } catch (IOException e) {
84 throw new RuntimeException(e);
89 public EditableRemoteRepository createRemoteInstance( String id, String name )
92 return BasicRemoteRepository.newFilesystemInstance(id, name, Paths.get("target/remotes"));
93 } catch (IOException e) {
94 throw new RuntimeException(e);
99 public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
101 BasicManagedRepository managedRepository;
103 managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories"));
104 } catch (IOException e) {
105 throw new RepositoryException(e);
107 updateManagedInstance( managedRepository, configuration );
108 return managedRepository;
113 public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
117 managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
118 managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
119 managedRepository.setBaseUri( new URI( "" ) );
120 managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
121 managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
122 managedRepository.setLayout( configuration.getLayout( ) );
123 managedRepository.setScanned( configuration.isScanned( ) );
124 managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
125 if (configuration.isReleases()) {
126 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
128 if (configuration.isSnapshots()) {
129 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
131 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
132 acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
133 acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
134 acf.setRetentionCount( configuration.getRetentionCount( ) );
135 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
136 icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
137 icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
138 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
139 srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
141 catch ( Exception e )
143 throw new RepositoryException( "Error", e );
150 public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
152 String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
154 BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories"));
155 updateManagedInstance(managedRepository, configuration);
156 return managedRepository;
157 } catch (IOException e) {
158 throw new RepositoryException(e);
163 public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
166 BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/remotes"));
167 updateRemoteInstance(remoteRepository, configuration);
168 return remoteRepository;
169 } catch (IOException e) {
170 throw new RepositoryException(e);
175 public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
179 remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
180 remoteRepository.setBaseUri( new URI( "" ) );
181 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
182 remoteRepository.setLayout( configuration.getLayout( ) );
183 remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
184 remoteRepository.setCheckPath( configuration.getCheckPath( ) );
185 remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
186 remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
187 remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
188 char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
189 remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
190 remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
191 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
192 rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
193 rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
194 rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
195 rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
196 rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
198 catch ( Exception e )
200 throw new RepositoryException( "Error", e );
206 public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
208 ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
209 configuration.setId( managedRepository.getId( ) );
210 configuration.setName(managedRepository.getName());
211 configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
212 configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
213 configuration.setDescription( managedRepository.getDescription( ) );
214 configuration.setLayout( managedRepository.getLayout( ) );
215 configuration.setScanned( managedRepository.isScanned( ) );
216 configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
217 configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
218 configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
219 ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
220 configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
221 configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
222 configuration.setRetentionCount( acf.getRetentionCount( ) );
223 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
224 configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
225 configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
226 StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
227 configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
228 return configuration;
233 public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
235 RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
236 configuration.setId( remoteRepository.getId( ) );
237 configuration.setName( remoteRepository.getName( ) );
238 configuration.setDescription( remoteRepository.getDescription( ) );
239 configuration.setLayout( remoteRepository.getLayout( ) );
240 configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
241 configuration.setCheckPath( remoteRepository.getCheckPath( ) );
242 configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
243 configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
244 configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
245 RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
248 PasswordCredentials pwdCreds = (PasswordCredentials) creds;
249 configuration.setUsername( pwdCreds.getUsername( ) );
250 configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
252 configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
253 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
254 configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
255 configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
256 configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
257 configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
258 return configuration;
262 public void handle(Event event) {
267 public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
272 public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
276 public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
282 public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
286 public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )