Coverage Report - net.sourceforge.pebble.web.view.HtmlView
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlView
4%
2/45
0%
0/16
2.75
 
 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;
 33  
 
 34  
 import java.io.File;
 35  
 import java.io.IOException;
 36  
 
 37  
 import javax.servlet.RequestDispatcher;
 38  
 import javax.servlet.ServletContext;
 39  
 import javax.servlet.ServletException;
 40  
 import javax.servlet.http.HttpServletRequest;
 41  
 import javax.servlet.http.HttpServletResponse;
 42  
 
 43  
 import net.sourceforge.pebble.Constants;
 44  
 import net.sourceforge.pebble.PebbleContext;
 45  
 import net.sourceforge.pebble.domain.AbstractBlog;
 46  
 import net.sourceforge.pebble.domain.Blog;
 47  
 import net.sourceforge.pebble.domain.StaticPage;
 48  
 import net.sourceforge.pebble.util.I18n;
 49  
 
 50  
 import org.apache.commons.logging.Log;
 51  
 import org.apache.commons.logging.LogFactory;
 52  
 
 53  
 /**
 54  
  * Represents a HTML view component, implemented by a JSP
 55  
  * and prepares the model for display.
 56  
  *
 57  
  * @author    Simon Brown
 58  
  */
 59  128
 public abstract class HtmlView extends JspView {
 60  
 
 61  
   public static final String SYSTEM_THEME = "_pebble";
 62  
   public static final String DEFAULT_THEME = "default";
 63  
 
 64  4
   private static Log log = LogFactory.getLog(HtmlView.class);
 65  
 
 66  
   /**
 67  
    * Gets the title of this view.
 68  
    *
 69  
    * @return  the title as a String
 70  
    */
 71  
   public String getContentType() {
 72  0
     AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
 73  0
     return "text/html; charset=" + blog.getCharacterEncoding();
 74  
   }
 75  
 
 76  
   /**
 77  
    * Gets the title of this view.
 78  
    *
 79  
    * @return  the title as a String
 80  
    */
 81  
   public abstract String getTitle();
 82  
 
 83  
   public String getLocalizedString(String key) {
 84  0
           return I18n.getMessage(((AbstractBlog)getModel().get(Constants.BLOG_KEY)).getLocale(), key);
 85  
   }
 86  
   
 87  
   
 88  
   /**
 89  
    * Gets the name of the theme to use.
 90  
    *
 91  
    * @return  the theme name as a String
 92  
    */
 93  
   protected String getTheme() {
 94  0
     AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
 95  0
     String theme = blog.getTheme();
 96  
 
 97  0
     if (!PebbleContext.getInstance().getConfiguration().isUserThemesEnabled()) {
 98  0
       if (theme != null && !theme.equals(DEFAULT_THEME) && !theme.equals(SYSTEM_THEME)) {
 99  0
         return DEFAULT_THEME;
 100  
       }
 101  
     }
 102  
 
 103  0
     return theme;
 104  
   }
 105  
 
 106  
   protected int getStatus() {
 107  0
     return HttpServletResponse.SC_OK;
 108  
   }
 109  
 
 110  
   /**
 111  
    * Dispatches this view.
 112  
    *
 113  
    * @param request  the HttpServletRequest instance
 114  
    * @param response the HttpServletResponse instance
 115  
    * @param context
 116  
    */
 117  
   public void dispatch(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws ServletException {
 118  0
     String theme = getTheme();
 119  0
     request.setAttribute(Constants.THEME, theme);
 120  0
     request.setAttribute(Constants.TITLE_KEY, getTitle());
 121  0
     log.debug("Content is " + getUri());
 122  0
     request.setAttribute("content", getUri());
 123  0
     String headUri = "/themes/" + theme + "/head.jsp";
 124  0
     if (hasThemeHeadUri(headUri)) {
 125  0
       request.setAttribute("themeHeadUri", headUri);
 126  
     }
 127  0
     String uri = "/themes/" + theme + "/" + getTemplate() + ".jsp";
 128  0
     log.debug("Dispatching to " + uri);
 129  
 
 130  0
     response.setHeader("Cache-Control","no-cache, no-store");
 131  0
     response.setDateHeader("Expires", 0);
 132  0
     response.setHeader("Pragma","no-cache");
 133  
 
 134  
     try {
 135  0
       RequestDispatcher dispatcher = context.getRequestDispatcher(uri);
 136  0
       dispatcher.forward(request, response);
 137  0
     } catch (IOException ioe) {
 138  0
       ioe.printStackTrace();
 139  0
       throw new ServletException(ioe);
 140  
     } finally {
 141  0
       AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
 142  0
       blog.log(request, getStatus());
 143  0
     }
 144  0
   }
 145  
 
 146  
   private String getTemplate() {
 147  0
     if (!(getModel().get(Constants.BLOG_KEY) instanceof Blog)) {
 148  0
       return "template";
 149  
     }
 150  0
     if (!getModel().contains(Constants.STATIC_PAGE_KEY)) {
 151  0
       return "template";
 152  
     }
 153  0
     StaticPage staticPage = (StaticPage) getModel().get(Constants.STATIC_PAGE_KEY);
 154  0
     Blog blog = (Blog) getModel().get(Constants.BLOG_KEY);
 155  0
     String templateFile = blog.getThemeDirectory() + "/" + staticPage.getTemplate() + ".jsp";
 156  0
     if (new File(templateFile).canRead()) {
 157  0
       return staticPage.getTemplate();
 158  
     }
 159  0
     return "template";
 160  
   }
 161  
 
 162  
   private boolean hasThemeHeadUri(String headUri) {
 163  0
     return new File(getServletContext().getRealPath(headUri)).canRead();
 164  
   }
 165  
 }