Coverage Report - org.owasp.dependencycheck.data.nexus.NexusSearch
 
Classes in this File Line Coverage Branch Coverage Complexity
NexusSearch
0%
0/57
0%
0/24
8.667
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2014 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.data.nexus;
 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.logging.Level;
 25  
 import java.util.logging.Logger;
 26  
 import javax.xml.parsers.DocumentBuilder;
 27  
 import javax.xml.parsers.DocumentBuilderFactory;
 28  
 import javax.xml.xpath.XPath;
 29  
 import javax.xml.xpath.XPathFactory;
 30  
 import org.owasp.dependencycheck.utils.InvalidSettingException;
 31  
 import org.owasp.dependencycheck.utils.Settings;
 32  
 import org.owasp.dependencycheck.utils.URLConnectionFactory;
 33  
 import org.w3c.dom.Document;
 34  
 
 35  
 /**
 36  
  * Class of methods to search Nexus repositories.
 37  
  *
 38  
  * @author colezlaw
 39  
  */
 40  
 public class NexusSearch {
 41  
 
 42  
     /**
 43  
      * The root URL for the Nexus repository service
 44  
      */
 45  
     private final URL rootURL;
 46  
 
 47  
     /**
 48  
      * Whether to use the Proxy when making requests
 49  
      */
 50  
     private boolean useProxy;
 51  
 
 52  
     /**
 53  
      * Used for logging.
 54  
      */
 55  0
     private static final Logger LOGGER = Logger.getLogger(NexusSearch.class
 56  
             .getName());
 57  
 
 58  
     /**
 59  
      * Creates a NexusSearch for the given repository URL.
 60  
      *
 61  
      * @param rootURL the root URL of the repository on which searches should execute. full URL's are calculated relative to this
 62  
      * URL, so it should end with a /
 63  
      */
 64  0
     public NexusSearch(URL rootURL) {
 65  0
         this.rootURL = rootURL;
 66  
         try {
 67  0
             if (null != Settings.getString(Settings.KEYS.PROXY_SERVER)
 68  
                     && Settings.getBoolean(Settings.KEYS.ANALYZER_NEXUS_PROXY)) {
 69  0
                 useProxy = true;
 70  0
                 LOGGER.fine("Using proxy");
 71  
             } else {
 72  0
                 useProxy = false;
 73  0
                 LOGGER.fine("Not using proxy");
 74  
             }
 75  0
         } catch (InvalidSettingException ise) {
 76  0
             useProxy = false;
 77  0
         }
 78  0
     }
 79  
 
 80  
     /**
 81  
      * Searches the configured Nexus repository for the given sha1 hash. If the artifact is found, a <code>MavenArtifact</code> is
 82  
      * populated with the coordinate information.
 83  
      *
 84  
      * @param sha1 The SHA-1 hash string for which to search
 85  
      * @return the populated Maven coordinates
 86  
      * @throws IOException if it's unable to connect to the specified repository or if the specified artifact is not found.
 87  
      */
 88  
     public MavenArtifact searchSha1(String sha1) throws IOException {
 89  0
         if (null == sha1 || !sha1.matches("^[0-9A-Fa-f]{40}$")) {
 90  0
             throw new IllegalArgumentException("Invalid SHA1 format");
 91  
         }
 92  
 
 93  0
         final URL url = new URL(rootURL, String.format("identify/sha1/%s",
 94  
                 sha1.toLowerCase()));
 95  
 
 96  0
         LOGGER.fine(String.format("Searching Nexus url %s", url.toString()));
 97  
 
 98  
         // Determine if we need to use a proxy. The rules:
 99  
         // 1) If the proxy is set, AND the setting is set to true, use the proxy
 100  
         // 2) Otherwise, don't use the proxy (either the proxy isn't configured,
 101  
         // or proxy is specifically
 102  
         // set to false
 103  0
         final HttpURLConnection conn = URLConnectionFactory.createHttpURLConnection(url, useProxy);
 104  
 
 105  0
         conn.setDoOutput(true);
 106  
 
 107  
         // JSON would be more elegant, but there's not currently a dependency
 108  
         // on JSON, so don't want to add one just for this
 109  0
         conn.addRequestProperty("Accept", "application/xml");
 110  0
         conn.connect();
 111  
 
 112  0
         if (conn.getResponseCode() == 200) {
 113  
             try {
 114  0
                 final DocumentBuilder builder = DocumentBuilderFactory
 115  
                         .newInstance().newDocumentBuilder();
 116  0
                 final Document doc = builder.parse(conn.getInputStream());
 117  0
                 final XPath xpath = XPathFactory.newInstance().newXPath();
 118  0
                 final String groupId = xpath
 119  
                         .evaluate(
 120  
                                 "/org.sonatype.nexus.rest.model.NexusArtifact/groupId",
 121  
                                 doc);
 122  0
                 final String artifactId = xpath.evaluate(
 123  
                         "/org.sonatype.nexus.rest.model.NexusArtifact/artifactId",
 124  
                         doc);
 125  0
                 final String version = xpath
 126  
                         .evaluate(
 127  
                                 "/org.sonatype.nexus.rest.model.NexusArtifact/version",
 128  
                                 doc);
 129  0
                 final String link = xpath
 130  
                         .evaluate(
 131  
                                 "/org.sonatype.nexus.rest.model.NexusArtifact/artifactLink",
 132  
                                 doc);
 133  0
                 final String pomLink = xpath
 134  
                         .evaluate(
 135  
                                 "/org.sonatype.nexus.rest.model.NexusArtifact/pomLink",
 136  
                                 doc);
 137  0
                 final MavenArtifact ma = new MavenArtifact(groupId, artifactId, version);
 138  0
                 if (link != null && !"".equals(link)) {
 139  0
                     ma.setArtifactUrl(link);
 140  
                 }
 141  0
                 if (pomLink != null && !"".equals(pomLink)) {
 142  0
                     ma.setPomUrl(pomLink);
 143  
                 }
 144  0
                 return ma;
 145  0
             } catch (Throwable e) {
 146  
                 // Anything else is jacked-up XML stuff that we really can't recover
 147  
                 // from well
 148  0
                 throw new IOException(e.getMessage(), e);
 149  
             }
 150  0
         } else if (conn.getResponseCode() == 404) {
 151  0
             throw new FileNotFoundException("Artifact not found in Nexus");
 152  
         } else {
 153  0
             final String msg = String.format("Could not connect to Nexus received response code: %d %s",
 154  
                     conn.getResponseCode(), conn.getResponseMessage());
 155  0
             LOGGER.fine(msg);
 156  0
             throw new IOException(msg);
 157  
         }
 158  
     }
 159  
 
 160  
     /**
 161  
      * Do a preflight request to see if the repository is actually working.
 162  
      *
 163  
      * @return whether the repository is listening and returns the /status URL correctly
 164  
      */
 165  
     public boolean preflightRequest() {
 166  
         try {
 167  0
             final HttpURLConnection conn = URLConnectionFactory.createHttpURLConnection(new URL(rootURL, "status"), useProxy);
 168  0
             conn.addRequestProperty("Accept", "application/xml");
 169  0
             conn.connect();
 170  0
             if (conn.getResponseCode() != 200) {
 171  0
                 LOGGER.log(Level.WARNING, "Expected 200 result from Nexus, got {0}", conn.getResponseCode());
 172  0
                 return false;
 173  
             }
 174  0
             final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 175  0
             final Document doc = builder.parse(conn.getInputStream());
 176  0
             if (!"status".equals(doc.getDocumentElement().getNodeName())) {
 177  0
                 LOGGER.log(Level.WARNING, "Expected root node name of status, got {0}", doc.getDocumentElement().getNodeName());
 178  0
                 return false;
 179  
             }
 180  0
         } catch (Throwable e) {
 181  0
             return false;
 182  0
         }
 183  
 
 184  0
         return true;
 185  
     }
 186  
 }
 187  
 
 188  
 // vim: cc=120:sw=4:ts=4:sts=4