You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepositoryProviderMock.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package org.apache.archiva.repository.mock;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
  21. import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
  22. import org.apache.archiva.configuration.RepositoryGroupConfiguration;
  23. import org.apache.archiva.repository.*;
  24. import org.apache.archiva.event.Event;
  25. import org.apache.archiva.repository.features.ArtifactCleanupFeature;
  26. import org.apache.archiva.repository.features.IndexCreationFeature;
  27. import org.apache.archiva.repository.features.RemoteIndexFeature;
  28. import org.apache.archiva.repository.features.StagingRepositoryFeature;
  29. import org.apache.archiva.repository.base.BasicManagedRepository;
  30. import org.apache.archiva.repository.base.BasicRemoteRepository;
  31. import org.apache.archiva.repository.base.PasswordCredentials;
  32. import org.springframework.stereotype.Service;
  33. import java.io.IOException;
  34. import java.net.URI;
  35. import java.nio.file.Paths;
  36. import java.time.Duration;
  37. import java.time.Period;
  38. import java.util.HashSet;
  39. import java.util.Set;
  40. /**
  41. * Just a simple mock class for the repository provider
  42. */
  43. @Service("mockRepositoryProvider")
  44. public class RepositoryProviderMock implements RepositoryProvider
  45. {
  46. private static final Set<RepositoryType> TYPES = new HashSet<>( );
  47. static
  48. {
  49. TYPES.add( RepositoryType.MAVEN );
  50. TYPES.add( RepositoryType.NPM );
  51. }
  52. @Override
  53. public Set<RepositoryType> provides( )
  54. {
  55. return TYPES;
  56. }
  57. @Override
  58. public EditableManagedRepository createManagedInstance( String id, String name ) throws IOException {
  59. return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories").resolve(id));
  60. }
  61. @Override
  62. public EditableRemoteRepository createRemoteInstance( String id, String name )
  63. {
  64. try {
  65. return BasicRemoteRepository.newFilesystemInstance( id, name , Paths.get("target/remotes"));
  66. } catch (IOException e) {
  67. throw new RuntimeException(e);
  68. }
  69. }
  70. @Override
  71. public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
  72. return null;
  73. }
  74. @Override
  75. public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
  76. BasicManagedRepository managedRepository = null;
  77. try {
  78. managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories").resolve(configuration.getId()));
  79. } catch (IOException e) {
  80. throw new RepositoryException(e);
  81. }
  82. updateManagedInstance( managedRepository, configuration );
  83. return managedRepository;
  84. }
  85. @Override
  86. public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
  87. {
  88. try
  89. {
  90. managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
  91. managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
  92. managedRepository.setBaseUri( new URI( "" ) );
  93. managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
  94. managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
  95. managedRepository.setLayout( configuration.getLayout( ) );
  96. managedRepository.setScanned( configuration.isScanned( ) );
  97. managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
  98. if (configuration.isReleases()) {
  99. managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
  100. }
  101. if (configuration.isSnapshots()) {
  102. managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
  103. }
  104. ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
  105. acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
  106. acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
  107. acf.setRetentionCount( configuration.getRetentionCount( ) );
  108. IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
  109. icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
  110. icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
  111. StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
  112. srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
  113. }
  114. catch ( Exception e )
  115. {
  116. throw new RepositoryException( "Error", e );
  117. }
  118. }
  119. @Override
  120. public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException {
  121. String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
  122. BasicManagedRepository managedRepository = null;
  123. try {
  124. managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
  125. } catch (IOException e) {
  126. throw new RepositoryException(e);
  127. }
  128. updateManagedInstance( managedRepository, configuration );
  129. managedRepository.getFeature(StagingRepositoryFeature.class).get().setStageRepoNeeded(false);
  130. return managedRepository;
  131. }
  132. @Override
  133. public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
  134. {
  135. BasicRemoteRepository remoteRepository = null;
  136. try {
  137. remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
  138. } catch (IOException e) {
  139. throw new RepositoryException(e);
  140. }
  141. updateRemoteInstance( remoteRepository, configuration );
  142. return remoteRepository;
  143. }
  144. @SuppressWarnings( "unchecked" )
  145. @Override
  146. public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
  147. {
  148. try
  149. {
  150. remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
  151. remoteRepository.setBaseUri( new URI( "" ) );
  152. remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
  153. remoteRepository.setLayout( configuration.getLayout( ) );
  154. remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
  155. remoteRepository.setCheckPath( configuration.getCheckPath( ) );
  156. remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
  157. remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
  158. remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
  159. char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
  160. remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
  161. remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
  162. RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
  163. rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
  164. rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
  165. rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
  166. rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
  167. rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
  168. }
  169. catch ( Exception e )
  170. {
  171. throw new RepositoryException( "Error", e );
  172. }
  173. }
  174. @Override
  175. public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
  176. return null;
  177. }
  178. @Override
  179. public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
  180. }
  181. @Override
  182. public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
  183. {
  184. ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
  185. configuration.setId( managedRepository.getId( ) );
  186. configuration.setName(managedRepository.getName());
  187. configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
  188. configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
  189. configuration.setDescription( managedRepository.getDescription( ) );
  190. configuration.setLayout( managedRepository.getLayout( ) );
  191. configuration.setScanned( managedRepository.isScanned( ) );
  192. configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
  193. configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
  194. configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
  195. ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
  196. configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
  197. configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
  198. configuration.setRetentionCount( acf.getRetentionCount( ) );
  199. IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
  200. configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
  201. configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
  202. StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
  203. configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
  204. return configuration;
  205. }
  206. @Override
  207. public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
  208. return null;
  209. }
  210. @Override
  211. public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
  212. {
  213. RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
  214. configuration.setId( remoteRepository.getId( ) );
  215. configuration.setName( remoteRepository.getName( ) );
  216. configuration.setDescription( remoteRepository.getDescription( ) );
  217. configuration.setLayout( remoteRepository.getLayout( ) );
  218. configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
  219. configuration.setCheckPath( remoteRepository.getCheckPath( ) );
  220. configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
  221. configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
  222. configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
  223. RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
  224. if (creds!=null)
  225. {
  226. PasswordCredentials pwdCreds = (PasswordCredentials) creds;
  227. configuration.setUsername( pwdCreds.getUsername( ) );
  228. configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
  229. }
  230. configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
  231. RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
  232. configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
  233. configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
  234. configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
  235. configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
  236. return configuration;
  237. }
  238. @Override
  239. public void handle(Event event) {
  240. }
  241. }