Coverage Report - org.owasp.dependencycheck.utils.UrlStringUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
UrlStringUtils
81%
18/22
66%
8/12
2.5
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Dependency-check-core is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-core is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-core. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.utils;
 20  
 
 21  
 import java.net.MalformedURLException;
 22  
 import java.net.URL;
 23  
 import java.util.ArrayList;
 24  
 import java.util.List;
 25  
 import java.util.regex.Pattern;
 26  
 
 27  
 /**
 28  
  *
 29  
  * @author Jeremy Long (jeremy.long@owasp.org)
 30  
  */
 31  
 public final class UrlStringUtils {
 32  
 
 33  
     /**
 34  
      * Private constructor for a utility class.
 35  
      */
 36  0
     private UrlStringUtils() {
 37  0
     }
 38  
     /**
 39  
      * A regular expression to test if a string contains a URL.
 40  
      */
 41  3
     private static final Pattern CONTAINS_URL_TEST = Pattern.compile("^.*(ht|f)tps?://.*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
 42  
     /**
 43  
      * A regular expression to test if a string is a URL.
 44  
      */
 45  3
     private static final Pattern IS_URL_TEST = Pattern.compile("^(ht|f)tps?://.*", Pattern.CASE_INSENSITIVE);
 46  
 
 47  
     /**
 48  
      * Tests if the text provided contains a URL. This is somewhat limited
 49  
      * search in that it only looks for (ftp|http|https)://
 50  
      *
 51  
      * @param text the text to search
 52  
      * @return true if the text contains a url, otherwise false
 53  
      */
 54  
     public static boolean containsUrl(String text) {
 55  59997
         return CONTAINS_URL_TEST.matcher(text).matches();
 56  
     }
 57  
 
 58  
     /**
 59  
      * Tests if the given text is url.
 60  
      *
 61  
      * @param text the string to test
 62  
      * @return returns true if the text is a url, otherwise false
 63  
      */
 64  
     public static boolean isUrl(String text) {
 65  54
         return IS_URL_TEST.matcher(text).matches();
 66  
     }
 67  
 
 68  
     /**
 69  
      * <p>Takes a URL, in String format, and adds the important parts of the URL
 70  
      * to a list of strings.</p>
 71  
      * <p>Example, given the following input:</p>
 72  
      * <code>"https://www.somedomain.com/path1/path2/file.php?id=439"</code>
 73  
      * <p>The function would return:</p>
 74  
      * <code>{"somedomain", "path1", "path2", "file"}</code>
 75  
      *
 76  
      * @param text a URL
 77  
      * @return importantParts a list of the important parts of the URL
 78  
      * @throws MalformedURLException thrown if the URL is malformed
 79  
      */
 80  
     public static List<String> extractImportantUrlData(String text) throws MalformedURLException {
 81  54
         final ArrayList<String> importantParts = new ArrayList<String>();
 82  54
         final URL url = new URL(text);
 83  54
         final String[] domain = url.getHost().split("\\.");
 84  
         //add the domain except www and the tld.
 85  159
         for (int i = 0; i < domain.length - 1; i++) {
 86  105
             final String sub = domain[i];
 87  105
             if (!"www".equalsIgnoreCase(sub)) {
 88  96
                 importantParts.add(sub);
 89  
             }
 90  
         }
 91  54
         final String document = url.getPath();
 92  54
         final String[] pathParts = document.split("[\\//]");
 93  54
         for (int i = 0; i < pathParts.length - 2; i++) {
 94  0
             if (!pathParts[i].isEmpty()) {
 95  0
                 importantParts.add(pathParts[i]);
 96  
             }
 97  
         }
 98  54
         if (pathParts.length > 0 && !pathParts[pathParts.length - 1].isEmpty()) {
 99  45
             final String fileNameNoExt = pathParts[pathParts.length - 1].replaceAll("\\..*{0,5}$", "");
 100  45
             importantParts.add(fileNameNoExt);
 101  
         }
 102  54
         return importantParts;
 103  
     }
 104  
 }