This commit is contained in:
fanfuxiaoran 2014-04-29 17:31:19 +08:00
parent 72c1829682
commit ec705fed2a
12 changed files with 459 additions and 280 deletions

View File

@ -1,186 +1,185 @@
package org.bench4q.web.api;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.bench4q.share.communication.HttpRequester;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.models.master.AgentModel;
import org.bench4q.share.models.master.AgentResponseModel;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
import org.bench4q.web.model.AgentListModel;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes("accessToken")
public class AgentActionController extends BaseControllerService {
private final String baseUrl =this.masterIp+ "agentManage";
public static int IN_IDLE = 1;
private String getBaseUrl() {
return baseUrl;
}
@RequestMapping("addAgentToPool")
@ResponseBody
// change to true or false
public Boolean addAgenttoPool(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String hostName, @RequestParam String maxLoad,
@RequestParam String port) {
if (!validateInput(hostName))
return false;
if (!validateInput(maxLoad))
return false;
if (!validateInput(port))
return false;
String content;
HttpResponse httpResponse;
try {
content = ObjectXmlExchange.toXml(AgentModel.class,
buildAgent(hostName, maxLoad, port, IN_IDLE));
System.out.println(content);
httpResponse = this.getHttpRequester().sendPostXml(
this.getBaseUrl() + "/addAgentToPool", content,
makeAccessTockenMap(accessToken));
if (HttpRequester.isInvalidResponse(httpResponse)) {
System.out.println(httpResponseNull);
return false;
}
System.out.println(httpResponse.getContent());
AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse
.getContent());
if (agentResponseModel.isSuccess()) {
System.out.println(SUCCESS);
return true;
} else {
System.out.println(FAIL);
System.err.println(agentResponseModel.getFailCauseString());
return false;
}
} catch (JAXBException e) {
System.err.println(FAIL);
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private AgentModel buildAgent(String hostName, String maxLoad, String port,
int currentStatus) {
AgentModel agent = new AgentModel();
agent.setHostName(hostName);
agent.setPort(Integer.parseInt(port));
agent.setCurrentStatus(currentStatus);
agent.setMaxLoad(Integer.parseInt(maxLoad));
agent.setRemainLoad(Integer.parseInt(maxLoad));
return agent;
}
@RequestMapping("removeAgentFromPool")
@ResponseBody
public Boolean removeAgentfromPool(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String hostName, @RequestParam String id) {
System.out.println("enter remove agent");
if (!validateInput(hostName))
return false;
if (!validateInput(id))
return false;
Map<String, String> params = this.makeParamsMap("agentId", id);
params.put("hostName", hostName);
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
this.getBaseUrl() + "/removeAgentFromPool", params,
this.makeAccessTockenMap(accessToken));
if (HttpRequester.isInvalidResponse(httpResponse)) {
System.out.println(httpResponseNull);
return false;
}
System.out.println(httpResponse.getContent());
AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse
.getContent());
if (agentResponseModel.isSuccess()) {
System.out.println(SUCCESS);
return true;
} else {
System.out.println(FAIL);
System.out.println(agentResponseModel.getFailCauseString());
return false;
}
} catch (Exception e) {
System.err.println(FAIL);
e.printStackTrace();
return false;
}
}
@RequestMapping(value = "loadAgents", method = { RequestMethod.POST,
RequestMethod.GET })
@ResponseBody
public AgentListModel loadAgentsFromPool(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken) {
System.out.println("enter load Agent");
try {
HttpResponse httpResponse = this.getHttpRequester().sendPost(
this.getBaseUrl() + "/queryAgentList", null,
this.makeAccessTockenMap(accessToken));
if (HttpRequester.isInvalidResponse(httpResponse)) {
System.out.println("reponse is null");
return null;
}
System.out.println(httpResponse.getContent());
AgentResponseModel agentResponseModel = this
.extractAgentResponseModel(httpResponse.getContent());
if (agentResponseModel.isSuccess()) {
System.out.println(SUCCESS);
return buildAgentListModel(agentResponseModel.getAgents());
} else {
System.out.println(FAIL);
System.out.println(agentResponseModel.getFailCauseString());
return null;
}
} catch (Exception e) {
System.out.println(FAIL);
e.printStackTrace();
return null;
}
}
private AgentListModel buildAgentListModel(List<AgentModel> agents) {
AgentListModel listModel = new AgentListModel();
listModel.setList(agents);
return listModel;
}
private AgentResponseModel extractAgentResponseModel(String content)
throws JAXBException {
AgentResponseModel resultModel = new AgentResponseModel();
Unmarshaller ummarshaller = JAXBContext.newInstance(
resultModel.getClass()).createUnmarshaller();
resultModel = (AgentResponseModel) ummarshaller
.unmarshal(new ByteArrayInputStream(content.getBytes()));
return resultModel;
}
}
package org.bench4q.web.api;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.bench4q.share.communication.HttpRequester;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.models.master.AgentModel;
import org.bench4q.share.models.master.AgentResponseModel;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
import org.bench4q.web.model.AgentListModel;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes("accessToken")
public class AgentActionController extends BaseControllerService {
private final String baseUrl =this.masterIp+ "agentManage";
public static int IN_IDLE = 1;
private String getBaseUrl() {
return baseUrl;
}
@RequestMapping("addAgentToPool")
@ResponseBody
// change to true or false
public Boolean addAgenttoPool(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String hostName, @RequestParam String maxLoad,
@RequestParam String port) {
if (!validateInput(hostName))
return false;
if (!validateInput(maxLoad))
return false;
if (!validateInput(port))
return false;
String content;
HttpResponse httpResponse;
try {
content = ObjectXmlExchange.toXml(AgentModel.class,
buildAgent(hostName, maxLoad, port, IN_IDLE));
httpResponse = this.getHttpRequester().sendPostXml(
this.getBaseUrl() + "/addAgentToPool", content,
makeAccessTockenMap(accessToken));
if (HttpRequester.isInvalidResponse(httpResponse)) {
System.out.println(httpResponseNull);
return false;
}
System.out.println(httpResponse.getContent());
AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse
.getContent());
if (agentResponseModel.isSuccess()) {
System.out.println(SUCCESS);
return true;
} else {
System.out.println(FAIL);
System.err.println(agentResponseModel.getFailCauseString());
return false;
}
} catch (JAXBException e) {
System.err.println(FAIL);
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private AgentModel buildAgent(String hostName, String maxLoad, String port,
int currentStatus) {
AgentModel agent = new AgentModel();
agent.setHostName(hostName);
agent.setPort(Integer.parseInt(port));
agent.setCurrentStatus(currentStatus);
agent.setMaxLoad(Integer.parseInt(maxLoad));
agent.setRemainLoad(Integer.parseInt(maxLoad));
return agent;
}
@RequestMapping("removeAgentFromPool")
@ResponseBody
public Boolean removeAgentfromPool(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String hostName, @RequestParam String id) {
System.out.println("enter remove agent");
if (!validateInput(hostName))
return false;
if (!validateInput(id))
return false;
Map<String, String> params = this.makeParamsMap("agentId", id);
params.put("hostName", hostName);
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
this.getBaseUrl() + "/removeAgentFromPool", params,
this.makeAccessTockenMap(accessToken));
if (HttpRequester.isInvalidResponse(httpResponse)) {
System.out.println(httpResponseNull);
return false;
}
System.out.println(httpResponse.getContent());
AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse
.getContent());
if (agentResponseModel.isSuccess()) {
System.out.println(SUCCESS);
return true;
} else {
System.out.println(FAIL);
System.out.println(agentResponseModel.getFailCauseString());
return false;
}
} catch (Exception e) {
System.err.println(FAIL);
e.printStackTrace();
return false;
}
}
@RequestMapping(value = "loadAgents", method = { RequestMethod.POST,
RequestMethod.GET })
@ResponseBody
public AgentListModel loadAgentsFromPool(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken) {
System.out.println("enter load Agent");
try {
HttpResponse httpResponse = this.getHttpRequester().sendPost(
this.getBaseUrl() + "/queryAgentList", null,
this.makeAccessTockenMap(accessToken));
if (HttpRequester.isInvalidResponse(httpResponse)) {
System.out.println("reponse is null");
return null;
}
System.out.println(httpResponse.getContent());
AgentResponseModel agentResponseModel = this
.extractAgentResponseModel(httpResponse.getContent());
if (agentResponseModel.isSuccess()) {
System.out.println(SUCCESS);
return buildAgentListModel(agentResponseModel.getAgents());
} else {
System.out.println(FAIL);
System.out.println(agentResponseModel.getFailCauseString());
return null;
}
} catch (Exception e) {
System.out.println(FAIL);
e.printStackTrace();
return null;
}
}
private AgentListModel buildAgentListModel(List<AgentModel> agents) {
AgentListModel listModel = new AgentListModel();
listModel.setList(agents);
return listModel;
}
private AgentResponseModel extractAgentResponseModel(String content)
throws JAXBException {
AgentResponseModel resultModel = new AgentResponseModel();
Unmarshaller ummarshaller = JAXBContext.newInstance(
resultModel.getClass()).createUnmarshaller();
resultModel = (AgentResponseModel) ummarshaller
.unmarshal(new ByteArrayInputStream(content.getBytes()));
return resultModel;
}
}

View File

@ -1,5 +0,0 @@
package org.bench4q.web.masterMessenger.exception;
public class MessergerExceptionHandler {
}

View File

@ -0,0 +1,123 @@
package org.bench4q.web.newapi;
import java.util.HashMap;
import java.util.Map;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.AgentModel;
import org.bench4q.share.models.master.AgentResponseModel;
import org.bench4q.web.masterMessenger.AgentManagerMessenger;
import org.bench4q.web.validation.AgentValidate;
import org.bench4q.web.validation.ValidateResponseModel;
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;
@Controller
@SessionAttributes({ "accessToken" })
public class AgentController extends BaseController {
public final int IN_IDLE = 1;
private final String SERVER_ERROR = "server error";
private AgentValidate agentValidate;
private AgentManagerMessenger agentManagerMessenger;
private AgentValidate getAgentValidate() {
return agentValidate;
}
@Autowired
private void setAgentValidate(AgentValidate agentValidate) {
this.agentValidate = agentValidate;
}
private AgentManagerMessenger getAgentManagerMessenger() {
return agentManagerMessenger;
}
@Autowired
private void setAgentManagerMessenger(
AgentManagerMessenger agentManagerMessenger) {
this.agentManagerMessenger = agentManagerMessenger;
}
@RequestMapping("addAgent")
@ResponseBody
public Map<String, Object> addAgent(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String hostName, @RequestParam String port,
@RequestParam String maxLoad) {
Map<String, Object> map = new HashMap<String, Object>();
ValidateResponseModel validateResponseModel = this.getAgentValidate()
.validateAddedAgent(hostName, maxLoad, port);
if (!validateResponseModel.isSuccess())
return fail(map, validateResponseModel.getMessage());
String agentModelContnet = MarshalHelper.tryMarshal(buildAgentModel(
hostName, port, maxLoad));
if (agentModelContnet == null) {
return fail(map, "agent input info is error");
}
AgentResponseModel agentResponseModel = this.getAgentManagerMessenger()
.addAgent(accessToken, agentModelContnet);
return processAgentResposeModel(agentResponseModel, map);
}
@RequestMapping("/deleteAgent")
@ResponseBody
public Map<String, Object> deleteAgent(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String agentId, @RequestParam String hostName) {
Map<String, Object> map = new HashMap<String, Object>();
ValidateResponseModel validateResponseModel = this.getAgentValidate()
.validateDeleteAgent(hostName, agentId);
if (!validateResponseModel.isSuccess()) {
return fail(map, validateResponseModel.getMessage());
}
AgentResponseModel agentResponseModel = this.getAgentManagerMessenger()
.deleteAgent(accessToken, agentId, hostName);
return processAgentResposeModel(agentResponseModel, map);
}
@RequestMapping("/loadAgents")
public Map<String, Object> loadAgents(
@ModelAttribute("accessToken") String accessToken) {
Map<String, Object> map = new HashMap<String, Object>();
AgentResponseModel agentResponseModel = this.getAgentManagerMessenger()
.loadAgents(accessToken);
map = processAgentResposeModel(agentResponseModel, map);
if ((Boolean) map.get("success")) {
map.put("agents", agentResponseModel.getAgents());
}
return map;
}
private Map<String, Object> processAgentResposeModel(
AgentResponseModel agentResponseModel, Map<String, Object> map) {
if (agentResponseModel == null) {
return fail(map, SERVER_ERROR);
}
if (agentResponseModel.isSuccess()) {
return success(map);
}
return fail(map, agentResponseModel.getFailCauseString());
}
private AgentModel buildAgentModel(String hostName, String port,
String maxLoad) {
AgentModel agent = new AgentModel();
agent.setHostName(hostName);
agent.setPort(Integer.parseInt(port));
agent.setCurrentStatus(IN_IDLE);
agent.setMaxLoad(Integer.parseInt(maxLoad));
agent.setRemainLoad(Integer.parseInt(maxLoad));
return agent;
}
}

View File

@ -0,0 +1,25 @@
package org.bench4q.web.newapi;
import java.util.HashMap;
import java.util.Map;
public class BaseController {
private final String SUCCESS = "success";
private final String FAILMESSAGE = "failMessage";
protected Map<String, Object> fail(Map<String, Object> map, String message) {
if (map == null)
map = new HashMap<String, Object>();
map.put(SUCCESS, new Boolean(false));
map.put(FAILMESSAGE, message);
return map;
}
protected Map<String, Object> success(Map<String, Object> map) {
if (map == null)
map = new HashMap<String, Object>();
map.put(SUCCESS, new Boolean(true));
return map;
}
}

View File

@ -0,0 +1,31 @@
package org.bench4q.web.validation;
public class AgentValidate {
public ValidateResponseModel validateAddedAgent(String hostName,
String maxLoad, String port) {
if (!ValidateHelper.validateIp(hostName)) {
return new ValidateResponseModel(false, "error host name");
}
if (!ValidateHelper.validateInteger(maxLoad)) {
return new ValidateResponseModel(false, "error maxLoad");
}
if (!ValidateHelper.validateInteger(port)) {
return new ValidateResponseModel(false, "error port");
}
return new ValidateResponseModel(true, "");
}
public ValidateResponseModel validateDeleteAgent(String hostName,
String agentId) {
if (!ValidateHelper.validateIp(hostName)) {
return new ValidateResponseModel(false, "error host name");
}
if (!ValidateHelper.validateInteger(agentId)) {
return new ValidateResponseModel(false, "error agentId");
}
return new ValidateResponseModel(true, "");
}
}

View File

@ -0,0 +1,49 @@
package org.bench4q.web.validation;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ValidateHelper {
public static boolean validateStringInput(String input) {
if (input == null || input.length() == 0) {
return false;
}
return true;
}
public static boolean validateInteger(String input) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(input);
return isNum.matches();
}
public static boolean validateIp(String input) {
input.replaceAll(" ", "");
String ip = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
Pattern pattern = Pattern.compile(ip);
Matcher matcher = pattern.matcher(input);
return matcher.matches();
}
public static void main(String args[]) {
String ip = " 133.133.12.1";
System.out.println(validateIp(ip));
String ipError = "122.122.1.r";
System.err.println(validateInteger(ipError));
String number = "123";
System.out.println(validateInteger(number));
System.err.println(validateInteger("u7"));
}
}

View File

@ -0,0 +1,29 @@
package org.bench4q.web.validation;
public class ValidateResponseModel {
private boolean success;
private String message;
public ValidateResponseModel(boolean success, String message) {
this.success = success;
this.message = message;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -0,0 +1,10 @@
$(function() {
getMap();
});
function getMap() {
$.post("/test", {}, function(data) {
console.log(data.success);
}, "json");
}

View File

@ -1,47 +0,0 @@
var files;
var file1;
var fileList = [];
var fileNames = [];
$(function() {
$("input[type=submit]").click(function() {
submit();
});
createFileInput(fileList);
});
function submit() {
uploadFilesAndScript();
}
function uploadFilesAndScript() {
var formData=new FormData();
var filesDiv=$("input[type=file]");
for(var i=0;i<filesDiv.length;i++){
formData.append("paramFiles",filesDiv[i].files[0],filesDiv[i].files[0].name);
}
formData.append("content","");
$.ajax({
url : "/uploadEditedScipt",
type : "POST",
contentType:false,
processData:false,
data : formData,
success : function(data) {
alert(data);
}
});
}
function createFileInput(fileList) {
var fileContainer = $("#files");
var input = $(document.createElement("input"));
var lable = $(document.createElement("lable"));
lable.text("test");
input.attr("type", "file");
input.attr("name", "file");
input.attr("id", "param");
fileContainer.append(lable);
fileContainer.append(input);
}

View File

@ -1,17 +0,0 @@
function upload(){
var files= document.getElementById("file");
var file=files.files[0];
// var file=$("#file").files[0];
console.log("fileName: " + file.name);
var url="/upload";
$.post(url,{script:file},function(data){
alert(data);
}," multipart/form-data")
};
$(function(){
$("#upload").click(function(e){
upload();
});
});

View File

@ -7,12 +7,14 @@
href="http://code.jquery.com/qunit/qunit-1.14.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<div id="files"></div>
<input type="submit" id="test"></input>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<div>
<span data="num">2</span> <span>+</span> <span data="num">3</span><span>=</span><span
data="result"></span>
</div>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.0.min.js"></script>
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
<script src="js/testUploadFile.js"></script>
<script src="js/testMap.js"></script>
</body>
</html>

View File

@ -1,20 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>upload file</title>
<link rel="stylesheet"
href="http://code.jquery.com/qunit/qunit-1.14.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<div>
<input type="file" id="file"> <input type="submit"
value="Upload" id="upload">
</div>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.0.min.js"></script>
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
<script src="js/uploadFile.js"></script>
</body>
</html>