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

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