This commit is contained in:
coderfengyun 2014-01-14 22:29:41 +08:00
parent 374b7084a7
commit 62dab4fa3d
3 changed files with 285 additions and 280 deletions

View File

@ -1,44 +1,45 @@
package org.bench4q.master.scriptrecord.httpcapture.generator;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.log4j.Logger;
import org.bench4q.master.exception.ExceptionLog;
public class ContentDecoder {
protected Logger logger = Logger.getLogger(ContentDecoder.class);
protected ContentDecoder() {
}
public static ContentDecoder createDecoder(String encodeType) {
if (encodeType == null) {
return new ContentDecoder();
}
if (encodeType.equalsIgnoreCase("gzip")) {
return new GzipDecoder();
} else {
return new ContentDecoder();
}
}
public byte[] decodeContent(InputStream inputStream) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
System.out.println(outputStream.toString());
System.out.println("ouputStream's size is" + outputStream.size());
return outputStream.toByteArray();
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
}
}
}
package org.bench4q.master.scriptrecord.httpcapture.generator;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.log4j.Logger;
import org.bench4q.master.exception.ExceptionLog;
public class ContentDecoder {
protected Logger logger = Logger.getLogger(ContentDecoder.class);
protected ContentDecoder() {
}
public static ContentDecoder createDecoder(String encodeType) {
if (encodeType == null) {
return new ContentDecoder();
}
if (encodeType.equalsIgnoreCase("gzip")) {
return new GzipDecoder();
} else {
return new ContentDecoder();
}
}
public byte[] decodeContent(InputStream inputStream) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
System.out.println(outputStream.toString());
System.out.println("ouputStream's size is" + outputStream.size());
return outputStream.toByteArray();
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
}
}
}

View File

@ -1,74 +1,74 @@
package org.bench4q.master.scriptrecord.httpcapture.generator;
import java.util.HashMap;
public class ResponseHeader {
public static HashMap<String, String> MIME_TYPE = new HashMap<String, String>() {
private static final long serialVersionUID = 1L;
{
put("text/plain", "text/plain");
put("text/html", "text/html");
put("text/comma-separated-values", "text/comma-separated-values");
}
};
private String respCode;
private String contentType;
private String charset;
private String contentEncoding;
private int contentLength;
public String getRespCode() {
return respCode;
}
public void setRespCode(String respCode) {
this.respCode = respCode;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public String getContentEncoding() {
return contentEncoding;
}
public void setContentEncoding(String contentEncoding) {
this.contentEncoding = contentEncoding;
}
public int getContentLength() {
return contentLength;
}
public void setContentLength(int contentLength) {
this.contentLength = contentLength;
}
boolean isValidResponseHeader() {
return (getContentType() != null)
&& (MIME_TYPE.containsKey(getContentType()))
&& (getRespCode() != null);
}
boolean isHtmlContent() {
return getContentType().toLowerCase().compareTo("text/html") == 0;
}
boolean isGoodRequest() {
return this.getRespCode().startsWith("200");
}
}
package org.bench4q.master.scriptrecord.httpcapture.generator;
import java.util.HashMap;
public class ResponseHeader {
public static HashMap<String, String> MIME_TYPE = new HashMap<String, String>() {
private static final long serialVersionUID = 1L;
{
put("text/plain", "text/plain");
put("text/html", "text/html");
put("text/comma-separated-values", "text/comma-separated-values");
}
};
private String respCode;
private String contentType;
private String charset;
private String contentEncoding;
private int contentLength;
public String getRespCode() {
return respCode;
}
public void setRespCode(String respCode) {
this.respCode = respCode;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public String getContentEncoding() {
return contentEncoding;
}
public void setContentEncoding(String contentEncoding) {
this.contentEncoding = contentEncoding;
}
public int getContentLength() {
return contentLength;
}
public void setContentLength(int contentLength) {
this.contentLength = contentLength;
}
boolean isValidResponseHeader() {
return (getContentType() != null)
&& (MIME_TYPE.containsKey(getContentType()))
&& (getRespCode() != null);
}
boolean isHtmlContent() {
return getContentType().toLowerCase().contains("text/html");
}
boolean isGoodRequest() {
return this.getRespCode().startsWith("200");
}
}

View File

@ -1,162 +1,166 @@
package org.bench4q.master.scriptrecord.httpcapture.generator;
import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;
import org.bench4q.master.exception.ExceptionLog;
public class ResponseParser {
private Logger logger = Logger.getLogger(ResponseParser.class);
private byte[] response;
private ResponseHeader responseHeader;
private String responseBody;
private byte[] getResponse() {
return response;
}
private void setResponse(byte[] response) {
this.response = response;
}
public ResponseHeader getResponseHeader() {
return responseHeader;
}
private void setResponseHeader(ResponseHeader responseHeader) {
this.responseHeader = responseHeader;
}
public String getResponseBody() {
return responseBody;
}
private void setResponseBody(String responseBody) {
this.responseBody = responseBody;
}
public ResponseParser(byte[] response) {
this.setResponse(response);
this.setResponseHeader(parseResponseHeader());
if (this.getResponseHeader().isValidResponseHeader()) {
this.parseResponseBody();
}
}
private ResponseHeader parseResponseHeader() {
ResponseHeader result = new ResponseHeader();
result.setCharset(parseCharset());
result.setContentEncoding(parseContentEncoding());
result.setContentLength(Integer.parseInt(parseContentLength()));
result.setContentType(parseContentType());
result.setRespCode(parseResponseCode());
return result;
}
private String preprocess(String respString) {
return respString.toLowerCase();
}
private String parseContentLength() {
String respStr = preprocess(new String(this.getResponse()));
int pos = respStr.indexOf("content-length:");
if (pos > -1) {
pos += 15;
int end = respStr.indexOf("\r\n", pos);
return respStr.substring(pos, end).trim();
}
return "-1";
}
private String parseContentEncoding() {
String respStr = preprocess(new String(this.getResponse()));
int pos = respStr.indexOf("content-encoding:");
if (pos > -1) {
pos += 18;
int end = respStr.indexOf("\r\n", pos);
return respStr.substring(pos, end);
}
return null;
}
private String parseCharset() {
String response = preprocess(new String(this.getResponse()));
String ret = null;
int pos = response.indexOf("content-type:");
if (pos > -1) {
pos += 14;
int end = response.indexOf("\r\n", pos);
int middle = response.indexOf(";", pos);
if (middle > -1 && middle < end) {
ret = response.substring(middle + 1, end);
}
}
if (ret != null) {
int begin = ret.indexOf("charset=");
ret = ret.substring(begin + 8);
}
return ret;
}
private String parseContentType() {
String response = preprocess(new String(this.getResponse()));
String contentType = null;
int pos = response.indexOf("content-type:");
if (pos > -1) {
pos += 14;
int end = response.indexOf("\r\n", pos);
int end2 = response.indexOf(";", pos);
if ((end2 > -1) && (end2 < end))
end = end2;
if (end > -1)
contentType = response.substring(pos, end).trim();
logger.debug(" Content-Type: " + contentType);
} else {
logger.debug(" No content-type header! First few lines:");
StringTokenizer st = new StringTokenizer(response, "\n");
int i = 0;
while ((st.hasMoreTokens()) && (i < 5)) {
logger.debug(st.nextToken());
++i;
}
}
return contentType;
}
private String parseResponseCode() {
String response = preprocess(new String(this.getResponse()));
String respCode = null;
int pos = response.indexOf(" ");
if (pos != -1) {
int end = response.indexOf(" ", pos + 1);
int end2 = response.indexOf("\n", pos + 1);
if ((end2 != -1) && (end2 < end))
end = end2;
if (end != -1)
respCode = response.substring(pos + 1, end).trim();
}
logger.debug("HTTP response code: " + respCode);
return respCode;
}
private void parseResponseBody() {
ContentDecoder contentDecoder = ContentDecoder.createDecoder(this
.getResponseHeader().getContentEncoding());
byte[] contentBodyAfterDecoded = contentDecoder
.decodeContent(new ByteArrayInputStream(this.getResponse(),
this.getResponse().length
- this.getResponseHeader().getContentLength(),
this.getResponseHeader().getContentLength()));
Charset charset = null;
try {
charset = Charset.forName(this.getResponseHeader().getCharset());
} catch (Exception e) {
charset = Charset.forName("utf-8");
logger.error(ExceptionLog.getStackTrace(e));
}
this.setResponseBody(new String(contentBodyAfterDecoded, charset));
}
}
package org.bench4q.master.scriptrecord.httpcapture.generator;
import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;
import org.bench4q.master.exception.ExceptionLog;
public class ResponseParser {
private Logger logger = Logger.getLogger(ResponseParser.class);
private byte[] response;
private ResponseHeader responseHeader;
private String responseBody;
private byte[] getResponse() {
return response;
}
private void setResponse(byte[] response) {
this.response = response;
}
public ResponseHeader getResponseHeader() {
return responseHeader;
}
private void setResponseHeader(ResponseHeader responseHeader) {
this.responseHeader = responseHeader;
}
public String getResponseBody() {
return responseBody;
}
private void setResponseBody(String responseBody) {
this.responseBody = responseBody;
}
public ResponseParser(byte[] response) {
this.setResponse(response);
this.setResponseHeader(parseResponseHeader());
if (this.getResponseHeader().isValidResponseHeader()
&& this.getResponseHeader().isHtmlContent()) {
this.parseResponseBody();
}
}
private ResponseHeader parseResponseHeader() {
ResponseHeader result = new ResponseHeader();
result.setCharset(parseCharset());
result.setContentEncoding(parseContentEncoding());
result.setContentLength(Integer.parseInt(parseContentLength()));
result.setContentType(parseContentType());
result.setRespCode(parseResponseCode());
return result;
}
private String preprocess(String respString) {
return respString.toLowerCase();
}
private String parseContentLength() {
String respStr = preprocess(new String(this.getResponse()));
int pos = respStr.indexOf("content-length:");
if (pos > -1) {
pos += 15;
int end = respStr.indexOf("\r\n", pos);
return respStr.substring(pos, end).trim();
}
return "-1";
}
private String parseContentEncoding() {
String respStr = preprocess(new String(this.getResponse()));
int pos = respStr.indexOf("content-encoding:");
if (pos > -1) {
pos += 18;
int end = respStr.indexOf("\r\n", pos);
return respStr.substring(pos, end);
}
return null;
}
private String parseCharset() {
String response = preprocess(new String(this.getResponse()));
String ret = null;
int pos = response.indexOf("content-type:");
if (pos > -1) {
pos += 14;
int end = response.indexOf("\r\n", pos);
int middle = response.indexOf(";", pos);
if (middle > -1 && middle < end) {
ret = response.substring(middle + 1, end);
}
}
if (ret != null) {
int begin = ret.indexOf("charset=");
ret = ret.substring(begin + 8);
}
return ret;
}
private String parseContentType() {
String response = preprocess(new String(this.getResponse()));
String contentType = null;
int pos = response.indexOf("content-type:");
if (pos > -1) {
pos += 14;
int end = response.indexOf("\r\n", pos);
int end2 = response.indexOf(";", pos);
if ((end2 > -1) && (end2 < end))
end = end2;
if (end > -1)
contentType = response.substring(pos, end).trim();
logger.debug(" Content-Type: " + contentType);
} else {
logger.debug(" No content-type header! First few lines:");
StringTokenizer st = new StringTokenizer(response, "\n");
int i = 0;
while ((st.hasMoreTokens()) && (i < 5)) {
logger.debug(st.nextToken());
++i;
}
}
return contentType;
}
private String parseResponseCode() {
String response = preprocess(new String(this.getResponse()));
String respCode = null;
int pos = response.indexOf(" ");
if (pos != -1) {
int end = response.indexOf(" ", pos + 1);
int end2 = response.indexOf("\n", pos + 1);
if ((end2 != -1) && (end2 < end))
end = end2;
if (end != -1)
respCode = response.substring(pos + 1, end).trim();
}
logger.debug("HTTP response code: " + respCode);
return respCode;
}
private void parseResponseBody() {
ContentDecoder contentDecoder = ContentDecoder.createDecoder(this
.getResponseHeader().getContentEncoding());
byte[] contentBodyAfterDecoded = contentDecoder
.decodeContent(new ByteArrayInputStream(this.getResponse(),
this.getResponse().length
- this.getResponseHeader().getContentLength(),
this.getResponseHeader().getContentLength()));
if (contentBodyAfterDecoded == null) {
return;
}
Charset charset = null;
try {
charset = Charset.forName(this.getResponseHeader().getCharset());
} catch (Exception e) {
charset = Charset.forName("utf-8");
logger.error(ExceptionLog.getStackTrace(e));
}
this.setResponseBody(new String(contentBodyAfterDecoded, charset));
}
}