Coverage Report - net.sourceforge.pebble.web.view.impl.AbstractRomeFeedView
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractRomeFeedView
62%
22/35
100%
4/4
1.5
AbstractRomeFeedView$FeedType
100%
11/11
N/A
1.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.view.impl;
 33  
 
 34  
 import com.sun.syndication.feed.synd.*;
 35  
 import com.sun.syndication.io.FeedException;
 36  
 import com.sun.syndication.io.SyndFeedOutput;
 37  
 import net.sourceforge.pebble.Constants;
 38  
 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
 39  
 import net.sourceforge.pebble.decorator.ContentDecoratorChain;
 40  
 import net.sourceforge.pebble.domain.*;
 41  
 import net.sourceforge.pebble.web.view.View;
 42  
 
 43  
 import javax.servlet.ServletContext;
 44  
 import javax.servlet.ServletException;
 45  
 import javax.servlet.http.HttpServletRequest;
 46  
 import javax.servlet.http.HttpServletResponse;
 47  
 import java.io.IOException;
 48  
 import java.text.SimpleDateFormat;
 49  
 import java.util.*;
 50  
 
 51  
 /**
 52  
  * Abstract view from which all ROME feeds are rendered.
 53  
  *
 54  
  * @author James Roper
 55  
  */
 56  
 public abstract class AbstractRomeFeedView extends View {
 57  
 
 58  
   private final FeedType feedType;
 59  
   private final SimpleDateFormat idDateFormat;
 60  
 
 61  12
   protected AbstractRomeFeedView(FeedType feedType) {
 62  12
     this.feedType = feedType;
 63  12
     this.idDateFormat = new SimpleDateFormat("yyyy-MM-dd");
 64  12
     idDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
 65  12
   }
 66  
 
 67  
   /**
 68  
    * Prepares the view for presentation.
 69  
    */
 70  
   @SuppressWarnings("unchecked")
 71  
   public void prepare() {
 72  0
     ContentDecoratorContext context = new ContentDecoratorContext();
 73  0
     context.setView(ContentDecoratorContext.SUMMARY_VIEW);
 74  0
     context.setMedia(ContentDecoratorContext.NEWS_FEED);
 75  
 
 76  0
     List<BlogEntry> blogEntries = (List<BlogEntry>) getModel().get(Constants.BLOG_ENTRIES);
 77  0
     ContentDecoratorChain.decorate(context, blogEntries);
 78  0
     getModel().put(Constants.BLOG_ENTRIES, blogEntries);
 79  0
   }
 80  
 
 81  
   /**
 82  
    * Gets the content type of this view.
 83  
    *
 84  
    * @return the content type as a String
 85  
    */
 86  
   public String getContentType() {
 87  0
     AbstractBlog blog = (AbstractBlog) getModel().get(Constants.BLOG_KEY);
 88  0
     return feedType.getContentType() + "; charset=" + blog.getCharacterEncoding();
 89  
   }
 90  
 
 91  
   /**
 92  
    * Dispatch the view using ROME
 93  
    *
 94  
    * @param request  the request
 95  
    * @param response the resopnse
 96  
    * @param context  the the servlet context
 97  
    * @throws ServletException
 98  
    */
 99  
   public void dispatch(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws ServletException {
 100  4
     SyndFeed syndFeed = getFeed();
 101  4
     syndFeed.setFeedType(getFeedType().getFeedType());
 102  
 
 103  4
     SyndFeedOutput output = new SyndFeedOutput();
 104  
 
 105  
     try {
 106  4
       output.output(syndFeed, response.getWriter());
 107  0
     } catch (IOException e) {
 108  0
       throw new ServletException("Error generating feed", e);
 109  0
     } catch (FeedException e) {
 110  0
       throw new ServletException("Error generating feed", e);
 111  4
     }
 112  4
   }
 113  
 
 114  
   /**
 115  
    * Get the feed to return.
 116  
    *
 117  
    * @return The feed to return.
 118  
    */
 119  
   protected abstract SyndFeed getFeed();
 120  
 
 121  
   /**
 122  
    * Get the feed type for this view
 123  
    *
 124  
    * @return The feed type
 125  
    */
 126  
   public FeedType getFeedType() {
 127  20
     return feedType;
 128  
   }
 129  
 
 130  
   protected SimpleDateFormat getIdDateFormat() {
 131  8
     return idDateFormat;
 132  
   }
 133  
 
 134  
   protected String generateId(AbstractBlog blog, Date date, String contentId) {
 135  16
     StringBuilder id = new StringBuilder("tag:");
 136  16
     id.append(blog.getDomainName()).append(",");
 137  16
     if (date != null) {
 138  8
       id.append(getIdDateFormat().format(date));
 139  
     } else {
 140  8
       id.append("0000-00-00");
 141  
     }
 142  16
     id.append(":").append(blog.getId());
 143  16
     if (contentId != null) {
 144  8
       id.append("/").append(contentId);
 145  
     }
 146  16
     return id.toString();
 147  
   }
 148  
 
 149  
   /**
 150  
    * The type of feed
 151  
    */
 152  4
   public enum FeedType {
 153  4
     ATOM("atom_1.0", "atom.xml", "application/atom+xml"),
 154  4
     RSS("rss_2.0", "rss.xml", "application/xml");
 155  
 
 156  
     private final String feedType;
 157  
     private final String fileName;
 158  
     private final String contentType;
 159  
 
 160  8
     FeedType(String feedType, String fileName, String contentType) {
 161  8
       this.feedType = feedType;
 162  8
       this.fileName = fileName;
 163  8
       this.contentType = contentType;
 164  8
     }
 165  
 
 166  
     /**
 167  
      * Get the ROME feed type, eg atom_1.0
 168  
      *
 169  
      * @return The ROME feed type
 170  
      */
 171  
     public String getFeedType() {
 172  4
       return feedType;
 173  
     }
 174  
 
 175  
     /**
 176  
      * Get the file name that this feed uses, eg atom.xml, rss.xml
 177  
      *
 178  
      * @return The filename
 179  
      */
 180  
     public String getFileName() {
 181  8
       return fileName;
 182  
     }
 183  
 
 184  
     /**
 185  
      * Get the content type to set when sending this feed
 186  
      *
 187  
      * @return The content type
 188  
      */
 189  
     public String getContentType() {
 190  8
       return contentType;
 191  
     }
 192  
   }
 193  
 
 194  
 }