add create script

This commit is contained in:
fanfuxiaoran 2014-03-25 17:35:33 +08:00
parent bac8098f27
commit fb91341d18
3 changed files with 746 additions and 601 deletions

View File

@ -1,353 +1,401 @@
package org.bench4q.web.api;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
import org.bench4q.web.model.BaseResponseModel;
import org.bench4q.web.service.BaseService;
import org.bench4q.web.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.xml.sax.SAXException;
import com.google.gson.Gson;
@Controller
@SessionAttributes("accessToken")
public class ScriptActionController {
private final String baseUrl = "RecordScript";
private ScriptService scriptService;
private final String BASECALLER = "ScriptActionController:";
private String getBaseUrl() {
return baseUrl;
}
private ScriptService getScriptService() {
return scriptService;
}
@Autowired
private void setScriptService(ScriptService scriptService) {
this.scriptService = scriptService;
}
@RequestMapping("loadScript")
public @ResponseBody
BaseResponseModel loadScript(
@ModelAttribute("accessToken") String accessToken)
throws CustomGenericException {
String caller = "ScriptActionController:loadScripts";
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken,
this.getBaseUrl() + "/loadScriptList",
OperateScriptServerResponseModel.class, null, caller);
if (operateScriptServerResponseModel.isSuccess()) {
List<ScriptModel> scripts = operateScriptServerResponseModel
.getScriptModels();
if(scripts==null)
scripts=new ArrayList<ScriptModel>();
return new BaseResponseModel(true, scripts);
} else {
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
}
@RequestMapping("deleteScript")
public @ResponseBody
BaseResponseModel deletescript(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId) throws CustomGenericException {
String caller = new String("ScriptActionComntroller:deleteScript");
String url = this.getBaseUrl() + "/deleteScript";
Map<String, String> params = BaseService.makeParamsMap("scriptId", scriptId);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params, caller);
if (operateScriptServerResponseModel.isSuccess()) {
return new BaseResponseModel(true);
} else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
@RequestMapping("editScript")
public @ResponseBody
BaseResponseModel editScript(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId) throws CustomGenericException {
String caller = this.BASECALLER + "editScript";
String url = this.getBaseUrl() + "/queryScriptById";
Map<String, String> params = BaseService.makeParamsMap("scriptId", scriptId);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params, caller);
if (operateScriptServerResponseModel.isSuccess()) {
List<ScriptModel> scripts = operateScriptServerResponseModel
.getScriptModels();
if (scripts == null || scripts.isEmpty())
return new BaseResponseModel(false, "data empty");
else {
Iterator<ScriptModel> it = scripts.iterator();
ScriptModel script = it.next();
if (script.getScriptContent() == null
|| script.getScriptContent().isEmpty())
return new BaseResponseModel(false, "data empty");
else {
try {
RunScenarioModel result = (RunScenarioModel) ObjectXmlExchange
.fromXml(RunScenarioModel.class,
script.getScriptContent());
Gson gson = new Gson();
String scriptContent = null;
scriptContent = gson.toJson(result);
return new BaseResponseModel(true,
(Object) scriptContent);
} catch (JAXBException e) {
throw new CustomGenericException("1",
"script content is invalidate", caller);
}
}
}
} else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
@RequestMapping("saveEditScript")
public @ResponseBody
BaseResponseModel saveScript(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId, @RequestParam String script)
throws CustomGenericException {
String url = this.getBaseUrl() + "/updateScript";
String caller = this.BASECALLER + "saveScript";
Gson gson = new Gson();
RunScenarioModel scriptModel = gson.fromJson(script,
RunScenarioModel.class);
try{
String scriptContent = ObjectXmlExchange.toXml(RunScenarioModel.class,
scriptModel);
Map<String, String> params = BaseService.makeParamsMap("scriptId", scriptId);
params.put("content", scriptContent);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params, caller);
if (operateScriptServerResponseModel.isSuccess()) {
return new BaseResponseModel(true);
} else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
catch(JAXBException e){
return new BaseResponseModel(false,e.getMessage());
}
}
@RequestMapping("startRecordServer")
public @ResponseBody
BaseResponseModel startRecordServer(
@ModelAttribute("accessToken") String accessToken)
throws JAXBException, CustomGenericException {
String url = this.getBaseUrl() + "/startScriptRecordServer";
String caller = BASECALLER + "startRecordServer";
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, null, caller);
if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true,
operateScriptServerResponseModel);
else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
@RequestMapping("stopRecordServer")
public @ResponseBody
BaseResponseModel stopRecordServer(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String port, @RequestParam String fileNameUUID)
throws CustomGenericException {
String caller = BASECALLER + "stopeRecordServer";
String url = this.getBaseUrl() + "/stopScriptRecordServer";
Map<String, String> params = BaseService.makeParamsMap("port", port);
params.put("fileNameUUID", fileNameUUID);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params, caller);
if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true);
else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
@RequestMapping("saveRecordScript")
public @ResponseBody
BaseResponseModel saveScriptToDB(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String port, @RequestParam String scriptName,
@RequestParam String fileNameUUID) throws CustomGenericException {
String caller = BASECALLER + "saveRecordScript";
String url = this.getBaseUrl() + "/saveScriptToDB";
Map<String, String> params = BaseService.makeParamsMap("scriptName",
scriptName);
params.put("port", port);
params.put("fileNameUUID", fileNameUUID);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params, caller);
if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true);
else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
@RequestMapping("uploadScript")
@ResponseBody
public BaseResponseModel uploadScript(
@ModelAttribute("accessToken") String accessToken,
@RequestParam("script") CommonsMultipartFile script,
@RequestParam String scriptName) throws CustomGenericException {
String caller=BASECALLER+"uploadScript";
String url=this.getBaseUrl()+"/uploadScript"+"/"+scriptName;
if (script.isEmpty()) {
return new BaseResponseModel(false,"empty file");
}
if (scriptName == null || scriptName.equals(""))
return new BaseResponseModel(false,"empty file name");
try{
String scenarioModel = new String(script.getBytes());
ObjectXmlExchange.fromXml(RunScenarioModel.class, scenarioModel);
OperateScriptServerResponseModel operateScriptServerResponseModel=
(OperateScriptServerResponseModel)this.getScriptService().
getCommunicateWithMaster().getResponseModelByPut(accessToken, url, scenarioModel, OperateScriptServerResponseModel.class, caller);
if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true,(Object)new String("upload script:" + scriptName + " success!"));
else
return new BaseResponseModel(false,operateScriptServerResponseModel.getFailCauseString());
} catch (JAXBException e) {
return new BaseResponseModel(false,"Failed:invalidated script file!");
}
}
@RequestMapping("validateScript")
public @ResponseBody
String validateScript(@ModelAttribute("accessToken") String accessToken,
@RequestParam String content) {
String result;
String message = "";
String schemaFileName = "src/main/webapp/WEB-INF/validateScript.xsd";
content = content.replace("runScenario", "runscenario")
.replace("poolSize", "poolsize")
.replace("usePlugin", "useplugin")
.replace("userBehavior", "userbehavior")
.replace("timerBehavior", "timerbehavior")
.replace("userbehaviors", "behaviors");
try {
result = validateXmlWithSchema(schemaFileName, content);
if (result == "true") {
message = "Success";
} else {
message = URLEncoder.encode(result, "UTF-8").replace("+", "");
}
} catch (Exception e) {
System.out.println("analysis of failure:" + e.toString());
message = "failed to read schema file";
}
// System.out.println(message);
return message;
}
private String validateXmlWithSchema(String schemaFileName, String xmlScript)
throws SAXException, IOException {
String validationResult = "";
SchemaFactory schemaFactory = SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema");
File schemaFile = new File(schemaFileName);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
InputStream script = new ByteArrayInputStream(xmlScript.getBytes());
Source source = new StreamSource(script);
try {
// System.out.println("enter validateXmlWithSchema");
validator.validate(source);
validationResult = "true";
} catch (Exception ex) {
validationResult = ex.toString();
}
return validationResult;
}
@RequestMapping("readSchemaContent")
public @ResponseBody
String readSchemaContent() throws Exception {
String schema = "";
FileReader fr = new FileReader("src/main/webapp/WEB-INF/schema.json");
BufferedReader br = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
while ((schema = br.readLine()) != null) {
sb.append(schema);
}
br.close();
String str = sb.toString();
return str;
}
}
package org.bench4q.web.api;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
import org.bench4q.web.model.BaseResponseModel;
import org.bench4q.web.service.BaseService;
import org.bench4q.web.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.xml.sax.SAXException;
import com.google.gson.Gson;
@Controller
@SessionAttributes("accessToken")
public class ScriptActionController {
private final String baseUrl = "RecordScript";
private ScriptService scriptService;
private final String BASECALLER = "ScriptActionController:";
private String getBaseUrl() {
return baseUrl;
}
private ScriptService getScriptService() {
return scriptService;
}
@Autowired
private void setScriptService(ScriptService scriptService) {
this.scriptService = scriptService;
}
@RequestMapping("loadScript")
public @ResponseBody
BaseResponseModel loadScript(
@ModelAttribute("accessToken") String accessToken)
throws CustomGenericException {
String caller = "ScriptActionController:loadScripts";
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken,
this.getBaseUrl() + "/loadScriptList",
OperateScriptServerResponseModel.class, null, caller);
if (operateScriptServerResponseModel.isSuccess()) {
List<ScriptModel> scripts = operateScriptServerResponseModel
.getScriptModels();
if (scripts == null)
scripts = new ArrayList<ScriptModel>();
return new BaseResponseModel(true, scripts);
} else {
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
}
@RequestMapping("deleteScript")
public @ResponseBody
BaseResponseModel deletescript(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId) throws CustomGenericException {
String caller = new String("ScriptActionComntroller:deleteScript");
String url = this.getBaseUrl() + "/deleteScript";
Map<String, String> params = BaseService.makeParamsMap("scriptId",
scriptId);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params, caller);
if (operateScriptServerResponseModel.isSuccess()) {
return new BaseResponseModel(true);
} else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
@RequestMapping("editScript")
public @ResponseBody
BaseResponseModel editScript(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId) throws CustomGenericException {
String caller = this.BASECALLER + "editScript";
String url = this.getBaseUrl() + "/queryScriptById";
Map<String, String> params = BaseService.makeParamsMap("scriptId",
scriptId);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params, caller);
if (operateScriptServerResponseModel.isSuccess()) {
List<ScriptModel> scripts = operateScriptServerResponseModel
.getScriptModels();
if (scripts == null || scripts.isEmpty())
return new BaseResponseModel(false, "data empty");
else {
Iterator<ScriptModel> it = scripts.iterator();
ScriptModel script = it.next();
if (script.getScriptContent() == null
|| script.getScriptContent().isEmpty())
return new BaseResponseModel(false, "data empty");
else {
try {
RunScenarioModel result = (RunScenarioModel) ObjectXmlExchange
.fromXml(RunScenarioModel.class,
script.getScriptContent());
Gson gson = new Gson();
String scriptContent = null;
scriptContent = gson.toJson(result);
return new BaseResponseModel(true,
(Object) scriptContent);
} catch (JAXBException e) {
throw new CustomGenericException("1",
"script content is invalidate", caller);
}
}
}
} else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
@RequestMapping("saveEditScript")
public @ResponseBody
BaseResponseModel saveScript(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId, @RequestParam String script)
throws CustomGenericException {
String url = this.getBaseUrl() + "/updateScript";
String caller = this.BASECALLER + "saveScript";
Gson gson = new Gson();
RunScenarioModel scriptModel = gson.fromJson(script,
RunScenarioModel.class);
try {
String scriptContent = ObjectXmlExchange.toXml(
RunScenarioModel.class, scriptModel);
Map<String, String> params = BaseService.makeParamsMap("scriptId",
scriptId);
params.put("content", scriptContent);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params,
caller);
if (operateScriptServerResponseModel.isSuccess()) {
return new BaseResponseModel(true);
} else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
} catch (JAXBException e) {
return new BaseResponseModel(false, e.getMessage());
}
}
@RequestMapping("startRecordServer")
public @ResponseBody
BaseResponseModel startRecordServer(
@ModelAttribute("accessToken") String accessToken)
throws JAXBException, CustomGenericException {
String url = this.getBaseUrl() + "/startScriptRecordServer";
String caller = BASECALLER + "startRecordServer";
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, null, caller);
if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true, operateScriptServerResponseModel);
else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
@RequestMapping("stopRecordServer")
public @ResponseBody
BaseResponseModel stopRecordServer(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String port, @RequestParam String fileNameUUID)
throws CustomGenericException {
String caller = BASECALLER + "stopeRecordServer";
String url = this.getBaseUrl() + "/stopScriptRecordServer";
Map<String, String> params = BaseService.makeParamsMap("port", port);
params.put("fileNameUUID", fileNameUUID);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params, caller);
if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true);
else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
@RequestMapping("saveRecordScript")
public @ResponseBody
BaseResponseModel saveScriptToDB(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String port, @RequestParam String scriptName,
@RequestParam String fileNameUUID) throws CustomGenericException {
String caller = BASECALLER + "saveRecordScript";
String url = this.getBaseUrl() + "/saveScriptToDB";
Map<String, String> params = BaseService.makeParamsMap("scriptName",
scriptName);
params.put("port", port);
params.put("fileNameUUID", fileNameUUID);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService()
.getCommunicateWithMaster()
.getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params, caller);
if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true);
else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
}
@RequestMapping("uploadScript")
@ResponseBody
public BaseResponseModel uploadScript(
@ModelAttribute("accessToken") String accessToken,
@RequestParam("script") CommonsMultipartFile script,
@RequestParam String scriptName) throws CustomGenericException {
if (script.isEmpty()) {
return new BaseResponseModel(false, "empty file");
}
if (scriptName == null || scriptName.equals(""))
return new BaseResponseModel(false, "empty file name");
try {
String scenarioModel = new String(script.getBytes());
ObjectXmlExchange.fromXml(RunScenarioModel.class, scenarioModel);
OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptService().uploadScript(accessToken,
scenarioModel, scriptName);
if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true, (Object) new String(
"upload script:" + scriptName + " success!"));
else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
} catch (JAXBException e) {
return new BaseResponseModel(false,
"Failed:invalidated script file!");
}
}
@RequestMapping("validateScript")
public @ResponseBody
String validateScript(@ModelAttribute("accessToken") String accessToken,
@RequestParam String content) {
String result;
String message = "";
String schemaFileName = "src/main/webapp/WEB-INF/validateScript.xsd";
content = content.replace("runScenario", "runscenario")
.replace("poolSize", "poolsize")
.replace("usePlugin", "useplugin")
.replace("userBehavior", "userbehavior")
.replace("timerBehavior", "timerbehavior")
.replace("userbehaviors", "behaviors");
try {
result = validateXmlWithSchema(schemaFileName, content);
if (result == "true") {
message = "Success";
} else {
message = URLEncoder.encode(result, "UTF-8").replace("+", "");
}
} catch (Exception e) {
System.out.println("analysis of failure:" + e.toString());
message = "failed to read schema file";
}
// System.out.println(message);
return message;
}
private String validateXmlWithSchema(String schemaFileName, String xmlScript)
throws SAXException, IOException {
String validationResult = "";
SchemaFactory schemaFactory = SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema");
File schemaFile = new File(schemaFileName);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
InputStream script = new ByteArrayInputStream(xmlScript.getBytes());
Source source = new StreamSource(script);
try {
// System.out.println("enter validateXmlWithSchema");
validator.validate(source);
validationResult = "true";
} catch (Exception ex) {
validationResult = ex.toString();
}
return validationResult;
}
@RequestMapping("readSchemaContent")
public @ResponseBody
String readSchemaContent() throws Exception {
String schema = "";
FileReader fr = new FileReader("src/main/webapp/WEB-INF/schema.json");
BufferedReader br = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
while ((schema = br.readLine()) != null) {
sb.append(schema);
}
br.close();
String str = sb.toString();
return str;
}
@RequestMapping(value = "uploadEditedScript")
@ResponseBody
public BaseResponseModel uploadPluginEditedScript(
@ModelAttribute("accessToken") String accessToken, ModelMap model,
@RequestBody List<BehaviorModel> behaviorModels,
@RequestBody List<UsePluginModel> usePluginModels,
@RequestBody String scriptName) throws CustomGenericException {
if (!validateScript(behaviorModels, usePluginModels))
return new BaseResponseModel(false, "invalidate script");
RunScenarioModel runScenarioModel = this.getScriptService()
.createRunScenarioModel(behaviorModels, usePluginModels);
try {
String script = ObjectXmlExchange.toXml(RunScenarioModel.class,
runScenarioModel);
OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptService().uploadScript(accessToken, script,
scriptName);
if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true, (Object) new String(
"upload script:" + scriptName + " success!"));
else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
} catch (JAXBException e) {
return new BaseResponseModel(false,
"Failed:invalidated script file!");
}
}
private boolean validateScript(List<BehaviorModel> behaviorModels,
List<UsePluginModel> usePluginModels) {
if (behaviorModels == null || behaviorModels.size() == 0)
return false;
if (usePluginModels == null || usePluginModels.size() == 0)
return false;
return true;
}
}

View File

@ -1,245 +1,294 @@
package org.bench4q.web.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBException;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BatchModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.PageModel;
import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
import org.springframework.stereotype.Component;
@Component
public class ScriptService extends BaseService {
private String baseUrl = "RecordScript";
public String getBaseUrl() {
return baseUrl;
}
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
public ScriptModel queryScript(int id, String accessToken)
throws CustomGenericException {
// check script id and return throw exception
String caller = "ScriptService:queryScript";
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getCommunicateWithMaster().getResponseModel(accessToken,
this.getBaseUrl() + "/queryScriptById",
OperateScriptServerResponseModel.class,
makeParamsMap("scriptId", String.valueOf(id)), caller);
if (operateScriptServerResponseModel.isSuccess())
return operateScriptServerResponseModel.getScriptModels().get(0);
else
throw new CustomGenericException("5", "query script failed", caller
+ ":" + id);
}
public List<ScriptModel> queryScripts(List<Integer> scriptIds,
String accessToken) throws CustomGenericException {
String caller = "ScriptService:queryScript";
List<ScriptModel> scriptModels = new ArrayList<ScriptModel>();
for (Integer scriptId : scriptIds) {
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getCommunicateWithMaster()
.getResponseModel(
accessToken,
this.getBaseUrl() + "/queryScriptById",
OperateScriptServerResponseModel.class,
makeParamsMap("scriptId", String.valueOf(scriptId)),
caller);
if (operateScriptServerResponseModel.isSuccess())
scriptModels.add(operateScriptServerResponseModel
.getScriptModels().get(0));
else
throw new CustomGenericException("5", "query script failed",
caller + ":" + scriptId);
}
return scriptModels;
}
public Map<String, Integer> getScriptNameIdMap(
List<ScriptModel> scriptModels) throws CustomGenericException {
Map<String, Integer> map = new HashMap<String, Integer>();
for (ScriptModel scriptModel : scriptModels) {
map.put(scriptModel.getName(), scriptModel.getId());
}
return map;
}
public Map<Integer, String> getScriptIdNameMap(
List<ScriptModel> scriptModels) throws CustomGenericException {
Map<Integer, String> map = new HashMap<Integer, String>();
for (ScriptModel scriptModel : scriptModels) {
map.put(scriptModel.getId(), scriptModel.getName());
}
return map;
}
public Map<String, Integer> getPageNameAndIdMap(
List<ScriptModel> scriptModels) throws JAXBException {
try {
Map<String, Integer> map = new HashMap<String, Integer>();
for (ScriptModel scriptModel : scriptModels) {
Map<String, Integer> pageMap = getPageNameAndIdMap((RunScenarioModel)
ObjectXmlExchange.fromXml(RunScenarioModel.class,
scriptModel.getScriptContent()));
if (pageMap != null)
map.putAll(pageMap);
}
return map;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
private Map<String, Integer> getPageNameAndIdMap(
RunScenarioModel runScenarioModel) {
List<PageModel> pageModels = runScenarioModel.getPages();
try {
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < pageModels.size(); i++) {
map.put("page_" + i, i);
}
return map;
} catch (NullPointerException e) {
return null;
}
}
public Map<String, Integer> getBehaviorNameAndIdMap(
List<ScriptModel> scriptModels) throws JAXBException {
try {
Map<String, Integer> map = new HashMap<String, Integer>();
for (ScriptModel scriptModel: scriptModels) {
Map<String, Integer> behaviorMap = getBehaviorNameAndIdMap((RunScenarioModel)
ObjectXmlExchange.fromXml(RunScenarioModel.class,
scriptModel.getScriptContent()));
if (behaviorMap != null)
map.putAll(behaviorMap);
}
return map;
} catch (NullPointerException e) {
return null;
}
}
public List<String> getScriptPages(RunScenarioModel runScenarioModel) {
try {
List<String> scriptPages = new ArrayList<String>();
for (int i = 1; i < scriptPages.size(); i++) {
scriptPages.add("page_" + i);
}
return scriptPages;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
public List<String> getScriptBehaviorUrl(RunScenarioModel runScenarioModel) {
List<BehaviorModel> behaviorModelsWithUrl = getBehaviorModelsHasUrl(runScenarioModel);
if (behaviorModelsWithUrl == null)
return null;
List<String> urList = new ArrayList<String>();
for (BehaviorModel behaviorModelWithUrl : behaviorModelsWithUrl) {
String url = generateUrl(behaviorModelWithUrl.getParameters());
urList.add(url);
}
return urList;
}
public Map<String, Integer> getBehaviorNameAndIdMap(
RunScenarioModel runScenarioModel) {
try {
Map<String, Integer> map = new HashMap<String, Integer>();
List<BehaviorModel> behaviorModels = this
.getBehaviorModelsHasUrl(runScenarioModel);
if (behaviorModels == null)
return null;
for (BehaviorModel behaviorModel : behaviorModels) {
String url = this.generateUrl(behaviorModel.getParameters());
if (url != null)
map.put(url, behaviorModel.getId());
}
return map;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
private String generateUrl(List<ParameterModel> parameterModels) {
String url = new String("");
int i = 0;
for (ParameterModel parameterModel : parameterModels) {
if (i == 1 && parameterModel.getValue() != null)
url += "?" + parameterModel.getValue();
if (i > 1 && parameterModel.getValue() != null)
url += "&" + parameterModel.getValue();
i++;
}
if (url.length() <= 0)
return null;
return url;
}
private List<BehaviorModel> getBehaviorModelsHasUrl(
RunScenarioModel runScenarioModel) {
try {
List<BehaviorModel> behaviorModels = getBehaviorModels(runScenarioModel);
if (behaviorModels == null)
return null;
List<BehaviorModel> behaviorModelsWithUrl = new ArrayList<BehaviorModel>();
for (BehaviorModel behaviorModel : behaviorModels) {
List<ParameterModel> parameterModels = new ArrayList<ParameterModel>();
parameterModels.addAll(behaviorModel.getParameters());
for (ParameterModel parameterModel : parameterModels) {
if (parameterModel.getKey().equals("url"))
behaviorModelsWithUrl.add(behaviorModel);
break;
}
}
return behaviorModelsWithUrl;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
private List<BehaviorModel> getBehaviorModels(
RunScenarioModel runScenarioModel) {
try {
List<BatchModel> batchModels = new ArrayList<BatchModel>();
for (PageModel pageModel : runScenarioModel.getPages())
batchModels.addAll(pageModel.getBatches());
List<BehaviorModel> behaviorModels = new ArrayList<BehaviorModel>();
for (BatchModel batchModel : batchModels)
behaviorModels.addAll(batchModel.getBehaviors());
return behaviorModels;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
}
package org.bench4q.web.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBException;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BatchModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.PageModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
import org.springframework.stereotype.Component;
@Component
public class ScriptService extends BaseService {
private String baseUrl = "RecordScript";
public String getBaseUrl() {
return baseUrl;
}
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
public ScriptModel queryScript(int id, String accessToken)
throws CustomGenericException {
// check script id and return throw exception
String caller = "ScriptService:queryScript";
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getCommunicateWithMaster().getResponseModel(accessToken,
this.getBaseUrl() + "/queryScriptById",
OperateScriptServerResponseModel.class,
makeParamsMap("scriptId", String.valueOf(id)), caller);
if (operateScriptServerResponseModel.isSuccess())
return operateScriptServerResponseModel.getScriptModels().get(0);
else
throw new CustomGenericException("5", "query script failed", caller
+ ":" + id);
}
public List<ScriptModel> queryScripts(List<Integer> scriptIds,
String accessToken) throws CustomGenericException {
String caller = "ScriptService:queryScript";
List<ScriptModel> scriptModels = new ArrayList<ScriptModel>();
for (Integer scriptId : scriptIds) {
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getCommunicateWithMaster()
.getResponseModel(
accessToken,
this.getBaseUrl() + "/queryScriptById",
OperateScriptServerResponseModel.class,
makeParamsMap("scriptId", String.valueOf(scriptId)),
caller);
if (operateScriptServerResponseModel.isSuccess())
scriptModels.add(operateScriptServerResponseModel
.getScriptModels().get(0));
else
throw new CustomGenericException("5", "query script failed",
caller + ":" + scriptId);
}
return scriptModels;
}
public Map<String, Integer> getScriptNameIdMap(
List<ScriptModel> scriptModels) throws CustomGenericException {
Map<String, Integer> map = new HashMap<String, Integer>();
for (ScriptModel scriptModel : scriptModels) {
map.put(scriptModel.getName(), scriptModel.getId());
}
return map;
}
public Map<Integer, String> getScriptIdNameMap(
List<ScriptModel> scriptModels) throws CustomGenericException {
Map<Integer, String> map = new HashMap<Integer, String>();
for (ScriptModel scriptModel : scriptModels) {
map.put(scriptModel.getId(), scriptModel.getName());
}
return map;
}
public Map<String, Integer> getPageNameAndIdMap(
List<ScriptModel> scriptModels) throws JAXBException {
try {
Map<String, Integer> map = new HashMap<String, Integer>();
for (ScriptModel scriptModel : scriptModels) {
Map<String, Integer> pageMap = getPageNameAndIdMap((RunScenarioModel) ObjectXmlExchange
.fromXml(RunScenarioModel.class,
scriptModel.getScriptContent()));
if (pageMap != null)
map.putAll(pageMap);
}
return map;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
private Map<String, Integer> getPageNameAndIdMap(
RunScenarioModel runScenarioModel) {
List<PageModel> pageModels = runScenarioModel.getPages();
try {
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < pageModels.size(); i++) {
map.put("page_" + i, i);
}
return map;
} catch (NullPointerException e) {
return null;
}
}
public Map<String, Integer> getBehaviorNameAndIdMap(
List<ScriptModel> scriptModels) throws JAXBException {
try {
Map<String, Integer> map = new HashMap<String, Integer>();
for (ScriptModel scriptModel : scriptModels) {
Map<String, Integer> behaviorMap = getBehaviorNameAndIdMap((RunScenarioModel) ObjectXmlExchange
.fromXml(RunScenarioModel.class,
scriptModel.getScriptContent()));
if (behaviorMap != null)
map.putAll(behaviorMap);
}
return map;
} catch (NullPointerException e) {
return null;
}
}
public List<String> getScriptPages(RunScenarioModel runScenarioModel) {
try {
List<String> scriptPages = new ArrayList<String>();
for (int i = 1; i < scriptPages.size(); i++) {
scriptPages.add("page_" + i);
}
return scriptPages;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
public List<String> getScriptBehaviorUrl(RunScenarioModel runScenarioModel) {
List<BehaviorModel> behaviorModelsWithUrl = getBehaviorModelsHasUrl(runScenarioModel);
if (behaviorModelsWithUrl == null)
return null;
List<String> urList = new ArrayList<String>();
for (BehaviorModel behaviorModelWithUrl : behaviorModelsWithUrl) {
String url = generateUrl(behaviorModelWithUrl.getParameters());
urList.add(url);
}
return urList;
}
public Map<String, Integer> getBehaviorNameAndIdMap(
RunScenarioModel runScenarioModel) {
try {
Map<String, Integer> map = new HashMap<String, Integer>();
List<BehaviorModel> behaviorModels = this
.getBehaviorModelsHasUrl(runScenarioModel);
if (behaviorModels == null)
return null;
for (BehaviorModel behaviorModel : behaviorModels) {
String url = this.generateUrl(behaviorModel.getParameters());
if (url != null)
map.put(url, behaviorModel.getId());
}
return map;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
private String generateUrl(List<ParameterModel> parameterModels) {
String url = new String("");
int i = 0;
for (ParameterModel parameterModel : parameterModels) {
if (i == 1 && parameterModel.getValue() != null)
url += "?" + parameterModel.getValue();
if (i > 1 && parameterModel.getValue() != null)
url += "&" + parameterModel.getValue();
i++;
}
if (url.length() <= 0)
return null;
return url;
}
private List<BehaviorModel> getBehaviorModelsHasUrl(
RunScenarioModel runScenarioModel) {
try {
List<BehaviorModel> behaviorModels = getBehaviorModels(runScenarioModel);
if (behaviorModels == null)
return null;
List<BehaviorModel> behaviorModelsWithUrl = new ArrayList<BehaviorModel>();
for (BehaviorModel behaviorModel : behaviorModels) {
List<ParameterModel> parameterModels = new ArrayList<ParameterModel>();
parameterModels.addAll(behaviorModel.getParameters());
for (ParameterModel parameterModel : parameterModels) {
if (parameterModel.getKey().equals("url"))
behaviorModelsWithUrl.add(behaviorModel);
break;
}
}
return behaviorModelsWithUrl;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
private List<BehaviorModel> getBehaviorModels(
RunScenarioModel runScenarioModel) {
try {
List<BatchModel> batchModels = new ArrayList<BatchModel>();
for (PageModel pageModel : runScenarioModel.getPages())
batchModels.addAll(pageModel.getBatches());
List<BehaviorModel> behaviorModels = new ArrayList<BehaviorModel>();
for (BatchModel batchModel : batchModels)
behaviorModels.addAll(batchModel.getBehaviors());
return behaviorModels;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
public RunScenarioModel createRunScenarioModel(
List<BehaviorModel> behaviorModels,
List<UsePluginModel> usedPluginModels) {
RunScenarioModel runScenarioModel = new RunScenarioModel();
runScenarioModel
.setPages(createPageModelsWithBehaviorList(behaviorModels));
runScenarioModel.setUsePlugins(usedPluginModels);
return runScenarioModel;
}
private List<PageModel> createPageModelsWithBehaviorList(
List<BehaviorModel> behaviorModels) {
List<PageModel> pageModels = new ArrayList<PageModel>();
for (BehaviorModel behaviorModel : behaviorModels) {
pageModels.add(createPageModelWithOneBehaviorModel(behaviorModel));
}
return pageModels;
}
private PageModel createPageModelWithOneBehaviorModel(
BehaviorModel behaviorModel) {
PageModel pageModel = new PageModel();
pageModel.setBatches(new ArrayList<BatchModel>());
pageModel.getBatches().add(
createBatchModelWithOneBehavior(behaviorModel));
return pageModel;
}
private BatchModel createBatchModelWithOneBehavior(
BehaviorModel behaviorModel) {
BatchModel batchModel = new BatchModel();
batchModel.setBehaviors(new ArrayList<BehaviorModel>());
batchModel.getBehaviors().add(behaviorModel);
batchModel.setChildId(-1);
batchModel.setParentId(-1);
batchModel.setId(behaviorModel.getId());
return batchModel;
}
public OperateScriptServerResponseModel uploadScript(String accessToken, String scenarioModel,
String scriptName)
throws CustomGenericException {
String url = "RecordScript" + "/uploadScript" + "/" + scriptName;
return (OperateScriptServerResponseModel) getCommunicateWithMaster()
.getResponseModelByPut(accessToken, url, scenarioModel,
OperateScriptServerResponseModel.class, "upload script");
}
}

View File

@ -1,7 +1,12 @@
package org.bench4q.web.api.test;
import java.util.ArrayList;
import java.util.List;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.api.ScriptActionController;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.model.BaseResponseModel;
@ -13,6 +18,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.ModelMap;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/resources/bench4qweb-servlet.xml" })
@ -20,6 +26,7 @@ public class ScriptActionControllerTest {
private ScriptActionController scriptActionController;
private LoginHelper loginHelper;
private String accessToken;
public LoginHelper getLoginHelper() {
return loginHelper;
}
@ -32,10 +39,13 @@ public class ScriptActionControllerTest {
public ScriptActionController getScriptActionController() {
return scriptActionController;
}
@Autowired
public void setScriptActionController(ScriptActionController scriptActionController) {
this.scriptActionController=scriptActionController;
public void setScriptActionController(
ScriptActionController scriptActionController) {
this.scriptActionController = scriptActionController;
}
@Before
public void setUp() {
this.accessToken = this.getLoginHelper().Login();
@ -48,4 +58,42 @@ public class ScriptActionControllerTest {
if (baseResponseModel.isSuccess())
Assert.assertNotNull(baseResponseModel.getData());
}
@Test
public void uploadPluginEditedScript() throws CustomGenericException {
@SuppressWarnings("unchecked")
int insertCountBefore = ((List<ScriptModel>) scriptActionController
.loadScript(loginHelper.Login()).getData()).size();
BaseResponseModel baseResponseModel = scriptActionController
.uploadPluginEditedScript(loginHelper.Login(), new ModelMap(),
createBheBehaviorModels(), createUsePluginModels(),
"testSript");
@SuppressWarnings("unchecked")
int insertCountAfter = ((List<ScriptModel>) scriptActionController
.loadScript(loginHelper.Login()).getData()).size();
Assert.assertTrue(baseResponseModel.isSuccess());
Assert.assertEquals(insertCountBefore + 1, insertCountAfter);
}
private List<BehaviorModel> createBheBehaviorModels() {
List<BehaviorModel> behaviorModels = new ArrayList<BehaviorModel>();
ParameterModel parameterModel = new ParameterModel();
parameterModel.setKey("url");
parameterModel.setValue("http://133.133.12.3:7979");
List<ParameterModel> parameterModels = new ArrayList<ParameterModel>();
BehaviorModel behaviorModel = BehaviorModel.UserBehaviorBuilder(0,
"url", "http", parameterModels);
behaviorModels.add(behaviorModel);
return behaviorModels;
}
private List<UsePluginModel> createUsePluginModels() {
List<UsePluginModel> usePluginModels = new ArrayList<UsePluginModel>();
UsePluginModel httpPluginModel = new UsePluginModel();
httpPluginModel.setId("http");
httpPluginModel.setName("Http");
usePluginModels.add(httpPluginModel);
return usePluginModels;
}
}