request.java

### File Source: request.java

         1  /*
         2   * Copyright (c) 2003-2006, 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
               /*
        P/P     *  Method: net.sourceforge.pebble.logging.Request__static_init
                *
                *  Postconditions:
                *    init'ed(AUTHOR_FEED_REGEX)
                *    init'ed(CATEGORY_FEED_REGEX)
                *    init'ed(RESPONSES_FEED_REGEX)
                *    init'ed(TAG_FEED_REGEX)
                */
        53    private static final Pattern RESPONSES_FEED_REGEX = Pattern.compile("\\/responses\\/.*.xml");
        54    private static final Pattern CATEGORY_FEED_REGEX = Pattern.compile("\\/categories\\/.*\\/.*.xml");
        55    private static final Pattern TAG_FEED_REGEX = Pattern.compile("\\/tags\\/.*\\/.*.xml");
        56    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) {
                 /*
        P/P       *  Method: void net.sourceforge.pebble.logging.Request(String)
                  *
                  *  Postconditions:
                  *    init'ed(this.blog)
                  *    init'ed(this.fileDownload)
                  *    this.logEntries == &new LinkedList(CountedUrl#1)
                  *    possibly_updated(this.name)
                  *    init'ed(this.newsFeed)
                  *    init'ed(this.pageView)
                  *    init'ed(this.url)
                  *    new LinkedList(CountedUrl#1) num objects == 1
                  */
        64      super(url);
        65    }
        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) {
                 /*
        P/P       *  Method: void net.sourceforge.pebble.logging.Request(String, Blog)
                  *
                  *  Postconditions:
                  *    this.blog == blog
                  *    init'ed(this.blog)
                  *    init'ed(this.fileDownload)
                  *    this.logEntries == &new LinkedList(CountedUrl#1)
                  *    possibly_updated(this.name)
                  *    init'ed(this.newsFeed)
                  *    init'ed(this.pageView)
                  *    init'ed(this.url)
                  *    new LinkedList(CountedUrl#1) num objects == 1
                  */
        74      super(url, blog);
        75    }
        76
        77    protected void setUrl(String url) {
                 /*
        P/P       *  Method: void setUrl(String)
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.domain.Blog:getPermalinkProvider(...)@163 != null
                  *
                  *  Preconditions:
                  *    (soft) init'ed(this.name)
                  *    (soft) init'ed(this.blog)
                  *
                  *  Presumptions:
                  *    java.util.regex.Pattern:compile(...)@53 != null
                  *    java.util.regex.Pattern:compile(...)@54 != null
                  *    java.util.regex.Pattern:compile(...)@55 != null
                  *    java.util.regex.Pattern:compile(...)@56 != null
                  *    java.util.regex.Pattern:matcher(...)@82 != null
                  *    ...
                  *
                  *  Postconditions:
                  *    possibly_updated(this.fileDownload)
                  *    init'ed(this.name)
                  *    possibly_updated(this.newsFeed)
                  *    possibly_updated(this.pageView)
                  *    this.url == url
                  *    init'ed(this.url)
                  *
                  *  Test Vectors:
                  *    this.blog: Addr_Set{null}, Inverse{null}
                  *    url: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:endsWith(...)@115: {0}, {1}
                  *    java.lang.String:endsWith(...)@120: {0}, {1}
                  *    java.lang.String:endsWith(...)@127: {0}, {1}
                  *    java.lang.String:endsWith(...)@134: {0}, {1}
                  *    java.lang.String:endsWith(...)@141: {0}, {1}
                  *    java.lang.String:equals(...)@106: {0}, {1}
                  *    java.lang.String:equals(...)@109: {1}, {0}
                  *    java.lang.String:equals(...)@109: {0}, {1}
                  *    ...
                  */
        78      super.setUrl(url);
        79
        80      if (url == null || url.length() == 0) {
        81        setName("None");
        82      } else if (RESPONSES_FEED_REGEX.matcher(url).matches()) {
        83        setName("Feed : Responses");
        84        setNewsFeed(true);
        85      } else if (CATEGORY_FEED_REGEX.matcher(url).matches()) {
        86        String categoryName = url.substring("/categories/".length(), url.lastIndexOf("/"));
        87        setName("Feed : category=" + categoryName);
        88        setNewsFeed(true);
        89      } else if (TAG_FEED_REGEX.matcher(url).matches()) {
        90        String tagName = url.substring("/tags/".length(), url.lastIndexOf("/"));
        91        setName("Feed : tag=" + tagName);
        92        setNewsFeed(true);
        93      } else if (AUTHOR_FEED_REGEX.matcher(url).matches()) {
        94        String authorName = url.substring("/authors/".length(), url.lastIndexOf("/"));
        95        setName("Feed : author=" + authorName);
        96        setNewsFeed(true);
        97      } 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        setName("Feed : Blog Entries");
       104        setNewsFeed(true);
       105      } else if (blog != null) {
       106        if (url.equals("/")) {
       107          setName("Home");
       108          setPageView(true);
       109        } else if (url.equals("/categories/") || url.equals("/categories")) {
       110          setName("Categories");
       111          setPageView(true);
       112        } else if (url.equals("/tags/") || url.equals("/tags")) {
       113          setName("Tags");
       114          setPageView(true);
       115        } else if (url.equals("/files") || (url.startsWith("/files/") && url.endsWith("/"))) {
       116          setName("Files");
       117          setPageView(true);
       118        } else if (url.startsWith("/categories/")) {
       119          String categoryName = url.substring("/categories/".length());
       120          if (categoryName.endsWith("/")) {
       121            categoryName = categoryName.substring(0, categoryName.length()-1);
       122          }
       123          setName("Category : " + categoryName);
       124          setPageView(true);
       125        } else if (url.startsWith("/tags/")) {
       126          String tagName = url.substring("/tags/".length());
       127          if (tagName.endsWith("/")) {
       128            tagName = tagName.substring(0, tagName.length()-1);
       129          }
       130          setName("Tag : " + tagName);
       131          setPageView(true);
       132        } else if (url.startsWith("/authors/")) {
       133          String authorName = url.substring("/authors/".length());
       134          if (authorName.endsWith("/")) {
       135            authorName = authorName.substring(0, authorName.length()-1);
       136          }
       137          setName("Author : " + authorName);
       138          setPageView(true);
       139        } else if (url.startsWith("/files/")) {
       140          String fileName = url.substring("/files/".length());
       141          if (fileName.endsWith("/")) {
       142            fileName = fileName.substring(0, fileName.length()-1);
       143          }
       144          setName("File : " + fileName);
       145          setFileDownload(true);
       146        } else if (url.startsWith("/pages/")) {
       147          String pageName = url.substring("/pages/".length());
       148          setName("Static Page : " + pageName);
       149          setPageView(true);
       150        } else if (url.startsWith("/search.action")) {
       151          setName("Search");
       152          setPageView(true);
       153        } else if (url.startsWith("/blogentries/")) {
       154          String pageNumber = url.substring("/blogentries/".length());
       155          if (pageNumber.indexOf(".") > -1) {
       156  	        pageNumber = pageNumber.substring(0, pageNumber.indexOf("."));
       157  		} else {
       158  			pageNumber = "1";
       159  		}
       160          setName("Blog Entries : Page " + pageNumber);
       161          setPageView(true);
       162        } else {
       163          matchOnPermalinkProvider(url, blog.getPermalinkProvider());
       164
       165          if (getName() == null) {
       166            // try with the default permalink provider
       167            DefaultPermalinkProvider defaultPermalinkProvider = new DefaultPermalinkProvider();
       168            defaultPermalinkProvider.setBlog(blog);
       169            matchOnPermalinkProvider(url, defaultPermalinkProvider);
       170          }
       171        }
       172      }
       173
       174      if (getName() == null) {
       175        setName(url);
       176        setPageView(true);
       177      }
       178    }
       179
       180    private void matchOnPermalinkProvider(String url,
       181        PermalinkProvider permalinkProvider) {
       182      try {
                   /*
        P/P         *  Method: void matchOnPermalinkProvider(String, PermalinkProvider)
                    *
                    *  Test Vectors:
                    *    net.sourceforge.pebble.api.permalink.PermalinkProvider:getBlogEntry(...)@184: Addr_Set{null}, Inverse{null}
                    *    net.sourceforge.pebble.api.permalink.PermalinkProvider:getDay(...)@200: Addr_Set{null}, Inverse{null}
                    *    net.sourceforge.pebble.api.permalink.PermalinkProvider:getMonth(...)@190: Addr_Set{null}, Inverse{null}
                    *    net.sourceforge.pebble.api.permalink.PermalinkProvider:isBlogEntryPermalink(...)@183: {0}, {1}
                    *    net.sourceforge.pebble.api.permalink.PermalinkProvider:isDayPermalink(...)@198: {0}, {1}
                    *    net.sourceforge.pebble.api.permalink.PermalinkProvider:isMonthPermalink(...)@189: {0}, {1}
                    *
                    *  Preconditions:
                    *    (soft) permalinkProvider != null
                    *    (soft) this.blog != null
                    *
                    *  Postconditions:
                    *    possibly_updated(this.name)
                    *    possibly_updated(this.pageView)
                    */
       183        if (permalinkProvider.isBlogEntryPermalink(url)) {
       184          BlogEntry blogEntry = permalinkProvider.getBlogEntry(url);
       185          if (blogEntry != null) {
       186            setName("Blog Entry : " + blogEntry.getTitle());
       187            setPageView(true);
       188          }
       189        } else if (permalinkProvider.isMonthPermalink(url)) {
       190          Month month = permalinkProvider.getMonth(url);
       191          SimpleDateFormat formatter = new SimpleDateFormat("MMMM yyyy", blog
       192              .getLocale());
       193          formatter.setTimeZone(blog.getTimeZone());
       194          if (month != null) {
       195            setName("Month : " + formatter.format(month.getDate()));
       196            setPageView(true);
       197          }
       198        } else if (permalinkProvider.isDayPermalink(url)) {
       199          Day day = null;
       200          day = permalinkProvider.getDay(url);
       201          SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy", blog
       202              .getLocale());
       203          formatter.setTimeZone(blog.getTimeZone());
       204          if (day != null) {
       205            setName("Day : " + formatter.format(day.getDate()));
       206            setPageView(true);
       207          }
       208        }
       209      } catch (IllegalArgumentException e) {
       210        setName("Error: " + url);
       211        setPageView(false);
       212      }
       213    }
       214
       215  }

SofCheck Inspector Build Version : 2.22510

request.java | 2010-Jun-25 19:40:32
request.class | 2010-Jul-19 20:23:38