1 29 30 package com.caucho.server.webapp; 31 32 import com.caucho.config.*; 33 import com.caucho.config.types.InitProgram; 34 import com.caucho.server.dispatch.ErrorFilterChain; 35 import com.caucho.server.dispatch.ForwardFilterChain; 36 import com.caucho.server.dispatch.Invocation; 37 import com.caucho.server.dispatch.MovedFilterChain; 38 import com.caucho.server.dispatch.RedirectFilterChain; 39 import com.caucho.server.dispatch.ServletConfigImpl; 40 import com.caucho.util.L10N; 41 42 import javax.annotation.PostConstruct; 43 import javax.servlet.FilterChain ; 44 import javax.servlet.ServletException ; 45 import javax.servlet.ServletRequest ; 46 import javax.servlet.ServletResponse ; 47 import javax.servlet.http.HttpServletResponse ; 48 import java.util.ArrayList ; 49 import java.util.logging.Level ; 50 import java.util.logging.Logger ; 51 import java.util.regex.Matcher ; 52 import java.util.regex.Pattern ; 53 54 57 public class RewriteInvocation { 58 private static final L10N L = new L10N(RewriteInvocation.class); 59 private static final Logger log 60 = Logger.getLogger(RewriteInvocation.class.getName()); 61 62 private final static FilterChain ACCEPT_CHAIN; 63 64 private final WebApp _webApp; 65 66 private final ArrayList <Program> _programList = new ArrayList <Program>(); 67 68 public RewriteInvocation() 69 { 70 _webApp = null; 71 } 72 73 public RewriteInvocation(WebApp webApp) 74 { 75 _webApp = webApp; 76 } 77 78 81 public void addDispatch(Accept accept) 82 { 83 _programList.add(accept); 84 } 85 86 89 public LoadBalance createLoadBalance() 90 { 91 if (_webApp == null) 92 throw new ConfigException(L.l("<load-balance> requires a web-app. Host-based <rewrite-dispatch> can not use <load-balance>.")); 93 94 LoadBalance loadBalance = new LoadBalance(_webApp); 95 96 _programList.add(loadBalance); 97 98 return loadBalance; 99 } 100 101 104 public void addMovedPermanently(Moved moved) 105 { 106 moved.setStatusCode(301); 107 108 _programList.add(moved); 109 } 110 111 114 public Forward createForward() 115 { 116 return new Forward(); 117 } 118 119 122 public void addForward(Forward forward) 123 { 124 _programList.add(forward); 125 } 126 127 130 public Error createForbidden() 131 { 132 Error error = new Error (HttpServletResponse.SC_FORBIDDEN); 133 134 _programList.add(error); 135 136 return error; 137 } 138 139 142 public Error createGone() 143 { 144 Error error = new Error (HttpServletResponse.SC_GONE); 145 146 _programList.add(error); 147 148 return error; 149 } 150 151 154 public Error createNotFound() 155 { 156 Error error = new Error (HttpServletResponse.SC_NOT_FOUND); 157 158 _programList.add(error); 159 160 return error; 161 } 162 163 166 public void addRewrite(Rewrite rewrite) 167 { 168 _programList.add(rewrite); 169 } 170 171 174 public void addRedirect(Redirect redirect) 175 { 176 _programList.add(redirect); 177 } 178 179 public FilterChain map(String uri, Invocation invocation) 180 throws ServletException 181 { 182 for (int i = 0; i < _programList.size(); i++) { 183 Program program = _programList.get(i); 184 185 uri = program.rewrite(uri); 186 187 FilterChain chain = program.dispatch(uri); 188 189 if (chain == ACCEPT_CHAIN) 190 return null; 191 else if (chain != null) 192 return chain; 193 } 194 195 return null; 196 } 197 198 static class Program { 199 public String rewrite(String uri) 200 { 201 return uri; 202 } 203 204 public FilterChain dispatch(String uri) 205 throws ServletException 206 { 207 return null; 208 } 209 } 210 211 public static class Rewrite extends Program { 212 private Pattern _regexp; 213 private String _replacement; 214 215 public String getTagName() 216 { 217 return "rewrite"; 218 } 219 220 223 public void setRegexp(String regexp) 224 { 225 _regexp = Pattern.compile(regexp); 226 } 227 228 231 public void setReplacement(String replacement) 232 { 233 _replacement = replacement; 234 } 235 236 239 @PostConstruct 240 public void init() 241 throws ConfigException 242 { 243 if (_regexp == null) 244 throw new ConfigException(L.l("{0} needs 'regexp' attribute.", 245 getTagName())); 246 if (_replacement == null) 247 throw new ConfigException(L.l("{0} needs 'replacement' attribute.", 248 getTagName())); 249 } 250 251 public String rewrite(String uri) 252 { 253 Matcher matcher = _regexp.matcher(uri); 254 255 if (matcher.find()) { 256 matcher.reset(); 257 return matcher.replaceAll(_replacement); 258 } 259 else 260 return uri; 261 } 262 } 263 264 public static class Accept extends Program { 265 private Pattern _regexp; 266 267 public String getTagName() 268 { 269 return "accept"; 270 } 271 272 275 public void setRegexp(String regexp) 276 { 277 _regexp = Pattern.compile(regexp); 278 } 279 280 283 @PostConstruct 284 public void init() 285 throws ConfigException 286 { 287 if (_regexp == null) 288 throw new ConfigException(L.l("{0} needs 'regexp' attribute.", 289 getTagName())); 290 } 291 292 public FilterChain dispatch(String uri) 293 { 294 Matcher matcher = _regexp.matcher(uri); 295 296 if (matcher.find()) 297 return ACCEPT_CHAIN; 298 else 299 return null; 300 } 301 } 302 303 public static class Redirect extends Program { 304 private String _target; 305 private Pattern _regexp; 306 307 310 public void setRegexp(String regexp) 311 { 312 _regexp = Pattern.compile(regexp); 313 } 314 315 public void setTarget(String target) 316 { 317 _target = target; 318 } 319 320 public FilterChain dispatch(String uri) 321 { 322 Matcher matcher = _regexp.matcher(uri); 323 324 if (matcher.find()) { 325 matcher.reset(); 326 uri = matcher.replaceAll(_target); 327 328 return new RedirectFilterChain(uri); 329 } 330 else 331 return null; 332 } 333 334 @PostConstruct 335 public void init() 336 throws ConfigException 337 { 338 if (_regexp == null) 339 throw new ConfigException(L.l("redirect needs 'regexp' attribute.")); 340 if (_target == null) 341 throw new ConfigException(L.l("redirect needs 'target' attribute.")); 342 } 343 } 344 345 public static class Moved extends Program { 346 private int _code = 302; 347 348 private String _target; 349 private Pattern _regexp; 350 351 354 public void setRegexp(String regexp) 355 { 356 _regexp = Pattern.compile(regexp); 357 } 358 359 public void setTarget(String target) 360 { 361 _target = target; 362 } 363 364 367 public void setStatusCode(int code) 368 { 369 _code = code; 370 } 371 372 public FilterChain dispatch(String uri) 373 { 374 Matcher matcher = _regexp.matcher(uri); 375 376 if (matcher.find()) { 377 matcher.reset(); 378 uri = matcher.replaceAll(_target); 379 380 return new MovedFilterChain(_code, uri); 381 } 382 else 383 return null; 384 } 385 386 @PostConstruct 387 public void init() 388 throws ConfigException 389 { 390 if (_regexp == null) 391 throw new ConfigException(L.l("moved needs 'regexp' attribute.")); 392 if (_target == null) 393 throw new ConfigException(L.l("moved needs 'target' attribute.")); 394 } 395 } 396 397 public static class LoadBalance extends Program { 398 private final WebApp _webApp; 399 400 private Pattern _regexp; 401 private ServletConfigImpl _servlet; 402 403 private BuilderProgramContainer _program = new BuilderProgramContainer(); 404 405 LoadBalance(WebApp webApp) 406 { 407 _webApp = webApp; 408 } 409 410 413 public void setRegexp(String regexp) 414 { 415 _regexp = Pattern.compile(regexp); 416 } 417 418 public void addBuilderProgram(BuilderProgram program) 419 { 420 _program.addProgram(program); 421 } 422 423 public FilterChain dispatch(String uri) 424 throws ServletException 425 { 426 Matcher matcher = _regexp.matcher(uri); 427 428 if (matcher.find()) { 429 return _servlet.createServletChain(); 430 } 431 else 432 return null; 433 } 434 435 @PostConstruct 436 public void init() 437 throws ConfigException, ServletException 438 { 439 if (_regexp == null) 440 throw new ConfigException(L.l("load-balance needs 'regexp' attribute.")); 441 442 try { 443 _servlet = new ServletConfigImpl(); 444 445 _servlet.setServletName("resin-dispatch-lb"); 446 Class cl = Class.forName("com.caucho.servlets.LoadBalanceServlet"); 447 _servlet.setServletClass("com.caucho.servlets.LoadBalanceServlet"); 448 449 _servlet.setInit(new InitProgram(_program)); 450 451 _webApp.addServlet(_servlet); 452 } catch (ClassNotFoundException e) { 453 log.log(Level.FINER, e.toString(), e); 454 455 throw new ConfigException(L.l("load-balance requires Resin Professional")); 456 } 457 } 458 } 459 460 public class Forward extends Program { 461 private String _target; 462 private Pattern _regexp; 463 464 467 public void setRegexp(String regexp) 468 { 469 _regexp = Pattern.compile(regexp); 470 } 471 472 public void setTarget(String target) 473 { 474 _target = target; 475 } 476 477 public FilterChain dispatch(String uri) 478 { 479 Matcher matcher = _regexp.matcher(uri); 480 481 if (matcher.find()) { 482 matcher.reset(); 483 484 String targetUri = matcher.replaceAll(_target); 485 486 FilterChain chain = new ForwardFilterChain(targetUri); 487 488 return chain; 489 } 490 else 491 return null; 492 } 493 494 @PostConstruct 495 public void init() 496 throws ConfigException 497 { 498 if (_regexp == null) 499 throw new ConfigException(L.l("redirect needs 'regexp' attribute.")); 500 if (_target == null) 501 throw new ConfigException(L.l("redirect needs 'target' attribute.")); 502 } 503 } 504 505 public static class Error extends Program { 506 private int _code; 507 private Pattern _regexp; 508 509 Error(int code) 510 { 511 _code = code; 512 } 513 514 517 public void setRegexp(String regexp) 518 { 519 _regexp = Pattern.compile(regexp); 520 } 521 522 public FilterChain dispatch(String uri) 523 { 524 Matcher matcher = _regexp.matcher(uri); 525 526 if (matcher.find()) { 527 matcher.reset(); 528 529 return new ErrorFilterChain(_code); 530 } 531 else 532 return null; 533 } 534 535 @PostConstruct 536 public void init() 537 throws ConfigException 538 { 539 if (_regexp == null) 540 throw new ConfigException(L.l("error needs 'regexp' attribute.")); 541 } 542 } 543 544 static { 545 ACCEPT_CHAIN = new FilterChain() { 546 public void doFilter(ServletRequest req, ServletResponse res) {} 547 }; 548 } 549 } 550 551 | Popular Tags |