PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/opup/src/old/java/com/atlassian/labs/opup/VersionClient.java

https://bitbucket.org/jwalton/opup
Java | 136 lines | 100 code | 27 blank | 9 comment | 11 complexity | b3075d0e29e103fd62c91ae22f8cf85a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.atlassian.labs.opup;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.HashSet;
  7. import java.util.List;
  8. import java.util.Set;
  9. import javax.xml.parsers.DocumentBuilder;
  10. import javax.xml.parsers.DocumentBuilderFactory;
  11. import javax.xml.parsers.ParserConfigurationException;
  12. import javax.xml.xpath.XPath;
  13. import javax.xml.xpath.XPathConstants;
  14. import javax.xml.xpath.XPathExpressionException;
  15. import javax.xml.xpath.XPathFactory;
  16. import org.apache.maven.artifact.versioning.ArtifactVersion;
  17. import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
  18. import org.w3c.dom.Document;
  19. import org.w3c.dom.Element;
  20. import org.w3c.dom.NodeList;
  21. import org.xml.sax.SAXException;
  22. public class VersionClient
  23. {
  24. final HttpCache cache;
  25. final String nexusUrl;
  26. final DocumentBuilder docBuilder;
  27. final VersionOverrides versionOverrides;
  28. public VersionClient(HttpCache cache, String nexusUrl, VersionOverrides overrides) throws ParserConfigurationException
  29. {
  30. this.cache = cache;
  31. this.nexusUrl = nexusUrl;
  32. this.docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  33. this.versionOverrides = overrides;
  34. }
  35. public List<ArtifactVersion> getVersions(String groupId, String artifactId, String packaging, ReportTarget report) throws IOException, XPathExpressionException, ParserConfigurationException, SAXException
  36. {
  37. // String url = "https://m2proxy.atlassian.com/service/local/repositories/maven.atlassian.com-internal/content/";
  38. //
  39. // url += groupId.replace('.', '/');
  40. // url += "/" + artifactId + "/";
  41. String url = nexusUrl + "/service/local/data_index?g=" + groupId + "&a=" + artifactId;
  42. // p= packaging
  43. // c= classifier
  44. InputStream in = cache.get(url);
  45. try
  46. {
  47. return getVersions(in, groupId + ":" + artifactId, packaging, report);
  48. }
  49. finally
  50. {
  51. in.close();
  52. }
  53. }
  54. List<ArtifactVersion> getVersions(InputStream xmlStream, String coords, String requiredPackaging, ReportTarget report) throws XPathExpressionException, SAXException, IOException
  55. {
  56. ArtifactVersion latestVersionWithDifferentPackaging = null;
  57. String differentPackaging = null;
  58. Document doc = docBuilder.parse(xmlStream);
  59. XPath xpath = XPathFactory.newInstance().newXPath();
  60. // NodeList nl = (NodeList) xpath.evaluate("content/data/content-item[leaf='false']", doc, XPathConstants.NODESET);
  61. NodeList nl = (NodeList) xpath.evaluate("search-results/data/artifact", doc, XPathConstants.NODESET);
  62. Set<ArtifactVersion> versions = new HashSet<ArtifactVersion>();
  63. for (int i = 0; i < nl.getLength(); i++)
  64. {
  65. Element e = (Element) nl.item(i);
  66. // TODO Confirm it has the right classifier, packaging etc.
  67. // String text = (String) xpath.evaluate("text", e, XPathConstants.STRING);
  68. String text = (String) xpath.evaluate("version", e, XPathConstants.STRING);
  69. String packaging = (String) xpath.evaluate("packaging", e, XPathConstants.STRING);
  70. DefaultArtifactVersion dav = new DefaultArtifactVersion(text);
  71. if (!versionOverrides.isValid(coords, dav))
  72. {
  73. continue;
  74. }
  75. if ("SNAPSHOT".equals(dav.getQualifier()))
  76. {
  77. continue;
  78. }
  79. if (packaging.equals(requiredPackaging))
  80. {
  81. versions.add(dav);
  82. }
  83. else
  84. {
  85. latestVersionWithDifferentPackaging = dav;
  86. differentPackaging = packaging;
  87. }
  88. }
  89. List<ArtifactVersion> vl = new ArrayList<ArtifactVersion>(versions);
  90. Collections.sort(vl);
  91. if (!vl.isEmpty() && latestVersionWithDifferentPackaging != null && (vl.get(vl.size() - 1).compareTo(latestVersionWithDifferentPackaging) < 0) && report != null)
  92. {
  93. report.packagingChange(coords, latestVersionWithDifferentPackaging, differentPackaging, requiredPackaging, vl.get(vl.size() - 1));
  94. }
  95. return vl;
  96. }
  97. public static List<ArtifactVersion> since(List<ArtifactVersion> versions, ArtifactVersion current)
  98. {
  99. int i = Collections.binarySearch(versions, current);
  100. if (i >= 0)
  101. {
  102. return versions.subList(i + 1, versions.size());
  103. }
  104. else
  105. {
  106. return versions.subList(-i -1, versions.size());
  107. }
  108. }
  109. }