First Commit

This commit is contained in:
Administrator
2025-05-12 12:04:42 +08:00
commit 6a5e13974c
1248 changed files with 366157 additions and 0 deletions

View File

@@ -0,0 +1,200 @@
package quant.rich.emoney.controller.api;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.protobuf.nano.MessageNano;
import org.apache.commons.lang3.StringUtils;
import lombok.extern.slf4j.Slf4j;
import nano.BaseResponse.Base_Response;
import quant.rich.emoney.entity.sqlite.ProtocolMatch;
import quant.rich.emoney.exception.RException;
import quant.rich.emoney.pojo.dto.EmoneyConvertResult;
import quant.rich.emoney.pojo.dto.EmoneyProtobufBody;
import quant.rich.emoney.service.sqlite.ProtocolMatchService;
@RestController
@RequestMapping("/api/v1/proto")
@Slf4j
public class ProtoDecodeControllerV1 {
@Autowired
private ProtocolMatchService protocolMatchService;
@SuppressWarnings("unchecked")
@PostMapping("/request/decode")
public <U extends MessageNano> EmoneyConvertResult requestDecode(
@RequestBody(required=true)
@NonNull
EmoneyProtobufBody body) {
Integer protocolId = body.getProtocolId();
if (Objects.isNull(protocolId)) {
throw RException.badRequest("protocolId cannot be null");
}
ProtocolMatch match = protocolMatchService.getById(protocolId);
if (Objects.isNull(match) || StringUtils.isBlank(match.getClassName())) {
throw RException.badRequest("暂无对应 protocolId = " + protocolId + " 的记录,可等 response decoder 搜集到后重试");
}
String className = new StringBuilder()
.append("nano.")
.append(match.getClassName())
.append("Request$")
.append(match.getClassName())
.append("_Request")
.toString();
// IndexInflow -> nano.IndexInflowRequest$IndexInflow_Request
Class<U> clazz;
try {
clazz = (Class<U>)Class.forName(className);
}
catch (Exception e) {
String msg = new StringBuilder()
.append("无法根据给定的 protocolId = ")
.append(protocolId)
.append(", className = ")
.append(className)
.append("找到对应类").toString();
log.warn(msg, e);
throw RException.internalServerError(msg);
}
byte[] buf;
try {
buf = body.protocolBodyToByte();
}
catch (Exception e) {
throw RException.badRequest("转换 protocolBody 错误");
}
try {
U nano = (U)MessageNano.mergeFrom((MessageNano)
clazz.getDeclaredConstructor().newInstance(), buf);
return EmoneyConvertResult
.ok(new ObjectMapper().valueToTree(nano))
.setProtocolId(protocolId)
.setSupposedClassName(className);
}
catch (Exception e) {
String msg = new StringBuilder()
.append("转换为类 ")
.append(className)
.append(" 时错误").toString();
log.warn(msg, e);
return EmoneyConvertResult.error(msg)
.setProtocolId(protocolId)
.setSupposedClassName(className);
}
}
@SuppressWarnings("unchecked")
@PostMapping("/response/decode")
public <U extends MessageNano> EmoneyConvertResult responseDecode(
@RequestBody(required=false)
@NonNull
EmoneyProtobufBody body) {
Integer protocolId = body.getProtocolId();
ProtocolMatch match = null;
if (Objects.isNull(protocolId)) {
log.warn("protocolId is null, cannot update protocolMatch");
}
else {
match = protocolMatchService.getById(protocolId);
if (Objects.isNull(match)) {
match = new ProtocolMatch().setProtocolId(protocolId);
}
}
byte[] buf;
try {
buf = body.protocolBodyToByte();
}
catch (Exception e) {
throw RException.badRequest("转换 protocolBody 错误");
}
Base_Response baseResponse;
try {
baseResponse = Base_Response.parseFrom(buf);
}
catch (Exception e) {
String msg = new StringBuilder()
.append("转换 BaseResponse 发生错误")
.toString();
log.warn(msg, e);
return EmoneyConvertResult
.error(msg)
.setProtocolId(protocolId);
}
Class<U> clazz;
String className =
baseResponse.detail.getTypeUrl()
.replace("type.googleapis.com/", "");
String rawClassName = className.substring(0, className.lastIndexOf('_'));
if (Objects.nonNull(match) && StringUtils.isBlank(match.getClassName())) {
match.setClassName(rawClassName);
protocolMatchService.saveOrUpdate(match);
}
className = new StringBuilder()
.append("nano.")
.append(rawClassName)
.append("Response$")
.append(className)
.toString();
try {
clazz = (Class<U>)Class.forName(className);
}
catch (Exception e) {
String msg = new StringBuilder()
.append("无法根据给定的 protocolId = ")
.append(protocolId)
.append(", className = ")
.append(className)
.append("找到对应类").toString();
log.warn(msg, e);
throw RException.internalServerError(msg);
}
try {
U nano = (U)MessageNano.mergeFrom(
(MessageNano)clazz.getDeclaredConstructor().newInstance(),
baseResponse.detail.getValue());
return EmoneyConvertResult
.ok(new ObjectMapper().valueToTree(nano))
.setProtocolId(protocolId)
.setSupposedClassName(className);
}
catch (Exception e) {
String msg = new StringBuilder()
.append("转换为类 ")
.append(className)
.append(" 时错误").toString();
log.warn(msg, e);
return EmoneyConvertResult.error(msg)
.setProtocolId(protocolId)
.setSupposedClassName(className);
}
}
}