mirror of
https://gitee.com/BDWare/cp.git
synced 2025-01-10 09:54:07 +00:00
finish the http input validation
This commit is contained in:
parent
a1b59cfbfd
commit
51ee18928b
@ -3,7 +3,6 @@ package org.bdware.sc;
|
|||||||
import com.google.gson.*;
|
import com.google.gson.*;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import com.google.gson.stream.MalformedJsonException;
|
import com.google.gson.stream.MalformedJsonException;
|
||||||
import groovy.util.logging.Log;
|
|
||||||
import org.apache.logging.log4j.Level;
|
import org.apache.logging.log4j.Level;
|
||||||
import org.apache.logging.log4j.core.config.Configurator;
|
import org.apache.logging.log4j.core.config.Configurator;
|
||||||
import org.bdware.analysis.BasicBlock;
|
import org.bdware.analysis.BasicBlock;
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package org.bdware.sc.compiler.ap;
|
package org.bdware.sc.compiler.ap;
|
||||||
|
|
||||||
import org.bdware.doip.codec.operations.BasicOperations;
|
|
||||||
import org.bdware.sc.bean.DoipOperationInfo;
|
import org.bdware.sc.bean.DoipOperationInfo;
|
||||||
import org.bdware.sc.compiler.AnnotationProcessor;
|
import org.bdware.sc.compiler.AnnotationProcessor;
|
||||||
import org.bdware.sc.engine.hook.DOOPHandler;
|
import org.bdware.sc.engine.hook.DOOPHandler;
|
||||||
@ -9,8 +8,6 @@ import org.bdware.sc.node.AnnotationNode;
|
|||||||
import org.bdware.sc.node.ContractNode;
|
import org.bdware.sc.node.ContractNode;
|
||||||
import org.bdware.sc.node.FunctionNode;
|
import org.bdware.sc.node.FunctionNode;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
|
||||||
// DOOP is designed for DoipModule which contains specific functions for RepositoryHandler
|
// DOOP is designed for DoipModule which contains specific functions for RepositoryHandler
|
||||||
public class DOOP extends AnnotationProcessor {
|
public class DOOP extends AnnotationProcessor {
|
||||||
|
@ -18,6 +18,7 @@ import org.bdware.sc.node.FunctionNode;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class DOOPHandler implements AnnotationHook {
|
public class DOOPHandler implements AnnotationHook {
|
||||||
public static Map<String, BasicOperations> funcNameToDoipOperations;
|
public static Map<String, BasicOperations> funcNameToDoipOperations;
|
||||||
@ -59,7 +60,7 @@ public class DOOPHandler implements AnnotationHook {
|
|||||||
|
|
||||||
// set doipMsgPackerArg struct's params
|
// set doipMsgPackerArg struct's params
|
||||||
doipMsgPackerArg.setSource("http");
|
doipMsgPackerArg.setSource("http");
|
||||||
doipMsgPackerArg.rawDoipMsg = convertContractRequestToDoipMessage(httpReq);
|
doipMsgPackerArg.rawDoipMsg = httpInputConvertContractRequestToDoipMessage(httpReq);
|
||||||
}
|
}
|
||||||
|
|
||||||
argPacks.arg = doipMsgPackerArg;
|
argPacks.arg = doipMsgPackerArg;
|
||||||
@ -72,7 +73,7 @@ public class DOOPHandler implements AnnotationHook {
|
|||||||
BasicOperations curOp = funcNameToDoipOperations.get(httpReq.getAction());
|
BasicOperations curOp = funcNameToDoipOperations.get(httpReq.getAction());
|
||||||
|
|
||||||
// get args rules and validate http args
|
// get args rules and validate http args
|
||||||
JsonElement httpArgsRules = getRulesForBasicOperation(curOp);
|
JsonElement httpArgsRules = getRulesForHTTPRequest(curOp);
|
||||||
ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs);
|
ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs);
|
||||||
visitor.visit(httpArgsRules);
|
visitor.visit(httpArgsRules);
|
||||||
if (!visitor.getStatus()) {
|
if (!visitor.getStatus()) {
|
||||||
@ -83,37 +84,102 @@ public class DOOPHandler implements AnnotationHook {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonElement getRulesForBasicOperation(BasicOperations basicOperation) {
|
public static JsonElement getRulesForHTTPRequest(BasicOperations basicOperation) {
|
||||||
|
|
||||||
switch(basicOperation) {
|
switch(basicOperation) {
|
||||||
case Hello:
|
case Hello:
|
||||||
return JsonParser.parseString("{\"!doid\":\"string\"}");
|
case Delete:
|
||||||
|
case ListOps:
|
||||||
|
return JsonParser.parseString("{\"!header\":{{\"!identifier\":\"string\"}}}");
|
||||||
case Retrieve:
|
case Retrieve:
|
||||||
return JsonParser.parseString("{\"!doid\":\"string\", \"!element\":\"boolean\"}");
|
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}}");
|
||||||
|
case Create:
|
||||||
|
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}, \"!body\":\"string\"}");
|
||||||
|
case Update:
|
||||||
|
return JsonParser.parseString("{\"!header\":{{\"!identifier\":\"string\"}}, \"!body\":\"string\"}");
|
||||||
|
case Search:
|
||||||
|
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"query\":\"string\", \"pageNum\":\"int\", \"pageSize\":\"int\", \"type\":\"string\"}}}");
|
||||||
|
case Extension:
|
||||||
|
case Unknown:
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DoipMessage convertContractRequestToDoipMessage(ContractRequest httpReq) {
|
public static DoipMessage httpInputConvertContractRequestToDoipMessage(ContractRequest httpReq) {
|
||||||
BasicOperations httpOperation = funcNameToDoipOperations.get(httpReq.getAction());
|
BasicOperations httpOperation = funcNameToDoipOperations.get(httpReq.getAction());
|
||||||
JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject();
|
JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject();
|
||||||
DoipMessage doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest("", httpReq.getAction()).create();
|
// taking Extension into consideration
|
||||||
|
JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null;
|
||||||
|
JsonObject body = jsonParams.get("body") != null ? jsonParams.get("body").getAsJsonObject() : null;
|
||||||
|
DoipMessage doipMessage = null;
|
||||||
switch(httpOperation) {
|
switch(httpOperation) {
|
||||||
case Hello:
|
case Hello:
|
||||||
doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(jsonParams.get("doid").getAsString(), httpReq.getAction()).create();
|
case Delete:
|
||||||
|
case ListOps:
|
||||||
|
doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpReq.getAction()).create();
|
||||||
break;
|
break;
|
||||||
case Retrieve:
|
case Retrieve:
|
||||||
DoipMessageFactory.DoipMessageBuilder msgBuilder = new DoipMessageFactory.DoipMessageBuilder().createRequest(jsonParams.get("doid").getAsString(), httpReq.getAction());
|
doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpReq.getAction()).create();
|
||||||
msgBuilder = msgBuilder.addAttributes("element", jsonParams.get("element").getAsString());
|
JsonElement element = header.get("element");
|
||||||
doipMessage = msgBuilder.create();
|
JsonElement includeElementData = header.get("includeElementData");
|
||||||
|
if(element != null) doipMessage.header.parameters.addAttribute("element", element.getAsString());
|
||||||
|
if(includeElementData != null) doipMessage.header.parameters.addAttribute("includeElementData", includeElementData.getAsBoolean());
|
||||||
|
break;
|
||||||
|
case Create:
|
||||||
|
doipMessage = new DoipMessageFactory.DoipMessageBuilder()
|
||||||
|
.createRequest(header.get("identifier").getAsString(), BasicOperations.Create.getName())
|
||||||
|
.setBody(body.getAsString().getBytes(StandardCharsets.UTF_8))
|
||||||
|
.create();
|
||||||
|
break;
|
||||||
|
case Update:
|
||||||
|
doipMessage = new DoipMessageFactory.DoipMessageBuilder()
|
||||||
|
.createRequest(header.get("identifier").getAsString(), BasicOperations.Update.getName())
|
||||||
|
.setBody(body.getAsString().getBytes(StandardCharsets.UTF_8))
|
||||||
|
.create();
|
||||||
|
break;
|
||||||
|
case Search:
|
||||||
|
DoipMessageFactory.DoipMessageBuilder searchBuilder = new DoipMessageFactory.DoipMessageBuilder()
|
||||||
|
.createRequest(header.get("identifier").getAsString(), BasicOperations.Search.getName());
|
||||||
|
JsonElement query = header.get("query");
|
||||||
|
if(query != null) searchBuilder.addAttributes("query", query.getAsString());
|
||||||
|
JsonElement pageNum = header.get("pageNum");
|
||||||
|
if(pageNum != null) searchBuilder.addAttributes("pageNum", pageNum.getAsInt());
|
||||||
|
JsonElement pageSize = header.get("pageSize");
|
||||||
|
if(pageSize != null) searchBuilder.addAttributes("pageSize", pageSize.getAsInt());
|
||||||
|
JsonElement type = header.get("type");
|
||||||
|
if(type != null) searchBuilder.addAttributes("type", type.getAsString());
|
||||||
|
|
||||||
|
doipMessage = searchBuilder.create();
|
||||||
|
break;
|
||||||
|
case Extension:
|
||||||
|
DoipMessageFactory.DoipMessageBuilder extensionBuilder = new DoipMessageFactory.DoipMessageBuilder();
|
||||||
|
if(header != null) {
|
||||||
|
if(header.get("identifier") != null) {
|
||||||
|
extensionBuilder = extensionBuilder
|
||||||
|
.createRequest(header.get("identifier").getAsString(), BasicOperations.Extension.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Map.Entry<String, JsonElement>> entries = header.entrySet();
|
||||||
|
for (Map.Entry<String, JsonElement> entry : entries) {
|
||||||
|
extensionBuilder.addAttributes(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(body != null) {
|
||||||
|
extensionBuilder.setBody(body.getAsString().getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
doipMessage = extensionBuilder.create();
|
||||||
|
break;
|
||||||
|
case Unknown:
|
||||||
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return doipMessage;
|
return doipMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DoipMessage convertJsonElementToDoipMessage(JsonElement jsonElementRet, DoipMessage msg) {
|
public static DoipMessage doipOuputConvertJsonElementToDoipMessage(JsonElement jsonElementRet, DoipMessage msg) {
|
||||||
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
|
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
|
||||||
|
|
||||||
String responseCodeStr = jsonElementRet.getAsJsonObject().get("doipResponseCode").getAsString();
|
String responseCodeStr = jsonElementRet.getAsJsonObject().get("doipResponseCode").getAsString();
|
||||||
|
@ -124,7 +124,7 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
|
|||||||
try {
|
try {
|
||||||
// 改变调用的函数 + 构造DoipMessagePacker
|
// 改变调用的函数 + 构造DoipMessagePacker
|
||||||
Object ret = ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg);
|
Object ret = ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg);
|
||||||
return DOOPHandler.convertJsonElementToDoipMessage((JsonElement) ret, msg);
|
return DOOPHandler.doipOuputConvertJsonElementToDoipMessage((JsonElement) ret, msg);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -10,14 +10,8 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
|
|
||||||
|
|
||||||
public class DoipClientTest {
|
public class DoipClientTest {
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
final AtomicInteger total = new AtomicInteger(0);
|
|
||||||
final AtomicInteger correct = new AtomicInteger(0);
|
|
||||||
int totalCount = 10000;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void doipClientTest(){
|
public void doipClientRetrieveTest(){
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
final AtomicInteger total = new AtomicInteger(0);
|
final AtomicInteger total = new AtomicInteger(0);
|
||||||
final AtomicInteger correct = new AtomicInteger(0);
|
final AtomicInteger correct = new AtomicInteger(0);
|
||||||
int totalCount = 1;
|
int totalCount = 1;
|
||||||
@ -49,4 +43,38 @@ public class DoipClientTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void doipClientHelloTest(){
|
||||||
|
final AtomicInteger total = new AtomicInteger(0);
|
||||||
|
final AtomicInteger correct = new AtomicInteger(0);
|
||||||
|
int totalCount = 1;
|
||||||
|
for (int i = 0; i < totalCount; i++) {
|
||||||
|
final DoipClientImpl doipClient = new DoipClientImpl();
|
||||||
|
doipClient.connect(ClientConfig.fromUrl("tcp://127.0.0.1:8080"));
|
||||||
|
doipClient.hello("aibd/do.e626924a-3b1c-492f-9a41-59179bfe0361", new DoipMessageCallback() {
|
||||||
|
@Override
|
||||||
|
public void onResult(DoipMessage msg) {
|
||||||
|
String str = new String(msg.body.encodedData);
|
||||||
|
System.out.println("Result is " + str);
|
||||||
|
//LOGGER.info("Retrieved:" + str
|
||||||
|
//+ " respCode:" + msg.header.parameters.response);
|
||||||
|
total.incrementAndGet();
|
||||||
|
if (str.contains("aaa"))
|
||||||
|
correct.incrementAndGet();
|
||||||
|
if (doipClient != null) doipClient.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
int circle = 0;
|
||||||
|
for (; total.get() < totalCount; ) {
|
||||||
|
if (++circle % 100 == 0) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(10);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user