KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.Filter JavaDoc;
24 import javax.servlet.FilterConfig JavaDoc;
25 import javax.servlet.ServletResponse JavaDoc;
26 import javax.servlet.ServletRequest JavaDoc;
27 import javax.servlet.FilterChain JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import java.util.regex.Matcher JavaDoc;
31 import java.util.regex.Pattern JavaDoc;
32 import java.io.IOException JavaDoc;
33 import com.methodhead.sitecontext.SiteContext;
34 import com.methodhead.aikp.IntKey;
35 import com.methodhead.persistable.PersistableException;
36 import com.methodhead.MhfException;
37 import com.methodhead.sitecontext.SiteContextFilter;
38 import org.apache.commons.lang.StringUtils;
39 import org.apache.log4j.Logger;
40 import org.apache.commons.lang.exception.ExceptionUtils;
41
42 /**
43  * <code>ShimFilter</code> extends <code>SiteContextFilter</code> to recognize
44  * page URLs and forward to view or edit accordingly. Page URLs end in
45  * ".shtml".
46  */

47 public class ShimFilter
48 extends
49   SiteContextFilter
50 implements
51   Filter JavaDoc {
52
53   // constructors /////////////////////////////////////////////////////////////
54

55   // constants ////////////////////////////////////////////////////////////////
56

57   // classes //////////////////////////////////////////////////////////////////
58

59   // methods //////////////////////////////////////////////////////////////////
60

61   public void init(
62     FilterConfig JavaDoc filterConfig )
63   throws
64     ServletException JavaDoc {
65
66     super.init( filterConfig );
67   }
68
69   /**
70    * Forwards to <code>/siteNotFound.do</code> unless an approot path or Struts
71    * action (*.do) is requested, in which case the request is handled normally.
72    */

73   protected void processUnknownSiteContext(
74     HttpServletRequest JavaDoc httpRequest,
75     ServletResponse JavaDoc response,
76     FilterChain JavaDoc chain,
77     String JavaDoc path,
78     String JavaDoc pathInfo )
79   throws
80     IOException JavaDoc,
81     ServletException JavaDoc {
82
83     //
84
// approot?
85
//
86
if ( APPROOT.equals( path ) ) {
87       httpRequest.getRequestDispatcher( pathInfo ).forward( httpRequest, response );
88       return;
89     }
90
91     //
92
// struts action?
93
//
94
if ( pathInfo.endsWith( ".do" ) ) {
95       httpRequest.getRequestDispatcher( pathInfo.toString() ).forward( httpRequest, response );
96       return;
97     }
98
99     //
100
// forward to site not found
101
//
102
httpRequest.getRequestDispatcher( "/siteNotFound.do" ).forward( httpRequest, response );
103   }
104
105   public void doFilter(
106     HttpServletRequest JavaDoc httpRequest,
107     ServletResponse JavaDoc response,
108     FilterChain JavaDoc chain,
109     SiteContext siteContext,
110     String JavaDoc pathInfo )
111   throws
112     IOException JavaDoc,
113     ServletException JavaDoc {
114
115     //
116
// empty path?
117
//
118
if ( pathInfo.equals( "" ) || pathInfo.equals( "/" ) ) {
119
120       //
121
// forward to view
122
//
123
httpRequest.getRequestDispatcher(
124         "/viewpage" ).forward( httpRequest, response );
125
126       return;
127     }
128
129     //
130
// shim page?
131
//
132
Matcher JavaDoc matcher = Pattern.compile( "^/(\\w+).shtml$" ).matcher( pathInfo );
133
134     if ( matcher.matches() ) {
135
136       String JavaDoc alias = matcher.group( 1 );
137
138       if ( logger_.isDebugEnabled() ) {
139         logger_.debug(
140           "Matched a shim page; alias=\"" + alias + "\"" );
141       }
142
143       //
144
// set up request attributes
145
//
146
httpRequest.setAttribute( ShimGlobals.PAGEALIAS_KEY, matcher.group( 1 ) );
147
148       //
149
// forward to edit?
150
//
151
if ( ShimGlobals.MODE_EDIT.equals(
152              httpRequest.getSession().getAttribute( ShimGlobals.MODE_KEY ) ) )
153         httpRequest.getRequestDispatcher(
154           "/editPage.do?alias=" + matcher.group( 1 ) ).forward(
155             httpRequest, response );
156
157       //
158
// forward to view
159
//
160
else
161         httpRequest.getRequestDispatcher(
162           "/viewpage" ).forward( httpRequest, response );
163
164       return;
165     }
166
167     //
168
// otherwise handle normally
169
//
170
super.doFilter( httpRequest, response, chain, siteContext, pathInfo );
171   }
172
173   public void destroy() {
174   }
175
176   // properties ///////////////////////////////////////////////////////////////
177

178   // attributes ///////////////////////////////////////////////////////////////
179

180   private static Logger logger_ = Logger.getLogger( ShimFilter.class );
181 }
182
Popular Tags