| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.data.nexus; |
| 19 | |
|
| 20 | |
import java.io.FileNotFoundException; |
| 21 | |
import java.io.IOException; |
| 22 | |
import java.net.URL; |
| 23 | |
import java.net.URLConnection; |
| 24 | |
import java.util.logging.Logger; |
| 25 | |
import javax.xml.parsers.DocumentBuilder; |
| 26 | |
import javax.xml.parsers.DocumentBuilderFactory; |
| 27 | |
import javax.xml.xpath.XPath; |
| 28 | |
import javax.xml.xpath.XPathFactory; |
| 29 | |
import org.w3c.dom.Document; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
public class NexusSearch { |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
private final URL rootURL; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 2 | private static final Logger LOGGER = Logger.getLogger(NexusSearch.class.getName()); |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 10 | public NexusSearch(URL rootURL) { |
| 55 | 10 | this.rootURL = rootURL; |
| 56 | 10 | } |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public MavenArtifact searchSha1(String sha1) throws IOException { |
| 68 | 14 | if (null == sha1 || !sha1.matches("^[0-9A-Fa-f]{40}$")) { |
| 69 | 4 | throw new IllegalArgumentException("Invalid SHA1 format"); |
| 70 | |
} |
| 71 | |
|
| 72 | 10 | final URL url = new URL(rootURL, String.format("identify/sha1/%s", sha1.toLowerCase())); |
| 73 | |
|
| 74 | 10 | LOGGER.fine(String.format("Searching Nexus url %s", url.toString())); |
| 75 | |
|
| 76 | 10 | final URLConnection conn = url.openConnection(); |
| 77 | 10 | conn.setDoOutput(true); |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | 10 | conn.addRequestProperty("Accept", "application/xml"); |
| 82 | 10 | conn.connect(); |
| 83 | |
|
| 84 | |
try { |
| 85 | 10 | final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 86 | 10 | final Document doc = builder.parse(conn.getInputStream()); |
| 87 | 10 | final XPath xpath = XPathFactory.newInstance().newXPath(); |
| 88 | 10 | final String groupId = xpath.evaluate("/org.sonatype.nexus.rest.model.NexusArtifact/groupId", doc); |
| 89 | 10 | final String artifactId = xpath.evaluate("/org.sonatype.nexus.rest.model.NexusArtifact/artifactId", doc); |
| 90 | 10 | final String version = xpath.evaluate("/org.sonatype.nexus.rest.model.NexusArtifact/version", doc); |
| 91 | 10 | final String link = xpath.evaluate("/org.sonatype.nexus.rest.model.NexusArtifact/artifactLink", doc); |
| 92 | 10 | return new MavenArtifact(groupId, artifactId, version, link); |
| 93 | 0 | } catch (FileNotFoundException fnfe) { |
| 94 | |
|
| 95 | |
|
| 96 | 0 | throw fnfe; |
| 97 | 0 | } catch (Exception e) { |
| 98 | |
|
| 99 | 0 | throw new IOException(e.getMessage(), e); |
| 100 | |
} |
| 101 | |
} |
| 102 | |
} |
| 103 | |
|
| 104 | |
|