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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.maven2.model.Artifact;
  21. import org.apache.archiva.metadata.model.ArtifactMetadata;
  22. import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
  23. import org.apache.archiva.model.ArtifactReference;
  24. import org.apache.archiva.repository.ManagedRepositoryContent;
  25. import org.apache.archiva.repository.storage.StorageAsset;
  26. import org.apache.archiva.repository.storage.StorageUtil;
  27. import org.apache.commons.io.FilenameUtils;
  28. import java.nio.file.Path;
  29. import java.text.DecimalFormat;
  30. import java.text.DecimalFormatSymbols;
  31. import java.util.Locale;
  32. import java.util.regex.Matcher;
  33. import java.util.regex.Pattern;
  34. /**
  35. * @author Olivier Lamy
  36. * @since 1.4-M3
  37. */
  38. public class ArtifactBuilder
  39. {
  40. private ManagedRepositoryContent managedRepositoryContent;
  41. private ArtifactMetadata artifactMetadata;
  42. public ArtifactBuilder()
  43. {
  44. // no op
  45. }
  46. public ArtifactBuilder withManagedRepositoryContent( ManagedRepositoryContent managedRepositoryContent )
  47. {
  48. this.managedRepositoryContent = managedRepositoryContent;
  49. return this;
  50. }
  51. public ArtifactBuilder forArtifactMetadata( ArtifactMetadata artifactMetadata )
  52. {
  53. this.artifactMetadata = artifactMetadata;
  54. return this;
  55. }
  56. public Artifact build()
  57. {
  58. ArtifactReference ref = new ArtifactReference();
  59. ref.setArtifactId( artifactMetadata.getProject() );
  60. ref.setGroupId( artifactMetadata.getNamespace() );
  61. ref.setVersion( artifactMetadata.getVersion() );
  62. String type = null, classifier = null;
  63. MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
  64. if ( facet != null )
  65. {
  66. type = facet.getType();
  67. classifier = facet.getClassifier();
  68. }
  69. ref.setClassifier( classifier );
  70. ref.setType( type );
  71. StorageAsset file = managedRepositoryContent.toFile( ref );
  72. String extension = getExtensionFromFile(file);
  73. Artifact artifact = new Artifact( ref.getGroupId(), ref.getArtifactId(), ref.getVersion() );
  74. artifact.setRepositoryId( artifactMetadata.getRepositoryId() );
  75. artifact.setClassifier( classifier );
  76. artifact.setPackaging( type );
  77. artifact.setType( type );
  78. artifact.setFileExtension( extension );
  79. artifact.setPath( managedRepositoryContent.toPath( ref ) );
  80. // TODO: find a reusable formatter for this
  81. double s = this.artifactMetadata.getSize();
  82. String symbol = "b";
  83. if ( s > 1024 )
  84. {
  85. symbol = "K";
  86. s /= 1024;
  87. if ( s > 1024 )
  88. {
  89. symbol = "M";
  90. s /= 1024;
  91. if ( s > 1024 )
  92. {
  93. symbol = "G";
  94. s /= 1024;
  95. }
  96. }
  97. }
  98. artifact.setContext( managedRepositoryContent.getId() );
  99. DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) );
  100. artifact.setSize( df.format( s ) + " " + symbol );
  101. artifact.setId( ref.getArtifactId() + "-" + ref.getVersion() + "." + ref.getType() );
  102. return artifact;
  103. }
  104. /**
  105. * Extract file extension
  106. */
  107. String getExtensionFromFile( StorageAsset file )
  108. {
  109. // we are just interested in the section after the last -
  110. String[] parts = file.getName().split( "-" );
  111. if ( parts.length > 0 )
  112. {
  113. // get anything after a dot followed by a letter a-z, including other dots
  114. Pattern p = Pattern.compile( "\\.([a-z]+[a-z0-9\\.]*)" );
  115. Matcher m = p.matcher( parts[parts.length - 1] );
  116. if ( m.find() )
  117. {
  118. return m.group( 1 );
  119. }
  120. }
  121. // just in case
  122. return StorageUtil.getExtension( file );
  123. }
  124. }