1 18 19 package org.apache.jmeter.protocol.http.util.accesslog; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileReader ; 24 import java.io.IOException ; 25 import java.util.StringTokenizer ; 26 import java.util.Vector ; 27 28 import org.apache.jmeter.junit.JMeterTestCase; 29 import org.apache.jorphan.logging.LoggingManager; 30 import org.apache.log.Logger; 31 32 78 79 public class TCLogParser implements LogParser 80 { 81 static Logger log = LoggingManager.getLoggerForClass(); 82 83 public static final String GET = "GET"; 84 public static final String POST = "POST"; 85 86 87 protected String RMETHOD = null; 88 91 protected String URL_PATH = null; 92 97 protected int COUNT = 0; 98 102 protected int PARSECOUNT = -1; 103 protected boolean useFILE = true; 104 105 protected File SOURCE = null; 106 protected String FILENAME = null; 107 protected BufferedReader READER = null; 108 109 112 protected Generator GEN = null; 113 protected Filter FILTER = null; 114 115 117 120 public TCLogParser() 121 { 122 super(); 123 } 124 125 128 public TCLogParser(String source) 129 { 130 setSourceFile(source); 131 } 132 133 137 public void setGenerator(Generator generator) 138 { 139 this.GEN = generator; 140 } 141 142 150 public void setUseParsedFile(boolean file) 151 { 152 this.useFILE = file; 153 } 154 155 162 public void setFilter(Filter filter) 163 { 164 FILTER = filter; 165 } 166 167 171 public void setSourceFile(String source) 172 { 173 this.FILENAME = source; 174 } 175 176 180 public File openFile(String filename) 181 { 182 return new File (filename); 183 } 184 185 189 public boolean parse() 190 { 191 if (this.SOURCE == null) 192 { 193 this.SOURCE = this.openFile(this.FILENAME); 194 } 195 try 196 { 197 if (this.READER == null) 198 { 199 this.READER = new BufferedReader (new FileReader (this.SOURCE)); 200 } 201 parse(this.READER); 202 } 203 catch (Exception exception) 204 { 205 exception.printStackTrace(); 206 } 207 return true; 208 } 209 210 220 public int parse(int count) 221 { 222 if (count > 0) 223 { 224 this.PARSECOUNT = count; 225 } 226 this.parse(); 227 return COUNT; 228 } 229 230 236 protected void parse(BufferedReader breader) 237 { 238 String line = null; 239 try 240 { 241 line = breader.readLine(); 244 if (line == null && COUNT >= this.PARSECOUNT) 245 { 246 this.READER.close(); 247 this.READER = null; 248 this.READER = new BufferedReader (new FileReader (this.SOURCE)); 249 parse(this.READER); 250 } 251 while (line != null) 252 { 253 if (line.length() > 0) 254 { 255 this.parseLine(line); 256 } 257 if (this.PARSECOUNT != -1 && COUNT >= this.PARSECOUNT) 263 { 264 break; 265 } 266 } 267 } 268 catch (IOException ioe) 269 { 270 ioe.printStackTrace(); 271 } 272 } 273 274 279 protected void parseLine(String line) 280 { 281 line = this.cleanURL(line); 284 this.GEN.setMethod(this.RMETHOD); 286 if (FILTER != null) 287 { 288 if (!FILTER.isFiltered(line)) 289 { 290 COUNT++; 292 line = FILTER.filter(line); 296 if (line != null) 297 { 298 createUrl(line); 299 } 300 } 301 } 302 else 303 { 304 COUNT++; 306 createUrl(line); 309 } 310 } 311 312 315 private void createUrl(String line) 316 { 317 String paramString = null; 318 paramString = this.stripFile(line); 320 if(paramString != null) 321 { 322 this.checkParamFormat(line); 323 this.convertStringToJMRequest(paramString); 325 } 326 } 327 328 346 public String cleanURL(String entry) 347 { 348 String url = entry; 349 if (entry.indexOf("\"") > -1 && checkMethod(entry)) 353 { 354 StringTokenizer tokens = null; 355 tokens = this.tokenize(entry, "\""); 359 while (tokens.hasMoreTokens()) 360 { 361 String toke = (String ) tokens.nextToken(); 362 if (checkMethod(toke)) 367 { 368 StringTokenizer token2 = this.tokenize(toke, " "); 369 while (token2.hasMoreTokens()) 370 { 371 String t = (String ) token2.nextElement(); 372 if(t.equalsIgnoreCase(GET)) 373 { 374 RMETHOD = GET; 375 } 376 else if(t.equalsIgnoreCase(POST)) 377 { 378 RMETHOD = POST; 379 } 380 if (t.startsWith("/")) 383 { 384 url = t; 385 break; 386 } 387 } 388 break; 389 } 390 } 391 return url; 392 } 393 else 394 { 395 return url; 397 } 398 } 399 400 407 public boolean checkMethod(String text) 408 { 409 if (text.indexOf("GET") > -1) 410 { 411 this.RMETHOD = GET; 412 return true; 413 } 414 else if (text.indexOf("POST") > -1) 415 { 416 this.RMETHOD = POST; 417 return true; 418 } 419 else 420 { 421 return false; 422 } 423 } 424 425 432 public String stripFile(String url) 433 { 434 if (url.indexOf("?") > -1) 435 { 436 StringTokenizer tokens = this.tokenize(url, "?"); 437 this.URL_PATH = tokens.nextToken(); 438 this.GEN.setPath(URL_PATH); 439 return tokens.nextToken(); 440 } 441 else 442 { 443 this.GEN.setPath(url); 444 return null; 445 } 446 } 447 448 454 public boolean checkURL(String url) 455 { 456 if (url.indexOf("?") > -1) 457 { 458 return true; 459 } 460 else 461 { 462 return false; 463 } 464 } 465 466 472 public boolean checkParamFormat(String text) 473 { 474 if (text.indexOf("&") > -1 && text.indexOf("=") > -1) 475 { 476 return true; 477 } 478 else 479 { 480 return false; 481 } 482 } 483 484 488 public void convertStringToJMRequest(String text) 489 { 490 this.GEN.setParams(this.convertStringtoNVPair(text)); 491 } 492 493 500 public NVPair[] convertStringtoNVPair(String stringparams) 501 { 502 Vector vparams = this.parseParameters(stringparams); 503 NVPair[] nvparams = new NVPair[vparams.size()]; 504 for (int idx = 0; idx < nvparams.length; idx++) 506 { 507 nvparams[idx] = this.parseOneParameter((String ) vparams.get(idx)); 508 } 509 return nvparams; 510 } 511 512 524 protected NVPair parseOneParameter(String parameter) 525 { 526 String name = null; 527 String value = null; 528 try 529 { 530 StringTokenizer param = this.tokenize(parameter, "="); 531 name = param.nextToken(); 532 value = param.nextToken(); 533 } 534 catch (Exception e) 535 { 536 } 540 if (value == null) 541 { 542 value = ""; 543 } 544 return new NVPair(name.trim(), value.trim()); 545 } 546 547 558 protected Vector parseParameters(String parameters) 559 { 560 Vector parsedParams = new Vector (); 561 StringTokenizer paramtokens = this.tokenize(parameters, "&"); 562 while (paramtokens.hasMoreElements()) 563 { 564 parsedParams.add(paramtokens.nextElement()); 565 } 566 return parsedParams; 567 } 568 569 575 public StringTokenizer tokenize(String line, String delim) 576 { 577 return new StringTokenizer (line, delim); 578 } 579 580 public void close() 581 { 582 try 583 { 584 this.READER.close(); 585 this.READER = null; 586 this.SOURCE = null; 587 } 588 catch (IOException e) 589 { 590 } 592 } 593 595 597 public static class Test extends JMeterTestCase 598 { 599 private static final TCLogParser tclp = new TCLogParser(); 600 601 private static final String URL1 = 602 "127.0.0.1 - - [08/Jan/2003:07:03:54 -0500] \"GET /addrbook/ HTTP/1.1\" 200 1981"; 603 604 private static final String URL2 = 605 "127.0.0.1 - - [08/Jan/2003:07:03:54 -0500] \"GET /addrbook?x=y HTTP/1.1\" 200 1981"; 606 607 public void testConstruct() throws Exception 608 { 609 TCLogParser tcp; 610 tcp = new TCLogParser(); 611 assertNull("Should not have set the filename",tcp.FILENAME); 612 613 String file = "testfiles/access.log"; 614 tcp = new TCLogParser(file); 615 assertEquals("Filename should have been saved",file,tcp.FILENAME); 616 } 617 618 public void testcleanURL() throws Exception 619 { 620 tclp.GEN = new StandardGenerator(); 621 tclp.GEN.generateRequest(); 622 String res = tclp.cleanURL(URL1); 623 assertEquals("/addrbook/",res); 624 assertNull(tclp.stripFile(res)); 625 } 626 public void testcheckURL() throws Exception 627 { 628 assertFalse("URL is not have a query",tclp.checkURL(URL1)); 629 assertTrue("URL is a query",tclp.checkURL(URL2)); 630 } 631 } 632 633 } | Popular Tags |