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.

ArtifactBuilder.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package org.apache.archiva.rest.services.utils;
  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.maven.model.Artifact;
  21. import org.apache.archiva.metadata.model.ArtifactMetadata;
  22. import org.apache.archiva.maven.metadata.model.MavenArtifactFacet;
  23. import org.apache.archiva.repository.content.BaseRepositoryContentLayout;
  24. import org.apache.archiva.repository.ManagedRepositoryContent;
  25. import org.apache.archiva.repository.content.LayoutException;
  26. import org.apache.archiva.repository.content.base.ArchivaItemSelector;
  27. import org.apache.archiva.repository.storage.StorageAsset;
  28. import org.apache.archiva.repository.storage.util.StorageUtil;
  29. import org.apache.commons.lang3.StringUtils;
  30. import java.text.DecimalFormat;
  31. import java.text.DecimalFormatSymbols;
  32. import java.util.Locale;
  33. import java.util.regex.Matcher;
  34. import java.util.regex.Pattern;
  35. /**
  36. * @author Olivier Lamy
  37. * @since 1.4-M3
  38. */
  39. public class ArtifactBuilder
  40. {
  41. private ManagedRepositoryContent managedRepositoryContent;
  42. private ArtifactMetadata artifactMetadata;
  43. public ArtifactBuilder()
  44. {
  45. // no op
  46. }
  47. public ArtifactBuilder withManagedRepositoryContent( ManagedRepositoryContent managedRepositoryContent )
  48. {
  49. this.managedRepositoryContent = managedRepositoryContent;
  50. return this;
  51. }
  52. public ArtifactBuilder forArtifactMetadata( ArtifactMetadata artifactMetadata )
  53. {
  54. this.artifactMetadata = artifactMetadata;
  55. return this;
  56. }
  57. public Artifact build()
  58. {
  59. String type = null, classifier = null;
  60. MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
  61. if ( facet != null )
  62. {
  63. type = facet.getType();
  64. classifier = facet.getClassifier();
  65. }
  66. ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder( )
  67. .withNamespace( artifactMetadata.getNamespace( ) )
  68. .withProjectId( artifactMetadata.getProject( ) )
  69. .withVersion( artifactMetadata.getProjectVersion( ) )
  70. .withArtifactId( artifactMetadata.getProject( ) )
  71. .withArtifactVersion( artifactMetadata.getVersion( ) );
  72. if (StringUtils.isNotEmpty( type ) ) {
  73. selectorBuilder.withType( type );
  74. }
  75. if (StringUtils.isNotEmpty( classifier )) {
  76. selectorBuilder.withClassifier( classifier );
  77. }
  78. BaseRepositoryContentLayout layout;
  79. try
  80. {
  81. layout = managedRepositoryContent.getLayout( BaseRepositoryContentLayout.class );
  82. }
  83. catch ( LayoutException e )
  84. {
  85. throw new RuntimeException( "Could not convert to layout " + e.getMessage( ) );
  86. }
  87. org.apache.archiva.repository.content.Artifact repoArtifact = layout.getArtifact( selectorBuilder.build( ) );
  88. String extension = repoArtifact.getExtension();
  89. Artifact artifact = new Artifact( repoArtifact.getVersion( ).getProject( ).getNamespace( ).getId( ), repoArtifact.getId( ), repoArtifact.getArtifactVersion( ) );
  90. artifact.setRepositoryId( artifactMetadata.getRepositoryId() );
  91. artifact.setClassifier( classifier );
  92. artifact.setPackaging( type );
  93. artifact.setType( type );
  94. artifact.setFileExtension( extension );
  95. artifact.setPath( managedRepositoryContent.toPath( repoArtifact ) );
  96. // TODO: find a reusable formatter for this
  97. double s = this.artifactMetadata.getSize();
  98. String symbol = "b";
  99. if ( s > 1024 )
  100. {
  101. symbol = "K";
  102. s /= 1024;
  103. if ( s > 1024 )
  104. {
  105. symbol = "M";
  106. s /= 1024;
  107. if ( s > 1024 )
  108. {
  109. symbol = "G";
  110. s /= 1024;
  111. }
  112. }
  113. }
  114. artifact.setContext( managedRepositoryContent.getId() );
  115. DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) );
  116. artifact.setSize( df.format( s ) + " " + symbol );
  117. artifact.setId( repoArtifact.getId() + "-" + repoArtifact.getArtifactVersion() + "." + repoArtifact.getType() );
  118. return artifact;
  119. }
  120. /**
  121. * Extract file extension
  122. */
  123. String getExtensionFromFile( StorageAsset file )
  124. {
  125. // we are just interested in the section after the last -
  126. String[] parts = file.getName().split( "-" );
  127. if ( parts.length > 0 )
  128. {
  129. // get anything after a dot followed by a letter a-z, including other dots
  130. Pattern p = Pattern.compile( "\\.([a-z]+[a-z0-9\\.]*)" );
  131. Matcher m = p.matcher( parts[parts.length - 1] );
  132. if ( m.find() )
  133. {
  134. return m.group( 1 );
  135. }
  136. }
  137. // just in case
  138. return StorageUtil.getExtension( file );
  139. }
  140. }