📄 tclogparser.java
字号:
// we filter the line first, before we try
// to separate the URL into file and
// parameters.
line = FILTER.filter(cleanedLine);
if (line != null) {
createUrl(cleanedLine, el);
}
} else {
log.debug("Line was filtered");
}
} else {
log.debug("filter was null");
// increment the current count
count++;
// in the case when the filter is not set, we
// parse all the lines
createUrl(cleanedLine, el);
}
return count;
}
/**
* @param line
*/
private void createUrl(String line, TestElement el) {
String paramString = null;
// check the URL for "?" symbol
paramString = this.stripFile(line, el);
if (paramString != null) {
this.checkParamFormat(line);
// now that we have stripped the file, we can parse the parameters
this.convertStringToJMRequest(paramString, el);
}
}
/**
* The method cleans the URL using the following algorithm.
* <ol>
* <li> check for double quotes
* <li> check the request method
* <li> tokenize using double quotes
* <li> find first token containing request method
* <li> tokenize string using space
* <li> find first token that begins with "/"
* </ol>
* Example Tomcat log entry:
* <p>
* 127.0.0.1 - - [08/Jan/2003:07:03:54 -0500] "GET /addrbook/ HTTP/1.1" 200
* 1981
* <p>
*
* @param entry
* @return cleaned url
*/
public String cleanURL(String entry) {
String url = entry;
// if the string contains atleast one double
// quote and checkMethod is true, go ahead
// and tokenize the string.
if (entry.indexOf("\"") > -1 && checkMethod(entry)) {
StringTokenizer tokens = null;
// we tokenize using double quotes. this means
// for tomcat we should have 3 tokens if there
// isn't any additional information in the logs
tokens = this.tokenize(entry, "\"");
while (tokens.hasMoreTokens()) {
String toke = tokens.nextToken();
// if checkMethod on the token is true
// we tokenzie it using space and escape
// the while loop. Only the first matching
// token will be used
if (checkMethod(toke)) {
StringTokenizer token2 = this.tokenize(toke, " ");
while (token2.hasMoreTokens()) {
String t = (String) token2.nextElement();
if (t.equalsIgnoreCase(GET)) {
RMETHOD = GET;
} else if (t.equalsIgnoreCase(POST)) {
RMETHOD = POST;
}
// there should only be one token
// that starts with slash character
if (t.startsWith("/")) {
url = t;
break;
}
}
break;
}
}
return url;
} else {
// we return the original string
return url;
}
}
/**
* The method checks for POST and GET methods currently. The other methods
* aren't supported yet.
*
* @param text
* @return if method is supported
*/
public boolean checkMethod(String text) {
if (text.indexOf("GET") > -1) {
this.RMETHOD = GET;
return true;
} else if (text.indexOf("POST") > -1) {
this.RMETHOD = POST;
return true;
} else {
return false;
}
}
/**
* Tokenize the URL into two tokens. If the URL has more than one "?", the
* parse may fail. Only the first two tokens are used. The first token is
* automatically parsed and set at URL_PATH.
*
* @param url
* @return String parameters
*/
public String stripFile(String url, TestElement el) {
if (url.indexOf("?") > -1) {
StringTokenizer tokens = this.tokenize(url, "?");
this.URL_PATH = tokens.nextToken();
el.setProperty(HTTPSamplerBase.PATH, URL_PATH);
return tokens.hasMoreTokens() ? tokens.nextToken() : null;
} else {
el.setProperty(HTTPSamplerBase.PATH, url);
return null;
}
}
/**
* Checks the string to make sure it has /path/file?name=value format. If
* the string doesn't have "?", it will return false.
*
* @param url
* @return boolean
*/
public boolean checkURL(String url) {
if (url.indexOf("?") > -1) {
return true;
} else {
return false;
}
}
/**
* Checks the string to see if it contains "&" and "=". If it does, return
* true, so that it can be parsed.
*
* @param text
* @return boolean
*/
public boolean checkParamFormat(String text) {
if (text.indexOf("&") > -1 && text.indexOf("=") > -1) {
return true;
} else {
return false;
}
}
/**
* Convert a single line into XML
*
* @param text
*/
public void convertStringToJMRequest(String text, TestElement el) {
((HTTPSamplerBase) el).parseArguments(text);
}
/**
* Parse the string parameters into NVPair[] array. Once they are parsed, it
* is returned. The method uses parseOneParameter(string) to convert each
* pair.
*
* @param stringparams
*/
public NVPair[] convertStringtoNVPair(String stringparams) {
Vector vparams = this.parseParameters(stringparams);
NVPair[] nvparams = new NVPair[vparams.size()];
// convert the Parameters
for (int idx = 0; idx < nvparams.length; idx++) {
nvparams[idx] = this.parseOneParameter((String) vparams.get(idx));
}
return nvparams;
}
/**
* Method expects name and value to be separated by an equal sign "=". The
* method uses StringTokenizer to make a NVPair object. If there happens to
* be more than one "=" sign, the others are ignored. The chance of a string
* containing more than one is unlikely and would not conform to HTTP spec.
* I should double check the protocol spec to make sure this is accurate.
*
* @param parameter
* to be parsed
* @return NVPair
*/
protected NVPair parseOneParameter(String parameter) {
String name = null;
String value = null;
try {
StringTokenizer param = this.tokenize(parameter, "=");
name = param.nextToken();
value = param.nextToken();
} catch (Exception e) {
// do nothing. it's naive, but since
// the utility is meant to parse access
// logs the formatting should be correct
}
if (value == null) {
value = "";
} else {
if (decode) {
try {
value = URLDecoder.decode(value,"UTF-8");
} catch (UnsupportedEncodingException e) {
log.warn(e.getMessage());
}
}
}
return new NVPair(name.trim(), value.trim());
}
/**
* Method uses StringTokenizer to convert the string into single pairs. The
* string should conform to HTTP protocol spec, which means the name/value
* pairs are separated by the ampersand symbol "&". Some one could write the
* querystrings by hand, but that would be round about and go against the
* purpose of this utility.
*
* @param parameters
* @return Vector
*/
protected Vector parseParameters(String parameters) {
Vector parsedParams = new Vector();
StringTokenizer paramtokens = this.tokenize(parameters, "&");
while (paramtokens.hasMoreElements()) {
parsedParams.add(paramtokens.nextElement());
}
return parsedParams;
}
/**
* Parses the line using java.util.StringTokenizer.
*
* @param line
* line to be parsed
* @param delim
* delimiter
* @return StringTokenizer
*/
public StringTokenizer tokenize(String line, String delim) {
return new StringTokenizer(line, delim);
}
public void close() {
try {
this.READER.close();
this.READER = null;
this.SOURCE = null;
} catch (IOException e) {
// do nothing
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -