use of com.weibo.yar.YarRequest in project motan by weibocom.
the class YarProtocolUtil method convert.
public static YarRequest convert(Request request, String packagerName) {
YarRequest yarRequest = new YarRequest();
yarRequest.setId(request.getRequestId());
yarRequest.setMethodName(request.getMethodName());
yarRequest.setPackagerName(packagerName);
yarRequest.setParameters(request.getArguments());
return yarRequest;
}
use of com.weibo.yar.YarRequest in project motan by weibocom.
the class YarMessageHandlerWarpper method handle.
@Override
public Object handle(Channel channel, Object message) {
FullHttpRequest httpRequest = (FullHttpRequest) message;
String uri = httpRequest.getUri();
// should not be null
int index = uri.indexOf("?");
String requestPath = uri;
Map<String, String> attachments = null;
if (index > -1) {
requestPath = uri.substring(0, index);
if (index != uri.length() - 1) {
attachments = getAttachMents(uri.substring(index + 1, uri.length()));
}
}
YarResponse yarResponse = null;
String packagerName = "JSON";
try {
ByteBuf buf = httpRequest.content();
final byte[] contentBytes = new byte[buf.readableBytes()];
buf.getBytes(0, contentBytes);
YarRequest yarRequest = new AttachmentRequest(YarProtocol.buildRequest(contentBytes), attachments);
yarRequest.setRequestPath(requestPath);
yarResponse = (YarResponse) orgHandler.handle(channel, yarRequest);
} catch (Exception e) {
LoggerUtil.error("YarMessageHandlerWarpper handle yar request fail.", e);
yarResponse = YarProtocolUtil.buildDefaultErrorResponse(e.getMessage(), packagerName);
}
byte[] responseBytes;
try {
responseBytes = YarProtocol.toProtocolBytes(yarResponse);
} catch (IOException e) {
throw new MotanFrameworkException("convert yar response to bytes fail.", e);
}
FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(responseBytes));
httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded");
httpResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, httpResponse.content().readableBytes());
if (HttpHeaders.isKeepAlive(httpRequest)) {
httpResponse.headers().set(HttpHeaders.Names.CONNECTION, Values.KEEP_ALIVE);
} else {
httpResponse.headers().set(HttpHeaders.Names.CONNECTION, Values.CLOSE);
}
return httpResponse;
}
use of com.weibo.yar.YarRequest in project motan by weibocom.
the class YarProtocolUtilTest method verifyMethodParam.
private <T> void verifyMethodParam(Class<T> interfaceClazz, T service, Method method, Object[] params, Object expectResult) throws Exception {
YarRequest yarRequest = new YarRequest();
yarRequest.setId(123);
yarRequest.setMethodName(method.getName());
yarRequest.setPackagerName("JSON");
yarRequest.setParameters(params);
Request request = YarProtocolUtil.convert(yarRequest, interfaceClazz);
assertNotNull(request);
assertEquals(method.getName(), request.getMethodName());
Object[] requestParams = request.getArguments();
assertEquals(params.length, requestParams.length);
Object result = method.invoke(service, requestParams);
assertEquals(expectResult, result);
}
Aggregations