1 22 23 24 package org.jboss.web.rewrite; 25 26 import java.util.Map ; 27 import java.util.regex.Matcher ; 28 import java.util.regex.Pattern ; 29 30 public class RewriteRule { 31 32 protected RewriteCond[] conditions = new RewriteCond[0]; 33 34 protected ThreadLocal pattern = new ThreadLocal (); 35 protected Substitution substitution = null; 36 37 protected String patternString = null; 38 protected String substitutionString = null; 39 40 public void parse(Map maps) { 41 if (!"-".equals(substitutionString)) { 43 substitution = new Substitution(); 44 substitution.setSub(substitutionString); 45 substitution.parse(maps); 46 } 47 int flags = 0; 49 if (isNocase()) { 50 flags |= Pattern.CASE_INSENSITIVE; 51 } 52 Pattern.compile(patternString, flags); 53 for (int i = 0; i < conditions.length; i++) { 55 conditions[i].parse(maps); 56 } 57 } 58 59 public void addCondition(RewriteCond condition) { 60 RewriteCond[] conditions = new RewriteCond[this.conditions.length + 1]; 61 for (int i = 0; i < this.conditions.length; i++) { 62 conditions[i] = this.conditions[i]; 63 } 64 conditions[this.conditions.length] = condition; 65 this.conditions = conditions; 66 } 67 68 73 public CharSequence evaluate(CharSequence url, Resolver resolver) { 74 Pattern pattern = (Pattern ) this.pattern.get(); 75 if (pattern == null) { 76 int flags = 0; 78 if (isNocase()) { 79 flags |= Pattern.CASE_INSENSITIVE; 80 } 81 pattern = Pattern.compile(patternString, flags); 82 this.pattern.set(pattern); 83 } 84 Matcher matcher = pattern.matcher(url); 85 if (!matcher.matches()) { 86 return null; 88 } 89 boolean done = false; 91 boolean rewrite = true; 92 Matcher lastMatcher = null; 93 int pos = 0; 94 while (!done) { 95 if (pos < conditions.length) { 96 rewrite = conditions[pos].evaluate(matcher, lastMatcher, resolver); 97 if (rewrite) { 98 Matcher lastMatcher2 = conditions[pos].getMatcher(); 99 if (lastMatcher2 != null) { 100 lastMatcher = lastMatcher2; 101 } 102 } else if (!conditions[pos].isOrnext()) { 103 done = true; 104 } 105 pos++; 106 } else { 107 done = true; 108 } 109 } 110 if (rewrite) { 112 if (substitution != null) { 113 return substitution.evaluate(matcher, lastMatcher, resolver); 114 } else { 115 return url; 116 } 117 } else { 118 return null; 119 } 120 } 121 122 123 126 public String toString() { 127 return "RewriteRule " + patternString + " " + substitutionString; 129 } 130 131 132 141 protected boolean chain = false; 142 143 150 protected boolean cookie = false; 151 protected String cookieName = null; 152 protected String cookieValue = null; 153 154 163 protected boolean env = false; 164 protected String envName = null; 165 protected String envValue = null; 166 167 172 protected boolean forbidden = false; 173 174 179 protected boolean gone = false; 180 181 186 protected boolean host = false; 187 188 195 protected boolean last = false; 196 197 205 protected boolean next = false; 206 207 212 protected boolean nocase = false; 213 214 224 protected boolean noescape = false; 225 226 238 protected boolean nosubreq = false; 239 240 251 252 255 256 262 protected boolean qsappend = false; 263 264 280 protected boolean redirect = false; 281 protected int redirectCode = 0; 282 283 290 protected int skip = 0; 291 292 299 protected boolean type = false; 300 protected String typeValue = null; 301 public boolean isChain() { 302 return chain; 303 } 304 public void setChain(boolean chain) { 305 this.chain = chain; 306 } 307 public RewriteCond[] getConditions() { 308 return conditions; 309 } 310 public void setConditions(RewriteCond[] conditions) { 311 this.conditions = conditions; 312 } 313 public boolean isCookie() { 314 return cookie; 315 } 316 public void setCookie(boolean cookie) { 317 this.cookie = cookie; 318 } 319 public String getCookieName() { 320 return cookieName; 321 } 322 public void setCookieName(String cookieName) { 323 this.cookieName = cookieName; 324 } 325 public String getCookieValue() { 326 return cookieValue; 327 } 328 public void setCookieValue(String cookieValue) { 329 this.cookieValue = cookieValue; 330 } 331 public boolean isEnv() { 332 return env; 333 } 334 public void setEnv(boolean env) { 335 this.env = env; 336 } 337 public String getEnvName() { 338 return envName; 339 } 340 public void setEnvName(String envName) { 341 this.envName = envName; 342 } 343 public String getEnvValue() { 344 return envValue; 345 } 346 public void setEnvValue(String envValue) { 347 this.envValue = envValue; 348 } 349 public boolean isForbidden() { 350 return forbidden; 351 } 352 public void setForbidden(boolean forbidden) { 353 this.forbidden = forbidden; 354 } 355 public boolean isGone() { 356 return gone; 357 } 358 public void setGone(boolean gone) { 359 this.gone = gone; 360 } 361 public boolean isLast() { 362 return last; 363 } 364 public void setLast(boolean last) { 365 this.last = last; 366 } 367 public boolean isNext() { 368 return next; 369 } 370 public void setNext(boolean next) { 371 this.next = next; 372 } 373 public boolean isNocase() { 374 return nocase; 375 } 376 public void setNocase(boolean nocase) { 377 this.nocase = nocase; 378 } 379 public boolean isNoescape() { 380 return noescape; 381 } 382 public void setNoescape(boolean noescape) { 383 this.noescape = noescape; 384 } 385 public boolean isNosubreq() { 386 return nosubreq; 387 } 388 public void setNosubreq(boolean nosubreq) { 389 this.nosubreq = nosubreq; 390 } 391 public boolean isQsappend() { 392 return qsappend; 393 } 394 public void setQsappend(boolean qsappend) { 395 this.qsappend = qsappend; 396 } 397 public boolean isRedirect() { 398 return redirect; 399 } 400 public void setRedirect(boolean redirect) { 401 this.redirect = redirect; 402 } 403 public int getRedirectCode() { 404 return redirectCode; 405 } 406 public void setRedirectCode(int redirectCode) { 407 this.redirectCode = redirectCode; 408 } 409 public int getSkip() { 410 return skip; 411 } 412 public void setSkip(int skip) { 413 this.skip = skip; 414 } 415 public Substitution getSubstitution() { 416 return substitution; 417 } 418 public void setSubstitution(Substitution substitution) { 419 this.substitution = substitution; 420 } 421 public boolean isType() { 422 return type; 423 } 424 public void setType(boolean type) { 425 this.type = type; 426 } 427 public String getTypeValue() { 428 return typeValue; 429 } 430 public void setTypeValue(String typeValue) { 431 this.typeValue = typeValue; 432 } 433 434 public String getPatternString() { 435 return patternString; 436 } 437 438 public void setPatternString(String patternString) { 439 this.patternString = patternString; 440 } 441 442 public String getSubstitutionString() { 443 return substitutionString; 444 } 445 446 public void setSubstitutionString(String substitutionString) { 447 this.substitutionString = substitutionString; 448 } 449 450 public boolean isHost() { 451 return host; 452 } 453 454 public void setHost(boolean host) { 455 this.host = host; 456 } 457 458 } 459 | Popular Tags |