Coverage Report - net.sourceforge.pebble.logging.Request
 
Classes in this File Line Coverage Branch Coverage Complexity
Request
95%
110/115
93%
75/80
11.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 net.sourceforge.pebble.api.permalink.PermalinkProvider;
 35  
 import net.sourceforge.pebble.domain.Blog;
 36  
 import net.sourceforge.pebble.domain.BlogEntry;
 37  
 import net.sourceforge.pebble.domain.Month;
 38  
 import net.sourceforge.pebble.domain.Day;
 39  
 import net.sourceforge.pebble.permalink.DefaultPermalinkProvider;
 40  
 
 41  
 import java.text.SimpleDateFormat;
 42  
 import java.util.regex.Pattern;
 43  
 
 44  
 
 45  
 /**
 46  
  * Represents a requested URL along with a count of how many times it
 47  
  * has been accessed.
 48  
  *
 49  
  * @author    Simon Brown
 50  
  */
 51  
 public class Request extends CountedUrl {
 52  
 
 53  4
   private static final Pattern RESPONSES_FEED_REGEX = Pattern.compile("\\/responses\\/.*.xml");
 54  4
   private static final Pattern CATEGORY_FEED_REGEX = Pattern.compile("\\/categories\\/.*\\/.*.xml");
 55  4
   private static final Pattern TAG_FEED_REGEX = Pattern.compile("\\/tags\\/.*\\/.*.xml");
 56  4
   private static final Pattern AUTHOR_FEED_REGEX = Pattern.compile("\\/authors\\/.*\\/.*.xml");
 57  
 
 58  
   /**
 59  
    * Creates a new instance representing the specified url.
 60  
    *
 61  
    * @param url   the url as a String
 62  
    */
 63  
   public Request(String url) {
 64  180
     super(url);
 65  180
   }
 66  
 
 67  
   /**
 68  
    * Creates a new instance representing the specified url.
 69  
    *
 70  
    * @param url     the url as a String
 71  
    * @param blog    the owning Blog
 72  
    */
 73  
   public Request(String url, Blog blog) {
 74  136
     super(url, blog);
 75  136
   }
 76  
 
 77  
   protected void setUrl(String url) {
 78  316
     super.setUrl(url);
 79  
 
 80  316
     if (url == null || url.length() == 0) {
 81  20
       setName("None");
 82  296
     } else if (RESPONSES_FEED_REGEX.matcher(url).matches()) {
 83  8
       setName("Feed : Responses");
 84  8
       setNewsFeed(true);
 85  288
     } else if (CATEGORY_FEED_REGEX.matcher(url).matches()) {
 86  16
       String categoryName = url.substring("/categories/".length(), url.lastIndexOf("/"));
 87  16
       setName("Feed : category=" + categoryName);
 88  16
       setNewsFeed(true);
 89  16
     } else if (TAG_FEED_REGEX.matcher(url).matches()) {
 90  8
       String tagName = url.substring("/tags/".length(), url.lastIndexOf("/"));
 91  8
       setName("Feed : tag=" + tagName);
 92  8
       setNewsFeed(true);
 93  8
     } else if (AUTHOR_FEED_REGEX.matcher(url).matches()) {
 94  8
       String authorName = url.substring("/authors/".length(), url.lastIndexOf("/"));
 95  8
       setName("Feed : author=" + authorName);
 96  8
       setNewsFeed(true);
 97  8
     } else if (url.indexOf("rss.xml") > -1 ||
 98  
         url.indexOf("feed.xml") > -1 ||
 99  
         url.indexOf("feed.action") > -1 ||
 100  
         url.indexOf("rdf.xml") > -1 ||
 101  
         url.indexOf("atom.xml") > -1
 102  
         ) {
 103  40
       setName("Feed : Blog Entries");
 104  40
       setNewsFeed(true);
 105  216
     } else if (blog != null) {
 106  96
       if (url.equals("/")) {
 107  4
         setName("Home");
 108  4
         setPageView(true);
 109  92
       } else if (url.equals("/categories/") || url.equals("/categories")) {
 110  8
         setName("Categories");
 111  8
         setPageView(true);
 112  84
       } else if (url.equals("/tags/") || url.equals("/tags")) {
 113  8
         setName("Tags");
 114  8
         setPageView(true);
 115  76
       } else if (url.equals("/files") || (url.startsWith("/files/") && url.endsWith("/"))) {
 116  8
         setName("Files");
 117  8
         setPageView(true);
 118  68
       } else if (url.startsWith("/categories/")) {
 119  8
         String categoryName = url.substring("/categories/".length());
 120  8
         if (categoryName.endsWith("/")) {
 121  4
           categoryName = categoryName.substring(0, categoryName.length()-1);
 122  
         }
 123  8
         setName("Category : " + categoryName);
 124  8
         setPageView(true);
 125  8
       } else if (url.startsWith("/tags/")) {
 126  8
         String tagName = url.substring("/tags/".length());
 127  8
         if (tagName.endsWith("/")) {
 128  4
           tagName = tagName.substring(0, tagName.length()-1);
 129  
         }
 130  8
         setName("Tag : " + tagName);
 131  8
         setPageView(true);
 132  8
       } else if (url.startsWith("/authors/")) {
 133  8
         String authorName = url.substring("/authors/".length());
 134  8
         if (authorName.endsWith("/")) {
 135  4
           authorName = authorName.substring(0, authorName.length()-1);
 136  
         }
 137  8
         setName("Author : " + authorName);
 138  8
         setPageView(true);
 139  8
       } else if (url.startsWith("/files/")) {
 140  8
         String fileName = url.substring("/files/".length());
 141  8
         if (fileName.endsWith("/")) {
 142  0
           fileName = fileName.substring(0, fileName.length()-1);
 143  
         }
 144  8
         setName("File : " + fileName);
 145  8
         setFileDownload(true);
 146  8
       } else if (url.startsWith("/pages/")) {
 147  8
         String pageName = url.substring("/pages/".length());
 148  8
         setName("Static Page : " + pageName);
 149  8
         setPageView(true);
 150  8
       } else if (url.startsWith("/search.action")) {
 151  8
         setName("Search");
 152  8
         setPageView(true);
 153  20
       } else if (url.startsWith("/blogentries/")) {
 154  4
         String pageNumber = url.substring("/blogentries/".length());
 155  4
         if (pageNumber.indexOf(".") > -1) {
 156  4
                 pageNumber = pageNumber.substring(0, pageNumber.indexOf("."));
 157  
                 } else {
 158  0
                         pageNumber = "1";
 159  
                 }
 160  4
         setName("Blog Entries : Page " + pageNumber);
 161  4
         setPageView(true);
 162  4
       } else {
 163  16
         matchOnPermalinkProvider(url, blog.getPermalinkProvider());
 164  
 
 165  16
         if (getName() == null) {
 166  
           // try with the default permalink provider
 167  4
           DefaultPermalinkProvider defaultPermalinkProvider = new DefaultPermalinkProvider();
 168  4
           defaultPermalinkProvider.setBlog(blog);
 169  4
           matchOnPermalinkProvider(url, defaultPermalinkProvider);
 170  
         }
 171  
       }
 172  
     }
 173  
 
 174  316
     if (getName() == null) {
 175  120
       setName(url);
 176  120
       setPageView(true);
 177  
     }
 178  316
   }
 179  
 
 180  
   private void matchOnPermalinkProvider(String url,
 181  
       PermalinkProvider permalinkProvider) {
 182  
     try {
 183  20
       if (permalinkProvider.isBlogEntryPermalink(url)) {
 184  12
         BlogEntry blogEntry = permalinkProvider.getBlogEntry(url);
 185  12
         if (blogEntry != null) {
 186  8
           setName("Blog Entry : " + blogEntry.getTitle());
 187  8
           setPageView(true);
 188  
         }
 189  12
       } else if (permalinkProvider.isMonthPermalink(url)) {
 190  4
         Month month = permalinkProvider.getMonth(url);
 191  4
         SimpleDateFormat formatter = new SimpleDateFormat("MMMM yyyy", blog
 192  
             .getLocale());
 193  4
         formatter.setTimeZone(blog.getTimeZone());
 194  4
         if (month != null) {
 195  4
           setName("Month : " + formatter.format(month.getDate()));
 196  4
           setPageView(true);
 197  
         }
 198  4
       } else if (permalinkProvider.isDayPermalink(url)) {
 199  4
         Day day = null;
 200  4
         day = permalinkProvider.getDay(url);
 201  4
         SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy", blog
 202  
             .getLocale());
 203  4
         formatter.setTimeZone(blog.getTimeZone());
 204  4
         if (day != null) {
 205  4
           setName("Day : " + formatter.format(day.getDate()));
 206  4
           setPageView(true);
 207  
         }
 208  
       }
 209  0
     } catch (IllegalArgumentException e) {
 210  0
       setName("Error: " + url);
 211  0
       setPageView(false);
 212  20
     }
 213  20
   }
 214  
 
 215  
 }