use of com.weibo.api.motan.protocol.yar.AttachmentRequest in project motan by weibocom.
the class YarMessageHandlerWarpperTest method testHandle.
@Test
public void testHandle() throws Exception {
YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
final YarResponse yarResponse = YarProtocolUtil.buildDefaultErrorResponse("test err", "JSON");
YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {
@Override
public Object handle(Channel channel, Object message) {
AttachmentRequest request = (AttachmentRequest) message;
verifyAttachments(request.getAttachments());
return yarResponse;
}
});
FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
assertNotNull(httpResponse);
assertNotNull(httpResponse.content());
YarResponse retYarResponse = getYarResponse(httpResponse);
assertNotNull(retYarResponse);
assertEquals(yarResponse, retYarResponse);
}
use of com.weibo.api.motan.protocol.yar.AttachmentRequest 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;
}
Aggregations