litte step refactor

This commit is contained in:
coderfengyun 2013-12-18 17:19:03 +08:00
parent 4750a06c52
commit 51d0bab4d9
7 changed files with 103 additions and 52 deletions

View File

@ -39,12 +39,12 @@ public class ProxyServer implements Runnable {
}
}
public void processResponse(HttpRequestHeader header, byte[] response)
throws Exception {
public void processResponse(HttpRequestHeader header, byte[] requestBody,
byte[] response) throws Exception {
for (Enumeration<Observer> e = this.listeners.elements(); e
.hasMoreElements();) {
Observer pl = (Observer) e.nextElement();
pl.processResponse(header, response);
pl.processResponse(header, requestBody, response);
}
}
@ -81,7 +81,7 @@ public class ProxyServer implements Runnable {
byte[] paramArrayOfByte) throws Exception;
public abstract void processResponse(
HttpRequestHeader paramHttpRequestHeader,
HttpRequestHeader paramHttpRequestHeader, byte[] requestBody,
byte[] paramArrayOfByte) throws Exception;
}
}

View File

@ -168,7 +168,7 @@ public class RequestHandler implements Runnable {
}
log.trace("transferred response");
this.proxyServer.processResponse(this.header,
this.proxyServer.processResponse(this.header, requestBody,
this.buffer.toByteArray());
log.trace("processed response");
}

View File

@ -47,17 +47,15 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
protected boolean headersExist = false;
private IScriptAdapter scriptAdapter;
private String testName;
@SuppressWarnings("unused")
private String testPath;
private boolean ignoreNextResponse;
private Pattern[] namePatterns;
private LinkedList<BehaviorBaseModel> outstandingInserts;
private Thread insertThread;
private String charset;
private long timeElapsedSinceLastestRequest = 0L;
private Date latestRequestDate = new Date();
private boolean isFirstRequest = true;
private int requestCount = 0;
private long timeElapsedSinceLastestRequest;
private Date latestRequestDate;
private int requestCount;
private boolean firstHtmlResponse;
static final boolean $assertionsDisabled = !(AbstractCodeGenerator.class
.desiredAssertionStatus());
@ -74,7 +72,16 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
return requestCount;
}
private boolean isFirstHtmlResponse() {
return firstHtmlResponse;
}
private void setFirstHtmlResponse(boolean firstHtmlResponse) {
this.firstHtmlResponse = firstHtmlResponse;
}
public AbstractCodeGenerator(IScriptAdapter adapter, String[] nameRegExps) {
this.reset();
this.scriptAdapter = adapter;
this.namePatterns = new Pattern[nameRegExps.length];
for (int i = 0; i < nameRegExps.length; ++i)
@ -86,6 +93,15 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
this.insertThread.start();
}
private void reset() {
this.latestRequestDate = new Date();
this.assertNumber = 0;
this.requestCount = 0;
this.latestRequestDate = new Date();
this.timeElapsedSinceLastestRequest = 0L;
this.setFirstHtmlResponse(true);
}
public IScriptAdapter getScriptAdapter() {
return this.scriptAdapter;
}
@ -95,7 +111,6 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
}
private void setTestPath(String testPath) {
this.testPath = testPath;
}
public long getTimeElapsedSinceLastestRequest() {
@ -103,7 +118,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
}
public boolean isFirstRequest() {
return this.isFirstRequest;
return this.requestCount == 0;
}
public String parseTestName() {
@ -273,7 +288,6 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
doSetData(action.getMultiPartData());
}
this.requestCount++;
doCallUrl(action, method, data_str, cont_len_str);
}
@ -308,8 +322,8 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
return ((HeaderValue[]) (HeaderValue[]) v.toArray(hv));
}
public void processResponse(HttpRequestHeader header, byte[] response)
throws Exception {
public void processResponse(HttpRequestHeader header, byte[] requestBody,
byte[] response) throws Exception {
if (this.ignoreNextResponse) {
log.debug("Ignoring response");
this.ignoreNextResponse = false;
@ -352,8 +366,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
log.debug("Ignoring response because content type is not known: "
+ contentType);
}
if (isFirstRequest())
this.isFirstRequest = false;
}
// TODO: edit this type

View File

@ -19,10 +19,6 @@ import org.bench4q.share.models.agent.scriptrecord.BehaviorBaseModel;
import org.bench4q.share.models.agent.scriptrecord.TimerBehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.bench4q.share.models.agent.scriptrecord.UserBehaviorModel;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Bench4qCodeGenerator extends AbstractCodeGenerator {
static final String TIMER_PROP = "generator.isac.timer";
@ -215,42 +211,29 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
if (htmlStart == -1 || htmlEnd == -1) {
return;
}
parseDocument(rootUrl, responseContent, htmlStart, htmlEnd);
}
private void parseDocument(String rootUrl, String responseContent,
int htmlStart, int htmlEnd) {
// TODO:reset the childrenUrls in scirptAdapter
String htmlContent = responseContent.substring(htmlStart, htmlEnd + 6);
saveToFile(htmlContent);
Document document = Jsoup.parse(htmlContent);
document.setBaseUri(rootUrl);
Elements allChildrenLinks = new Elements();
allChildrenLinks.addAll(document.select("[src]"));
allChildrenLinks.addAll(document.select("link[href]"));
logger.info("Following url shuold in children urls: \n");
for (Element element : allChildrenLinks) {
String childAbsoluteUrl = element.absUrl("src");
if (childAbsoluteUrl.isEmpty()) {
childAbsoluteUrl = element.absUrl("href");
}
int parentBatchId = this.getScriptAdapter()
.getParentBatchIdWithParentUrl(rootUrl);
if (parentBatchId == -1) {
continue;
}
logger.info(childAbsoluteUrl);
this.getScriptAdapter()
.getChildrenUrls()
.add(ChildrenUrl.createChilldUrl(childAbsoluteUrl,
parentBatchId));
}
HtmlDocumentParser documentParser = new HtmlDocumentParser(htmlContent,
rootUrl);
List<ChildrenUrl> childrenUrls = documentParser
.buildParentChildrenRelationship(this.getScriptAdapter()
.getParentBatchIdWithParentUrl(rootUrl));
this.getScriptAdapter().getChildrenUrls().addAll(childrenUrls);
}
private void saveToFile(String htmlContent) {
// For Test
// TODO: For Test
try {
File storeFile = new File("RecordScriptTestCase/baidu.html");
FileUtils.writeStringToFile(storeFile, htmlContent);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

View File

@ -0,0 +1,54 @@
package org.bench4q.master.scriptrecord.httpcapture.generator;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HtmlDocumentParser {
private Document document;
private static Logger logger = Logger.getLogger(HtmlDocumentParser.class);
private Document getDocument() {
return document;
}
private void setDocument(Document document) {
this.document = document;
}
public HtmlDocumentParser(String htmlContent, String baseUrl) {
this.setDocument(Jsoup.parse(htmlContent, baseUrl));
}
public void beginParse() {
}
public List<ChildrenUrl> buildParentChildrenRelationship(int parentBatchId) {
List<ChildrenUrl> result = new ArrayList<ChildrenUrl>();
if (parentBatchId == -1) {
return result;
}
Elements allChildrenLinks = new Elements();
allChildrenLinks.addAll(this.getDocument().select("[src]"));
allChildrenLinks.addAll(this.getDocument().select("link[href]"));
logger.info("Following url shuold in children urls: \n");
for (Element element : allChildrenLinks) {
String childAbsoluteUrl = element.absUrl("src");
if (childAbsoluteUrl.isEmpty()) {
childAbsoluteUrl = element.absUrl("href");
}
logger.info(childAbsoluteUrl);
result.add(ChildrenUrl.createChilldUrl(childAbsoluteUrl,
parentBatchId));
}
return result;
}
}

View File

@ -10,7 +10,7 @@ import javax.xml.bind.annotation.XmlRootElement;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
@XmlRootElement(name = "runScenario")
public class RunScenarioModelTest {
public class RunScenarioModelNew {
private int poolSize;
private List<UsePluginModel> usePlugins;
private List<Page> pages;
@ -44,7 +44,7 @@ public class RunScenarioModelTest {
this.pages = pages;
}
public RunScenarioModelTest() {
public RunScenarioModelNew() {
this.setUsePlugins(new ArrayList<UsePluginModel>());
}
}

View File

@ -45,7 +45,8 @@ public class TestCodeGenerator extends TestRecordBase {
inputStream.close();
outputStream.close();
IScriptGenerator scriptGenerator = this.codeGenerator;
scriptGenerator.processResponse(header, outputStream.toByteArray());
scriptGenerator.processResponse(header, new byte[0],
outputStream.toByteArray());
assertTrue(this.scriptAdapter.getChildrenUrls().size() > 0);
}