Coverage Report - org.owasp.dependencycheck.data.nexus.NexusSearch
 
Classes in this File Line Coverage Branch Coverage Complexity
NexusSearch
75%
36/48
58%
7/12
6.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.net.URLConnection;
 25  
 import java.util.logging.Level;
 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.XPathFactory;
 31  
 import org.owasp.dependencycheck.utils.InvalidSettingException;
 32  
 import org.owasp.dependencycheck.utils.Settings;
 33  
 import org.owasp.dependencycheck.utils.URLConnectionFactory;
 34  
 import org.w3c.dom.Document;
 35  
 
 36  
 /**
 37  
  * Class of methods to search Nexus repositories.
 38  
  *
 39  
  * @author colezlaw
 40  
  */
 41  
 public class NexusSearch {
 42  
 
 43  
     /**
 44  
      * The root URL for the Nexus repository service
 45  
      */
 46  
     private final URL rootURL;
 47  
 
 48  
     /**
 49  
      * Whether to use the Proxy when making requests
 50  
      */
 51  
     private boolean useProxy;
 52  
 
 53  
     /**
 54  
      * Used for logging.
 55  
      */
 56  1
     private static final Logger LOGGER = Logger.getLogger(NexusSearch.class
 57  
             .getName());
 58  
 
 59  
     /**
 60  
      * Creates a NexusSearch for the given repository URL.
 61  
      *
 62  
      * @param rootURL the root URL of the repository on which searches should execute. full URL's are calculated
 63  
      * relative to this URL, so it should end with a /
 64  
      */
 65  4
     public NexusSearch(URL rootURL) {
 66  4
         this.rootURL = rootURL;
 67  
         try {
 68  4
             if (null != Settings.getString(Settings.KEYS.PROXY_URL)
 69  
                     && Settings.getBoolean(Settings.KEYS.ANALYZER_NEXUS_PROXY)) {
 70  0
                 useProxy = true;
 71  0
                 LOGGER.fine("Using proxy");
 72  
             } else {
 73  4
                 useProxy = false;
 74  4
                 LOGGER.fine("Not using proxy");
 75  
             }
 76  0
         } catch (InvalidSettingException ise) {
 77  0
             useProxy = false;
 78  4
         }
 79  4
     }
 80  
 
 81  
     /**
 82  
      * Searches the configured Nexus repository for the given sha1 hash. If the artifact is found, a
 83  
      * <code>MavenArtifact</code> is populated with the coordinate information.
 84  
      *
 85  
      * @param sha1 The SHA-1 hash string for which to search
 86  
      * @return the populated Maven coordinates
 87  
      * @throws IOException if it's unable to connect to the specified repositor or if the specified artifact is not
 88  
      * found.
 89  
      */
 90  
     public MavenArtifact searchSha1(String sha1) throws IOException {
 91  4
         if (null == sha1 || !sha1.matches("^[0-9A-Fa-f]{40}$")) {
 92  2
             throw new IllegalArgumentException("Invalid SHA1 format");
 93  
         }
 94  
 
 95  2
         final URL url = new URL(rootURL, String.format("identify/sha1/%s",
 96  
                 sha1.toLowerCase()));
 97  
 
 98  2
         LOGGER.fine(String.format("Searching Nexus url %s", url.toString()));
 99  
 
 100  
         // Determine if we need to use a proxy. The rules:
 101  
         // 1) If the proxy is set, AND the setting is set to true, use the proxy
 102  
         // 2) Otherwise, don't use the proxy (either the proxy isn't configured,
 103  
         // or proxy is specifically
 104  
         // set to false
 105  2
         URLConnection conn = null;
 106  2
         conn = URLConnectionFactory.createHttpURLConnection(url, useProxy);
 107  
 
 108  2
         conn.setDoOutput(true);
 109  
 
 110  
         // JSON would be more elegant, but there's not currently a dependency
 111  
         // on JSON, so don't want to add one just for this
 112  2
         conn.addRequestProperty("Accept", "application/xml");
 113  2
         conn.connect();
 114  
 
 115  
         try {
 116  2
             final DocumentBuilder builder = DocumentBuilderFactory
 117  
                     .newInstance().newDocumentBuilder();
 118  2
             final Document doc = builder.parse(conn.getInputStream());
 119  1
             final XPath xpath = XPathFactory.newInstance().newXPath();
 120  1
             final String groupId = xpath
 121  
                     .evaluate(
 122  
                             "/org.sonatype.nexus.rest.model.NexusArtifact/groupId",
 123  
                             doc);
 124  1
             final String artifactId = xpath.evaluate(
 125  
                     "/org.sonatype.nexus.rest.model.NexusArtifact/artifactId",
 126  
                     doc);
 127  1
             final String version = xpath
 128  
                     .evaluate(
 129  
                             "/org.sonatype.nexus.rest.model.NexusArtifact/version",
 130  
                             doc);
 131  1
             final String link = xpath
 132  
                     .evaluate(
 133  
                             "/org.sonatype.nexus.rest.model.NexusArtifact/artifactLink",
 134  
                             doc);
 135  1
             return new MavenArtifact(groupId, artifactId, version, link);
 136  1
         } catch (FileNotFoundException fnfe) {
 137  
             /* This is what we get when the SHA1 they sent doesn't exist in
 138  
              * Nexus. This is useful upstream for recovery, so we just re-throw it
 139  
              */
 140  1
             throw fnfe;
 141  0
         } catch (Throwable e) {
 142  
             // Anything else is jacked-up XML stuff that we really can't recover
 143  
             // from well
 144  0
             throw new IOException(e.getMessage(), e);
 145  
         }
 146  
     }
 147  
 
 148  
     /**
 149  
      * Do a preflight request to see if the repository is actually working.
 150  
      *
 151  
      * @return whether the repository is listening and returns the /status URL correctly
 152  
      */
 153  
     public boolean preflightRequest() {
 154  
         try {
 155  4
             final HttpURLConnection conn = URLConnectionFactory.createHttpURLConnection(new URL(rootURL, "status"), useProxy);
 156  4
             conn.addRequestProperty("Accept", "application/xml");
 157  4
             conn.connect();
 158  4
             if (conn.getResponseCode() != 200) {
 159  0
                 LOGGER.log(Level.WARNING, "Expected 200 result from Nexus, got {0}", conn.getResponseCode());
 160  0
                 return false;
 161  
             }
 162  4
             final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 163  4
             final Document doc = builder.parse(conn.getInputStream());
 164  4
             if (!"status".equals(doc.getDocumentElement().getNodeName())) {
 165  0
                 LOGGER.log(Level.WARNING, "Expected root node name of status, got {0}", doc.getDocumentElement().getNodeName());
 166  0
                 return false;
 167  
             }
 168  0
         } catch (Throwable e) {
 169  0
             return false;
 170  4
         }
 171  
 
 172  4
         return true;
 173  
     }
 174  
 }
 175  
 
 176  
 // vim: cc=120:sw=4:ts=4:sts=4