YarMessageHandlerWarpper implements MessageHandler { @Override public Object handle(Channel channel, Object message) { FullHttpRequest httpRequest = (FullHttpRequest) message; String uri = httpRequest.getUri(); 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; } YarMessageHandlerWarpper(MessageHandler orgHandler); @Override Object handle(Channel channel, Object message);  }
@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); }
@Test public void testAbnormal() throws Exception { final String errmsg = "rpc process error"; YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() { @Override public Object handle(Channel channel, Object message) { throw new RuntimeException(errmsg); } }); FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(null, uri)); assertNotNull(httpResponse); assertEquals(HttpResponseStatus.OK, httpResponse.getStatus()); YarResponse retYarResponse = getYarResponse(httpResponse); assertNotNull(retYarResponse); assertNotNull(retYarResponse.getError()); YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] {"params", 456}); httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri)); assertNotNull(httpResponse); assertEquals(HttpResponseStatus.OK, httpResponse.getStatus()); retYarResponse = getYarResponse(httpResponse); assertNotNull(retYarResponse); assertEquals(errmsg, retYarResponse.getError()); }