001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import javax.servlet.Filter;
024: import javax.servlet.FilterConfig;
025: import javax.servlet.ServletResponse;
026: import javax.servlet.ServletRequest;
027: import javax.servlet.FilterChain;
028: import javax.servlet.ServletException;
029: import javax.servlet.http.HttpServletRequest;
030: import java.util.regex.Matcher;
031: import java.util.regex.Pattern;
032: import java.io.IOException;
033: import com.methodhead.sitecontext.SiteContext;
034: import com.methodhead.aikp.IntKey;
035: import com.methodhead.persistable.PersistableException;
036: import com.methodhead.MhfException;
037: import com.methodhead.sitecontext.SiteContextFilter;
038: import org.apache.commons.lang.StringUtils;
039: import org.apache.log4j.Logger;
040: import org.apache.commons.lang.exception.ExceptionUtils;
041:
042: /**
043: * <code>ShimFilter</code> extends <code>SiteContextFilter</code> to recognize
044: * page URLs and forward to view or edit accordingly. Page URLs end in
045: * ".shtml".
046: */
047: public class ShimFilter extends SiteContextFilter implements Filter {
048:
049: // constructors /////////////////////////////////////////////////////////////
050:
051: // constants ////////////////////////////////////////////////////////////////
052:
053: // classes //////////////////////////////////////////////////////////////////
054:
055: // methods //////////////////////////////////////////////////////////////////
056:
057: public void init(FilterConfig filterConfig) throws ServletException {
058:
059: super .init(filterConfig);
060: }
061:
062: /**
063: * Forwards to <code>/siteNotFound.do</code> unless an approot path or Struts
064: * action (*.do) is requested, in which case the request is handled normally.
065: */
066: protected void processUnknownSiteContext(
067: HttpServletRequest httpRequest, ServletResponse response,
068: FilterChain chain, String path, String pathInfo)
069: throws IOException, ServletException {
070:
071: //
072: // approot?
073: //
074: if (APPROOT.equals(path)) {
075: httpRequest.getRequestDispatcher(pathInfo).forward(
076: httpRequest, response);
077: return;
078: }
079:
080: //
081: // struts action?
082: //
083: if (pathInfo.endsWith(".do")) {
084: httpRequest.getRequestDispatcher(pathInfo.toString())
085: .forward(httpRequest, response);
086: return;
087: }
088:
089: //
090: // forward to site not found
091: //
092: httpRequest.getRequestDispatcher("/siteNotFound.do").forward(
093: httpRequest, response);
094: }
095:
096: public void doFilter(HttpServletRequest httpRequest,
097: ServletResponse response, FilterChain chain,
098: SiteContext siteContext, String pathInfo)
099: throws IOException, ServletException {
100:
101: //
102: // empty path?
103: //
104: if (pathInfo.equals("") || pathInfo.equals("/")) {
105:
106: //
107: // forward to view
108: //
109: httpRequest.getRequestDispatcher("/viewpage").forward(
110: httpRequest, response);
111:
112: return;
113: }
114:
115: //
116: // shim page?
117: //
118: Matcher matcher = Pattern.compile("^/(\\w+).shtml$").matcher(
119: pathInfo);
120:
121: if (matcher.matches()) {
122:
123: String alias = matcher.group(1);
124:
125: if (logger_.isDebugEnabled()) {
126: logger_.debug("Matched a shim page; alias=\"" + alias
127: + "\"");
128: }
129:
130: //
131: // set up request attributes
132: //
133: httpRequest.setAttribute(ShimGlobals.PAGEALIAS_KEY, matcher
134: .group(1));
135:
136: //
137: // forward to edit?
138: //
139: if (ShimGlobals.MODE_EDIT.equals(httpRequest.getSession()
140: .getAttribute(ShimGlobals.MODE_KEY)))
141: httpRequest.getRequestDispatcher(
142: "/editPage.do?alias=" + matcher.group(1))
143: .forward(httpRequest, response);
144:
145: //
146: // forward to view
147: //
148: else
149: httpRequest.getRequestDispatcher("/viewpage").forward(
150: httpRequest, response);
151:
152: return;
153: }
154:
155: //
156: // otherwise handle normally
157: //
158: super .doFilter(httpRequest, response, chain, siteContext,
159: pathInfo);
160: }
161:
162: public void destroy() {
163: }
164:
165: // properties ///////////////////////////////////////////////////////////////
166:
167: // attributes ///////////////////////////////////////////////////////////////
168:
169: private static Logger logger_ = Logger.getLogger(ShimFilter.class);
170: }
|