modify scriptActionController and add ScenarioModel to uploadEditScript
This commit is contained in:
parent
8980b2d01c
commit
ca60b7c2e3
|
@ -1,432 +1,484 @@
|
|||
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.apache.log4j.Logger;
|
||||
import org.bench4q.share.helper.ExceptionLog;
|
||||
import org.bench4q.share.helper.MarshalHelper;
|
||||
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.PluginEditScriptRequestModel;
|
||||
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.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
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(
|
||||
@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,
|
||||
JAXBException {
|
||||
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);
|
||||
|
||||
/*
|
||||
* Logger.getLogger(ScriptActionController.class).info(
|
||||
* MarshalHelper.marshal(OperateScriptServerResponseModel.class,
|
||||
* operateScriptServerResponseModel));
|
||||
*/
|
||||
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;
|
||||
Logger.getLogger(ScriptActionController.class).info(
|
||||
"edit script Before toJson :"
|
||||
+ script.getScriptContent());
|
||||
scriptContent = gson.toJson(result);
|
||||
Logger.getLogger(ScriptActionController.class).info(
|
||||
"edit script after toJson :" + scriptContent);
|
||||
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 saveEditScript(
|
||||
@ModelAttribute("accessToken") String accessToken,
|
||||
@RequestParam String scriptId, @RequestParam String script)
|
||||
throws CustomGenericException {
|
||||
String url = this.getBaseUrl() + "/updateScript"+"/"+scriptId;
|
||||
String caller = this.BASECALLER + "saveScript";
|
||||
Gson gson = new Gson();
|
||||
RunScenarioModel scriptModel = gson.fromJson(script,
|
||||
RunScenarioModel.class);
|
||||
try {
|
||||
String scriptContent = ObjectXmlExchange.toXml(
|
||||
RunScenarioModel.class, scriptModel);
|
||||
|
||||
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
|
||||
.getScriptService()
|
||||
.getCommunicateWithMaster()
|
||||
.getResponseModelByPut(accessToken, url,scriptContent,
|
||||
OperateScriptServerResponseModel.class,
|
||||
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(value = "uploadScript", method = RequestMethod.POST)
|
||||
@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());
|
||||
MarshalHelper.unmarshal(RunScenarioModel.class, scenarioModel);
|
||||
OperateScriptServerResponseModel operateScriptServerResponseModel = this
|
||||
.getScriptService().uploadScript(accessToken,
|
||||
scenarioModel, scriptName);
|
||||
Logger.getLogger(ScriptActionController.class).info(
|
||||
"script:" + scenarioModel);
|
||||
if (operateScriptServerResponseModel.isSuccess())
|
||||
return new BaseResponseModel(true, (Object) new String(
|
||||
"upload script:" + scriptName + " success!"));
|
||||
|
||||
else
|
||||
return new BaseResponseModel(false,
|
||||
operateScriptServerResponseModel.getFailCauseString());
|
||||
} catch (JAXBException e) {
|
||||
Logger.getLogger(ScriptActionController.class).info(
|
||||
ExceptionLog.getStackTrace(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 = "uploadPluginEditedScript", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public BaseResponseModel uploadPluginEditedScript(
|
||||
@ModelAttribute("accessToken") String accessToken,
|
||||
@RequestBody String content) throws CustomGenericException {
|
||||
Gson gson = new Gson();
|
||||
Logger.getLogger(ScriptActionController.class).info(content);
|
||||
PluginEditScriptRequestModel pluginEditScriptRequestModel = null;
|
||||
try {
|
||||
pluginEditScriptRequestModel = gson.fromJson(content,
|
||||
PluginEditScriptRequestModel.class);
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(ScriptActionController.class).info(
|
||||
ExceptionLog.getStackTrace(e));
|
||||
}
|
||||
List<BehaviorModel> behaviorModelsList = pluginEditScriptRequestModel
|
||||
.getBehaviorModels();
|
||||
List<UsePluginModel> usePluginModelsList = pluginEditScriptRequestModel
|
||||
.getUsePluginModels();
|
||||
String scriptName = pluginEditScriptRequestModel.getScriptName();
|
||||
if (!validateScript(behaviorModelsList, usePluginModelsList))
|
||||
return new BaseResponseModel(false, "invalidate script");
|
||||
RunScenarioModel runScenarioModel = this
|
||||
.getScriptService()
|
||||
.createRunScenarioModel(behaviorModelsList, usePluginModelsList);
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
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.apache.log4j.Logger;
|
||||
import org.bench4q.share.helper.ExceptionLog;
|
||||
import org.bench4q.share.helper.MarshalHelper;
|
||||
import org.bench4q.share.models.agent.RunScenarioModel;
|
||||
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.PluginEditScriptRequestModel;
|
||||
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.model.ScenarioModel;
|
||||
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.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
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(
|
||||
@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,
|
||||
JAXBException {
|
||||
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);
|
||||
|
||||
/*
|
||||
* Logger.getLogger(ScriptActionController.class).info(
|
||||
* MarshalHelper.marshal(OperateScriptServerResponseModel.class,
|
||||
* operateScriptServerResponseModel));
|
||||
*/
|
||||
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;
|
||||
Logger.getLogger(ScriptActionController.class).info(
|
||||
"edit script Before toJson :"
|
||||
+ script.getScriptContent());
|
||||
scriptContent = gson.toJson(result);
|
||||
Logger.getLogger(ScriptActionController.class).info(
|
||||
"edit script after toJson :" + scriptContent);
|
||||
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 saveEditScript(
|
||||
@ModelAttribute("accessToken") String accessToken,
|
||||
@RequestParam String scriptId, @RequestParam String script)
|
||||
throws CustomGenericException {
|
||||
String url = this.getBaseUrl() + "/updateScript" + "/" + scriptId;
|
||||
String caller = this.BASECALLER + "saveScript";
|
||||
Gson gson = new Gson();
|
||||
RunScenarioModel scriptModel = gson.fromJson(script,
|
||||
RunScenarioModel.class);
|
||||
try {
|
||||
String scriptContent = ObjectXmlExchange.toXml(
|
||||
RunScenarioModel.class, scriptModel);
|
||||
|
||||
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
|
||||
.getScriptService()
|
||||
.getCommunicateWithMaster()
|
||||
.getResponseModelByPut(accessToken, url, scriptContent,
|
||||
OperateScriptServerResponseModel.class, 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(value = "uploadScript", method = RequestMethod.POST)
|
||||
@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());
|
||||
MarshalHelper.unmarshal(RunScenarioModel.class, scenarioModel);
|
||||
OperateScriptServerResponseModel operateScriptServerResponseModel = this
|
||||
.getScriptService().uploadScript(accessToken,
|
||||
scenarioModel, scriptName);
|
||||
Logger.getLogger(ScriptActionController.class).info(
|
||||
"script:" + scenarioModel);
|
||||
if (operateScriptServerResponseModel.isSuccess())
|
||||
return new BaseResponseModel(true, (Object) new String(
|
||||
"upload script:" + scriptName + " success!"));
|
||||
|
||||
else
|
||||
return new BaseResponseModel(false,
|
||||
operateScriptServerResponseModel.getFailCauseString());
|
||||
} catch (JAXBException e) {
|
||||
Logger.getLogger(ScriptActionController.class).info(
|
||||
ExceptionLog.getStackTrace(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 = "uploadEditScript", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public BaseResponseModel uploadEditScript(
|
||||
@ModelAttribute("accessToken") String accessToken,
|
||||
@RequestBody String content) throws CustomGenericException {
|
||||
System.out.println("uploadEditScript");
|
||||
ScenarioModel scenarioModel = null;
|
||||
Gson gson = new Gson();
|
||||
try {
|
||||
scenarioModel = gson.fromJson(content, ScenarioModel.class);
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(ScriptActionController.class).info(
|
||||
ExceptionLog.getStackTrace(e));
|
||||
}
|
||||
|
||||
RunScenarioModel runScenarioModel = scenarioModel.getRunScenarioModel();
|
||||
List<PageModel> pageModels = runScenarioModel.getPages();
|
||||
List<UsePluginModel> usePluginModels = runScenarioModel.getUsePlugins();
|
||||
String scriptName = scenarioModel.getScriptName();
|
||||
if (!validateScenarioModel(pageModels, usePluginModels))
|
||||
return new BaseResponseModel(false, "invalidate script");
|
||||
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 validateScenarioModel(List<PageModel> pageModels,
|
||||
List<UsePluginModel> usePluginModels) {
|
||||
|
||||
if (pageModels == null || pageModels.size() == 0)
|
||||
return false;
|
||||
if (usePluginModels == null || usePluginModels.size() == 0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "uploadPluginEditedScript", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public BaseResponseModel uploadPluginEditedScript(
|
||||
@ModelAttribute("accessToken") String accessToken,
|
||||
@RequestBody String content) throws CustomGenericException {
|
||||
Gson gson = new Gson();
|
||||
Logger.getLogger(ScriptActionController.class).info(content);
|
||||
PluginEditScriptRequestModel pluginEditScriptRequestModel = null;
|
||||
try {
|
||||
pluginEditScriptRequestModel = gson.fromJson(content,
|
||||
PluginEditScriptRequestModel.class);
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(ScriptActionController.class).info(
|
||||
ExceptionLog.getStackTrace(e));
|
||||
}
|
||||
List<BehaviorModel> behaviorModelsList = pluginEditScriptRequestModel
|
||||
.getBehaviorModels();
|
||||
List<UsePluginModel> usePluginModelsList = pluginEditScriptRequestModel
|
||||
.getUsePluginModels();
|
||||
String scriptName = pluginEditScriptRequestModel.getScriptName();
|
||||
if (!validateScript(behaviorModelsList, usePluginModelsList))
|
||||
return new BaseResponseModel(false, "invalidate script");
|
||||
RunScenarioModel runScenarioModel = this
|
||||
.getScriptService()
|
||||
.createRunScenarioModel(behaviorModelsList, usePluginModelsList);
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package org.bench4q.web.model;
|
||||
|
||||
import org.bench4q.share.models.agent.RunScenarioModel;
|
||||
|
||||
public class ScenarioModel {
|
||||
private String scriptName;
|
||||
private RunScenarioModel runScenarioModel;
|
||||
public String getScriptName() {
|
||||
return scriptName;
|
||||
}
|
||||
public void setScriptName(String scriptName) {
|
||||
this.scriptName = scriptName;
|
||||
}
|
||||
public RunScenarioModel getRunScenarioModel() {
|
||||
return runScenarioModel;
|
||||
}
|
||||
public void setRunScenarioModel(RunScenarioModel runScenarioModel) {
|
||||
this.runScenarioModel = runScenarioModel;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue