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.

ManagedRepositoryContentMock.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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.common.filelock.DefaultFileLockManager;
  21. import org.apache.archiva.common.utils.VersionUtil;
  22. import org.apache.archiva.metadata.model.ArtifactMetadata;
  23. import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
  24. import org.apache.archiva.model.ArchivaArtifact;
  25. import org.apache.archiva.model.ArtifactReference;
  26. import org.apache.archiva.model.ProjectReference;
  27. import org.apache.archiva.model.VersionedReference;
  28. import org.apache.archiva.repository.*;
  29. import org.apache.archiva.repository.storage.FilesystemStorage;
  30. import org.apache.archiva.repository.storage.RepositoryStorage;
  31. import org.apache.archiva.repository.storage.StorageAsset;
  32. import org.apache.commons.lang.StringUtils;
  33. import org.springframework.stereotype.Service;
  34. import java.io.IOException;
  35. import java.nio.file.Paths;
  36. import java.util.HashMap;
  37. import java.util.Map;
  38. import java.util.Set;
  39. import java.util.regex.Matcher;
  40. import java.util.regex.Pattern;
  41. /**
  42. * @author Martin Stockhammer <martin_s@apache.org>
  43. */
  44. @Service("managedRepositoryContent#mock")
  45. public class ManagedRepositoryContentMock implements ManagedRepositoryContent
  46. {
  47. private static final String PATH_SEPARATOR = "/";
  48. private static final String GROUP_SEPARATOR = ".";
  49. public static final String MAVEN_METADATA = "maven-metadata.xml";
  50. private ManagedRepository repository;
  51. private RepositoryStorage fsStorage;
  52. ManagedRepositoryContentMock(ManagedRepository repo) {
  53. this.repository = repo;
  54. this.fsStorage = repo;
  55. }
  56. @Override
  57. public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException
  58. {
  59. }
  60. @Override
  61. public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException
  62. {
  63. }
  64. @Override
  65. public void deleteGroupId( String groupId ) throws ContentNotFoundException
  66. {
  67. }
  68. @Override
  69. public void deleteProject( String namespace, String projectId ) throws RepositoryException
  70. {
  71. }
  72. @Override
  73. public String getId( )
  74. {
  75. return repository.getId();
  76. }
  77. @Override
  78. public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException
  79. {
  80. return null;
  81. }
  82. @Override
  83. public String getRepoRoot( )
  84. {
  85. return getRepoRootAsset().getFilePath().toString();
  86. }
  87. private StorageAsset getRepoRootAsset() {
  88. if (fsStorage==null) {
  89. try {
  90. fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. return fsStorage.getAsset("");
  96. }
  97. @Override
  98. public ManagedRepository getRepository( )
  99. {
  100. return repository;
  101. }
  102. @Override
  103. public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException
  104. {
  105. return null;
  106. }
  107. @Override
  108. public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException
  109. {
  110. return null;
  111. }
  112. @Override
  113. public boolean hasContent( ArtifactReference reference )
  114. {
  115. return false;
  116. }
  117. @Override
  118. public boolean hasContent( ProjectReference reference )
  119. {
  120. return false;
  121. }
  122. @Override
  123. public boolean hasContent( VersionedReference reference )
  124. {
  125. return false;
  126. }
  127. @Override
  128. public void setRepository( ManagedRepository repo )
  129. {
  130. this.repository = repo;
  131. }
  132. private Map<ArtifactReference, String> refs = new HashMap<>();
  133. @Override
  134. public ArtifactReference toArtifactReference( String path ) throws LayoutException
  135. {
  136. if ( StringUtils.isBlank( path ) )
  137. {
  138. throw new LayoutException( "Unable to convert blank path." );
  139. }
  140. ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
  141. ArtifactReference artifact = new ArtifactReference();
  142. artifact.setGroupId( metadata.getNamespace() );
  143. artifact.setArtifactId( metadata.getProject() );
  144. artifact.setVersion( metadata.getVersion() );
  145. MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
  146. if ( facet != null )
  147. {
  148. artifact.setClassifier( facet.getClassifier() );
  149. artifact.setType( facet.getType() );
  150. }
  151. refs.put(artifact, path);
  152. return artifact;
  153. }
  154. public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
  155. {
  156. String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
  157. int len = parts.length;
  158. if ( len < 4 )
  159. {
  160. throw new IllegalArgumentException(
  161. "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
  162. }
  163. String id = parts[--len];
  164. String baseVersion = parts[--len];
  165. String artifactId = parts[--len];
  166. StringBuilder groupIdBuilder = new StringBuilder();
  167. for ( int i = 0; i < len - 1; i++ )
  168. {
  169. groupIdBuilder.append( parts[i] );
  170. groupIdBuilder.append( '.' );
  171. }
  172. groupIdBuilder.append( parts[len - 1] );
  173. return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
  174. }
  175. private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
  176. public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
  177. String id )
  178. {
  179. if ( !id.startsWith( projectId + "-" ) )
  180. {
  181. throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
  182. + "' doesn't start with artifact ID '" + projectId + "'" );
  183. }
  184. MavenArtifactFacet facet = new MavenArtifactFacet();
  185. int index = projectId.length() + 1;
  186. String version;
  187. String idSubStrFromVersion = id.substring( index );
  188. if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
  189. {
  190. // non-snapshot versions, or non-timestamped snapshot versions
  191. version = projectVersion;
  192. }
  193. else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
  194. {
  195. // timestamped snapshots
  196. try
  197. {
  198. int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
  199. if ( mainVersionLength == 0 )
  200. {
  201. throw new IllegalArgumentException(
  202. "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
  203. }
  204. Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
  205. m.matches();
  206. String timestamp = m.group( 1 );
  207. String buildNumber = m.group( 2 );
  208. facet.setTimestamp( timestamp );
  209. facet.setBuildNumber( Integer.parseInt( buildNumber ) );
  210. version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
  211. }
  212. catch ( IllegalStateException e )
  213. {
  214. throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
  215. + "' doesn't contain a timestamped version matching snapshot '"
  216. + projectVersion + "'", e);
  217. }
  218. }
  219. else
  220. {
  221. // invalid
  222. throw new IllegalArgumentException(
  223. "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
  224. + projectVersion + "'" );
  225. }
  226. String classifier;
  227. String ext;
  228. index += version.length();
  229. if ( index == id.length() )
  230. {
  231. // no classifier or extension
  232. classifier = null;
  233. ext = null;
  234. }
  235. else
  236. {
  237. char c = id.charAt( index );
  238. if ( c == '-' )
  239. {
  240. // classifier up until '.'
  241. int extIndex = id.indexOf( '.', index );
  242. if ( extIndex >= 0 )
  243. {
  244. classifier = id.substring( index + 1, extIndex );
  245. ext = id.substring( extIndex + 1 );
  246. }
  247. else
  248. {
  249. classifier = id.substring( index + 1 );
  250. ext = null;
  251. }
  252. }
  253. else if ( c == '.' )
  254. {
  255. // rest is the extension
  256. classifier = null;
  257. ext = id.substring( index + 1 );
  258. }
  259. else
  260. {
  261. throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
  262. + "' expected classifier or extension but got '"
  263. + id.substring( index ) + "'" );
  264. }
  265. }
  266. ArtifactMetadata metadata = new ArtifactMetadata();
  267. metadata.setId( id );
  268. metadata.setNamespace( namespace );
  269. metadata.setProject( projectId );
  270. metadata.setRepositoryId( repoId );
  271. metadata.setProjectVersion( projectVersion );
  272. metadata.setVersion( version );
  273. facet.setClassifier( classifier );
  274. // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
  275. // to select the correct order to apply multiple extensions mappings to a preferred type
  276. // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
  277. // perhaps the plugins could register missing entries in configuration, then we just use configuration
  278. // here?
  279. String type = null;
  280. // use extension as default
  281. if ( type == null )
  282. {
  283. type = ext;
  284. }
  285. // TODO: should we allow this instead?
  286. if ( type == null )
  287. {
  288. throw new IllegalArgumentException(
  289. "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
  290. }
  291. facet.setType( type );
  292. metadata.addFacet( facet );
  293. return metadata;
  294. }
  295. @Override
  296. public StorageAsset toFile( ArtifactReference reference )
  297. {
  298. return getRepoRootAsset().resolve( refs.get(reference));
  299. }
  300. @Override
  301. public StorageAsset toFile( ArchivaArtifact reference )
  302. {
  303. return null;
  304. }
  305. private String formatAsDirectory( String directory )
  306. {
  307. return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
  308. }
  309. public String toMetadataPath( ProjectReference reference )
  310. {
  311. StringBuilder path = new StringBuilder();
  312. path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
  313. path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
  314. path.append( MAVEN_METADATA );
  315. return path.toString();
  316. }
  317. public String toMetadataPath( VersionedReference reference )
  318. {
  319. StringBuilder path = new StringBuilder();
  320. path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
  321. path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
  322. if ( reference.getVersion() != null )
  323. {
  324. // add the version only if it is present
  325. path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
  326. }
  327. path.append( MAVEN_METADATA );
  328. return path.toString();
  329. }
  330. @Override
  331. public String toPath( ArtifactReference reference )
  332. {
  333. return null;
  334. }
  335. @Override
  336. public String toPath( ArchivaArtifact reference )
  337. {
  338. return null;
  339. }
  340. }