Search in sources :

Example 1 with CommandExecutor

use of org.apache.zookeeper.server.command.CommandExecutor in project zookeeper by apache.

the class NettyServerCnxn method checkFourLetterWord.

/**
 * Return if four letter word found and responded to, otw false *
 */
private boolean checkFourLetterWord(final Channel channel, ByteBuf message, final int len) {
    // for cmds. They are all 4-bytes which fits inside of an int
    if (!FourLetterCommands.isKnown(len)) {
        return false;
    }
    String cmd = FourLetterCommands.getCommandString(len);
    // Stops automatic reads of incoming data on this channel. We don't
    // expect any more traffic from the client when processing a 4LW
    // so this shouldn't break anything.
    channel.config().setAutoRead(false);
    packetReceived(4);
    final PrintWriter pwriter = new PrintWriter(new BufferedWriter(new SendBufferWriter()));
    // ZOOKEEPER-2693: don't execute 4lw if it's not enabled.
    if (!FourLetterCommands.isEnabled(cmd)) {
        LOG.debug("Command {} is not executed because it is not in the whitelist.", cmd);
        NopCommand nopCmd = new NopCommand(pwriter, this, cmd + " is not executed because it is not in the whitelist.");
        nopCmd.start();
        return true;
    }
    LOG.info("Processing {} command from {}", cmd, channel.remoteAddress());
    if (len == FourLetterCommands.setTraceMaskCmd) {
        ByteBuffer mask = ByteBuffer.allocate(8);
        message.readBytes(mask);
        mask.flip();
        long traceMask = mask.getLong();
        ZooTrace.setTextTraceLevel(traceMask);
        SetTraceMaskCommand setMask = new SetTraceMaskCommand(pwriter, this, traceMask);
        setMask.start();
        return true;
    } else {
        CommandExecutor commandExecutor = new CommandExecutor();
        return commandExecutor.execute(this, pwriter, len, zkServer, factory);
    }
}
Also used : NopCommand(org.apache.zookeeper.server.command.NopCommand) SetTraceMaskCommand(org.apache.zookeeper.server.command.SetTraceMaskCommand) CommandExecutor(org.apache.zookeeper.server.command.CommandExecutor) ByteBuffer(java.nio.ByteBuffer) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 2 with CommandExecutor

use of org.apache.zookeeper.server.command.CommandExecutor in project zookeeper by apache.

the class NIOServerCnxn method checkFourLetterWord.

/**
 * Return if four letter word found and responded to, otw false *
 */
private boolean checkFourLetterWord(final SelectionKey k, final int len) throws IOException {
    // for cmds. They are all 4-bytes which fits inside of an int
    if (!FourLetterCommands.isKnown(len)) {
        return false;
    }
    String cmd = FourLetterCommands.getCommandString(len);
    packetReceived(4);
    /**
     * cancel the selection key to remove the socket handling
     * from selector. This is to prevent netcat problem wherein
     * netcat immediately closes the sending side after sending the
     * commands and still keeps the receiving channel open.
     * The idea is to remove the selectionkey from the selector
     * so that the selector does not notice the closed read on the
     * socket channel and keep the socket alive to write the data to
     * and makes sure to close the socket after its done writing the data
     */
    if (k != null) {
        try {
            k.cancel();
        } catch (Exception e) {
            LOG.error("Error cancelling command selection key", e);
        }
    }
    final PrintWriter pwriter = new PrintWriter(new BufferedWriter(new SendBufferWriter()));
    // ZOOKEEPER-2693: don't execute 4lw if it's not enabled.
    if (!FourLetterCommands.isEnabled(cmd)) {
        LOG.debug("Command {} is not executed because it is not in the whitelist.", cmd);
        NopCommand nopCmd = new NopCommand(pwriter, this, cmd + " is not executed because it is not in the whitelist.");
        nopCmd.start();
        return true;
    }
    LOG.info("Processing {} command from {}", cmd, sock.socket().getRemoteSocketAddress());
    if (len == FourLetterCommands.setTraceMaskCmd) {
        incomingBuffer = ByteBuffer.allocate(8);
        int rc = sock.read(incomingBuffer);
        if (rc < 0) {
            throw new IOException("Read error");
        }
        incomingBuffer.flip();
        long traceMask = incomingBuffer.getLong();
        ZooTrace.setTextTraceLevel(traceMask);
        SetTraceMaskCommand setMask = new SetTraceMaskCommand(pwriter, this, traceMask);
        setMask.start();
        return true;
    } else {
        CommandExecutor commandExecutor = new CommandExecutor();
        return commandExecutor.execute(this, pwriter, len, zkServer, factory);
    }
}
Also used : NopCommand(org.apache.zookeeper.server.command.NopCommand) SetTraceMaskCommand(org.apache.zookeeper.server.command.SetTraceMaskCommand) CommandExecutor(org.apache.zookeeper.server.command.CommandExecutor) IOException(java.io.IOException) CancelledKeyException(java.nio.channels.CancelledKeyException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 3 with CommandExecutor

use of org.apache.zookeeper.server.command.CommandExecutor in project zookeeper by apache.

the class NettyServerCnxn method checkFourLetterWord.

/**
 * Return if four letter word found and responded to, otw false *
 */
private boolean checkFourLetterWord(final Channel channel, ChannelBuffer message, final int len) throws IOException {
    // for cmds. They are all 4-bytes which fits inside of an int
    if (!FourLetterCommands.isKnown(len)) {
        return false;
    }
    String cmd = FourLetterCommands.getCommandString(len);
    channel.setInterestOps(0).awaitUninterruptibly();
    packetReceived();
    final PrintWriter pwriter = new PrintWriter(new BufferedWriter(new SendBufferWriter()));
    // ZOOKEEPER-2693: don't execute 4lw if it's not enabled.
    if (!FourLetterCommands.isEnabled(cmd)) {
        LOG.debug("Command {} is not executed because it is not in the whitelist.", cmd);
        NopCommand nopCmd = new NopCommand(pwriter, this, cmd + " is not executed because it is not in the whitelist.");
        nopCmd.start();
        return true;
    }
    LOG.info("Processing " + cmd + " command from " + channel.getRemoteAddress());
    if (len == FourLetterCommands.setTraceMaskCmd) {
        ByteBuffer mask = ByteBuffer.allocate(8);
        message.readBytes(mask);
        mask.flip();
        long traceMask = mask.getLong();
        ZooTrace.setTextTraceLevel(traceMask);
        SetTraceMaskCommand setMask = new SetTraceMaskCommand(pwriter, this, traceMask);
        setMask.start();
        return true;
    } else {
        CommandExecutor commandExecutor = new CommandExecutor();
        return commandExecutor.execute(this, pwriter, len, zkServer, factory);
    }
}
Also used : NopCommand(org.apache.zookeeper.server.command.NopCommand) SetTraceMaskCommand(org.apache.zookeeper.server.command.SetTraceMaskCommand) CommandExecutor(org.apache.zookeeper.server.command.CommandExecutor) ByteBuffer(java.nio.ByteBuffer) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

BufferedWriter (java.io.BufferedWriter)3 PrintWriter (java.io.PrintWriter)3 CommandExecutor (org.apache.zookeeper.server.command.CommandExecutor)3 NopCommand (org.apache.zookeeper.server.command.NopCommand)3 SetTraceMaskCommand (org.apache.zookeeper.server.command.SetTraceMaskCommand)3 ByteBuffer (java.nio.ByteBuffer)2 IOException (java.io.IOException)1 CancelledKeyException (java.nio.channels.CancelledKeyException)1