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.6KB

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