| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 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 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 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 | |
|
| 68 | |
|
| 69 | |
|
| 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 | |
|
| 78 | |
|
| 79 | |
|
| 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 | |
|
| 90 | |
|
| 91 | |
|
| 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 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 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 | |
} |