use of com.weibo.yar.YarResponse 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.YarResponse in project motan by weibocom.
the class YarProtocolUtilTest method testConvertYarResponse.
@Test
public void testConvertYarResponse() {
DefaultResponse response = new DefaultResponse();
response.setRequestId(456);
response.setValue("stringValue");
YarResponse yarResponse = YarProtocolUtil.convert(response, "JSON");
assertNotNull(yarResponse);
Response newResponse = YarProtocolUtil.convert(yarResponse);
assertEquals(response.getRequestId(), newResponse.getRequestId());
assertEquals(response.getValue(), newResponse.getValue());
response.setException(new RuntimeException("test exception"));
yarResponse = YarProtocolUtil.convert(response, "JSON");
assertNotNull(yarResponse);
newResponse = YarProtocolUtil.convert(yarResponse);
assertEquals(response.getRequestId(), newResponse.getRequestId());
// yarresponse的异常会转为motan业务异常
assertEquals(new MotanBizException(response.getException().getMessage()).getMessage(), newResponse.getException().getMessage());
}
use of com.weibo.yar.YarResponse in project motan by weibocom.
the class YarMessageHandlerWarpperTest method getYarResponse.
private YarResponse getYarResponse(FullHttpResponse httpResponse) throws Exception {
ByteBuf buf = httpResponse.content();
final byte[] contentBytes = new byte[buf.readableBytes()];
buf.getBytes(0, contentBytes);
YarResponse yarResponse = YarProtocol.buildResponse(contentBytes);
return yarResponse;
}
use of com.weibo.yar.YarResponse in project motan by weibocom.
the class YarMessageRouter method handle.
@Override
public Object handle(Channel channel, Object message) {
YarRequest yarRequest = (YarRequest) message;
String packagerName = yarRequest.getPackagerName();
Provider<?> provider = providerMap.get(yarRequest.getRequestPath());
if (provider == null) {
throw new MotanServiceException("can not find service provider. request path:" + yarRequest.getRequestPath());
}
Class<?> clazz = provider.getInterface();
Request request = YarProtocolUtil.convert(yarRequest, clazz);
Response response = call(request, provider);
YarResponse yarResponse = YarProtocolUtil.convert(response, packagerName);
return yarResponse;
}
use of com.weibo.yar.YarResponse in project motan by weibocom.
the class YarProtocolUtil method convert.
public static YarResponse convert(Response response, String packagerName) {
YarResponse yarResponse = new YarResponse();
yarResponse.setId(response.getRequestId());
yarResponse.setPackagerName(packagerName);
if (response.getException() != null) {
if (response.getException() instanceof MotanBizException) {
yarResponse.setError(response.getException().getCause().getMessage());
} else {
yarResponse.setError(response.getException().getMessage());
}
} else {
yarResponse.setRet(response.getValue());
}
return yarResponse;
}
Aggregations