use of org.msec.rpc.HttpFormatException in project MSEC by Tencent.
the class RequestDecoder method deserializeHTTPPackage.
private RpcRequest deserializeHTTPPackage(String request) {
RpcRequest rpcRequest = new RpcRequest();
HttpRequestParser httpParser = new HttpRequestParser();
try {
httpParser.parseRequest(request);
} catch (IOException ex) {
log.error("HTTP Request parse failed." + ex.getMessage());
rpcRequest.setException(new IllegalArgumentException("Request decode error: HTTP Request parse failed." + ex.getMessage()));
return rpcRequest;
} catch (HttpFormatException ex) {
log.error("HTTP Request parse failed." + ex.getMessage());
rpcRequest.setException(new IllegalArgumentException("Request decode error: HTTP Request parse failed." + ex.getMessage()));
return rpcRequest;
}
rpcRequest.setHttpCgiName(httpParser.getCgiPath());
if (httpParser.getCgiPath().compareToIgnoreCase("/list") == 0) {
return rpcRequest;
}
if (httpParser.getCgiPath().compareToIgnoreCase("/invoke") != 0) {
log.error("HTTP cgi not found: " + httpParser.getCgiPath());
rpcRequest.setException(new IllegalArgumentException("HTTP cgi not found: " + httpParser.getURI()));
return rpcRequest;
}
String serviceMethodName = httpParser.getQueryString("methodName");
int pos = serviceMethodName.lastIndexOf('.');
if (pos == -1 || pos == 0 || pos == serviceMethodName.length() - 1) {
log.error("Invalid serviceMethodName (" + serviceMethodName + "). Must be in format like *.*");
rpcRequest.setException(new IllegalArgumentException("Invalid serviceMethodName (" + serviceMethodName + "). Must be in format like *.*"));
return rpcRequest;
}
String serviceName = serviceMethodName.substring(0, pos);
String methodName = serviceMethodName.substring(pos + 1);
String seq = httpParser.getQueryString("seq");
String param = httpParser.getQueryString("param");
if (param == null || param.isEmpty()) {
param = httpParser.getMessageBody();
}
try {
param = java.net.URLDecoder.decode(param, "UTF-8");
} catch (UnsupportedEncodingException e) {
param = "";
}
rpcRequest.setServiceName(serviceName);
rpcRequest.setMethodName(methodName);
if (seq == null || seq.isEmpty()) {
rpcRequest.setSeq(ServiceFactory.generateSequence());
} else {
rpcRequest.setSeq(Long.valueOf(seq).longValue());
}
log.info("In HTTP package service: " + serviceName + " method: " + methodName + " param: " + param);
rpcRequest.setFromModule("UnknownModule");
rpcRequest.setFlowid(rpcRequest.getSeq() ^ NettyCodecUtils.generateColorId(serviceName + methodName));
AccessMonitor.add("frm.rpc request incoming: " + methodName);
ServiceFactory.ServiceMethodEntry serviceMethodEntry = ServiceFactory.getServiceMethodEntry(serviceName, methodName);
if (serviceMethodEntry == null) {
log.error("No service method registered: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName());
rpcRequest.setException(new IllegalArgumentException("No service method registered: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName()));
return rpcRequest;
}
Message.Builder builder = serviceMethodEntry.getParamTypeBuilder();
try {
builder.clear();
JsonFormat.merge(param, builder);
} catch (JsonFormat.ParseException ex) {
log.error("Json parse failed." + ex.getMessage());
rpcRequest.setException(new IllegalArgumentException("Request decode error: Json parse failed." + ex.getMessage()));
return rpcRequest;
}
if (!builder.isInitialized()) {
log.error("Json to Protobuf failed: missing required fields");
rpcRequest.setException(new IllegalArgumentException("Json to Protobuf failed: missing required fields: " + builder.getInitializationErrorString()));
return rpcRequest;
}
rpcRequest.setParameter(builder.build());
log.info("RPC Request received. ServiceMethodName: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName() + "\tSeq: " + rpcRequest.getSeq() + "\tParameter: " + rpcRequest.getParameter());
return rpcRequest;
}
Aggregations