Coverage Report - net.sourceforge.pebble.web.filter.UriTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
UriTransformer
87%
127/145
84%
103/122
21.333
 
 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.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  
  */
 45  172
 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 */
 66  4
   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) {
 78  192
     PermalinkProvider permalinkProvider = blog.getPermalinkProvider();
 79  192
     DefaultPermalinkProvider defaultPermalinkProvider = new DefaultPermalinkProvider();
 80  192
     defaultPermalinkProvider.setBlog(permalinkProvider.getBlog());
 81  
 
 82  192
     log.trace("URI before transformation : " + uri);
 83  
 
 84  192
     if (uri == null || uri.trim().equals("")) {
 85  4
       uri = "/";
 86  
     }
 87  
 
 88  
     // try to transform the URI with the permalink provider in use
 89  192
     String result = getUri(uri, permalinkProvider);
 90  192
     if (result == null) {
 91  
       // for backwards compatibility, try the default permalink provider
 92  172
       result = getUri(uri, defaultPermalinkProvider);
 93  
     }
 94  
 
 95  
     // if the result is still null, try the other URL patterns to transform the URI
 96  192
     if (result == null) {
 97  168
       if (uri.equals("/categories") || uri.equals("/categories/")) {
 98  
         // URI of the form /categories/
 99  8
         result = "/viewCategories.action";
 100  160
       } else if (uri.matches(CATEGORIES_REGEX + ".*\\/.*xml")) {
 101  
           // URI of the form /category[/subcategories]/[rss|rdf|atom].xml
 102  12
           int indexOfLastSlash = uri.lastIndexOf("/");
 103  12
           String categoryId = uri.substring(CATEGORIES.length(), indexOfLastSlash);
 104  
 
 105  12
           if (uri.endsWith("rdf.xml")) {
 106  4
             result = "/feed.action?category=" + categoryId + "&flavor=rdf";
 107  8
           } else if (uri.endsWith("atom.xml")) {
 108  4
             result = "/feed.action?category=" + categoryId + "&flavor=atom";
 109  
           } else {
 110  4
             result = "/feed.action?category=" + categoryId + "&flavor=rss20";
 111  
           }
 112  12
       } else if (uri.startsWith(CATEGORIES)) {
 113  
         // URI of the form /categories/category/
 114  12
         String category = uri.substring(CATEGORIES.length(), uri.length());
 115  12
         if (category.endsWith("/")) {
 116  8
           category = category.substring(0, category.length()-1);
 117  
         }
 118  12
         result = "/viewCategory.action?category=" + category;
 119  12
       } else if (uri.equals("/tags") || uri.equals("/tags/")) {
 120  
           // URI of the form /tags/
 121  8
           result = "/viewTags.action";
 122  128
       } else if (uri.matches(TAGS_REGEX + ".*\\/.*xml")) {
 123  
         // URI of the form /tags/tag/[rss|rdf|atom].xml
 124  12
         int indexOfLastSlash = uri.lastIndexOf("/");
 125  12
         String tag = uri.substring(TAGS.length(), indexOfLastSlash);
 126  
 
 127  12
         if (uri.endsWith("rdf.xml")) {
 128  4
           result = "/feed.action?tag=" + tag + "&flavor=rdf";
 129  8
         } else if (uri.endsWith("atom.xml")) {
 130  4
           result = "/feed.action?tag=" + tag + "&flavor=atom";
 131  
         } else {
 132  4
           result = "/feed.action?tag=" + tag + "&flavor=rss20";
 133  
         }
 134  12
       } else if (uri.matches(AUTHORS_REGEX + ".*\\/.*xml")) {
 135  
         // URI of the form /authors/username/[rss|rdf|atom].xml
 136  12
         int indexOfLastSlash = uri.lastIndexOf("/");
 137  12
         String author = uri.substring(AUTHORS.length(), indexOfLastSlash);
 138  
 
 139  12
         if (uri.endsWith("rdf.xml")) {
 140  4
           result = "/feed.action?author=" + author + "&flavor=rdf";
 141  8
         } else if (uri.endsWith("atom.xml")) {
 142  4
           result = "/feed.action?author=" + author + "&flavor=atom";
 143  
         } else {
 144  4
           result = "/feed.action?author=" + author + "&flavor=rss20";
 145  
         }
 146  12
       } else if (uri.startsWith(TAGS)) {
 147  
         // URI of the form /tags/tag/
 148  8
         String tag = uri.substring(TAGS.length(), uri.length());
 149  8
         if (tag.endsWith("/")) {
 150  4
           tag = tag.substring(0, tag.length()-1);
 151  
         }
 152  8
         result = "/viewTag.action?tag=" + Tag.encode(tag);
 153  8
       } else if (uri.startsWith(AUTHORS)) {
 154  
         // URI of the form /authors/usename/
 155  4
         String author = uri.substring(AUTHORS.length(), uri.length());
 156  4
         if (author.endsWith("/")) {
 157  4
           author = author.substring(0, author.length()-1);
 158  
         }
 159  4
         result = "/aboutAuthor.action?user=" + author;
 160  4
       } else if (uri.equals("/pages/") || uri.equals("/pages")) {
 161  8
         result = "/viewStaticPage.action?name=index";
 162  84
       } else if (uri.startsWith("/pages/")) {
 163  
         // url matches /pages/xyz.html
 164  4
         String name = uri.substring(7, uri.length()-5);
 165  
 
 166  4
         result = "/viewStaticPage.action?name=";
 167  4
         result += name;
 168  4
       } else if (uri.startsWith("/images/")) {
 169  
         // url matches /images/xyz.xyz
 170  8
         String name = uri.substring(7, uri.length());
 171  
 
 172  8
         result = "/file.action?type=" + FileMetaData.BLOG_IMAGE + "&name=";
 173  8
         result += name;
 174  8
       } else if (uri.startsWith("/files/")) {
 175  
         // url matches /files/xyz.xyz
 176  4
         String name = uri.substring(6, uri.length());
 177  
 
 178  4
         result = "/file.action?type=" + FileMetaData.BLOG_FILE + "&name=";
 179  4
         result += name;
 180  4
       } else if (uri.startsWith("/theme/")) {
 181  
         // url matches /files/xyz.xyz
 182  0
         String name = uri.substring(6, uri.length());
 183  
 
 184  0
         result = "/file.action?type=" + FileMetaData.THEME_FILE + "&name=";
 185  0
         result += name;
 186  0
       } else if (uri.matches("\\/help\\/\\w*\\.html")) {
 187  
         // url matches /help/xyz.html
 188  0
         String name = uri.substring(6, uri.length());
 189  
 
 190  0
         result = "/viewHelp.secureaction?name=";
 191  0
         result += name.substring(0, name.length()-5);
 192  0
       } else if (uri.equals("/help") || uri.equals("/help/")) {
 193  
         // url matches /help/
 194  0
         result = "/viewHelp.secureaction?name=index";
 195  68
       } else if (uri.equals("/responses/rss.xml")) {
 196  
         // url is for a response feed
 197  4
         result = "/responseFeed.action?flavor=rss20";
 198  64
       } else if (uri.startsWith("/responses/rss.xml?entry=")) {
 199  
         // url is for a response feed
 200  4
         result = "/responseFeed.action?flavor=rss20&" + uri.substring("/responses/rss.xml?".length());
 201  60
       } else if (uri.startsWith("/rss.xml")) {
 202  
         // url matches rss.xml
 203  8
         result = "/feed.action?flavor=rss20";
 204  52
       } else if (uri.startsWith("/feed.xml")) {
 205  
         // url matches feed.xml
 206  0
         result = "/feed.action?flavor=rss20";
 207  52
       } else if (uri.startsWith("/rdf.xml")) {
 208  
         // url matches rdf.xml
 209  4
         result = "/feed.action?flavor=rdf";
 210  48
       } else if (uri.startsWith("/responses/atom.xml")) {
 211  
         // url is for a response feed
 212  4
         result = "/responseFeed.action?flavor=atom";
 213  44
       } else if (uri.startsWith("/atom.xml")) {
 214  
         // url matches atom.xml
 215  4
         result = "/feed.action?flavor=atom";
 216  40
       } else if (uri.equals("/today.html")) {
 217  
         // URI of the form /today.html
 218  4
         result = "/viewDay.action";
 219  36
       } else if (uri.equals("/about.html")) {
 220  
         // URI of the form /about.html
 221  4
         result = "/about.action";
 222  32
       } else if (uri.startsWith("/blogentries/")) {
 223  
         // view blog entries by page /blogentries/1.html
 224  4
         String page = uri.substring(13, uri.length()-5);
 225  4
         result = "/viewBlogEntriesByPage.action?page=" + page;
 226  4
       } else if (uri.equals("/") || uri.equals("/index.jsp") || uri.equals("/index.html")) {
 227  16
           result = "/viewHomePage.action";
 228  
       } else {
 229  12
         result = uri;
 230  
       }
 231  
     }
 232  
 
 233  192
     log.trace("URI after transformation : " + result);
 234  
 
 235  192
     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  
 
 250  16
     log.trace("URI before transformation : " + uri);
 251  
 
 252  16
     if (uri == null || uri.trim().equals("")) {
 253  0
       uri = "/";
 254  
     }
 255  
 
 256  16
     if (uri.startsWith("/rss.xml")) {
 257  
       // url matches rss.xml
 258  8
       result = "/feed.action?flavor=rss20";
 259  8
     } else if (uri.startsWith("/feed.xml")) {
 260  
       // url matches feed.xml
 261  0
       result = "/feed.action?flavor=rss20";
 262  8
     } else if (uri.startsWith("/rdf.xml")) {
 263  
       // url matches rdf.xml
 264  0
       result = "/feed.action?flavor=rdf";
 265  8
     } else if (uri.startsWith("/atom.xml")) {
 266  
       // url matches atom.xml
 267  0
       result = "/feed.action?flavor=atom";
 268  8
     } else if (uri.equals("/") || uri.equals("/index.jsp") || uri.equals("/index.html")) {
 269  4
         result = "/viewHomePage.action";
 270  4
     } else if (uri.matches("\\/help\\/\\w*\\.html")) {
 271  
       // url matches /help/xyz.html
 272  0
       String name = uri.substring(6, uri.length());
 273  
 
 274  0
       result = "/viewHelp.secureaction?name=";
 275  0
       result += name.substring(0, name.length()-5);
 276  0
     } else {
 277  4
       result = uri;
 278  
     }
 279  
 
 280  16
     log.trace("URI after transformation : " + result);
 281  
 
 282  16
     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) {
 297  364
     String result = null;
 298  
 
 299  364
     if (permalinkProvider.isBlogEntryPermalink(uri)) {
 300  20
       BlogEntry blogEntry = permalinkProvider.getBlogEntry(uri);
 301  20
       if (blogEntry != null) {
 302  16
         result = "/viewBlogEntry.action?entry=" + blogEntry.getId();
 303  
       }
 304  20
     } else if (permalinkProvider.isDayPermalink(uri)) {
 305  4
       Day day = permalinkProvider.getDay(uri);
 306  4
       if (day != null) {
 307  4
         result = "/viewDay.action";
 308  4
         result += "?year=" + day.getMonth().getYear().getYear();
 309  4
         result += "&month=" + day.getMonth().getMonth();
 310  4
         result += "&day=" + day.getDay();
 311  
       }
 312  4
     } else if (permalinkProvider.isMonthPermalink(uri)) {
 313  4
       Month month = permalinkProvider.getMonth(uri);
 314  4
       if (month != null) {
 315  4
         result = "/viewMonth.action";
 316  4
         result += "?year=" + month.getYear().getYear();
 317  4
         result += "&month=" + month.getMonth();
 318  
       }
 319  
     }
 320  
 
 321  364
     return result;
 322  
   }
 323  
 
 324  
 }