Coverage Report - net.sourceforge.pebble.logging.Referer
 
Classes in this File Line Coverage Branch Coverage Complexity
Referer
85%
52/61
88%
39/44
7.25
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 package net.sourceforge.pebble.logging;
 33  
 
 34  
 import java.net.URLDecoder;
 35  
 import java.util.regex.Matcher;
 36  
 import java.util.regex.Pattern;
 37  
 
 38  
 /**
 39  
  * Represents a referer URL along with a count of how many times
 40  
  * it has been refered from.
 41  
  *
 42  
  * @author    Simon Brown
 43  
  */
 44  
 public class Referer extends CountedUrl {
 45  
 
 46  
   /** regular expression to pull out the query from a Google referer */
 47  4
   private static final Pattern GOOGLE_QUERY_STRING_PATTERN = Pattern.compile("[?&]q=[^&]+&*");
 48  
 
 49  
   /** the prefix for all Google referers */
 50  
   private static final String GOOGLE_PREFIX = "http://www.google.";
 51  
 
 52  
   /** regular expression to pull out the query from a Google referer */
 53  4
   private static final Pattern GOOGLE_IMAGES_QUERY_STRING_PATTERN = Pattern.compile("[?&]prev=[^&]+&*");
 54  
 
 55  
   /** the prefix for all Google imagesearch referers */
 56  
   private static final String GOOGLE_IMAGES_PREFIX = "http://images.google.";
 57  
 
 58  
   /** regular expression to pull out the query from a Yahoo! referer */
 59  4
   private static final Pattern YAHOO_QUERY_STRING_PATTERN = Pattern.compile("[?&]p=[^&]+&*");
 60  
 
 61  
   /** the prefix for all Yahoo! referers */
 62  
   private static final String YAHOO_PREFIX = "http://search.yahoo.";
 63  
 
 64  
   /** regular expression to pull out the query from an MSN referer */
 65  4
   private static final Pattern MSN_QUERY_STRING_PATTERN = Pattern.compile("[?&]q=[^&]+&*");
 66  
 
 67  
   /** the prefix for all MSN referers */
 68  
   private static final String MSN_PREFIX = "http://search.msn.";
 69  
 
 70  
   /** the prefix for all MSN Beta referers */
 71  
   private static final String MSN_BETA_PREFIX = "http://beta.search.msn.";
 72  
 
 73  
   /** regular expression to pull out the query from an BING referer */
 74  4
   private static final Pattern BING_QUERY_STRING_PATTERN = Pattern.compile("[?&]q=[^&]+&*");
 75  
 
 76  
   /** the prefix for all BING referers */
 77  
   private static final String BING_PREFIX = "http://www.bing.com";
 78  
 
 79  
   /** pattern for java.blogs welcome page referers */
 80  4
   private static final Pattern JAVABLOGS_WELCOME_PATTERN = Pattern.compile(".*javablogs.com/Welcome.*");
 81  
 
 82  
   /** pattern for java.blogs hot entries page referers */
 83  4
   private static final Pattern JAVABLOGS_HOT_ENTRIES_PATTERN = Pattern.compile(".*javablogs.com/ViewHotBlogEntries.*");
 84  
 
 85  
   /**
 86  
    * Creates a new instance representing the specified url.
 87  
    *
 88  
    * @param url   the url as a String
 89  
    */
 90  
   public Referer(String url) {
 91  240
     super(url);
 92  240
   }
 93  
 
 94  
   protected void setUrl(String url) {
 95  240
     super.setUrl(url);
 96  
 
 97  240
     if (url == null || url.length() == 0) {
 98  20
       setName("None");
 99  220
     } else if (url.length() > GOOGLE_PREFIX.length() &&
 100  
         url.substring(0, GOOGLE_PREFIX.length()).equalsIgnoreCase(GOOGLE_PREFIX)) {
 101  
 
 102  40
       String query = extractQuery(GOOGLE_QUERY_STRING_PATTERN, url);
 103  40
       setName("Google : " + query);
 104  40
     } else if (url.length() > GOOGLE_IMAGES_PREFIX.length() &&
 105  
         url.substring(0, GOOGLE_IMAGES_PREFIX.length()).equalsIgnoreCase(GOOGLE_IMAGES_PREFIX)) {
 106  
 
 107  8
       String query = extractQuery(GOOGLE_IMAGES_QUERY_STRING_PATTERN, url);
 108  8
       query = extractQuery(GOOGLE_QUERY_STRING_PATTERN, query);
 109  8
       setName("Google Images : " + query);
 110  8
     } else if (url.length() > YAHOO_PREFIX.length() &&
 111  
         url.substring(0, YAHOO_PREFIX.length()).equalsIgnoreCase(YAHOO_PREFIX)) {
 112  
 
 113  28
       String query = extractQuery(YAHOO_QUERY_STRING_PATTERN, url);
 114  28
       setName("Yahoo! : " + query);
 115  28
     } else if (url.length() > MSN_PREFIX.length() &&
 116  
         url.substring(0, MSN_PREFIX.length()).equalsIgnoreCase(MSN_PREFIX)) {
 117  
 
 118  0
       String query = extractQuery(MSN_QUERY_STRING_PATTERN, url);
 119  0
       setName("MSN : " + query);
 120  0
     } else if (url.length() > MSN_BETA_PREFIX.length() &&
 121  
         url.substring(0, MSN_BETA_PREFIX.length()).equalsIgnoreCase(MSN_BETA_PREFIX)) {
 122  
 
 123  0
       String query = extractQuery(MSN_QUERY_STRING_PATTERN, url);
 124  0
       setName("MSN beta : " + query);
 125  0
     } else if (url.length() >= BING_PREFIX.length() &&
 126  
         url.substring(0, BING_PREFIX.length()).equalsIgnoreCase(BING_PREFIX)) {
 127  
 
 128  24
       String query = extractQuery(BING_QUERY_STRING_PATTERN, url);
 129  24
       setName("Bing : " + query);
 130  24
     } else if (JAVABLOGS_WELCOME_PATTERN.matcher(url).matches()) {
 131  16
       setName("java.blogs : Welcome");
 132  104
     } else if (JAVABLOGS_HOT_ENTRIES_PATTERN.matcher(url).matches()) {
 133  4
       setName("java.blogs : Hot Entries");
 134  
     } else {
 135  100
       setName(url);
 136  
     }
 137  240
   }
 138  
 
 139  
   private String extractQuery(Pattern pattern, String url) {
 140  108
     Matcher m = pattern.matcher(url);
 141  108
     String query = "";
 142  108
     if (m.find()) {
 143  68
       int start = m.start();
 144  68
       int end = m.end();
 145  68
       query = url.substring(start+3, end);
 146  68
       if (query.endsWith("&")) {
 147  36
         query = query.substring(0, query.length()-1);
 148  
       }
 149  
       try {
 150  68
         query = URLDecoder.decode(query, "UTF-8");
 151  4
       } catch (Exception e) {
 152  64
       }
 153  
     }
 154  
 
 155  108
     return query;
 156  
   }
 157  
 
 158  
   /**
 159  
    * Gets a regex expression that will filter out other referers with the same domain.
 160  
    *
 161  
    * @return  a regex as a String
 162  
    */
 163  
   public String getDomainFilter() {
 164  
     // and set the domain name
 165  8
     if (getUrl() == null) {
 166  0
       return null;
 167  
     }
 168  
 
 169  8
     int index = getUrl().indexOf("://");
 170  8
     if (index == -1) {
 171  4
       return getUrl();
 172  
     }
 173  
     
 174  4
     String domainName = getUrl().substring(index+3);
 175  4
     index = domainName.indexOf("/");
 176  
 
 177  4
     if (index > -1) {
 178  0
       domainName = domainName.substring(0, index);
 179  
     }
 180  
 
 181  4
     if (domainName.indexOf(":") > -1) {
 182  
       // the domain name still has a port number so remove it
 183  0
       domainName = domainName.substring(0, domainName.indexOf(":"));
 184  
     }
 185  
 
 186  4
     return ".*" + domainName + ".*";
 187  
   }
 188  
 
 189  
 }