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 12KB

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