1 18 19 package org.apache.jmeter.protocol.http.util.accesslog; 20 21 import java.util.ArrayList ; 22 23 import org.apache.jmeter.junit.JMeterTestCase; 24 import org.apache.oro.text.regex.Pattern; 25 import org.apache.oro.text.regex.Perl5Compiler; 26 import org.apache.oro.text.regex.Perl5Matcher; 27 28 89 90 public class LogFilter implements Filter 91 { 92 93 94 protected boolean CHANGEEXT = false; 95 protected String OLDEXT = null; 96 protected String NEWEXT = null; 97 98 protected String [] INCFILE = null; 99 protected String [] EXCFILE = null; 100 protected boolean FILEFILTER = false; 101 protected boolean USEFILE = true; 102 103 protected String [] INCPTRN = null; 104 protected String [] EXCPTRN = null; 105 protected boolean PTRNFILTER = false; 106 107 protected ArrayList EXCPATTERNS = new ArrayList (); 108 protected ArrayList INCPATTERNS = new ArrayList (); 109 110 protected String NEWFILE = null; 111 112 protected Perl5Matcher MATCHER = null; 113 114 117 public LogFilter() 118 { 119 super(); 120 } 121 122 130 public void setReplaceExtension(String oldext, String newext) 131 { 132 if (oldext != null && newext != null) 133 { 134 this.CHANGEEXT = true; 135 if (oldext.indexOf(".") < 0 && newext.indexOf(".") < 0) 136 { 137 this.OLDEXT = "." + oldext; 138 this.NEWEXT = "." + newext; 139 } 140 else 141 { 142 this.OLDEXT = oldext; 143 this.NEWEXT = newext; 144 } 145 } 146 } 147 148 153 public void includeFiles(String [] filenames) 154 { 155 if (filenames != null && filenames.length > 0) 156 { 157 INCFILE = filenames; 158 this.FILEFILTER = true; 159 } 160 } 161 162 167 public void excludeFiles(String [] filenames) 168 { 169 if (filenames != null && filenames.length > 0) 170 { 171 EXCFILE = filenames; 172 this.FILEFILTER = true; 173 } 174 } 175 176 184 public void includePattern(String [] regexp) 185 { 186 if (regexp != null && regexp.length > 0) 187 { 188 INCPTRN = regexp; 189 this.PTRNFILTER = true; 190 for (int idx = 0; idx < INCPTRN.length; idx++) 193 { 194 this.INCPATTERNS.add(this.createPattern(INCPTRN[idx])); 195 } 196 } 197 } 198 199 208 public void excludePattern(String [] regexp) 209 { 210 if (regexp != null && regexp.length > 0) 211 { 212 EXCPTRN = regexp; 213 this.PTRNFILTER = true; 214 for (int idx = 0; idx < EXCPTRN.length; idx++) 217 { 218 this.EXCPATTERNS.add(this.createPattern(EXCPTRN[idx])); 219 } 220 } 221 } 222 223 240 public boolean isFiltered(String path) 241 { 242 if (this.FILEFILTER || this.PTRNFILTER || this.CHANGEEXT) 246 { 247 if (this.FILEFILTER) 248 { 249 return filterFile(path); 250 } 251 else if (this.PTRNFILTER) 252 { 253 return filterPattern(path); 254 } 255 else if (this.CHANGEEXT) 256 { 257 return replaceExtension(path); 258 } 259 else 260 { 261 return false; 262 } 263 } 264 else 265 { 266 return false; 267 } 268 } 269 270 287 protected boolean filterFile(String file) 288 { 289 if (this.EXCFILE != null) 292 { 293 return excFile(file); 294 } 295 else if (this.INCFILE != null) 296 { 297 return incFile(file); 298 } 299 return false; 300 } 301 302 311 public boolean incFile(String text) 312 { 313 this.USEFILE = false; 318 for (int idx = 0; idx < this.INCFILE.length; idx++) 319 { 320 if (text.indexOf(this.INCFILE[idx]) > -1) 321 { 322 this.USEFILE = true; 323 break; 324 } 325 } 326 return this.USEFILE; 327 } 328 329 338 public boolean excFile(String text) 339 { 340 this.USEFILE = true; 345 boolean exc = false; 346 for (int idx = 0; idx < this.EXCFILE.length; idx++) 347 { 348 if (text.indexOf(this.EXCFILE[idx]) > -1) 349 { 350 exc = true; 351 this.USEFILE = false; 352 break; 353 } 354 } 355 return exc; 356 } 357 358 370 protected boolean filterPattern(String text) 371 { 372 if (MATCHER == null) 373 { 374 MATCHER = new Perl5Matcher(); 375 } 376 if (this.INCPTRN != null) 377 { 378 return incPattern(text); 379 } 380 else if (this.EXCPTRN != null) 381 { 382 return excPattern(text); 383 } 384 return false; 385 } 386 387 394 protected boolean incPattern(String text) 395 { 396 this.USEFILE = false; 397 for (int idx = 0; idx < this.INCPATTERNS.size(); idx++) 398 { 399 if (MATCHER.contains(text, (Pattern) this.INCPATTERNS.get(idx))) 400 { 401 this.USEFILE = true; 402 break; 403 } 404 } 405 return this.USEFILE; 406 } 407 408 415 protected boolean excPattern(String text) 416 { 417 this.USEFILE = true; 418 boolean exc = false; 419 for (int idx = 0; idx < this.EXCPATTERNS.size(); idx++) 420 { 421 if (MATCHER.contains(text, (Pattern) this.EXCPATTERNS.get(idx))) 422 { 423 exc = true; 424 this.USEFILE = false; 425 break; 426 } 427 } 428 return exc; 429 } 430 431 445 public boolean replaceExtension(String text) 446 { 447 int pt = text.indexOf(this.OLDEXT); 448 if (pt > -1) 449 { 450 int extsize = this.OLDEXT.length(); 451 this.NEWFILE = 452 text.substring(0, pt) 453 + this.NEWEXT 454 + text.substring(pt + extsize); 455 return true; 456 } 457 else 458 { 459 return false; 460 } 461 } 462 463 469 public String filter(String text) 470 { 471 if (this.CHANGEEXT) 472 { 473 return this.NEWFILE; 474 } 475 else if (this.USEFILE) 476 { 477 return text; 478 } 479 else 480 { 481 return null; 482 } 483 } 484 485 491 public Pattern createPattern(String pattern) 492 { 493 try 494 { 495 Perl5Compiler comp = new Perl5Compiler(); 496 return comp.compile(pattern, Perl5Compiler.READ_ONLY_MASK); 497 } 498 catch (Exception exception) 499 { 500 exception.printStackTrace(); 501 return null; 502 } 503 } 504 505 507 public static class Test extends JMeterTestCase 508 { 509 510 private static final String TESTSTR = "/test/helloworld.html"; 511 private static final String TESTSTROUT = "/test/helloworld.jsp"; 512 513 private static class TestData{ 514 private final String file; 515 private final boolean exclfile; 516 private final boolean inclfile; 517 private final boolean exclpatt; 518 private final boolean inclpatt; 519 TestData(String f, boolean exf, boolean inf, boolean exp, boolean inp){ 520 file = f; 521 exclfile = exf; 522 inclfile = inf; 523 exclpatt = exp; 524 inclpatt = inp; 525 } 526 } 527 528 private static final String [] INCL = { "hello.html", "index.html", "/index.jsp" }; 529 private static final String [] PATTERNS = { "index", ".jtml" }; 530 private static final TestData[] TESTDATA = { 531 new TestData("/test/hello.jsp", true, false, true, false), 533 new TestData("/test/one/hello.html", false, true, true, false), 534 new TestData("hello.jsp", true, false, true, false), 535 new TestData("hello.htm", true, false, true, false), 536 new TestData("/test/open.jsp", true, false, true, false), 537 new TestData("/test/open.html", true, false, true, false), 538 new TestData("/index.jsp", false, true, false, true), 539 new TestData("/index.jhtml", true, false, false, true), 540 new TestData("newindex.jsp", true, false, false, true), 541 new TestData("oldindex.jsp", true, false, false, true), 542 new TestData("oldindex1.jsp", true, false, false, true), 543 new TestData("oldindex2.jsp", true, false, false, true), 544 new TestData("oldindex3.jsp", true, false, false, true), 545 new TestData("oldindex4.jsp", true, false, false, true), 546 new TestData("oldindex5.jsp", true, false, false, true), 547 new TestData("oldindex6.jsp", true, false, false, true), 548 new TestData("/test/index.htm", true, false, false, true) 549 }; 550 551 552 public void testConstruct() 553 { 554 new LogFilter(); 555 } 556 557 private LogFilter testf; 558 559 public void setUp() 560 { 561 testf = new LogFilter(); 562 } 563 564 public void testReplaceExtension() 565 { 566 testf.setReplaceExtension("html", "jsp"); 567 testf.isFiltered(TESTSTR); assertEquals(TESTSTROUT,testf.filter(TESTSTR)); 569 } 570 571 public void testExcludeFiles() 572 { 573 testf.excludeFiles(INCL); 574 for (int idx = 0; idx < TESTDATA.length; idx++) 575 { 576 TestData td = TESTDATA[idx]; 577 String theFile = td.file; 578 boolean expect = td.exclfile; 579 580 testf.isFiltered(theFile); 581 String line = testf.filter(theFile); 582 if (line != null) 583 { 584 assertTrue("Expect to accept "+theFile,expect); 585 } 586 else 587 { 588 assertFalse("Expect to reject "+theFile,expect); 589 } 590 } 591 } 592 593 public void testIncludeFiles() 594 { 595 testf.includeFiles(INCL); 596 for (int idx = 0; idx < TESTDATA.length; idx++) 597 { 598 TestData td = TESTDATA[idx]; 599 String theFile = td.file; 600 boolean expect = td.inclfile; 601 602 testf.isFiltered(theFile); 603 String line = testf.filter(theFile); 604 if (line != null) 605 { 606 assertTrue("Expect to accept "+theFile,expect); 607 } 608 else 609 { 610 assertFalse("Expect to reject "+theFile,expect); 611 } 612 } 613 614 } 615 616 public void testExcludePattern() 617 { 618 testf.excludePattern(PATTERNS); 619 for (int idx = 0; idx < TESTDATA.length; idx++) 620 { 621 TestData td = TESTDATA[idx]; 622 String theFile = td.file; 623 boolean expect = td.exclpatt; 624 625 testf.isFiltered(theFile); 626 String line = testf.filter(theFile); 627 if (line != null) 628 { 629 assertTrue("Expect to accept "+theFile,expect); 630 } 631 else 632 { 633 assertFalse("Expect to reject "+theFile,expect); 634 } 635 } 636 } 637 638 public void testIncludePattern() 639 { 640 testf.includePattern(PATTERNS); 641 for (int idx = 0; idx < TESTDATA.length; idx++) 642 { 643 TestData td = TESTDATA[idx]; 644 String theFile = td.file; 645 boolean expect = td.inclpatt; 646 647 testf.isFiltered(theFile); 648 String line = testf.filter(theFile); 649 if (line != null) 650 { 651 assertTrue("Expect to accept "+theFile,expect); 652 } 653 else 654 { 655 assertFalse("Expect to reject "+theFile,expect); 656 } 657 } 658 } 659 } 660 } 661 | Popular Tags |