diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/api/ScriptController.java b/Bench4Q-Master/src/main/java/org/bench4q/master/api/ScriptController.java index fb078af6..56dfe802 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/api/ScriptController.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/api/ScriptController.java @@ -1,5 +1,3 @@ -<<<<<<< HEAD -<<<<<<< HEAD package org.bench4q.master.api; import java.io.File; @@ -306,617 +304,3 @@ public class ScriptController extends BaseController { } } -======= -package org.bench4q.master.api; - -import java.io.File; -import java.net.UnknownHostException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.UUID; - -import javax.xml.bind.JAXBException; - -import org.apache.commons.io.FileUtils; -import org.apache.log4j.Logger; -import org.bench4q.master.domain.entity.Port; -import org.bench4q.master.domain.entity.Script; -import org.bench4q.master.domain.factory.BusinessModelMapFactory; -import org.bench4q.master.domain.service.PortPoolService; -import org.bench4q.master.domain.service.ScriptService; -import org.bench4q.master.domain.service.UserService; -import org.bench4q.master.exception.Bench4QException; -import org.bench4q.master.exception.ExceptionLog; -import org.bench4q.recorder.ScriptCapturer; -import org.bench4q.share.helper.MarshalHelper; -import org.bench4q.share.models.agent.RunScenarioModel; -import org.bench4q.share.models.master.OperateScriptServerResponseModel; -import org.bench4q.share.models.master.ScriptModel; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -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; - -@Controller -@RequestMapping("/RecordScript") -public class ScriptController extends BaseController { - private ScriptCapturer scriptCapturer; - private ScriptService scriptService; - private PortPoolService portPoolService = new PortPoolService(); - private static final Object PORT_LOCK = new Object(); - private Logger logger = Logger.getLogger(ScriptController.class); - - private ScriptCapturer getScriptCapturer() { - return scriptCapturer; - } - - @Autowired - private void setScriptCapturer(ScriptCapturer scriptCapturer) { - this.scriptCapturer = scriptCapturer; - } - - private ScriptService getScriptService() { - return scriptService; - } - - @Autowired - private void setScriptService(ScriptService scriptService) { - this.scriptService = scriptService; - } - - private PortPoolService getPortPoolService() { - return portPoolService; - } - - @Autowired - private void setPortPoolService(PortPoolService portPoolService) { - this.portPoolService = portPoolService; - } - - @RequestMapping(value = "/startScriptRecordServer", method = { - RequestMethod.GET, RequestMethod.POST }) - @ResponseBody - public OperateScriptServerResponseModel startScriptRecordServer() - throws UnknownHostException { - if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { - return buildReponseModel(false, - "has no power for recording script!!!", "", -1, null, null, - null); - } - Port port = new Port(); - synchronized (PORT_LOCK) { - port = this.getPortPoolService().getAPortNotInUse(); - if (port == null) { - return buildReponseModel(false, "port is in use!", "", -1, - null, null, null); - } - } - UUID uuid = UUID.randomUUID(); - this.getScriptCapturer().startRecord(port.getPort(), - buildScriptSavePath(), uuid.toString()); - - return buildReponseModel(true, "", this.getScriptCapturer() - .getIpHttpCaptureServerAdress(), port.getPort(), null, null, - uuid.toString()); - } - - private String buildScriptSavePath() { - String dirString = "Scripts" + System.getProperty("file.separator") - + this.getPrincipal().getUserName() - + System.getProperty("file.separator") - + new SimpleDateFormat("yyyyMMdd").format(new Date()); - File dirFile = new File(dirString); - if (!dirFile.exists()) { - dirFile.mkdirs(); - } - return dirString; - } - - @RequestMapping(value = "/stopScriptRecordServer", method = { - RequestMethod.POST, RequestMethod.GET }) - @ResponseBody - public OperateScriptServerResponseModel stopScriptRecordServer( - @RequestParam int port, @RequestParam String fileNameUUID) { - if (!checkScope(UserService.NORAML_AUTHENTICATION)) { - return buildReponseModel(false, - "has no power for stopScriptCapture!!!", "", -1, null, - null, null); - } - - this.getScriptCapturer().stopCurrentRecord(port); - synchronized (PORT_LOCK) { - this.getPortPoolService().backPort(port); - } - - return buildReponseModel(true, "RecordServer stop", "", port, null, - null, fileNameUUID); - } - - @RequestMapping(value = "/saveScriptToDB", method = { RequestMethod.GET, - RequestMethod.POST }) - @ResponseBody - public OperateScriptServerResponseModel saveScriptToDB( - @RequestParam String scriptName, @RequestParam int port, - @RequestParam String fileNameUUID) { - if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { - return buildReponseModel(false, - "saveScriptToDB check your scope fail!", "", -1, null, - null, null); - } - - File file = new File(buildScriptSavePath() - + System.getProperty("file.separator") + fileNameUUID + ".xml"); - if (!file.exists()) { - return buildReponseModel(false, "no that script", null, -1, null, - null, fileNameUUID); - } - try { - String content = FileUtils.readFileToString(file); - logger.info("when saveToDB, scriptContent is " + content); - boolean success = this.getScriptService().saveScript(scriptName, - this.getPrincipal().getId(), content); - // this.getUserService().add - return buildReponseModel(success, "Save to DataBase!!", "", port, - null, null, null); - } catch (Exception e) { - return buildReponseModel(false, "exception when read from file", - null, -1, null, null, fileNameUUID); - } - } - - @RequestMapping(value = "/uploadScript/{scriptName}", method = RequestMethod.PUT) - @ResponseBody - public OperateScriptServerResponseModel uploadScriptToDB( - @PathVariable String scriptName, - @RequestBody RunScenarioModel scenarioModel) - throws Bench4QException { - if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { - throw new Bench4QException(HAVE_NO_POWER, HAVE_NO_POWER - + "for upload a script", "/uploadScript"); - } - try { - boolean success = this.getScriptService().saveScript( - scriptName, - this.getPrincipal().getId(), - MarshalHelper - .marshal(RunScenarioModel.class, scenarioModel)); - logger.info("upload script:" - + MarshalHelper.marshal(RunScenarioModel.class, - scenarioModel)); - return buildReponseModel(success, null, null, -1, null, null, null); - } catch (JAXBException e) { - this.logger.error(ExceptionLog.getStackTrace(e)); - return buildReponseModel(false, - "The script is not in the right format", null, -1, null, - null, null); - } - } - - @RequestMapping(value = "/loadScriptList", method = { RequestMethod.POST, - RequestMethod.GET }) - @ResponseBody - public OperateScriptServerResponseModel loadScriptList() { - if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { - return null; - } - List