KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > ShimRequestProcessor


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import org.apache.struts.tiles.TilesRequestProcessor;
24 import org.apache.log4j.Logger;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import org.apache.struts.config.ForwardConfig;
28 import com.methodhead.sitecontext.SiteContext;
29 import java.util.regex.Matcher JavaDoc;
30 import java.util.regex.Pattern JavaDoc;
31 import java.io.IOException JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33
34 /**
35  * A request processor that can process Shim page requests (e.g.,
36  * <tt>/home.shtml</tt>). Shim page URIs can then be used in Struts forwards
37  * (e.g., <code>&lt;forward name="success"
38  * path="/reviewfinalized.shtml"/&gt;</code>). The processor correctly handles
39  * Struts page URIs referenced in Struts modules.
40  */

41 public class ShimRequestProcessor
42 extends
43   TilesRequestProcessor {
44
45   // constructors /////////////////////////////////////////////////////////////
46

47   // constants ////////////////////////////////////////////////////////////////
48

49   // classes //////////////////////////////////////////////////////////////////
50

51   // methods //////////////////////////////////////////////////////////////////
52

53   /**
54    * Extends default behavior to detect Shim page requests (e.g.,
55    * <tt>/home.shtml</tt>) and forwards as {@link com.methodhead.shim.ShimFilter
56    * ShimFilter} would if such a request is made (not unit tested). An
57    * explicit page must be requested (i.e., <tt>/</tt> won't work).
58    */

59   protected void doForward(
60     String JavaDoc uri,
61     HttpServletRequest JavaDoc request,
62     HttpServletResponse JavaDoc response )
63   throws
64     ServletException JavaDoc,
65     IOException JavaDoc {
66
67     SiteContext context = SiteContext.getContext( request );
68
69     //
70
// shim page? uris can take the following form:
71
//
72
// /foo.shtml - a shim page in the default Struts module
73
// /bar/foo.shtml - a shim page in the bar Struts module
74
//
75
Matcher JavaDoc matcher = Pattern.compile( ".*\\/(\\w+).shtml$" ).matcher( uri );
76
77     if ( matcher.matches() ) {
78
79       if ( logger_.isDebugEnabled() ) {
80         logger_.debug(
81           "Matched shim page \"" + matcher.group( 1 ) + "\"" );
82       }
83
84       //
85
// forward to edit?
86
//
87
if ( ShimGlobals.MODE_EDIT.equals( request.getSession().getAttribute( ShimGlobals.MODE_KEY ) ) ) {
88         super.doForward( "/editPage.do?alias=" + matcher.group( 1 ), request, response );
89       }
90
91       //
92
// forward to view
93
//
94
else {
95         request.setAttribute( ShimGlobals.PAGEALIAS_KEY, matcher.group( 1 ) );
96         super.doForward( "/viewpage", request, response );
97       }
98
99       return;
100     }
101
102     //
103
// process normally
104
//
105
super.doForward( uri, request, response );
106   }
107
108   // properties ///////////////////////////////////////////////////////////////
109

110   // attributes ///////////////////////////////////////////////////////////////
111

112   private Logger logger_ = Logger.getLogger( ShimRequestProcessor.class );
113 }
114
Popular Tags