Search in sources :

Example 1 with CommandContext

use of org.apache.dubbo.qos.command.CommandContext in project dubbo by alibaba.

the class HttpCommandDecoder method decode.

public static CommandContext decode(HttpRequest request) {
    CommandContext commandContext = null;
    if (request != null) {
        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
        String path = queryStringDecoder.path();
        String[] array = path.split("/");
        if (array.length == 2) {
            String name = array[1];
            // process GET request and POST request separately. Check url for GET, and check body for POST
            if (request.getMethod() == HttpMethod.GET) {
                if (queryStringDecoder.parameters().isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    List<String> valueList = new ArrayList<String>();
                    for (List<String> values : queryStringDecoder.parameters().values()) {
                        valueList.addAll(values);
                    }
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}), true);
                }
            } else if (request.getMethod() == HttpMethod.POST) {
                HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(request);
                List<String> valueList = new ArrayList<String>();
                for (InterfaceHttpData interfaceHttpData : httpPostRequestDecoder.getBodyHttpDatas()) {
                    if (interfaceHttpData.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
                        Attribute attribute = (Attribute) interfaceHttpData;
                        try {
                            valueList.add(attribute.getValue());
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
                if (valueList.isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}), true);
                }
            }
        }
    }
    return commandContext;
}
Also used : CommandContext(org.apache.dubbo.qos.command.CommandContext) Attribute(io.netty.handler.codec.http.multipart.Attribute) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with CommandContext

use of org.apache.dubbo.qos.command.CommandContext in project dubbo by alibaba.

the class HttpProcessHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
    CommandContext commandContext = HttpCommandDecoder.decode(msg);
    // return 404 when fail to construct command context
    FullHttpResponse response;
    if (commandContext == null) {
        log.warn("can not found commandContext url: " + msg.getUri());
        response = http404();
    } else {
        commandContext.setRemote(ctx.channel());
        try {
            String result = commandExecutor.execute(commandContext);
            response = http200(result);
        } catch (NoSuchCommandException ex) {
            log.error("can not find commandContext: " + commandContext, ex);
            response = http404();
        } catch (Exception qosEx) {
            log.error("execute commandContext: " + commandContext + " got exception", qosEx);
            response = http500(qosEx.getMessage());
        }
    }
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
Also used : CommandContext(org.apache.dubbo.qos.command.CommandContext) NoSuchCommandException(org.apache.dubbo.qos.command.NoSuchCommandException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) NoSuchCommandException(org.apache.dubbo.qos.command.NoSuchCommandException)

Example 3 with CommandContext

use of org.apache.dubbo.qos.command.CommandContext in project dubbo by alibaba.

the class TelnetProcessHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
    if (StringUtils.isBlank(msg)) {
        ctx.writeAndFlush(QosProcessHandler.PROMPT);
    } else {
        CommandContext commandContext = TelnetCommandDecoder.decode(msg);
        commandContext.setRemote(ctx.channel());
        try {
            String result = commandExecutor.execute(commandContext);
            if (StringUtils.isEquals(QosConstants.CLOSE, result)) {
                ctx.writeAndFlush(getByeLabel()).addListener(ChannelFutureListener.CLOSE);
            } else {
                ctx.writeAndFlush(result + QosConstants.BR_STR + QosProcessHandler.PROMPT);
            }
        } catch (NoSuchCommandException ex) {
            ctx.writeAndFlush(msg + " :no such command");
            ctx.writeAndFlush(QosConstants.BR_STR + QosProcessHandler.PROMPT);
            log.error("can not found command " + commandContext, ex);
        } catch (Exception ex) {
            ctx.writeAndFlush(msg + " :fail to execute commandContext by " + ex.getMessage());
            ctx.writeAndFlush(QosConstants.BR_STR + QosProcessHandler.PROMPT);
            log.error("execute commandContext got exception " + commandContext, ex);
        }
    }
}
Also used : CommandContext(org.apache.dubbo.qos.command.CommandContext) NoSuchCommandException(org.apache.dubbo.qos.command.NoSuchCommandException) NoSuchCommandException(org.apache.dubbo.qos.command.NoSuchCommandException)

Example 4 with CommandContext

use of org.apache.dubbo.qos.command.CommandContext in project dubbo by alibaba.

the class TelnetCommandDecoder method decode.

public static final CommandContext decode(String str) {
    CommandContext commandContext = null;
    if (!StringUtils.isBlank(str)) {
        String[] array = str.split("(?<![\\\\]) ");
        if (array.length > 0) {
            String name = array[0];
            String[] targetArgs = new String[array.length - 1];
            System.arraycopy(array, 1, targetArgs, 0, array.length - 1);
            commandContext = CommandContextFactory.newInstance(name, targetArgs, false);
            commandContext.setOriginRequest(str);
        }
    }
    return commandContext;
}
Also used : CommandContext(org.apache.dubbo.qos.command.CommandContext)

Example 5 with CommandContext

use of org.apache.dubbo.qos.command.CommandContext in project dubbo by alibaba.

the class TelnetCommandDecoderTest method testDecode.

@Test
public void testDecode() throws Exception {
    CommandContext context = TelnetCommandDecoder.decode("test a b");
    assertThat(context.getCommandName(), equalTo("test"));
    assertThat(context.isHttp(), is(false));
    assertThat(context.getArgs(), arrayContaining("a", "b"));
}
Also used : CommandContext(org.apache.dubbo.qos.command.CommandContext) Test(org.junit.jupiter.api.Test)

Aggregations

CommandContext (org.apache.dubbo.qos.command.CommandContext)7 Test (org.junit.jupiter.api.Test)3 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 NoSuchCommandException (org.apache.dubbo.qos.command.NoSuchCommandException)2 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)1 Attribute (io.netty.handler.codec.http.multipart.Attribute)1 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)1 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1