File Source: WeblogTrackbackRequest.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.util;
    20  
    21  import java.io.UnsupportedEncodingException;
    22  import java.net.URLDecoder;
    23  import javax.servlet.http.HttpServletRequest;
    24  import org.apache.commons.logging.Log;
    25  import org.apache.commons.logging.LogFactory;
    26  import org.apache.roller.weblogger.WebloggerException;
    27  import org.apache.roller.weblogger.business.WebloggerFactory;
    28  import org.apache.roller.weblogger.business.WeblogManager;
    29  import org.apache.roller.weblogger.pojos.WeblogEntry;
    30  
    31  
    32  /**
    33   * Represents a request to post a weblog entry trackback.
    34   */
    35  public class WeblogTrackbackRequest extends WeblogRequest {
    36      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.rendering.util.WeblogTrackbackRequest__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    37      private static Log log = LogFactory.getLog(WeblogTrackbackRequest.class);
    38      
    39      private static final String TRACKBACK_SERVLET = "/roller-ui/rendering/trackback";
    40      
    41      // lightweight attributes
    42      private String blogName = null;
    43      private String url = null;
    44      private String excerpt = null;
    45      private String title = null;
    46      private String weblogAnchor = null;
    47      
    48      // heavyweight attributes
    49      private WeblogEntry weblogEntry = null;
    50      
    51      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.rendering.util.WeblogTrackbackRequest()
              * 
              *  Postconditions:
              *    this.authenticUser == null
              *    this.blogName == null
              *    this.excerpt == null
              *    this.locale == null
              *    this.localeInstance == null
              *    this.pathInfo == null
              *    this.request == null
              *    this.title == null
              *    this.url == null
              *    this.user == null
              *    ...
              */
    52      public WeblogTrackbackRequest() {}
    53      
    54      
    55      public WeblogTrackbackRequest(HttpServletRequest request) 
    56              throws InvalidRequestException {
    57          
    58          // let our parent take care of their business first
    59          // parent determines weblog handle and locale if specified
                 /* 
    P/P           *  Method: void org.apache.roller.weblogger.ui.rendering.util.WeblogTrackbackRequest(HttpServletRequest)
                  *    org.apache.roller.weblogger.ui.rendering.util.WeblogTrackbackRequest fails for all possible inputs
                  * 
                  *  Preconditions:
                  *    (soft) org/apache/roller/weblogger/ui/rendering/util/WeblogRequest.log != null
                  *    (soft) request != null
                  * 
                  *  Presumptions:
                  *    java.lang.String:equals(...)@68 == 1
                  *    java.lang.String:length(...)@79 >= 1
                  *    javax.servlet.http.HttpServletRequest:getServletPath(...)@62 != null
                  */
    60          super(request);
    61          
    62          String servlet = request.getServletPath();
    63          
    64          // we only want the path info left over from after our parents parsing
    65          String pathInfo = this.getPathInfo();
    66          
    67          // was this request bound for the comment servlet?
    68          if(servlet == null || !TRACKBACK_SERVLET.equals(servlet)) {
    69              throw new InvalidRequestException("not a weblog trackback request, "+
    70                      request.getRequestURL());
    71          }
    72          
    73          
    74          /*
    75           * parse path info.  we expect ...
    76           *
    77           * /entry/<anchor> - permalink
    78           */
    79          if(pathInfo != null && pathInfo.trim().length() > 0) {
    80              
    81              // we should only ever get 2 path elements
    82              String[] pathElements = pathInfo.split("/");
+   83              if(pathElements.length == 2) {
    84                  
+   85                  String context = pathElements[0];
    86                  if("entry".equals(context)) {
    87                      try {
    88                          this.weblogAnchor = 
    89                                  URLDecoder.decode(pathElements[1], "UTF-8");
    90                      } catch (UnsupportedEncodingException ex) {
    91                          // should never happen
    92                          log.error(ex);
    93                      }
    94                      
    95                  } else {
    96                      throw new InvalidRequestException("bad path info, "+
    97                              request.getRequestURL());
    98                  }
    99                  
   100              } else {
+  101                  throw new InvalidRequestException("bad path info, "+
   102                          request.getRequestURL());
   103              }
   104              
   105          } else {
   106              // bad request
+  107              throw new InvalidRequestException("bad path info, "+
   108                      request.getRequestURL());
   109          }
   110          
   111          
   112          /*
   113           * parse request parameters
   114           *
   115           * the only params we currently care about are:
   116           *   blog_name - comment author
   117           *   url - comment referring url
   118           *   excerpt - comment contents
   119           *   title - comment title
   120           */
   121          if(request.getParameter("blog_name") != null) {
   122              this.blogName = request.getParameter("blog_name");
   123          }
   124          
   125          if(request.getParameter("url") != null) {
   126              this.url = request.getParameter("url");
   127          }
   128          
   129          if(request.getParameter("excerpt") != null) {
   130              this.excerpt = request.getParameter("excerpt");
   131          }
   132          
   133          if(request.getParameter("title") != null) {
   134              this.title = request.getParameter("title");
   135          }
   136          
   137          // a little bit of validation, trackbacks enforce that all params
   138          // must have a value, so any nulls equals a bad request
   139          if(this.blogName == null || this.url == null || 
   140                  this.excerpt == null || this.title == null) {
   141              throw new InvalidRequestException("bad request data.  did not "+
   142                      "receive values for all trackback params (blog_name, url, excerpt, title)");
   143          }
   144          
   145          if(log.isDebugEnabled()) {
   146              log.debug("name = "+this.blogName);
   147              log.debug("url = "+this.url);
   148              log.debug("excerpt = "+this.excerpt);
   149              log.debug("title = "+this.title);
   150              log.debug("weblogAnchor = "+this.weblogAnchor);
   151          }
   152      }
   153  
   154      public String getBlogName() {
                 /* 
    P/P           *  Method: String getBlogName()
                  * 
                  *  Preconditions:
                  *    init'ed(this.blogName)
                  * 
                  *  Postconditions:
                  *    return_value == this.blogName
                  *    init'ed(return_value)
                  */
   155          return blogName;
   156      }
   157  
   158      public void setBlogName(String blogName) {
                 /* 
    P/P           *  Method: void setBlogName(String)
                  * 
                  *  Postconditions:
                  *    this.blogName == blogName
                  *    init'ed(this.blogName)
                  */
   159          this.blogName = blogName;
   160      }
   161  
   162      public String getUrl() {
                 /* 
    P/P           *  Method: String getUrl()
                  * 
                  *  Preconditions:
                  *    init'ed(this.url)
                  * 
                  *  Postconditions:
                  *    return_value == this.url
                  *    init'ed(return_value)
                  */
   163          return url;
   164      }
   165  
   166      public void setUrl(String url) {
                 /* 
    P/P           *  Method: void setUrl(String)
                  * 
                  *  Postconditions:
                  *    this.url == url
                  *    init'ed(this.url)
                  */
   167          this.url = url;
   168      }
   169  
   170      public String getExcerpt() {
                 /* 
    P/P           *  Method: String getExcerpt()
                  * 
                  *  Preconditions:
                  *    init'ed(this.excerpt)
                  * 
                  *  Postconditions:
                  *    return_value == this.excerpt
                  *    init'ed(return_value)
                  */
   171          return excerpt;
   172      }
   173  
   174      public void setExcerpt(String excerpt) {
                 /* 
    P/P           *  Method: void setExcerpt(String)
                  * 
                  *  Postconditions:
                  *    this.excerpt == excerpt
                  *    init'ed(this.excerpt)
                  */
   175          this.excerpt = excerpt;
   176      }
   177  
   178      public String getTitle() {
                 /* 
    P/P           *  Method: String getTitle()
                  * 
                  *  Preconditions:
                  *    init'ed(this.title)
                  * 
                  *  Postconditions:
                  *    return_value == this.title
                  *    init'ed(return_value)
                  */
   179          return title;
   180      }
   181  
   182      public void setTitle(String title) {
                 /* 
    P/P           *  Method: void setTitle(String)
                  * 
                  *  Postconditions:
                  *    this.title == title
                  *    init'ed(this.title)
                  */
   183          this.title = title;
   184      }
   185  
   186      public String getWeblogAnchor() {
                 /* 
    P/P           *  Method: String getWeblogAnchor()
                  * 
                  *  Preconditions:
                  *    init'ed(this.weblogAnchor)
                  * 
                  *  Postconditions:
                  *    return_value == this.weblogAnchor
                  *    init'ed(return_value)
                  */
   187          return weblogAnchor;
   188      }
   189  
   190      public void setWeblogAnchor(String weblogAnchor) {
                 /* 
    P/P           *  Method: void setWeblogAnchor(String)
                  * 
                  *  Postconditions:
                  *    this.weblogAnchor == weblogAnchor
                  *    init'ed(this.weblogAnchor)
                  */
   191          this.weblogAnchor = weblogAnchor;
   192      }
   193  
   194      public WeblogEntry getWeblogEntry() {
   195          
                 /* 
    P/P           *  Method: WeblogEntry getWeblogEntry()
                  * 
                  *  Preconditions:
                  *    init'ed(this.weblogEntry)
                  *    (soft) log != null
                  *    (soft) init'ed(this.weblog)
                  *    (soft) org/apache/roller/weblogger/ui/rendering/util/WeblogRequest.log != null
                  *    (soft) init'ed(this.weblogAnchor)
                  *    (soft) init'ed(this.weblogHandle)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@198 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@198 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  *    this.weblogEntry == return_value
                  *    init'ed(this.weblog)
                  * 
                  *  Test Vectors:
                  *    this.weblogEntry: Inverse{null}, Addr_Set{null}
                  *    this.weblogAnchor: Addr_Set{null}, Inverse{null}
                  */
   196          if(weblogEntry == null && weblogAnchor != null) {
   197              try {
   198                  WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
   199                  weblogEntry = wmgr.getWeblogEntryByAnchor(getWeblog(), weblogAnchor);
   200              } catch (WebloggerException ex) {
   201                  log.error("Error getting weblog entry "+weblogAnchor, ex);
   202              }
   203          }
   204          
   205          return weblogEntry;
   206      }
   207  
   208      public void setWeblogEntry(WeblogEntry weblogEntry) {
                 /* 
    P/P           *  Method: void setWeblogEntry(WeblogEntry)
                  * 
                  *  Postconditions:
                  *    this.weblogEntry == weblogEntry
                  *    init'ed(this.weblogEntry)
                  */
   209          this.weblogEntry = weblogEntry;
   210      }
   211      
   212  }








SofCheck Inspector Build Version : 2.18479
WeblogTrackbackRequest.java 2009-Jan-02 14:25:36
WeblogTrackbackRequest.class 2009-Sep-04 03:12:46