/opup/src/old/java/com/atlassian/labs/opup/VersionClient.java
Java | 136 lines | 100 code | 27 blank | 9 comment | 11 complexity | b3075d0e29e103fd62c91ae22f8cf85a MD5 | raw file
Possible License(s): BSD-3-Clause
- package com.atlassian.labs.opup;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.xpath.XPath;
- import javax.xml.xpath.XPathConstants;
- import javax.xml.xpath.XPathExpressionException;
- import javax.xml.xpath.XPathFactory;
- import org.apache.maven.artifact.versioning.ArtifactVersion;
- import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- public class VersionClient
- {
- final HttpCache cache;
- final String nexusUrl;
- final DocumentBuilder docBuilder;
- final VersionOverrides versionOverrides;
- public VersionClient(HttpCache cache, String nexusUrl, VersionOverrides overrides) throws ParserConfigurationException
- {
- this.cache = cache;
- this.nexusUrl = nexusUrl;
- this.docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- this.versionOverrides = overrides;
- }
- public List<ArtifactVersion> getVersions(String groupId, String artifactId, String packaging, ReportTarget report) throws IOException, XPathExpressionException, ParserConfigurationException, SAXException
- {
- // String url = "https://m2proxy.atlassian.com/service/local/repositories/maven.atlassian.com-internal/content/";
- //
- // url += groupId.replace('.', '/');
- // url += "/" + artifactId + "/";
- String url = nexusUrl + "/service/local/data_index?g=" + groupId + "&a=" + artifactId;
- // p= packaging
- // c= classifier
- InputStream in = cache.get(url);
- try
- {
- return getVersions(in, groupId + ":" + artifactId, packaging, report);
- }
- finally
- {
- in.close();
- }
- }
- List<ArtifactVersion> getVersions(InputStream xmlStream, String coords, String requiredPackaging, ReportTarget report) throws XPathExpressionException, SAXException, IOException
- {
- ArtifactVersion latestVersionWithDifferentPackaging = null;
- String differentPackaging = null;
- Document doc = docBuilder.parse(xmlStream);
- XPath xpath = XPathFactory.newInstance().newXPath();
- // NodeList nl = (NodeList) xpath.evaluate("content/data/content-item[leaf='false']", doc, XPathConstants.NODESET);
- NodeList nl = (NodeList) xpath.evaluate("search-results/data/artifact", doc, XPathConstants.NODESET);
- Set<ArtifactVersion> versions = new HashSet<ArtifactVersion>();
- for (int i = 0; i < nl.getLength(); i++)
- {
- Element e = (Element) nl.item(i);
- // TODO Confirm it has the right classifier, packaging etc.
- // String text = (String) xpath.evaluate("text", e, XPathConstants.STRING);
- String text = (String) xpath.evaluate("version", e, XPathConstants.STRING);
- String packaging = (String) xpath.evaluate("packaging", e, XPathConstants.STRING);
- DefaultArtifactVersion dav = new DefaultArtifactVersion(text);
- if (!versionOverrides.isValid(coords, dav))
- {
- continue;
- }
- if ("SNAPSHOT".equals(dav.getQualifier()))
- {
- continue;
- }
- if (packaging.equals(requiredPackaging))
- {
- versions.add(dav);
- }
- else
- {
- latestVersionWithDifferentPackaging = dav;
- differentPackaging = packaging;
- }
- }
- List<ArtifactVersion> vl = new ArrayList<ArtifactVersion>(versions);
- Collections.sort(vl);
- if (!vl.isEmpty() && latestVersionWithDifferentPackaging != null && (vl.get(vl.size() - 1).compareTo(latestVersionWithDifferentPackaging) < 0) && report != null)
- {
- report.packagingChange(coords, latestVersionWithDifferentPackaging, differentPackaging, requiredPackaging, vl.get(vl.size() - 1));
- }
- return vl;
- }
- public static List<ArtifactVersion> since(List<ArtifactVersion> versions, ArtifactVersion current)
- {
- int i = Collections.binarySearch(versions, current);
- if (i >= 0)
- {
- return versions.subList(i + 1, versions.size());
- }
- else
- {
- return versions.subList(-i -1, versions.size());
- }
- }
- }