001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2006 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.ui;
021:
022: import java.io.IOException;
023: import java.io.PrintWriter;
024:
025: import javax.servlet.*;
026: import javax.servlet.http.HttpServletRequest;
027:
028: import org.apache.log4j.Logger;
029: import org.apache.log4j.NDC;
030:
031: import com.ecyrd.jspwiki.WikiContext;
032: import com.ecyrd.jspwiki.WikiEngine;
033: import com.ecyrd.jspwiki.tags.WikiTagBase;
034:
035: /**
036: * Provides basic filtering for JSPWiki. This filter only makes sure
037: * JSPWiki is running, and also does a bunch of sanitychecks.
038: *
039: * @author Janne Jalkanen
040: *
041: */
042: public class WikiServletFilter implements Filter {
043: protected static final Logger log = Logger
044: .getLogger(WikiServletFilter.class);
045: protected WikiEngine m_engine = null;
046:
047: public WikiServletFilter() {
048: super ();
049: }
050:
051: public void init(FilterConfig config) throws ServletException {
052: ServletContext context = config.getServletContext();
053: m_engine = WikiEngine.getInstance(context, null);
054: }
055:
056: public void destroy() {
057: }
058:
059: public void doFilter(ServletRequest request,
060: ServletResponse response, FilterChain chain)
061: throws IOException, ServletException {
062: //
063: // Sanity check; it might be true in some conditions, but we need to know where.
064: //
065: if (chain == null) {
066: throw new ServletException(
067: "FilterChain is null, even if it should not be. Please report this to the jspwiki development team.");
068: }
069:
070: if (m_engine == null) {
071: PrintWriter out = response.getWriter();
072: out
073: .print("<html><head><title>Fatal problem with JSPWiki</title></head>");
074: out.print("<body>");
075: out.print("<h1>JSPWiki has not been started</h1>");
076: out
077: .print("<p>JSPWiki is not running. This is probably due to a configuration error in your jspwiki.properties file, ");
078: out
079: .print("or a problem with your servlet container. Please double-check everything before issuing a bug report ");
080: out.print("at jspwiki.org.</p>");
081: out
082: .print("<p>We apologize for the inconvenience. No, really, we do. We're trying to ");
083: out
084: .print("JSPWiki as easy as we can, but there is only so much we have time to test ");
085: out.print("platforms.</p>");
086: out.print("</body>");
087: return;
088: }
089:
090: // Write the response to a dummy response because we want to
091: // replace markers with scripts/stylesheet.
092: HttpServletRequest httpRequest = (HttpServletRequest) request;
093:
094: httpRequest.setCharacterEncoding(m_engine.getContentEncoding());
095:
096: try {
097: NDC.push(m_engine.getApplicationName() + ":"
098: + httpRequest.getRequestURL());
099:
100: chain.doFilter(request, response);
101: } finally {
102: NDC.pop();
103: NDC.remove();
104: }
105:
106: }
107:
108: /**
109: * Figures out the wiki context from the request. This method does not create the
110: * context if it does not exist.
111: *
112: * @param request The request to examine
113: * @return A valid WikiContext value (or null, if the context could not be located).
114: */
115: protected WikiContext getWikiContext(ServletRequest request) {
116: HttpServletRequest httpRequest = (HttpServletRequest) request;
117:
118: WikiContext ctx = (WikiContext) httpRequest
119: .getAttribute(WikiTagBase.ATTR_CONTEXT);
120:
121: return ctx;
122: }
123:
124: }
|