Coverage Report - net.sourceforge.pebble.web.action.FeedAction
 
Classes in this File Line Coverage Branch Coverage Complexity
FeedAction
82%
38/46
50%
16/32
5
 
 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.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.service.LastModifiedService;
 38  
 import net.sourceforge.pebble.web.view.NotModifiedView;
 39  
 import net.sourceforge.pebble.web.view.View;
 40  
 import net.sourceforge.pebble.web.view.impl.AbstractRomeFeedView;
 41  
 import net.sourceforge.pebble.web.view.impl.FeedView;
 42  
 import net.sourceforge.pebble.web.view.impl.RdfView;
 43  
 
 44  
 import javax.inject.Inject;
 45  
 import javax.servlet.ServletException;
 46  
 import javax.servlet.http.HttpServletRequest;
 47  
 import javax.servlet.http.HttpServletResponse;
 48  
 import java.util.*;
 49  
 
 50  
 /**
 51  
  * Gets the RSS for a blog.
 52  
  *
 53  
  * @author Simon Brown
 54  
  */
 55  12
 public class FeedAction extends Action {
 56  
 
 57  
   @Inject
 58  
   private LastModifiedService lastModifiedService;
 59  
 
 60  
   /**
 61  
    * Peforms the processing associated with this action.
 62  
    *
 63  
    * @param request  the HttpServletRequest instance
 64  
    * @param response the HttpServletResponse instance
 65  
    * @return the name of the next view
 66  
    */
 67  
   public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
 68  12
     AbstractBlog blog = (AbstractBlog) getModel().get(Constants.BLOG_KEY);
 69  12
     String flavor = request.getParameter("flavor");
 70  
 
 71  12
     if (lastModifiedService.checkAndProcessLastModified(request, response, blog.getLastModified(), null)) {
 72  4
       return new NotModifiedView();
 73  
     }
 74  
 
 75  
     List<BlogEntry> blogEntries;
 76  8
     String s = request.getParameter("includeAggregatedContent");
 77  8
     boolean includeAggregatedContent = (s == null || s.equalsIgnoreCase("true"));
 78  
 
 79  8
     if (blog instanceof Blog) {
 80  8
       Tag tag = getTag((Blog) blog, request);
 81  8
       Category category = getCategory((Blog) blog, request);
 82  8
       String author = getAuthor(request);
 83  
 
 84  8
       if (tag != null) {
 85  0
         blogEntries = ((Blog) blog).getRecentPublishedBlogEntries(tag);
 86  0
         getModel().put("tag", tag);
 87  8
       } else if (category != null) {
 88  4
         blogEntries = ((Blog) blog).getRecentPublishedBlogEntries(category);
 89  4
         getModel().put("category", category);
 90  4
       } else if (author != null) {
 91  0
         blogEntries = ((Blog) blog).getRecentPublishedBlogEntries(author);
 92  0
         getModel().put("author", author);
 93  
       } else {
 94  4
         blogEntries = ((Blog) blog).getRecentPublishedBlogEntries();
 95  
       }
 96  8
     } else {
 97  0
       blogEntries = blog.getRecentBlogEntries();
 98  
     }
 99  
 
 100  8
     List<BlogEntry> blogEntriesForFeed = new ArrayList<BlogEntry>();
 101  8
     for (BlogEntry entry : blogEntries) {
 102  4
       if (includeAggregatedContent || !entry.isAggregated()) {
 103  4
         blogEntriesForFeed.add(entry);
 104  
       }
 105  
     }
 106  
 
 107  8
     Collections.sort(blogEntriesForFeed, new BlogEntryComparator());
 108  
 
 109  8
     getModel().put(Constants.BLOG_ENTRIES, blogEntriesForFeed);
 110  
 
 111  
     // set the locale of this feed request to be English
 112  8
     javax.servlet.jsp.jstl.core.Config.set(
 113  
         request,
 114  
         javax.servlet.jsp.jstl.core.Config.FMT_LOCALE,
 115  
         Locale.ENGLISH);
 116  
 
 117  8
     if (flavor != null && flavor.equalsIgnoreCase("atom")) {
 118  0
       return new FeedView(AbstractRomeFeedView.FeedType.ATOM);
 119  8
     } else if (flavor != null && flavor.equalsIgnoreCase("rdf")) {
 120  0
       return new RdfView();
 121  
     } else {
 122  8
       return new FeedView(AbstractRomeFeedView.FeedType.RSS);
 123  
     }
 124  
   }
 125  
 
 126  
   /**
 127  
    * Helper method to find a named tag from a request parameter.
 128  
    *
 129  
    * @param blog    the blog for which the feed is for
 130  
    * @param request the HTTP request containing the tag parameter
 131  
    * @return a Tag instance, or null if the tag isn't
 132  
    *         specified or can't be found
 133  
    */
 134  
   private Tag getTag(Blog blog, HttpServletRequest request) {
 135  8
     String tag = request.getParameter("tag");
 136  8
     if (tag != null) {
 137  0
       return new Tag(tag, blog);
 138  
     } else {
 139  8
       return null;
 140  
     }
 141  
   }
 142  
 
 143  
   /**
 144  
    * Helper method to find a named category from a request parameter.
 145  
    *
 146  
    * @param blog    the blog for which the feed is for
 147  
    * @param request the HTTP request containing the category parameter
 148  
    * @return a Category instance, or null if the category isn't
 149  
    *         specified or can't be found
 150  
    */
 151  
   private Category getCategory(Blog blog, HttpServletRequest request) {
 152  8
     String categoryId = request.getParameter("category");
 153  8
     if (categoryId != null) {
 154  4
       return blog.getCategory(categoryId);
 155  
     } else {
 156  4
       return null;
 157  
     }
 158  
   }
 159  
 
 160  
   /**
 161  
    * Helper method to find a named author from a request parameter.
 162  
    *
 163  
    * @param request the HTTP request containing the tag parameter
 164  
    * @return a String username, or null if the author isn't specified
 165  
    */
 166  
   private String getAuthor(HttpServletRequest request) {
 167  8
     return request.getParameter("author");
 168  
   }
 169  
 
 170  
   public void setLastModifiedService(LastModifiedService lastModifiedService) {
 171  12
     this.lastModifiedService = lastModifiedService;
 172  12
   }
 173  
 }