feedaction.java

### File Source: feedaction.java

             /*
        P/P   *  Method: net.sourceforge.pebble.web.action.FeedAction__static_init
              */
         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.action;
        33
        34  import net.sourceforge.pebble.Constants;
        35  import net.sourceforge.pebble.comparator.BlogEntryComparator;
        36  import net.sourceforge.pebble.domain.*;
        37  import net.sourceforge.pebble.web.view.NotModifiedView;
        38  import net.sourceforge.pebble.web.view.View;
        39  import net.sourceforge.pebble.web.view.impl.AtomView;
        40  import net.sourceforge.pebble.web.view.impl.RdfView;
        41  import net.sourceforge.pebble.web.view.impl.RssView;
        42
             /*
        P/P   *  Method: void net.sourceforge.pebble.web.action.FeedAction()
              */
        43  import javax.servlet.ServletException;
        44  import javax.servlet.http.HttpServletRequest;
        45  import javax.servlet.http.HttpServletResponse;
        46  import java.text.SimpleDateFormat;
        47  import java.util.*;
        48
        49  /**
        50   * Gets the RSS for a blog.
        51   *
        52   * @author    Simon Brown
        53   */
        54  public class FeedAction extends Action {
        55
        56    /**
        57     * Peforms the processing associated with this action.
        58     *
        59     * @param request  the HttpServletRequest instance
        60     * @param response the HttpServletResponse instance
        61     * @return the name of the next view
        62     */
        63    public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
                 /*
        P/P       *  Method: View process(HttpServletRequest, HttpServletResponse)
                  *
                  *  Preconditions:
                  *    request != null
                  *    response != null
                  *    this.model != null
                  *    this.model.data != null
                  *
                  *  Presumptions:
                  *    java.util.HashMap:get(...)@63 != null
                  *    java.util.Iterator:next(...)@116 != null
                  *    init'ed(java.util.Locale.ENGLISH)
                  *    net.sourceforge.pebble.domain.AbstractBlog:getLastModified(...)@79 != null
                  *    net.sourceforge.pebble.domain.AbstractBlog:getRecentBlogEntries(...)@110 != null
                  *    ...
                  *
                  *  Postconditions:
                  *    return_value in Addr_Set{&new NotModifiedView(process#5),&new NotModifiedView(process#7),&new AtomView(process#10),&new RdfView(process#11),&new RssView(process#12)}
                  *    new AtomView(process#10) num objects <= 1
                  *    new NotModifiedView(process#5) num objects <= 1
                  *    new NotModifiedView(process#7) num objects <= 1
                  *    new RdfView(process#11) num objects <= 1
                  *    new RssView(process#12) num objects <= 1
                  *
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@83: {0}, {1}
                  *    java.lang.String:equals(...)@85: {0}, {1}
                  *    java.lang.String:equalsIgnoreCase(...)@135: {0}, {1}
                  *    java.lang.String:equalsIgnoreCase(...)@137: {0}, {1}
                  *    java.lang.String:equalsIgnoreCase(...)@73: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@115: {1}, {0}
                  *    javax.servlet.http.HttpServletRequest:getHeader(...)@70: Addr_Set{null}, Inverse{null}
                  *    javax.servlet.http.HttpServletRequest:getHeader(...)@71: Addr_Set{null}, Inverse{null}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@186: Addr_Set{null}, Inverse{null}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@65: Addr_Set{null}, Inverse{null}
                  *    ...
                  */
        64      AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
        65      String flavor = request.getParameter("flavor");
        66
        67      SimpleDateFormat httpFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
        68      httpFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        69
        70      String ifModifiedSince = request.getHeader("If-Modified-Since");
        71      String ifNoneMatch = request.getHeader("If-None-Match");
        72
        73      if (flavor != null && flavor.equalsIgnoreCase("atom")) {
        74        response.setContentType("application/atom+xml; charset=" + blog.getCharacterEncoding());
        75      } else {
        76        response.setContentType("application/xml; charset=" + blog.getCharacterEncoding());
        77      }
        78
        79      Date lastModified = blog.getLastModified();
        80      response.setDateHeader("Last-Modified", lastModified.getTime());
        81      response.setHeader("ETag", "\"" + httpFormat.format(lastModified) + "\"");
        82
        83      if (ifModifiedSince != null && ifModifiedSince.equals(httpFormat.format(lastModified))) {
        84        return new NotModifiedView();
        85      } else if (ifNoneMatch != null && ifNoneMatch.equals("\"" + httpFormat.format(lastModified) + "\"")) {
        86        return new NotModifiedView();
        87      } else {
        88        List blogEntries;
        89        String s = request.getParameter("includeAggregatedContent");
        90        boolean includeAggregatedContent = (s == null || s.equalsIgnoreCase("true"));
        91
        92        if (blog instanceof Blog) {
        93          Tag tag = getTag((Blog)blog, request);
        94          Category category = getCategory((Blog)blog, request);
        95          String author = getAuthor(request);
        96
        97          if (tag != null) {
        98            blogEntries = ((Blog)blog).getRecentPublishedBlogEntries(tag);
        99            getModel().put("tag", tag);
       100          } else if (category != null) {
       101            blogEntries = ((Blog)blog).getRecentPublishedBlogEntries(category);
       102            getModel().put("category", category);
       103          } else if (author != null) {
       104            blogEntries = ((Blog)blog).getRecentPublishedBlogEntries(author);
       105            getModel().put("author", author);
       106          } else {
       107            blogEntries = ((Blog)blog).getRecentPublishedBlogEntries();
       108          }
       109        } else {
       110          blogEntries = blog.getRecentBlogEntries();
       111        }
       112
       113        List blogEntriesForFeed = new ArrayList();
       114        Iterator it = blogEntries.iterator();
       115        while (it.hasNext()) {
       116          BlogEntry entry = (BlogEntry)it.next();
       117
       118          if (!includeAggregatedContent && entry.isAggregated()) {
       119            continue;
       120          } else {
       121            blogEntriesForFeed.add(entry);
       122          }
       123        }
       124
       125        Collections.sort(blogEntriesForFeed, new BlogEntryComparator());
       126
       127        getModel().put(Constants.BLOG_ENTRIES, blogEntriesForFeed);
       128
       129        // set the locale of this feed request to be English
       130        javax.servlet.jsp.jstl.core.Config.set(
       131            request,
       132            javax.servlet.jsp.jstl.core.Config.FMT_LOCALE,
       133            Locale.ENGLISH);
       134
       135        if (flavor != null && flavor.equalsIgnoreCase("atom")) {
       136          return new AtomView();
       137        } else if (flavor != null && flavor.equalsIgnoreCase("rdf")) {
       138          return new RdfView();
       139        } else {
       140          return new RssView();
       141        }
       142      }
       143    }
       144
       145    /**
       146     * Helper method to find a named tag from a request parameter.
       147     *
       148     * @param blog      the blog for which the feed is for
       149     * @param request   the HTTP request containing the tag parameter
       150     * @return  a Tag instance, or null if the tag isn't
       151     *          specified or can't be found
       152     */
       153    private Tag getTag(Blog blog, HttpServletRequest request) {
                 /*
        P/P       *  Method: Tag getTag(Blog, HttpServletRequest)
                  *
                  *  Preconditions:
                  *    request != null
                  *
                  *  Postconditions:
                  *    return_value == One-of{&new Tag(getTag#1), null}
                  *    return_value in Addr_Set{null,&new Tag(getTag#1)}
                  *    new Tag(getTag#1) num objects <= 1
                  *
                  *  Test Vectors:
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@154: Addr_Set{null}, Inverse{null}
                  */
       154      String tag = request.getParameter("tag");
       155      if (tag != null) {
       156        return new Tag(tag, blog);
       157      } else {
       158        return null;
       159      }
       160    }
       161
       162    /**
       163     * Helper method to find a named category from a request parameter.
       164     *
       165     * @param blog      the blog for which the feed is for
       166     * @param request   the HTTP request containing the category parameter
       167     * @return  a Category instance, or null if the category isn't
       168     *          specified or can't be found
       169     */
       170    private Category getCategory(Blog blog, HttpServletRequest request) {
                 /*
        P/P       *  Method: Category getCategory(Blog, HttpServletRequest)
                  *
                  *  Preconditions:
                  *    request != null
                  *    (soft) blog != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *
                  *  Test Vectors:
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@171: Addr_Set{null}, Inverse{null}
                  */
       171      String categoryId = request.getParameter("category");
       172      if (categoryId != null) {
       173        return blog.getCategory(categoryId);
       174      } else {
       175        return null;
       176      }
       177    }
       178
       179    /**
       180     * Helper method to find a named author from a request parameter.
       181     *
       182     * @param request   the HTTP request containing the tag parameter
       183     * @return  a String username, or null if the author isn't specified
       184     */
       185    private String getAuthor(HttpServletRequest request) {
                 /*
        P/P       *  Method: String getAuthor(HttpServletRequest)
                  *
                  *  Preconditions:
                  *    request != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
       186      return request.getParameter("author");
       187    }
       188
       189  }

SofCheck Inspector Build Version : 2.22510

feedaction.java | 2010-Jun-25 19:40:34
feedaction.class | 2010-Jul-19 20:23:38