class KeySet extends BaseCommand {

  @Override
  public void cmdExecute(final Message clientMessage, final ServerConnection serverConnection,
      final SecurityService securityService, long start) throws IOException, InterruptedException {
    Part regionNamePart = null;
    String regionName = null;
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    serverConnection.setAsTrue(REQUIRES_CHUNKED_RESPONSE);

    // Retrieve the region name from the message parts
    regionNamePart = clientMessage.getPart(0);
    regionName = regionNamePart.getCachedString();
    ChunkedMessage chunkedResponseMsg = serverConnection.getChunkedResponseMessage();
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
      logger.debug("{}: Received key set request ({} bytes) from {} for region {}",
          serverConnection.getName(), clientMessage.getPayloadLength(),
          serverConnection.getSocketString(), regionName);
    }

    // Process the key set request
    if (regionName == null) {
      String message = null;
      // if (regionName == null) (can only be null)
      {
        message = String.format("%s: The input region name for the key set request is null",
            serverConnection.getName());
        logger.warn("{}: The input region name for the key set request is null",
            serverConnection.getName());
      }
      writeKeySetErrorResponse(clientMessage, MessageType.KEY_SET_DATA_ERROR, message,
          serverConnection);
      serverConnection.setAsTrue(RESPONDED);
      return;
    }

    LocalRegion region = (LocalRegion) serverConnection.getCache().getRegion(regionName);
    if (region == null) {
      String reason = String.format("%s was not found during key set request",
          regionName);
      writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection);
      serverConnection.setAsTrue(RESPONDED);
      return;
    }

    if (isInTransaction() && region.getPartitionAttributes() != null) {
      // GEODE-5186: fail the the transaction if it is a retry after failover for keySet on
      // partitioned region
      if (clientMessage.isRetry()) {
        keySetWriteChunkedException(clientMessage,
            new TransactionException(
                "Failover on a set operation of a partitioned region is not allowed in a transaction."),
            serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
      }
    }

    try {
      securityService.authorize(Resource.DATA, Operation.READ, regionName);
    } catch (NotAuthorizedException ex) {
      keySetWriteChunkedException(clientMessage, ex, serverConnection);
      serverConnection.setAsTrue(RESPONDED);
      return;
    }

    KeySetOperationContext keySetContext = null;
    AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
    if (authzRequest != null) {
      try {
        keySetContext = authzRequest.keySetAuthorize(regionName);
      } catch (NotAuthorizedException ex) {
        writeChunkedException(clientMessage, ex, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
      }
    }
    // Update the statistics and write the reply
    // bserverStats.incLong(processDestroyTimeId,
    // DistributionStats.getStatTime() - start);
    // start = DistributionStats.getStatTime();

    // Send header
    chunkedResponseMsg.setMessageType(MessageType.RESPONSE);
    chunkedResponseMsg.setTransactionId(clientMessage.getTransactionId());
    chunkedResponseMsg.sendHeader();

    // Send chunk response
    try {
      fillAndSendKeySetResponseChunks(region, regionName, keySetContext, serverConnection);
      serverConnection.setAsTrue(RESPONDED);
    } catch (Exception e) {
      // If an interrupted exception is thrown , rethrow it
      checkForInterrupt(serverConnection, e);

      // Otherwise, write an exception message and continue
      writeChunkedException(clientMessage, e, serverConnection,
          serverConnection.getChunkedResponseMessage());
      serverConnection.setAsTrue(RESPONDED);
      return;
    }

    if (isDebugEnabled) {
      // logger.fine(getName() + ": Sent chunk (1 of 1) of register interest
      // response (" + chunkedResponseMsg.getBufferLength() + " bytes) for
      // region " + regionName + " key " + key);
      logger.debug("{}: Sent key set response for the region {}", serverConnection.getName(),
          regionName);
    }
    // bserverStats.incLong(writeDestroyResponseTimeId,
    // DistributionStats.getStatTime() - start);
    // bserverStats.incInt(destroyResponsesId, 1);

  }

}