File Source: OldCommentsRequest.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.ui.rendering.velocity.deprecated;
    20  
    21  import java.util.Enumeration;
    22  import java.util.HashSet;
    23  import java.util.Locale;
    24  import java.util.Set;
    25  import javax.servlet.http.HttpServletRequest;
    26  import org.apache.commons.lang.StringUtils;
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29  
    30  
    31  /**
    32   * Represents an *old* request for a Roller weblog comments permalink.
    33   * 
    34   * any url from ... /comments/*
    35   * 
    36   * While these urls are no longer used we do provide redirect support for them
    37   * for users who have upgraded from earlier versions.  We keep this class to
    38   * help with parsing these urls since they are fairly complex.
    39   */
    40  public class OldCommentsRequest {
    41      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.rendering.velocity.deprecated.OldCommentsRequest__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    42      private static Log log = LogFactory.getLog(OldCommentsRequest.class);
    43      
    44      // various page types
    45      public static final String MAIN = "main";
    46      public static final String PERMALINK = "permalink";
    47      public static final String ARCHIVE = "archive";
    48      
    49      private String context = null;
    50      private String pageType = null;
    51      private String weblogHandle = null;
    52      private String weblogAnchor = null;
    53      private String weblogPage = null;
    54      private String weblogCategory = null;
    55      private String weblogDate = null;
    56      
    57      
    58      /**
    59       * Construct the WeblogPageRequest by parsing the incoming url
    60       */
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.rendering.velocity.deprecated.OldCommentsRequest(HttpServletRequest)
              * 
              *  Preconditions:
              *    log != null
              *    request != null
              * 
              *  Presumptions:
              *    java.lang.String:equals(...)@74 == 1
              *    java.lang.String:length(...)@97 >= 2
              *    javax.servlet.http.HttpServletRequest:getPathInfo(...)@67 != null
              *    javax.servlet.http.HttpServletRequest:getServletPath(...)@66 != null
              * 
              *  Postconditions:
              *    this.context == &"weblog"
              *    this.pageType in Addr_Set{null,&"permalink",&"archive"}
              *    init'ed(this.weblogAnchor)
              *    init'ed(this.weblogCategory)
              *    this.weblogDate == null
              *    this.weblogHandle == null
              *    this.weblogPage == null
              * 
              *  Test Vectors:
              *    javax.servlet.http.HttpServletRequest:getParameter(...)@161: Addr_Set{null}, Inverse{null}
              *    javax.servlet.http.HttpServletRequest:getParameter(...)@166: Addr_Set{null}, Inverse{null}
              *    javax.servlet.http.HttpServletRequest:getParameter(...)@171: Addr_Set{null}, Inverse{null}
              */
    61      public OldCommentsRequest(HttpServletRequest request) throws Exception {
    62          
    63          // parse the request object and figure out what we've got
    64          log.debug("parsing url "+request.getRequestURL());
    65          
    66          String servlet = request.getServletPath();
    67          String pathInfo = request.getPathInfo();
    68          
    69          // make sure this request was destined for the comments servlet
    70          if(servlet != null) {
    71              // strip off the leading slash
    72              servlet = servlet.substring(1);
    73              
    74              if("comments".equals(servlet)) {
    75                  this.context = "weblog";
    76              } else {
    77                  // not a request to the page servlet
    78                  throw new Exception("not a weblog page request, "+request.getRequestURL());
    79              }
    80          } else {
    81              throw new Exception("not a weblog page request, "+request.getRequestURL());
    82          }
    83          
    84          
    85          /*
    86           * parse path info
    87           *
    88           * we expect one of the following forms of urls ...
    89           *
    90           * [handle] - get default page for user for today's date
    91           * [handle]/[date] - get default page for user for specified date
    92           * [handle]/[pagelink] - get specified page for today's date
    93           * [handle]/[pagelink]/[date] - get specified page for specified date
    94           * [handle]/[pagelink]/[anchor] - get specified page & entry (by anchor)
    95           * [handle]/[pagelink]/[date]/[anchor] - get specified page & entry (by anchor)
    96           */
    97          if(pathInfo != null && pathInfo.trim().length() > 1) {
    98              // strip off the leading slash
    99              pathInfo = pathInfo.substring(1);
   100              String[] pathElements = pathInfo.split("/");
   101              
+  102              if ( pathElements.length == 1 ) {
   103                  
   104                  // /handle
+  105                  this.weblogHandle = pathElements[0];
   106                  this.weblogPage = "Weblog";
   107                  this.pageType = MAIN;
   108                  
+  109              } else if ( pathElements.length == 2 ) {
   110                  
   111                  // /handle/date or /handle/page
+  112                  this.weblogHandle = pathElements[0];
   113                  this.weblogPage = "Weblog";
   114                  
   115                  if(this.isValidDateString(pathElements[1])) {
   116                      this.weblogDate = pathElements[1];
   117                      this.pageType = ARCHIVE;
   118                  } else {
   119                      this.weblogPage = pathElements[1];
   120                      this.pageType = MAIN;
   121                  }
   122                  
+  123              } else if ( pathElements.length == 3 ) {
   124                  
   125                  // /handle/page/date or /handle/page/anchor
+  126                  this.weblogHandle = pathElements[0];
   127                  this.weblogPage = pathElements[1];
   128                  
   129                  if(this.isValidDateString(pathElements[2])) {
   130                      this.weblogDate = pathElements[2];
   131                      this.pageType = ARCHIVE;
   132                  } else {
   133                      this.weblogAnchor = pathElements[2];
   134                      this.pageType = PERMALINK;
   135                  }
   136                  
+  137              } else if ( pathElements.length == 4 ) {
   138                  
   139                  // /handle/page/date/anchor
+  140                  this.weblogHandle = pathElements[0];
   141                  this.weblogPage = pathElements[1];
   142                  this.weblogDate = pathElements[2];
   143                  this.weblogAnchor = pathElements[3];
   144                  this.pageType = PERMALINK;
   145              }
   146              
   147          } else {
   148              // invalid request ... path info is empty
   149              throw new Exception("not a weblog page request, "+request.getRequestURL());
   150          }
   151          
   152          
   153          /*
   154           * parse request parameters
   155           *
   156           * the only params we currently care about are:
   157           *   anchor - specifies a weblog entry
   158           *   entry - specifies a weblog entry
   159           *   catname - specifies a weblog category
   160           */
   161          if(request.getParameter("anchor") != null) {
   162              this.weblogAnchor = request.getParameter("anchor");
   163              this.pageType = PERMALINK;
   164          }
   165          
   166          if(request.getParameter("entry") != null) {
   167              this.weblogAnchor = request.getParameter("entry");
   168              this.pageType = PERMALINK;
   169          }
   170          
   171          if(request.getParameter("catname") != null) {
   172              String cat = request.getParameter("catname");
   173              
   174              this.weblogCategory = cat;
   175              this.pageType = ARCHIVE;
   176          }
   177  
   178          // comments only supported permalinks, so if anchor is null then error
+  179          if(this.weblogAnchor == null) {
   180              throw new Exception("invalid comments request, no anchor");
   181          }
   182      }
   183      
   184      
   185      private boolean isValidDateString(String dateString) {
                 /* 
    P/P           *  Method: bool isValidDateString(String)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   186          return (dateString != null && dateString.length() > 3 && StringUtils.isNumeric(dateString));
   187      }
   188  
   189      public String getContext() {
                 /* 
    P/P           *  Method: String getContext()
                  * 
                  *  Preconditions:
                  *    init'ed(this.context)
                  * 
                  *  Postconditions:
                  *    return_value == this.context
                  *    init'ed(return_value)
                  */
   190          return context;
   191      }
   192  
   193      public String getWeblogHandle() {
                 /* 
    P/P           *  Method: String getWeblogHandle()
                  * 
                  *  Preconditions:
                  *    init'ed(this.weblogHandle)
                  * 
                  *  Postconditions:
                  *    return_value == this.weblogHandle
                  *    init'ed(return_value)
                  */
   194          return weblogHandle;
   195      }
   196  
   197      public String getWeblogAnchor() {
                 /* 
    P/P           *  Method: String getWeblogAnchor()
                  * 
                  *  Preconditions:
                  *    init'ed(this.weblogAnchor)
                  * 
                  *  Postconditions:
                  *    return_value == this.weblogAnchor
                  *    init'ed(return_value)
                  */
   198          return weblogAnchor;
   199      }
   200  
   201      public String getWeblogPage() {
                 /* 
    P/P           *  Method: String getWeblogPage()
                  * 
                  *  Preconditions:
                  *    init'ed(this.weblogPage)
                  * 
                  *  Postconditions:
                  *    return_value == this.weblogPage
                  *    init'ed(return_value)
                  */
   202          return weblogPage;
   203      }
   204  
   205      public String getWeblogCategory() {
                 /* 
    P/P           *  Method: String getWeblogCategory()
                  * 
                  *  Preconditions:
                  *    init'ed(this.weblogCategory)
                  * 
                  *  Postconditions:
                  *    return_value == this.weblogCategory
                  *    init'ed(return_value)
                  */
   206          return weblogCategory;
   207      }
   208  
   209      public String getWeblogDate() {
                 /* 
    P/P           *  Method: String getWeblogDate()
                  * 
                  *  Preconditions:
                  *    init'ed(this.weblogDate)
                  * 
                  *  Postconditions:
                  *    return_value == this.weblogDate
                  *    init'ed(return_value)
                  */
   210          return weblogDate;
   211      }
   212  
   213      public String getPageType() {
                 /* 
    P/P           *  Method: String getPageType()
                  * 
                  *  Preconditions:
                  *    init'ed(this.pageType)
                  * 
                  *  Postconditions:
                  *    return_value == this.pageType
                  *    init'ed(return_value)
                  */
   214          return pageType;
   215      }
   216      
   217  }








SofCheck Inspector Build Version : 2.18479
OldCommentsRequest.java 2009-Jan-02 14:24:50
OldCommentsRequest.class 2009-Sep-04 03:12:45