1 20 21 package com.methodhead.sitecontext; 22 23 import javax.servlet.Filter ; 24 import javax.servlet.FilterConfig ; 25 import javax.servlet.ServletResponse ; 26 import javax.servlet.ServletRequest ; 27 import javax.servlet.FilterChain ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.http.HttpServletRequest ; 30 import java.util.regex.Matcher ; 31 import java.util.regex.Pattern ; 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import java.io.IOException ; 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 { 47 48 50 52 55 public static final String APPROOT = "approot"; 56 57 60 public static final String APPROOTPATH = "/" + APPROOT + "/"; 61 62 64 66 70 public void init( 71 FilterConfig filterConfig ) 72 throws 73 ServletException { 74 75 String strutsModulesStr = filterConfig.getInitParameter( "strutsModules" ); 79 80 if ( strutsModulesStr == null ) { 81 return; 82 } 83 84 String [] strutsModulesArr = strutsModulesStr.split( "," ); 85 86 for ( int i = 0; i < strutsModulesArr.length; i++ ) { 87 strutsModules_.put( strutsModulesArr[ i ], strutsModulesArr[ i ] ); 88 } 89 } 90 91 95 protected void processUnknownSiteContext( 96 HttpServletRequest httpRequest, 97 ServletResponse response, 98 FilterChain chain, 99 String path, 100 String pathInfo ) 101 throws 102 IOException , 103 ServletException { 104 105 chain.doFilter( httpRequest, response ); 109 } 110 111 public void doFilter( 112 ServletRequest request, 113 ServletResponse response, 114 FilterChain chain ) 115 throws 116 IOException , 117 ServletException { 118 119 HttpServletRequest httpRequest = ( HttpServletRequest )request; 125 126 String sitePath = 127 httpRequest.getRequestURI().substring( 128 httpRequest.getContextPath().length() ); 130 String domain = httpRequest.getServerName(); 132 String pathInfo = ""; 134 String path = ""; 136 SiteContext siteContext = null; 137 138 if ( logger_.isDebugEnabled() ) { 142 logger_.debug( 143 "Attempting to match domain=\"" + domain + 144 "\" and sitePath=\"" + sitePath + "\"." ); 145 } 146 147 siteContext = new SiteContext(); 148 149 if ( sitePath.length() > 0 && !sitePath.equals( "/" ) ) { 153 154 Matcher matcher = 158 Pattern.compile( "^(\\/(\\w+))?(\\/(.*))?$" ).matcher( sitePath ); 159 160 if ( !matcher.matches() ) { 161 throw new RuntimeException ( 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 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 if ( strutsModules_.containsKey( path ) ) { 196 197 if ( logger_.isDebugEnabled() ) { 198 logger_.debug( "Matched Struts module \"" + path + "\"" ); 199 } 200 201 path = ""; 205 } 206 207 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 else if ( siteContext.loadForDomainAndPath( domain, "" ) ) { 223 224 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 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 httpRequest.setAttribute( SiteContext.SITECONTEXT_KEY, siteContext ); 260 261 doFilter( httpRequest, response, chain, siteContext, pathInfo ); 265 } 266 267 protected void doFilter( 268 HttpServletRequest httpRequest, 269 ServletResponse response, 270 FilterChain chain, 271 SiteContext siteContext, 272 String pathInfo ) 273 throws 274 IOException , 275 ServletException { 276 277 if ( pathInfo.endsWith( ".do" ) ) { 281 282 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 httpRequest.getRequestDispatcher( 296 pathInfo.toString() ).forward( httpRequest, response ); 297 } 298 299 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 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 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 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 367 369 private static Logger logger_ = Logger.getLogger( SiteContextFilter.class ); 370 371 private Map strutsModules_ = new HashMap (); 372 } 373 | Popular Tags |