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 org.apache.struts.tiles.TilesRequestProcessor;
024: import org.apache.log4j.Logger;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027: import org.apache.struts.config.ForwardConfig;
028: import com.methodhead.sitecontext.SiteContext;
029: import java.util.regex.Matcher;
030: import java.util.regex.Pattern;
031: import java.io.IOException;
032: import javax.servlet.ServletException;
033:
034: /**
035: * A request processor that can process Shim page requests (e.g.,
036: * <tt>/home.shtml</tt>). Shim page URIs can then be used in Struts forwards
037: * (e.g., <code><forward name="success"
038: * path="/reviewfinalized.shtml"/></code>). The processor correctly handles
039: * Struts page URIs referenced in Struts modules.
040: */
041: public class ShimRequestProcessor extends TilesRequestProcessor {
042:
043: // constructors /////////////////////////////////////////////////////////////
044:
045: // constants ////////////////////////////////////////////////////////////////
046:
047: // classes //////////////////////////////////////////////////////////////////
048:
049: // methods //////////////////////////////////////////////////////////////////
050:
051: /**
052: * Extends default behavior to detect Shim page requests (e.g.,
053: * <tt>/home.shtml</tt>) and forwards as {@link com.methodhead.shim.ShimFilter
054: * ShimFilter} would if such a request is made (not unit tested). An
055: * explicit page must be requested (i.e., <tt>/</tt> won't work).
056: */
057: protected void doForward(String uri, HttpServletRequest request,
058: HttpServletResponse response) throws ServletException,
059: IOException {
060:
061: SiteContext context = SiteContext.getContext(request);
062:
063: //
064: // shim page? uris can take the following form:
065: //
066: // /foo.shtml - a shim page in the default Struts module
067: // /bar/foo.shtml - a shim page in the bar Struts module
068: //
069: Matcher matcher = Pattern.compile(".*\\/(\\w+).shtml$")
070: .matcher(uri);
071:
072: if (matcher.matches()) {
073:
074: if (logger_.isDebugEnabled()) {
075: logger_.debug("Matched shim page \"" + matcher.group(1)
076: + "\"");
077: }
078:
079: //
080: // forward to edit?
081: //
082: if (ShimGlobals.MODE_EDIT.equals(request.getSession()
083: .getAttribute(ShimGlobals.MODE_KEY))) {
084: super .doForward("/editPage.do?alias="
085: + matcher.group(1), request, response);
086: }
087:
088: //
089: // forward to view
090: //
091: else {
092: request.setAttribute(ShimGlobals.PAGEALIAS_KEY, matcher
093: .group(1));
094: super .doForward("/viewpage", request, response);
095: }
096:
097: return;
098: }
099:
100: //
101: // process normally
102: //
103: super .doForward(uri, request, response);
104: }
105:
106: // properties ///////////////////////////////////////////////////////////////
107:
108: // attributes ///////////////////////////////////////////////////////////////
109:
110: private Logger logger_ = Logger
111: .getLogger(ShimRequestProcessor.class);
112: }
|