| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.data.central; |
| 19 | |
|
| 20 | |
import java.io.FileNotFoundException; |
| 21 | |
import java.io.IOException; |
| 22 | |
import java.net.HttpURLConnection; |
| 23 | |
import java.net.URL; |
| 24 | |
import java.util.ArrayList; |
| 25 | |
import java.util.List; |
| 26 | |
import java.util.logging.Logger; |
| 27 | |
import javax.xml.parsers.DocumentBuilder; |
| 28 | |
import javax.xml.parsers.DocumentBuilderFactory; |
| 29 | |
import javax.xml.xpath.XPath; |
| 30 | |
import javax.xml.xpath.XPathConstants; |
| 31 | |
import javax.xml.xpath.XPathFactory; |
| 32 | |
import org.owasp.dependencycheck.data.nexus.MavenArtifact; |
| 33 | |
import org.owasp.dependencycheck.utils.Settings; |
| 34 | |
import org.owasp.dependencycheck.utils.URLConnectionFactory; |
| 35 | |
import org.w3c.dom.Document; |
| 36 | |
import org.w3c.dom.NodeList; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public class CentralSearch { |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
private final URL rootURL; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
private boolean useProxy; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 1 | private static final Logger LOGGER = Logger.getLogger(CentralSearch.class.getName()); |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | 6 | public CentralSearch(URL rootURL) { |
| 67 | 6 | this.rootURL = rootURL; |
| 68 | 6 | if (null != Settings.getString(Settings.KEYS.PROXY_SERVER)) { |
| 69 | 0 | useProxy = true; |
| 70 | 0 | LOGGER.fine("Using proxy"); |
| 71 | |
} else { |
| 72 | 6 | useProxy = false; |
| 73 | 6 | LOGGER.fine("Not using proxy"); |
| 74 | |
} |
| 75 | 6 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public List<MavenArtifact> searchSha1(String sha1) throws IOException { |
| 87 | 7 | if (null == sha1 || !sha1.matches("^[0-9A-Fa-f]{40}$")) { |
| 88 | 2 | throw new IllegalArgumentException("Invalid SHA1 format"); |
| 89 | |
} |
| 90 | |
|
| 91 | 5 | final URL url = new URL(rootURL + String.format("?q=1:\"%s\"&wt=xml", sha1)); |
| 92 | |
|
| 93 | 5 | LOGGER.fine(String.format("Searching Central url %s", url.toString())); |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | 5 | final HttpURLConnection conn = URLConnectionFactory.createHttpURLConnection(url, useProxy); |
| 100 | |
|
| 101 | 5 | conn.setDoOutput(true); |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | 5 | conn.addRequestProperty("Accept", "application/xml"); |
| 106 | 5 | conn.connect(); |
| 107 | |
|
| 108 | 5 | if (conn.getResponseCode() == 200) { |
| 109 | 5 | boolean missing = false; |
| 110 | |
try { |
| 111 | 5 | final DocumentBuilder builder = DocumentBuilderFactory |
| 112 | |
.newInstance().newDocumentBuilder(); |
| 113 | 5 | final Document doc = builder.parse(conn.getInputStream()); |
| 114 | 5 | final XPath xpath = XPathFactory.newInstance().newXPath(); |
| 115 | 5 | final String numFound = xpath.evaluate("/response/result/@numFound", doc); |
| 116 | 5 | if ("0".equals(numFound)) { |
| 117 | 1 | missing = true; |
| 118 | |
} else { |
| 119 | 4 | final ArrayList<MavenArtifact> result = new ArrayList<MavenArtifact>(); |
| 120 | 4 | final NodeList docs = (NodeList) xpath.evaluate("/response/result/doc", doc, XPathConstants.NODESET); |
| 121 | 9 | for (int i = 0; i < docs.getLength(); i++) { |
| 122 | 5 | final String g = xpath.evaluate("./str[@name='g']", docs.item(i)); |
| 123 | 5 | LOGGER.finest(String.format("GroupId: %s", g)); |
| 124 | 5 | final String a = xpath.evaluate("./str[@name='a']", docs.item(i)); |
| 125 | 5 | LOGGER.finest(String.format("ArtifactId: %s", a)); |
| 126 | 5 | final String v = xpath.evaluate("./str[@name='v']", docs.item(i)); |
| 127 | 5 | LOGGER.finest(String.format("Version: %s", v)); |
| 128 | 5 | result.add(new MavenArtifact(g, a, v, url.toString())); |
| 129 | |
} |
| 130 | |
|
| 131 | 4 | return result; |
| 132 | |
} |
| 133 | 0 | } catch (Throwable e) { |
| 134 | |
|
| 135 | |
|
| 136 | 0 | throw new IOException(e.getMessage(), e); |
| 137 | 1 | } |
| 138 | |
|
| 139 | 1 | if (missing) { |
| 140 | 1 | throw new FileNotFoundException("Artifact not found in Central"); |
| 141 | |
} |
| 142 | 0 | } else { |
| 143 | 0 | final String msg = String.format("Could not connect to Central received response code: %d %s", |
| 144 | |
conn.getResponseCode(), conn.getResponseMessage()); |
| 145 | 0 | LOGGER.fine(msg); |
| 146 | 0 | throw new IOException(msg); |
| 147 | |
} |
| 148 | |
|
| 149 | 0 | return null; |
| 150 | |
} |
| 151 | |
} |