Search in sources :

Example 6 with YarResponse

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;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) YarRequest(com.weibo.yar.YarRequest) YarResponse(com.weibo.yar.YarResponse) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) IOException(java.io.IOException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) AttachmentRequest(com.weibo.api.motan.protocol.yar.AttachmentRequest)

Example 7 with YarResponse

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());
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) YarResponse(com.weibo.yar.YarResponse) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) YarResponse(com.weibo.yar.YarResponse) MotanBizException(com.weibo.api.motan.exception.MotanBizException) Test(org.junit.Test)

Example 8 with YarResponse

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;
}
Also used : YarResponse(com.weibo.yar.YarResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 9 with 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;
}
Also used : Response(com.weibo.api.motan.rpc.Response) YarResponse(com.weibo.yar.YarResponse) YarRequest(com.weibo.yar.YarRequest) YarRequest(com.weibo.yar.YarRequest) Request(com.weibo.api.motan.rpc.Request) YarResponse(com.weibo.yar.YarResponse) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 10 with 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;
}
Also used : YarResponse(com.weibo.yar.YarResponse) MotanBizException(com.weibo.api.motan.exception.MotanBizException)

Aggregations

YarResponse (com.weibo.yar.YarResponse)10 YarRequest (com.weibo.yar.YarRequest)5 Test (org.junit.Test)5 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)3 MotanBizException (com.weibo.api.motan.exception.MotanBizException)2 AttachmentRequest (com.weibo.api.motan.protocol.yar.AttachmentRequest)2 YarMessageRouter (com.weibo.api.motan.protocol.yar.YarMessageRouter)2 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)2 Response (com.weibo.api.motan.rpc.Response)2 Channel (com.weibo.api.motan.transport.Channel)2 ByteBuf (io.netty.buffer.ByteBuf)2 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)1 DefaultProvider (com.weibo.api.motan.rpc.DefaultProvider)1 Provider (com.weibo.api.motan.rpc.Provider)1 Request (com.weibo.api.motan.rpc.Request)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 IOException (java.io.IOException)1