Search in sources :

Example 6 with YarRequest

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;
}
Also used : YarRequest(com.weibo.yar.YarRequest)

Example 7 with 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;
}
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 8 with YarRequest

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);
}
Also used : YarRequest(com.weibo.yar.YarRequest) YarRequest(com.weibo.yar.YarRequest) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest)

Aggregations

YarRequest (com.weibo.yar.YarRequest)8 YarResponse (com.weibo.yar.YarResponse)5 Test (org.junit.Test)4 Request (com.weibo.api.motan.rpc.Request)3 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)3 AttachmentRequest (com.weibo.api.motan.protocol.yar.AttachmentRequest)2 YarMessageRouter (com.weibo.api.motan.protocol.yar.YarMessageRouter)2 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)2 Channel (com.weibo.api.motan.transport.Channel)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 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)1 Provider (com.weibo.api.motan.rpc.Provider)1 Response (com.weibo.api.motan.rpc.Response)1 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 IOException (java.io.IOException)1