use of com.baidu.brpc.exceptions.RpcException in project brpc-java by baidu.
the class DefaultServerPushProtocol method decodeBodyByteBuf.
public SPBody decodeBodyByteBuf(ByteBuf bodyByteBuf) {
try {
int readableBytes = bodyByteBuf.readableBytes();
byte[] bodyBytes = new byte[readableBytes];
bodyByteBuf.readBytes(bodyBytes);
Schema<SPBody> schema = RuntimeSchema.getSchema(SPBody.class);
SPBody spBody = new SPBody();
ProtobufIOUtil.mergeFrom(bodyBytes, spBody, schema);
return spBody;
} catch (Exception e) {
throw new RpcException(e);
} finally {
if (bodyByteBuf != null) {
bodyByteBuf.release();
}
}
}
use of com.baidu.brpc.exceptions.RpcException in project brpc-java by baidu.
the class SofaRpcProtocol method decodeResponse.
@Override
public RpcResponse decodeResponse(Object packet, ChannelHandlerContext ctx) throws Exception {
SofaRpcDecodePacket responsePacket = (SofaRpcDecodePacket) packet;
ByteBuf metaBuf = responsePacket.getMetaBuf();
ByteBuf protoBuf = responsePacket.getProtoBuf();
try {
RpcResponse rpcResponse = new RpcResponse();
SofaRpcProto.SofaRpcMeta responseMeta = (SofaRpcProto.SofaRpcMeta) ProtobufUtils.parseFrom(metaBuf, defaultRpcMetaInstance);
Long correlationId = responseMeta.getSequenceId();
rpcResponse.setCorrelationId(correlationId);
ChannelInfo channelInfo = ChannelInfo.getClientChannelInfo(ctx.channel());
RpcFuture future = channelInfo.removeRpcFuture(rpcResponse.getCorrelationId());
if (future == null) {
return rpcResponse;
}
rpcResponse.setRpcFuture(future);
int compressType = getStandardCompressType(responseMeta.getCompressType());
rpcResponse.setCompressType(compressType);
try {
if (responseMeta != null && responseMeta.getErrorCode() == 0) {
Compress compress = compressManager.getCompress(compressType);
Object result = compress.uncompressOutput(protoBuf, future.getRpcMethodInfo());
rpcResponse.setResult(result);
} else {
rpcResponse.setException(new RpcException(RpcException.SERVICE_EXCEPTION, responseMeta.getReason()));
}
} catch (Exception ex) {
LOG.warn("decode response failed");
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "decode response failed", ex);
}
return rpcResponse;
} finally {
if (metaBuf != null) {
metaBuf.release();
}
if (protoBuf != null) {
protoBuf.release();
}
}
}
use of com.baidu.brpc.exceptions.RpcException in project brpc-java by baidu.
the class SofaRpcProtocol method decodeRequest.
@Override
public Request decodeRequest(Object packet) throws Exception {
Request request = this.createRequest();
SofaRpcDecodePacket requestPacket = (SofaRpcDecodePacket) packet;
ByteBuf metaBuf = requestPacket.getMetaBuf();
ByteBuf protoBuf = requestPacket.getProtoBuf();
try {
SofaRpcProto.SofaRpcMeta requestMeta = (SofaRpcProto.SofaRpcMeta) ProtobufUtils.parseFrom(metaBuf, defaultRpcMetaInstance);
request.setCorrelationId(requestMeta.getSequenceId());
if (StringUtils.isBlank(requestMeta.getMethod())) {
String errorMsg = "method is null";
LOG.error(errorMsg);
request.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errorMsg));
return request;
}
ServiceManager serviceManager = ServiceManager.getInstance();
RpcMethodInfo rpcMethodInfo = serviceManager.getService(requestMeta.getMethod().toLowerCase());
if (rpcMethodInfo == null) {
String errorMsg = String.format("Fail to find method=%s", requestMeta.getMethod());
LOG.error(errorMsg);
request.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errorMsg));
return request;
}
request.setServiceName(rpcMethodInfo.getServiceName());
request.setMethodName(rpcMethodInfo.getMethodName());
request.setRpcMethodInfo(rpcMethodInfo);
request.setTargetMethod(rpcMethodInfo.getMethod());
request.setTarget(rpcMethodInfo.getTarget());
int compressType = getStandardCompressType(requestMeta.getCompressType());
request.setCompressType(compressType);
Compress compress = compressManager.getCompress(compressType);
try {
Object requestProto = compress.uncompressInput(protoBuf, rpcMethodInfo);
request.setArgs(new Object[] { requestProto });
} catch (Exception ex) {
String errorMsg = String.format("decode request failed, msg=%s", ex.getMessage());
LOG.error(errorMsg);
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, errorMsg);
}
return request;
} finally {
if (metaBuf != null) {
metaBuf.release();
}
if (protoBuf != null) {
protoBuf.release();
}
}
}
use of com.baidu.brpc.exceptions.RpcException in project brpc-java by baidu.
the class SofaRpcProtocol method decode.
@Override
public SofaRpcDecodePacket decode(ChannelHandlerContext ctx, DynamicCompositeByteBuf in, boolean isDecodingRequest) throws BadSchemaException, TooBigDataException, NotEnoughDataException {
if (in.readableBytes() < FIXED_LEN) {
throw notEnoughDataException;
}
ByteBuf fixHeaderBuf = in.retainedSlice(FIXED_LEN);
try {
byte[] magic = new byte[4];
fixHeaderBuf.readBytes(magic);
if (!Arrays.equals(magic, MAGIC_HEAD)) {
throw new BadSchemaException("not valid magic head for sofa");
}
int metaSize = fixHeaderBuf.readIntLE();
int bodySize = (int) fixHeaderBuf.readLongLE();
int msgSize = (int) fixHeaderBuf.readLongLE();
if (msgSize != metaSize + bodySize) {
throw new BadSchemaException("msgSize != metaSize + bodySize");
}
// 512M
if (bodySize > 512 * 1024 * 1024) {
throw new TooBigDataException("to big body size:" + bodySize);
}
if (in.readableBytes() < FIXED_LEN + msgSize) {
throw notEnoughDataException;
}
in.skipBytes(FIXED_LEN);
SofaRpcDecodePacket packet = new SofaRpcDecodePacket();
try {
// meta
ByteBuf metaBuf = in.readRetainedSlice(metaSize);
packet.setMetaBuf(metaBuf);
// body
ByteBuf protoBuf = in.readRetainedSlice(bodySize);
packet.setProtoBuf(protoBuf);
return packet;
} catch (Exception ex) {
LOG.debug("decode failed, ex={}", ex.getMessage());
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, ex);
}
} finally {
fixHeaderBuf.release();
}
}
use of com.baidu.brpc.exceptions.RpcException in project brpc-java by baidu.
the class HttpRpcProtocol method decodeRequest.
@Override
public Request decodeRequest(Object packet) {
try {
HttpRequest httpRequest = (HttpRequest) this.createRequest();
httpRequest.setMsg(packet);
long correlationId = parseCorrelationId(httpRequest.headers().get(CORRELATION_ID), null);
httpRequest.setCorrelationId(correlationId);
String contentTypeAndEncoding = httpRequest.headers().get(HttpHeaderNames.CONTENT_TYPE);
if (StringUtils.isBlank(contentTypeAndEncoding)) {
String errMsg = String.format("content-type is null, uri:%s", httpRequest.uri());
LOG.warn(errMsg);
httpRequest.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errMsg));
return httpRequest;
}
contentTypeAndEncoding = contentTypeAndEncoding.toLowerCase();
String[] splits = StringUtils.split(contentTypeAndEncoding, ";");
int protocolType = HttpRpcProtocol.parseProtocolType(splits[0]);
String encoding = this.encoding;
for (String split : splits) {
split = split.trim();
if (split.startsWith("charset=")) {
encoding = split.substring("charset=".length());
}
}
httpRequest.headers().set(PROTOCOL_TYPE, protocolType);
httpRequest.headers().set(HttpHeaderNames.CONTENT_ENCODING, encoding);
// set http headers to attachment
if (httpRequest.getKvAttachment() == null) {
httpRequest.setKvAttachment(new HashMap<String, Object>());
}
for (Map.Entry<String, String> entry : httpRequest.headers()) {
httpRequest.getKvAttachment().put(entry.getKey(), entry.getValue());
}
ByteBuf byteBuf = httpRequest.content();
int bodyLen = byteBuf.readableBytes();
if (bodyLen == 0) {
String errMsg = String.format("body should not be null, uri:%s", httpRequest.uri());
LOG.warn(errMsg);
httpRequest.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errMsg));
return httpRequest;
}
byte[] requestBytes = new byte[bodyLen];
byteBuf.readBytes(requestBytes, 0, bodyLen);
Object body = decodeBody(protocolType, encoding, requestBytes);
QueryStringDecoder queryStringDecoder = new QueryStringDecoder(httpRequest.uri());
String path = queryStringDecoder.path();
String serviceName = null;
String methodName = null;
if (protocolType == Options.ProtocolType.PROTOCOL_HTTP_PROTOBUF_VALUE || protocolType == Options.ProtocolType.PROTOCOL_HTTP_JSON_VALUE) {
boolean pathError;
try {
serviceName = path.substring(1, path.lastIndexOf("/"));
methodName = path.substring(path.lastIndexOf("/") + 1);
pathError = serviceName == null || serviceName.isEmpty() || methodName == null || methodName.isEmpty();
} catch (Exception e) {
LOG.warn("url format is error", e);
pathError = true;
}
if (pathError) {
String errMsg = String.format("url format is error, path:%s", path);
httpRequest.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errMsg));
return httpRequest;
}
} else {
JsonObject bodyObject = (JsonObject) body;
methodName = bodyObject.get("method").getAsString();
serviceName = path;
}
ServiceManager serviceManager = ServiceManager.getInstance();
RpcMethodInfo rpcMethodInfo = serviceManager.getService(serviceName, methodName);
if (rpcMethodInfo == null) {
String errMsg = String.format("Fail to find path=%s", path);
LOG.warn(errMsg);
httpRequest.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errMsg));
return httpRequest;
}
httpRequest.setServiceName(rpcMethodInfo.getServiceName());
httpRequest.setMethodName(rpcMethodInfo.getMethodName());
httpRequest.setRpcMethodInfo(rpcMethodInfo);
httpRequest.setTargetMethod(rpcMethodInfo.getMethod());
httpRequest.setTarget(rpcMethodInfo.getTarget());
try {
httpRequest.setArgs(parseRequestParam(protocolType, body, rpcMethodInfo));
} catch (RpcException e) {
LOG.error("parse request param error", e);
httpRequest.setException(e);
return httpRequest;
}
return httpRequest;
} finally {
((FullHttpRequest) packet).release();
}
}
Aggregations