use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class DefaultRpcCodecTest method testOriginalTypeArrayRequest.
@Test
public void testOriginalTypeArrayRequest() throws Exception {
DefaultRequest request = getRequest("int[]", new Object[] { new int[] { 1, 2 } });
testCodecRequest(request);
}
use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class NettyHttpRequestHandler method buildRpcRequest.
protected DefaultRequest buildRpcRequest(FullHttpRequest httpRequest) throws UnsupportedEncodingException {
String uri = httpRequest.uri();
String[] uriInfo = uri.split("\\?");
String[] serviceInfo = uriInfo[0].split("/");
if (serviceInfo.length != 4) {
throw new MotanServiceException("invalid request uri! uri like '/${group}/${service}/${method}'");
}
DefaultRequest rpcRequest = new DefaultRequest();
rpcRequest.setAttachment(URLParamType.group.getName(), serviceInfo[1]);
rpcRequest.setInterfaceName(serviceInfo[2]);
rpcRequest.setMethodName(serviceInfo[3]);
HashMap<String, String> params = new HashMap<String, String>();
if (uriInfo.length == 2) {
addParams(params, uriInfo[1]);
}
ByteBuf buf = httpRequest.content();
final byte[] contentBytes = new byte[buf.readableBytes()];
buf.getBytes(0, contentBytes);
String body = new String(contentBytes, "UTF-8");
addParams(params, body);
MethodInfo mi = methodDescMap.get(rpcRequest.getMethodName());
if (mi != null && mi.isDuplicate()) {
mi = null;
String paramDesc = params.get("paramDesc");
if (StringUtils.isBlank(paramDesc)) {
throw new MotanServiceException("request method name conflict! paramDesc is required!" + rpcRequest.getMethodName());
}
mi = methodDescMap.get(rpcRequest.getMethodName() + paramDesc);
}
if (mi == null) {
throw new MotanServiceException("request method name not found" + rpcRequest.getMethodName());
}
rpcRequest.setParamtersDesc(mi.getMethodDesc());
// TODO other info
addAttachment(rpcRequest, httpRequest.headers());
rpcRequest.setArguments(parseArguments(params.get("params"), mi));
return rpcRequest;
}
use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class MotanServerCallHandler method unaryCall.
private <ReqT, RespT> Listener<ReqT> unaryCall(final ServerCall<ReqT, RespT> call, final Metadata headers) {
final ServerCallStreamObserverImpl<ReqT, RespT> responseObserver = new ServerCallStreamObserverImpl<ReqT, RespT>(call);
// see ServerCalls
call.request(2);
return new ServerCall.Listener<ReqT>() {
ReqT request;
@Override
public void onMessage(ReqT request) {
this.request = request;
}
@SuppressWarnings("unchecked")
@Override
public void onHalfClose() {
if (request != null) {
DefaultRequest motanRequest = getBaseMotanRequest(headers);
String ip = NetUtils.getHostName(call.attributes().get(ServerCall.REMOTE_ADDR_KEY));
if (ip != null) {
motanRequest.setAttachment(URLParamType.host.getName(), ip);
}
if (responseStream) {
motanRequest.setArguments(new Object[] { request, responseObserver });
} else {
motanRequest.setArguments(new Object[] { request });
}
Response response = null;
try {
response = provider.call(motanRequest);
if (response.getValue() != null) {
responseObserver.onNext((RespT) response.getValue());
responseObserver.onCompleted();
}
} catch (Exception e) {
responseObserver.onError(e);
return;
}
responseObserver.freeze();
if (call.isReady()) {
onReady();
}
} else {
call.close(Status.INTERNAL.withDescription("Half-closed without a request"), new Metadata());
}
}
@Override
public void onCancel() {
responseObserver.cancelled = true;
if (responseObserver.onCancelHandler != null) {
responseObserver.onCancelHandler.run();
}
}
@Override
public void onReady() {
if (responseObserver.onReadyHandler != null) {
responseObserver.onReadyHandler.run();
}
}
};
}
use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class MotanServerCallHandler method streamCall.
@SuppressWarnings("unchecked")
private <ReqT, RespT> Listener<ReqT> streamCall(final ServerCall<ReqT, RespT> call, Metadata headers) {
final ServerCallStreamObserverImpl<ReqT, RespT> responseObserver = new ServerCallStreamObserverImpl<ReqT, RespT>(call);
DefaultRequest request = getBaseMotanRequest(headers);
request.setArguments(new Object[] { responseObserver });
Response response = provider.call(request);
final StreamObserver<ReqT> requestObserver = (StreamObserver<ReqT>) response.getValue();
responseObserver.freeze();
if (responseObserver.autoFlowControlEnabled) {
call.request(1);
}
return new ServerCall.Listener<ReqT>() {
boolean halfClosed = false;
@Override
public void onMessage(ReqT request) {
requestObserver.onNext(request);
if (responseObserver.autoFlowControlEnabled) {
call.request(1);
}
}
@Override
public void onHalfClose() {
halfClosed = true;
requestObserver.onCompleted();
}
@Override
public void onCancel() {
responseObserver.cancelled = true;
if (responseObserver.onCancelHandler != null) {
responseObserver.onCancelHandler.run();
}
if (!halfClosed) {
requestObserver.onError(Status.CANCELLED.asException());
}
}
@Override
public void onReady() {
if (responseObserver.onReadyHandler != null) {
responseObserver.onReadyHandler.run();
}
}
};
}
use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class ProtobufCodec method decodeRequest.
private Object decodeRequest(CodedInputStream input, long requestId, Serialization serialization) throws IOException, ClassNotFoundException {
String interfaceName = input.readString();
String methodName = input.readString();
String paramtersDesc = input.readString();
DefaultRequest rpcRequest = new DefaultRequest();
rpcRequest.setRequestId(requestId);
rpcRequest.setInterfaceName(interfaceName);
rpcRequest.setMethodName(methodName);
rpcRequest.setParamtersDesc(paramtersDesc);
rpcRequest.setArguments(decodeRequestParameter(input, paramtersDesc, serialization));
rpcRequest.setAttachments(decodeRequestAttachments(input));
return rpcRequest;
}
Aggregations