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.

RepositoryMetadataWriter.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package org.apache.archiva.repository.metadata;
  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.FileUtils;
  21. import org.apache.archiva.model.ArchivaRepositoryMetadata;
  22. import org.apache.archiva.model.Plugin;
  23. import org.apache.archiva.repository.storage.StorageAsset;
  24. import org.apache.archiva.xml.XMLException;
  25. import org.apache.archiva.xml.XMLWriter;
  26. import org.apache.commons.collections4.CollectionUtils;
  27. import org.apache.commons.lang.StringUtils;
  28. import org.dom4j.Document;
  29. import org.dom4j.DocumentHelper;
  30. import org.dom4j.Element;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import java.io.FileWriter;
  34. import java.io.IOException;
  35. import java.io.OutputStreamWriter;
  36. import java.io.Writer;
  37. import java.nio.file.Path;
  38. import java.util.Collections;
  39. import java.util.Comparator;
  40. import java.util.Iterator;
  41. import java.util.List;
  42. /**
  43. * RepositoryMetadataWriter
  44. */
  45. public class RepositoryMetadataWriter
  46. {
  47. private static final Logger log = LoggerFactory.getLogger(RepositoryMetadataWriter.class);
  48. public static void write( ArchivaRepositoryMetadata metadata, StorageAsset outputFile )
  49. throws RepositoryMetadataException
  50. {
  51. boolean thrown = false;
  52. try (OutputStreamWriter writer = new OutputStreamWriter( outputFile.getWriteStream(true)))
  53. {
  54. write( metadata, writer );
  55. writer.flush();
  56. }
  57. catch ( IOException e )
  58. {
  59. thrown = true;
  60. throw new RepositoryMetadataException(
  61. "Unable to write metadata file: " + outputFile.getPath() + " - " + e.getMessage(), e );
  62. }
  63. finally
  64. {
  65. if ( thrown )
  66. {
  67. try {
  68. outputFile.getStorage().removeAsset(outputFile);
  69. } catch (IOException e) {
  70. log.error("Could not remove asset {}", outputFile);
  71. }
  72. }
  73. }
  74. }
  75. public static void write( ArchivaRepositoryMetadata metadata, Writer writer )
  76. throws RepositoryMetadataException
  77. {
  78. Document doc = DocumentHelper.createDocument();
  79. Element root = DocumentHelper.createElement( "metadata" );
  80. doc.setRootElement( root );
  81. addOptionalElementText( root, "groupId", metadata.getGroupId() );
  82. addOptionalElementText( root, "artifactId", metadata.getArtifactId() );
  83. addOptionalElementText( root, "version", metadata.getVersion() );
  84. if ( CollectionUtils.isNotEmpty( metadata.getPlugins() ) )
  85. {
  86. Element plugins = root.addElement( "plugins" );
  87. List<Plugin> pluginList = metadata.getPlugins();
  88. Collections.sort( pluginList, PluginComparator.INSTANCE );
  89. for ( Plugin plugin : metadata.getPlugins() )
  90. {
  91. Element p = plugins.addElement( "plugin" );
  92. p.addElement( "prefix" ).setText( plugin.getPrefix() );
  93. p.addElement( "artifactId" ).setText( plugin.getArtifactId() );
  94. addOptionalElementText( p, "name", plugin.getName() );
  95. }
  96. }
  97. if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) //
  98. || StringUtils.isNotBlank( metadata.getReleasedVersion() ) //
  99. || StringUtils.isNotBlank( metadata.getLatestVersion() ) //
  100. || StringUtils.isNotBlank( metadata.getLastUpdated() ) //
  101. || ( metadata.getSnapshotVersion() != null ) )
  102. {
  103. Element versioning = root.addElement( "versioning" );
  104. addOptionalElementText( versioning, "latest", metadata.getLatestVersion() );
  105. addOptionalElementText( versioning, "release", metadata.getReleasedVersion() );
  106. if ( metadata.getSnapshotVersion() != null )
  107. {
  108. Element snapshot = versioning.addElement( "snapshot" );
  109. String bnum = String.valueOf( metadata.getSnapshotVersion().getBuildNumber() );
  110. addOptionalElementText( snapshot, "buildNumber", bnum );
  111. addOptionalElementText( snapshot, "timestamp", metadata.getSnapshotVersion().getTimestamp() );
  112. }
  113. if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) )
  114. {
  115. Element versions = versioning.addElement( "versions" );
  116. Iterator<String> it = metadata.getAvailableVersions().iterator();
  117. while ( it.hasNext() )
  118. {
  119. String version = it.next();
  120. versions.addElement( "version" ).setText( version );
  121. }
  122. }
  123. addOptionalElementText( versioning, "lastUpdated", metadata.getLastUpdated() );
  124. }
  125. try
  126. {
  127. XMLWriter.write( doc, writer );
  128. }
  129. catch ( XMLException e )
  130. {
  131. throw new RepositoryMetadataException( "Unable to write xml contents to writer: " + e.getMessage(), e );
  132. }
  133. }
  134. private static void addOptionalElementText( Element elem, String elemName, String text )
  135. {
  136. if ( StringUtils.isBlank( text ) )
  137. {
  138. return;
  139. }
  140. elem.addElement( elemName ).setText( text );
  141. }
  142. private static class PluginComparator
  143. implements Comparator<Plugin>
  144. {
  145. private static final PluginComparator INSTANCE = new PluginComparator();
  146. @Override
  147. public int compare( Plugin plugin, Plugin plugin2 )
  148. {
  149. if ( plugin.getPrefix() != null && plugin2.getPrefix() != null )
  150. {
  151. return plugin.getPrefix().compareTo( plugin2.getPrefix() );
  152. }
  153. if ( plugin.getName() != null && plugin2.getName() != null )
  154. {
  155. return plugin.getName().compareTo( plugin2.getName() );
  156. }
  157. // we assume artifactId is not null which sounds good :-)
  158. return plugin.getArtifactId().compareTo( plugin2.getArtifactId() );
  159. }
  160. }
  161. }