KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > sitecontext > SiteContextFilter


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.sitecontext;
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.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.io.IOException JavaDoc;
35 import com.methodhead.sitecontext.SiteContext;
36 import com.methodhead.aikp.IntKey;
37 import com.methodhead.persistable.PersistableException;
38 import com.methodhead.MhfException;
39 import org.apache.commons.lang.StringUtils;
40 import org.apache.log4j.Logger;
41 import org.apache.log4j.Level;
42 import org.apache.commons.lang.exception.ExceptionUtils;
43
44 public class SiteContextFilter
45 implements
46   Filter JavaDoc {
47
48   // constructors /////////////////////////////////////////////////////////////
49

50   // constants ////////////////////////////////////////////////////////////////
51

52   /**
53    * The URL path the filter identifies with the web app root.
54    */

55   public static final String JavaDoc APPROOT = "approot";
56
57   /**
58    * The app root wrapped with forward slashes for convenience.
59    */

60   public static final String JavaDoc APPROOTPATH = "/" + APPROOT + "/";
61
62   // classes //////////////////////////////////////////////////////////////////
63

64   // methods //////////////////////////////////////////////////////////////////
65

66   /**
67    * Reads a comma-separated list of Strut module names from the
68    * <code>strutsModules</code> init param.
69    */

70   public void init(
71     FilterConfig JavaDoc filterConfig )
72   throws
73     ServletException JavaDoc {
74
75     //
76
// set up map of struts modules
77
//
78
String JavaDoc strutsModulesStr = filterConfig.getInitParameter( "strutsModules" );
79
80     if ( strutsModulesStr == null ) {
81       return;
82     }
83
84     String JavaDoc[] strutsModulesArr = strutsModulesStr.split( "," );
85
86     for ( int i = 0; i < strutsModulesArr.length; i++ ) {
87       strutsModules_.put( strutsModulesArr[ i ], strutsModulesArr[ i ] );
88     }
89   }
90
91   /**
92    * This method is called when no site context can be loaded for the given
93    * URL. By default, <code>chain.doFilter()</code> is called.
94    */

95   protected void processUnknownSiteContext(
96     HttpServletRequest JavaDoc httpRequest,
97     ServletResponse JavaDoc response,
98     FilterChain JavaDoc chain,
99     String JavaDoc path,
100     String JavaDoc pathInfo )
101   throws
102     IOException JavaDoc,
103     ServletException JavaDoc {
104
105     //
106
// process normally and hope for the best
107
//
108
chain.doFilter( httpRequest, response );
109   }
110
111   public void doFilter(
112     ServletRequest JavaDoc request,
113     ServletResponse JavaDoc response,
114     FilterChain JavaDoc chain )
115   throws
116     IOException JavaDoc,
117     ServletException JavaDoc {
118
119     //
120
// get some things we'll need; consider the following url:
121
//
122
// http://site1.com:8080/mhf/subsite/test.txt
123
//
124
HttpServletRequest JavaDoc httpRequest = ( HttpServletRequest JavaDoc )request;
125
126     String JavaDoc sitePath =
127       httpRequest.getRequestURI().substring(
128         httpRequest.getContextPath().length() ); // "subsite/test.txt"
129

130     String JavaDoc domain = httpRequest.getServerName(); // "site1.com"
131

132     String JavaDoc pathInfo = ""; // "test.txt"
133

134     String JavaDoc path = ""; // "subsite"
135

136     SiteContext siteContext = null;
137
138     //
139
// try to match a site context
140
//
141
if ( logger_.isDebugEnabled() ) {
142       logger_.debug(
143         "Attempting to match domain=\"" + domain +
144         "\" and sitePath=\"" + sitePath + "\"." );
145     }
146
147     siteContext = new SiteContext();
148
149     //
150
// do we have a sitePath to match?
151
//
152
if ( sitePath.length() > 0 && !sitePath.equals( "/" ) ) {
153
154       //
155
// break out the path and pathInfo
156
//
157
Matcher JavaDoc matcher =
158         Pattern.compile( "^(\\/(\\w+))?(\\/(.*))?$" ).matcher( sitePath );
159
160       if ( !matcher.matches() ) {
161         throw new RuntimeException JavaDoc(
162           "Couldn't match url \"" + domain + sitePath + "\"." );
163       }
164
165       path = matcher.group( 2 );
166
167       if ( path == null ) {
168         path = "";
169       }
170
171       pathInfo = matcher.group( 4 );
172
173       if ( pathInfo == null )
174         pathInfo = "";
175       else
176         pathInfo = "/" + pathInfo;
177     }
178
179     //
180
// is this a Cactus test?
181
//
182
if ( "ServletRedirector".equals( path ) || "JspRedirector".equals( path ) ) {
183
184       if ( logger_.isDebugEnabled() ) {
185         logger_.debug( "Matched Cactus url \"" + path + "\"; filtering normally" );
186       }
187
188       chain.doFilter( request, response );
189       return;
190     }
191
192     //
193
// is the path one of the Struts modules we know about?
194
//
195
if ( strutsModules_.containsKey( path ) ) {
196
197       if ( logger_.isDebugEnabled() ) {
198         logger_.debug( "Matched Struts module \"" + path + "\"" );
199       }
200       
201       //
202
// get rid of the path
203
//
204
path = "";
205     }
206
207     //
208
// at this point, path may indicate a sub-site, a subdirectory, or a struts
209
// module; load site context for domain and path?
210
//
211
if ( siteContext.loadForDomainAndPath( domain, path ) ) {
212       if ( logger_.isDebugEnabled() ) {
213         logger_.debug(
214           "Matched domain and path; domain=\"" + domain + "\", " +
215           "path=\"" + path + "\"" );
216       }
217     }
218
219     //
220
// maybe path is a subdir; load for just domain?
221
//
222
else if ( siteContext.loadForDomainAndPath( domain, "" ) ) {
223
224       //
225
// didn't use path to determine site context, it must be part of the
226
// pathInfo
227
//
228
pathInfo = "/" + path + pathInfo;
229
230       if ( logger_.isDebugEnabled() ) {
231         logger_.debug(
232           "Matched domain only; domain=\"" + domain + "\", " +
233           "path=\"" + path + "\"" );
234       }
235     }
236     else {
237
238       if ( logger_.isDebugEnabled() ) {
239         logger_.debug(
240           "Couldn't load site context for domain=\"" + domain + "\", " +
241           "path=\"" + path + "\"; filtering normally" );
242       }
243
244       //
245
// process an unknown site context
246
//
247
processUnknownSiteContext( httpRequest, response, chain, path, pathInfo );
248       return;
249     }
250
251     if ( logger_.isDebugEnabled() ) {
252       logger_.debug(
253         "Loaded site context " + siteContext );
254     }
255
256     //
257
// set request attribute
258
//
259
httpRequest.setAttribute( SiteContext.SITECONTEXT_KEY, siteContext );
260
261     //
262
// handle the request accordingly
263
//
264
doFilter( httpRequest, response, chain, siteContext, pathInfo );
265   }
266
267   protected void doFilter(
268     HttpServletRequest JavaDoc httpRequest,
269     ServletResponse JavaDoc response,
270     FilterChain JavaDoc chain,
271     SiteContext siteContext,
272     String JavaDoc pathInfo )
273   throws
274     IOException JavaDoc,
275     ServletException JavaDoc {
276
277     //
278
// struts action?
279
//
280
if ( pathInfo.endsWith( ".do" ) ) {
281
282       //
283
// is there a path?
284
//
285
if ( !"".equals( siteContext.getString( "path" ) ) ) {
286
287         if ( logger_.isDebugEnabled() ) {
288           logger_.debug(
289             "Handling struts action with a path; forwarding to " + pathInfo );
290         }
291
292         //
293
// forward to the action without the path
294
//
295
httpRequest.getRequestDispatcher(
296           pathInfo.toString() ).forward( httpRequest, response );
297       }
298
299       //
300
// otherwise, handle the request normally
301
//
302
else {
303         if ( logger_.isDebugEnabled() ) {
304           logger_.debug(
305             "Handling struts action " + pathInfo );
306         }
307
308         chain.doFilter( httpRequest, response );
309       }
310
311       return;
312     }
313
314     //
315
// no pathInfo?
316
//
317
if ( pathInfo.equals( "" ) || pathInfo.equals( "/" ) ) {
318
319       if ( logger_.isDebugEnabled() ) {
320         logger_.debug(
321           "Handling empty pathInfo; forwarding to \"" + "/" +
322           siteContext.getInt( "id" ) + "/index.jsp\"." );
323       }
324
325       httpRequest.getRequestDispatcher(
326         "/" + siteContext.getInt( "id" ) + "/index.jsp" ).forward(
327           httpRequest, response );
328       return;
329     }
330     
331
332     //
333
// root file (not in any site context)
334
//
335
int i = -1;
336     if ( ( i = pathInfo.indexOf( APPROOTPATH ) ) >= 0 ) {
337
338       if ( logger_.isDebugEnabled() ) {
339         logger_.debug(
340           "Handling root file; forwarding to \"/" + pathInfo.substring( APPROOTPATH.length() ) );
341       }
342
343       httpRequest.getRequestDispatcher(
344         "/" + pathInfo.substring( i + APPROOTPATH.length() ) ).forward( httpRequest, response );
345       return;
346     }
347
348     //
349
// normal file; add site context id to site path and forward on
350
//
351
if ( logger_.isDebugEnabled() ) {
352       logger_.debug(
353         "Normal file; forwarding to \"" + "/" + siteContext.getInt( "id" ) +
354         pathInfo + "\"." );
355     }
356     httpRequest.getRequestDispatcher(
357       "/" + siteContext.getInt( "id" ) + pathInfo ).forward(
358         httpRequest, response );
359     return;
360   }
361
362   public void destroy() {
363   }
364
365   // properties ///////////////////////////////////////////////////////////////
366

367   // attributes ///////////////////////////////////////////////////////////////
368

369   private static Logger logger_ = Logger.getLogger( SiteContextFilter.class );
370
371   private Map JavaDoc strutsModules_ = new HashMap JavaDoc();
372 }
373
Popular Tags