Search in sources :

Example 1 with RestHttpResponse

use of rpc.turbo.transport.server.rest.protocol.RestHttpResponse in project turbo-rpc by hank-whu.

the class RestHttResponseEncoder method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RestHttpResponse)) {
        ctx.write(msg, promise);
        return;
    }
    RestHttpResponse restHttpResponse = (RestHttpResponse) msg;
    HttpResponseStatus status = restHttpResponse.getStatus();
    if (status == null) {
        status = INTERNAL_SERVER_ERROR;
    }
    if (restHttpResponse.getResult() == null) {
        status = INTERNAL_SERVER_ERROR;
        restHttpResponse.setStatus(status);
        restHttpResponse.setResult("UNKNOWN");
    }
    doResponse(ctx, promise, restHttpResponse);
}
Also used : RestHttpResponse(rpc.turbo.transport.server.rest.protocol.RestHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus)

Example 2 with RestHttpResponse

use of rpc.turbo.transport.server.rest.protocol.RestHttpResponse in project turbo-rpc by hank-whu.

the class NettyRestHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, final FullHttpRequest httpRequest) throws Exception {
    boolean keepAlive = HttpUtil.isKeepAlive(httpRequest);
    String uri = httpRequest.uri();
    HttpMethod httpMethod = httpRequest.method();
    int index = uri.indexOf('&', invokerFactory.restPrefix.length());
    if (index < 0) {
        index = uri.length();
    }
    if (invokerFactory.restPrefix.length() >= index) {
        if (logger.isInfoEnabled()) {
            logger.info("not support this method " + toString(httpRequest));
        }
        doRequestFilter(httpRequest, null);
        ctx.write(new RestHttpResponse(null, httpRequest, NOT_FOUND, NOT_SUPPORT_THIS_METHOD, keepAlive), ctx.voidPromise());
        return;
    }
    String restPath = uri.substring(invokerFactory.restPrefix.length(), index);
    final Invoker<CompletableFuture<?>> invoker = invokerFactory.get(restPath);
    CompletableFuture<?> future = null;
    try {
        if (invoker == null) {
            if (logger.isInfoEnabled()) {
                logger.info("not support this method " + toString(httpRequest));
            }
            doRequestFilter(httpRequest, null);
            ctx.write(new RestHttpResponse(null, httpRequest, NOT_FOUND, NOT_SUPPORT_THIS_METHOD, keepAlive), ctx.voidPromise());
            return;
        }
        boolean allowHandle = doRequestFilter(httpRequest, invoker);
        if (!allowHandle) {
            ctx.write(new RestHttpResponse(invoker, httpRequest, SERVICE_UNAVAILABLE, RestServerFilter.SERVER_FILTER_DENY, keepAlive), ctx.voidPromise());
            return;
        }
        Object params = null;
        if (httpMethod == HttpMethod.GET) {
            params = HttpParamExtractor.extractFromQueryPath(invoker, uri, index);
        } else if (httpMethod == HttpMethod.POST) {
            params = HttpParamExtractor.extractFromBody(invoker, jsonMapper, httpRequest.content());
        } else {
            if (logger.isInfoEnabled()) {
                logger.info("only support get and post " + toString(httpRequest));
            }
            ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, ONLY_SUPPORT_GET_POST, keepAlive), ctx.voidPromise());
            return;
        }
        if (params == null) {
            future = invoker.invoke();
        } else if (params instanceof MethodParam) {
            future = invoker.invoke((MethodParam) params);
        } else if (params instanceof Object[]) {
            future = invoker.invoke((Object[]) params);
        } else {
            future = invoker.invoke((Object) params);
        }
    } catch (Throwable e) {
        if (logger.isWarnEnabled()) {
            logger.warn(uri + " error ", e);
        }
        ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, e, keepAlive), ctx.voidPromise());
        return;
    }
    if (future == null) {
        if (logger.isWarnEnabled()) {
            logger.warn("unknown error " + toString(httpRequest));
        }
        ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, UNKNOWN, keepAlive), ctx.voidPromise());
        return;
    }
    future.whenComplete((result, throwable) -> {
        if (result != null) {
            ctx.write(new RestHttpResponse(invoker, httpRequest, OK, result, keepAlive), ctx.voidPromise());
        } else if (throwable != null) {
            ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, throwable, keepAlive), ctx.voidPromise());
        } else {
            ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, UNKNOWN, keepAlive), ctx.voidPromise());
        }
    });
}
Also used : MethodParam(rpc.turbo.param.MethodParam) CompletableFuture(java.util.concurrent.CompletableFuture) RestHttpResponse(rpc.turbo.transport.server.rest.protocol.RestHttpResponse) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Aggregations

RestHttpResponse (rpc.turbo.transport.server.rest.protocol.RestHttpResponse)2 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 MethodParam (rpc.turbo.param.MethodParam)1