Search in sources :

Example 1 with HttpMessage

use of com.bonree.brfs.common.net.http.HttpMessage in project BRFS by zhangnianli.

the class NettyHttpRequestHandler method requestReceived.

public void requestReceived(ChannelHandlerContext ctx, FullHttpRequest request) {
    LOG.debug("ctx{}, handle request[{}:{}]", ctx.channel(), request.method(), request.uri());
    MessageHandler handler = methodToOps.get(request.method());
    if (handler == null) {
        LOG.error("Exception context[{}] method[{}] is unknown", ctx.toString(), request.method());
        ResponseSender.sendError(ctx, HttpResponseStatus.METHOD_NOT_ALLOWED, HttpResponseStatus.METHOD_NOT_ALLOWED.reasonPhrase());
        return;
    }
    String path = new QueryStringDecoder(request.uri(), CharsetUtil.UTF_8, true).path();
    String uri = request.uri();
    byte[] content = new byte[request.content().readableBytes()];
    request.content().readBytes(content);
    HttpMessage message = new HttpMessage() {

        @Override
        public String getPath() {
            return path;
        }

        @Override
        public Map<String, String> getParams() {
            return HttpParamsDecoder.decodeFromUri(uri);
        }

        @Override
        public byte[] getContent() {
            return content;
        }
    };
    executors.submit(new Runnable() {

        @Override
        public void run() {
            try {
                if (!handler.isValidRequest(message)) {
                    LOG.error("Exception context[{}] method[{}] invalid request message[{}]", ctx.toString(), message.getPath());
                    ResponseSender.sendError(ctx, HttpResponseStatus.BAD_REQUEST, HttpResponseStatus.BAD_REQUEST.reasonPhrase());
                    return;
                }
                handler.handle(message, new DefaultNettyHandleResultCallback(ctx));
            } catch (Exception e) {
                LOG.error("message handle error", e);
                ResponseSender.sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR, e.toString());
            }
        }
    });
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) MessageHandler(com.bonree.brfs.common.net.http.MessageHandler) HttpMessage(com.bonree.brfs.common.net.http.HttpMessage)

Aggregations

HttpMessage (com.bonree.brfs.common.net.http.HttpMessage)1 MessageHandler (com.bonree.brfs.common.net.http.MessageHandler)1 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)1