File Source: uritransformer.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.web.filter;
    33  
    34  import net.sourceforge.pebble.domain.*;
    35  import net.sourceforge.pebble.permalink.DefaultPermalinkProvider;
    36  import net.sourceforge.pebble.api.permalink.PermalinkProvider;
    37  import org.apache.commons.logging.Log;
    38  import org.apache.commons.logging.LogFactory;
    39  
    40  /**
    41   * Responsible for converting an incoming URI to a real URI used by Pebble.
    42   *
    43   * @author    Simon Brown
    44   */
         /* 
    P/P   *  Method: void net.sourceforge.pebble.web.filter.UriTransformer()
          */
    45  public class UriTransformer {
    46  
    47    /** literal used at the start of category URIs */
    48    private static final String CATEGORIES = "/categories";
    49  
    50    /** literal used at the start of category URIs, in regex form */
    51    private static final String CATEGORIES_REGEX = "\\/categories\\/";
    52  
    53    /** literal used at the start of tag URIs */
    54    private static final String TAGS = "/tags/";
    55  
    56    /** literal used at the start of tag URIs, in regex form */
    57    private static final String TAGS_REGEX = "\\/tags\\/";
    58  
    59    /** literal used at the start of author URIs */
    60    private static final String AUTHORS = "/authors/";
    61  
    62    /** literal used at the start of tag URIs, in regex form */
    63    private static final String AUTHORS_REGEX = "\\/authors\\/";
    64  
    65    /** the log used by this class */
           /* 
    P/P     *  Method: net.sourceforge.pebble.web.filter.UriTransformer__static_init
            * 
            *  Postconditions:
            *    init'ed(log)
            */
    66    private static Log log = LogFactory.getLog(UriTransformer.class);
    67  
    68    /**
    69     * Checks for URI patterns and converts them to the appropriate action.
    70     *
    71     * @param uri     the initial URI
    72     * @param blog    the current Blog instance
    73     *
    74     * @return    the URI to used to service the original request (could be
    75     *            the same)
    76     */
    77    public String getUri(String uri, Blog blog) {
             /* 
    P/P       *  Method: String getUri(String, Blog)
              * 
              *  Preconditions:
              *    blog != null
              *    log != null
              * 
              *  Presumptions:
              *    net.sourceforge.pebble.domain.Blog:getPermalinkProvider(...)@78 != null
              * 
              *  Postconditions:
              *    return_value != null
              * 
              *  Test Vectors:
              *    uri: Addr_Set{null}, Inverse{null}
              *    java.lang.String:endsWith(...)@105: {0}, {1}
              *    java.lang.String:endsWith(...)@107: {0}, {1}
              *    java.lang.String:endsWith(...)@115: {0}, {1}
              *    java.lang.String:endsWith(...)@127: {0}, {1}
              *    java.lang.String:endsWith(...)@129: {0}, {1}
              *    java.lang.String:endsWith(...)@139: {0}, {1}
              *    java.lang.String:endsWith(...)@141: {0}, {1}
              *    java.lang.String:endsWith(...)@149: {0}, {1}
              *    java.lang.String:endsWith(...)@156: {0}, {1}
              *    ...
              */
    78      PermalinkProvider permalinkProvider = blog.getPermalinkProvider();
    79      DefaultPermalinkProvider defaultPermalinkProvider = new DefaultPermalinkProvider();
    80      defaultPermalinkProvider.setBlog(permalinkProvider.getBlog());
    81  
    82      log.trace("URI before transformation : " + uri);
    83  
    84      if (uri == null || uri.trim().equals("")) {
    85        uri = "/";
    86      }
    87  
    88      // try to transform the URI with the permalink provider in use
    89      String result = getUri(uri, permalinkProvider);
    90      if (result == null) {
    91        // for backwards compatibility, try the default permalink provider
    92        result = getUri(uri, defaultPermalinkProvider);
    93      }
    94  
    95      // if the result is still null, try the other URL patterns to transform the URI
    96      if (result == null) {
    97        if (uri.equals("/categories") || uri.equals("/categories/")) {
    98          // URI of the form /categories/
    99          result = "/viewCategories.action";
   100        } else if (uri.matches(CATEGORIES_REGEX + ".*\\/.*xml")) {
   101            // URI of the form /category[/subcategories]/[rss|rdf|atom].xml
   102            int indexOfLastSlash = uri.lastIndexOf("/");
   103            String categoryId = uri.substring(CATEGORIES.length(), indexOfLastSlash);
   104  
   105            if (uri.endsWith("rdf.xml")) {
   106              result = "/feed.action?category=" + categoryId + "&flavor=rdf";
   107            } else if (uri.endsWith("atom.xml")) {
   108              result = "/feed.action?category=" + categoryId + "&flavor=atom";
   109            } else {
   110              result = "/feed.action?category=" + categoryId + "&flavor=rss20";
   111            }
   112        } else if (uri.startsWith(CATEGORIES)) {
   113          // URI of the form /categories/category/
   114          String category = uri.substring(CATEGORIES.length(), uri.length());
   115          if (category.endsWith("/")) {
   116            category = category.substring(0, category.length()-1);
   117          }
   118          result = "/viewCategory.action?category=" + category;
   119        } else if (uri.equals("/tags") || uri.equals("/tags/")) {
   120            // URI of the form /tags/
   121            result = "/viewTags.action";
   122        } else if (uri.matches(TAGS_REGEX + ".*\\/.*xml")) {
   123          // URI of the form /tags/tag/[rss|rdf|atom].xml
   124          int indexOfLastSlash = uri.lastIndexOf("/");
   125          String tag = uri.substring(TAGS.length(), indexOfLastSlash);
   126  
   127          if (uri.endsWith("rdf.xml")) {
   128            result = "/feed.action?tag=" + tag + "&flavor=rdf";
   129          } else if (uri.endsWith("atom.xml")) {
   130            result = "/feed.action?tag=" + tag + "&flavor=atom";
   131          } else {
   132            result = "/feed.action?tag=" + tag + "&flavor=rss20";
   133          }
   134        } else if (uri.matches(AUTHORS_REGEX + ".*\\/.*xml")) {
   135          // URI of the form /authors/username/[rss|rdf|atom].xml
   136          int indexOfLastSlash = uri.lastIndexOf("/");
   137          String author = uri.substring(AUTHORS.length(), indexOfLastSlash);
   138  
   139          if (uri.endsWith("rdf.xml")) {
   140            result = "/feed.action?author=" + author + "&flavor=rdf";
   141          } else if (uri.endsWith("atom.xml")) {
   142            result = "/feed.action?author=" + author + "&flavor=atom";
   143          } else {
   144            result = "/feed.action?author=" + author + "&flavor=rss20";
   145          }
   146        } else if (uri.startsWith(TAGS)) {
   147          // URI of the form /tags/tag/
   148          String tag = uri.substring(TAGS.length(), uri.length());
   149          if (tag.endsWith("/")) {
   150            tag = tag.substring(0, tag.length()-1);
   151          }
   152          result = "/viewTag.action?tag=" + Tag.encode(tag);
   153        } else if (uri.startsWith(AUTHORS)) {
   154          // URI of the form /authors/usename/
   155          String author = uri.substring(AUTHORS.length(), uri.length());
   156          if (author.endsWith("/")) {
   157            author = author.substring(0, author.length()-1);
   158          }
   159          result = "/aboutAuthor.action?user=" + author;
   160        } else if (uri.equals("/pages/") || uri.equals("/pages")) {
   161          result = "/viewStaticPage.action?name=index";
   162        } else if (uri.startsWith("/pages/")) {
   163          // url matches /pages/xyz.html
   164          String name = uri.substring(7, uri.length()-5);
   165  
   166          result = "/viewStaticPage.action?name=";
   167          result += name;
   168        } else if (uri.startsWith("/images/")) {
   169          // url matches /images/xyz.xyz
   170          String name = uri.substring(7, uri.length());
   171  
   172          result = "/file.action?type=" + FileMetaData.BLOG_IMAGE + "&name=";
   173          result += name;
   174        } else if (uri.startsWith("/files/")) {
   175          // url matches /files/xyz.xyz
   176          String name = uri.substring(6, uri.length());
   177  
   178          result = "/file.action?type=" + FileMetaData.BLOG_FILE + "&name=";
   179          result += name;
   180        } else if (uri.startsWith("/theme/")) {
   181          // url matches /files/xyz.xyz
   182          String name = uri.substring(6, uri.length());
   183  
   184          result = "/file.action?type=" + FileMetaData.THEME_FILE + "&name=";
   185          result += name;
   186        } else if (uri.matches("\\/help\\/\\w*\\.html")) {
   187          // url matches /help/xyz.html
   188          String name = uri.substring(6, uri.length());
   189  
   190          result = "/viewHelp.secureaction?name=";
   191          result += name.substring(0, name.length()-5);
   192        } else if (uri.equals("/help") || uri.equals("/help/")) {
   193          // url matches /help/
   194          result = "/viewHelp.secureaction?name=index";
   195        } else if (uri.equals("/responses/rss.xml")) {
   196          // url is for a response feed
   197          result = "/responseFeed.action?flavor=rss20";
   198        } else if (uri.startsWith("/responses/rss.xml?entry=")) {
   199          // url is for a response feed
   200          result = "/responseFeed.action?flavor=rss20&" + uri.substring("/responses/rss.xml?".length());
   201        } else if (uri.startsWith("/rss.xml")) {
   202          // url matches rss.xml
   203          result = "/feed.action?flavor=rss20";
   204        } else if (uri.startsWith("/feed.xml")) {
   205          // url matches feed.xml
   206          result = "/feed.action?flavor=rss20";
   207        } else if (uri.startsWith("/rdf.xml")) {
   208          // url matches rdf.xml
   209          result = "/feed.action?flavor=rdf";
   210        } else if (uri.startsWith("/responses/atom.xml")) {
   211          // url is for a response feed
   212          result = "/responseFeed.action?flavor=atom";
   213        } else if (uri.startsWith("/atom.xml")) {
   214          // url matches atom.xml
   215          result = "/feed.action?flavor=atom";
   216        } else if (uri.equals("/today.html")) {
   217          // URI of the form /today.html
   218          result = "/viewDay.action";
   219        } else if (uri.equals("/about.html")) {
   220          // URI of the form /about.html
   221          result = "/about.action";
   222        } else if (uri.startsWith("/blogentries/")) {
   223          // view blog entries by page /blogentries/1.html
   224          String page = uri.substring(13, uri.length()-5);
   225          result = "/viewBlogEntriesByPage.action?page=" + page;
   226        } else if (uri.equals("/") || uri.equals("/index.jsp") || uri.equals("/index.html")) {
   227            result = "/viewHomePage.action";
   228        } else {
   229          result = uri;
   230        }
   231      }
   232  
   233      log.trace("URI after transformation : " + result);
   234  
   235      return result;
   236    }
   237  
   238    /**
   239     * Checks for URI patterns and converts them to the appropriate action.
   240     *
   241     * @param uri     the initial URI
   242     * @param blog    the current Blog instance
   243     *
   244     * @return    the URI to used to service the original request (could be
   245     *            the same)
   246     */
   247    public String getUri(String uri, MultiBlog blog) {
   248      String result;
   249  
             /* 
    P/P       *  Method: String getUri(String, MultiBlog)
              * 
              *  Preconditions:
              *    log != null
              * 
              *  Postconditions:
              *    return_value != null
              * 
              *  Test Vectors:
              *    uri: Addr_Set{null}, Inverse{null}
              *    java.lang.String:equals(...)@252: {0}, {1}
              *    java.lang.String:equals(...)@268: {1}, {0}
              *    java.lang.String:equals(...)@268: {0}, {1}
              *    java.lang.String:matches(...)@270: {0}, {1}
              *    java.lang.String:startsWith(...)@256: {0}, {1}
              *    java.lang.String:startsWith(...)@259: {0}, {1}
              *    java.lang.String:startsWith(...)@262: {0}, {1}
              *    java.lang.String:startsWith(...)@265: {0}, {1}
              */
   250      log.trace("URI before transformation : " + uri);
   251  
   252      if (uri == null || uri.trim().equals("")) {
   253        uri = "/";
   254      }
   255  
   256      if (uri.startsWith("/rss.xml")) {
   257        // url matches rss.xml
   258        result = "/feed.action?flavor=rss20";
   259      } else if (uri.startsWith("/feed.xml")) {
   260        // url matches feed.xml
   261        result = "/feed.action?flavor=rss20";
   262      } else if (uri.startsWith("/rdf.xml")) {
   263        // url matches rdf.xml
   264        result = "/feed.action?flavor=rdf";
   265      } else if (uri.startsWith("/atom.xml")) {
   266        // url matches atom.xml
   267        result = "/feed.action?flavor=atom";
   268      } else if (uri.equals("/") || uri.equals("/index.jsp") || uri.equals("/index.html")) {
   269          result = "/viewHomePage.action";
   270      } else if (uri.matches("\\/help\\/\\w*\\.html")) {
   271        // url matches /help/xyz.html
   272        String name = uri.substring(6, uri.length());
   273  
   274        result = "/viewHelp.secureaction?name=";
   275        result += name.substring(0, name.length()-5);
   276      } else {
   277        result = uri;
   278      }
   279  
   280      log.trace("URI after transformation : " + result);
   281  
   282      return result;
   283    }
   284  
   285    /**
   286     * Checks for URI patterns and converts them to the appropriate action, using
   287     * the specified permalink provider.
   288     *
   289     * @param uri   the initial URI
   290     * @param permalinkProvider   a PermalinkProvider instance to try and
   291     *                            transform the URI with
   292     *
   293     * @return    the URI to used to service the original request (could be
   294     *            the same)
   295     */
   296    private String getUri(String uri, PermalinkProvider permalinkProvider) {
             /* 
    P/P       *  Method: String getUri(String, PermalinkProvider)
              * 
              *  Preconditions:
              *    permalinkProvider != null
              * 
              *  Presumptions:
              *    net.sourceforge.pebble.domain.Day:getMonth(...)@308 != null
              *    net.sourceforge.pebble.domain.Day:getMonth(...)@309 != null
              *    net.sourceforge.pebble.domain.Month:getYear(...)@308 != null
              *    net.sourceforge.pebble.domain.Month:getYear(...)@316 != null
              * 
              *  Postconditions:
              *    init'ed(return_value)
              * 
              *  Test Vectors:
              *    net.sourceforge.pebble.api.permalink.PermalinkProvider:getBlogEntry(...)@300: Addr_Set{null}, Inverse{null}
              *    net.sourceforge.pebble.api.permalink.PermalinkProvider:getDay(...)@305: Addr_Set{null}, Inverse{null}
              *    net.sourceforge.pebble.api.permalink.PermalinkProvider:getMonth(...)@313: Addr_Set{null}, Inverse{null}
              *    net.sourceforge.pebble.api.permalink.PermalinkProvider:isBlogEntryPermalink(...)@299: {0}, {1}
              *    net.sourceforge.pebble.api.permalink.PermalinkProvider:isDayPermalink(...)@304: {0}, {1}
              *    net.sourceforge.pebble.api.permalink.PermalinkProvider:isMonthPermalink(...)@312: {0}, {1}
              */
   297      String result = null;
   298  
   299      if (permalinkProvider.isBlogEntryPermalink(uri)) {
   300        BlogEntry blogEntry = permalinkProvider.getBlogEntry(uri);
   301        if (blogEntry != null) {
   302          result = "/viewBlogEntry.action?entry=" + blogEntry.getId();
   303        }
   304      } else if (permalinkProvider.isDayPermalink(uri)) {
   305        Day day = permalinkProvider.getDay(uri);
   306        if (day != null) {
   307          result = "/viewDay.action";
   308          result += "?year=" + day.getMonth().getYear().getYear();
   309          result += "&month=" + day.getMonth().getMonth();
   310          result += "&day=" + day.getDay();
   311        }
   312      } else if (permalinkProvider.isMonthPermalink(uri)) {
   313        Month month = permalinkProvider.getMonth(uri);
   314        if (month != null) {
   315          result = "/viewMonth.action";
   316          result += "?year=" + month.getYear().getYear();
   317          result += "&month=" + month.getMonth();
   318        }
   319      }
   320  
   321      return result;
   322    }
   323  
   324  }








SofCheck Inspector Build Version : 2.22510
uritransformer.java 2010-Jun-25 19:40:32
uritransformer.class 2010-Jul-19 20:23:38