@Override
  public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    String[] urlParts =
        checkSyntax(
            iRequest.url,
            3,
            "Syntax error: cluster/<database>/<cluster-name>[/<limit>]<br>Limit is optional and is setted to 20 by default. Set expressely to 0 to have no limits.");

    iRequest.data.commandInfo = "Browse cluster";
    iRequest.data.commandDetail = urlParts[2];

    ODatabaseDocument db = null;

    try {
      db = getProfiledDatabaseInstance(iRequest);

      if (db.getClusterIdByName(urlParts[2]) > -1) {
        final int limit = urlParts.length > 3 ? Integer.parseInt(urlParts[3]) : 20;

        final List<OIdentifiable> response = new ArrayList<OIdentifiable>();
        for (ORecord rec : db.browseCluster(urlParts[2])) {
          if (limit > 0 && response.size() >= limit) break;

          response.add(rec);
        }

        iResponse.writeRecords(response);
      } else iResponse.send(OHttpUtils.STATUS_NOTFOUND_CODE, null, null, null, null);

    } finally {
      if (db != null) db.close();
    }
    return false;
  }
  @Override
  public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    final String[] urlParts =
        checkSyntax(iRequest.url, 3, "Syntax error: index/<database>/<index-name>/<key>/[<value>]");

    iRequest.data.commandInfo = "Index remove";

    ODatabaseDocumentTx db = null;

    try {
      db = getProfiledDatabaseInstance(iRequest);

      final OIndex<?> index = db.getMetadata().getIndexManager().getIndex(urlParts[2]);
      if (index == null)
        throw new IllegalArgumentException("Index name '" + urlParts[2] + "' not found");

      final boolean found;
      if (urlParts.length > 4) found = index.remove(urlParts[3], new ORecordId(urlParts[3]));
      else found = index.remove(urlParts[3]);

      if (found)
        iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
      else
        iResponse.send(
            OHttpUtils.STATUS_NOTFOUND_CODE,
            OHttpUtils.STATUS_NOTFOUND_DESCRIPTION,
            OHttpUtils.CONTENT_TEXT_PLAIN,
            null,
            null);
    } finally {
      if (db != null) db.close();
    }
    return false;
  }
 protected void sendAuthorizationRequest(
     final OHttpRequest iRequest, final OHttpResponse iResponse, final String iDatabaseName)
     throws IOException {
   // UNAUTHORIZED
   iRequest.sessionId = SESSIONID_UNAUTHORIZED;
   iResponse.send(
       OHttpUtils.STATUS_AUTH_CODE,
       OHttpUtils.STATUS_AUTH_DESCRIPTION,
       OHttpUtils.CONTENT_TEXT_PLAIN,
       "401 Unauthorized.",
       "WWW-Authenticate: Basic realm=\"OrientDB db-" + iDatabaseName + "\"",
       false);
 }
  @Override
  public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    final String[] args = checkSyntax(iRequest.url, 1, "Syntax error: connections[/<database>]");

    iRequest.data.commandInfo = "Server status";

    try {
      final StringWriter jsonBuffer = new StringWriter();
      final OJSONWriter json = new OJSONWriter(jsonBuffer);
      json.beginObject();

      final String databaseName = args.length > 1 && args[1].length() > 0 ? args[1] : null;

      writeConnections(json, databaseName);

      json.endObject();

      iResponse.send(
          OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, jsonBuffer.toString(), null);

    } finally {
    }
    return false;
  }
  @Override
  public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    checkSyntax(iRequest.url, 1, "Syntax error: server");

    iRequest.data.commandInfo = "Server status";

    try {
      StringWriter jsonBuffer = new StringWriter();
      OJSONWriter json = new OJSONWriter(jsonBuffer);

      json.beginObject();

      json.beginCollection(1, true, "connections");

      String lastCommandOn;
      String connectedOn;

      final List<OClientConnection> conns = OClientConnectionManager.instance().getConnections();
      for (OClientConnection c : conns) {
        final ONetworkProtocolData data = c.data;

        synchronized (dateTimeFormat) {
          lastCommandOn = dateTimeFormat.format(new Date(data.lastCommandReceived));
          connectedOn = dateTimeFormat.format(new Date(c.since));
        }

        json.beginObject(2);
        writeField(json, 2, "connectionId", c.id);
        writeField(
            json,
            2,
            "remoteAddress",
            c.protocol.getChannel() != null ? c.protocol.getChannel().toString() : "Disconnected");
        writeField(json, 2, "db", data.lastDatabase != null ? data.lastDatabase : "-");
        writeField(json, 2, "user", data.lastUser != null ? data.lastUser : "******");
        writeField(json, 2, "totalRequests", data.totalRequests);
        writeField(json, 2, "commandInfo", data.commandInfo);
        writeField(json, 2, "commandDetail", data.commandDetail);
        writeField(json, 2, "lastCommandOn", lastCommandOn);
        writeField(json, 2, "lastCommandInfo", data.lastCommandInfo);
        writeField(json, 2, "lastCommandDetail", data.lastCommandDetail);
        writeField(json, 2, "lastExecutionTime", data.lastCommandExecutionTime);
        writeField(json, 2, "totalWorkingTime", data.totalCommandExecutionTime);
        writeField(json, 2, "connectedOn", connectedOn);
        writeField(json, 2, "protocol", c.protocol.getType());
        writeField(json, 2, "clientId", data.clientId);

        final StringBuilder driver = new StringBuilder();
        if (data.driverName != null) {
          driver.append(data.driverName);
          driver.append(" v");
          driver.append(data.driverVersion);
          driver.append(" Protocol v");
          driver.append(data.protocolVersion);
        }

        writeField(json, 2, "driver", driver.toString());
        json.endObject(2);
      }
      json.endCollection(1, false);

      json.beginCollection(1, true, "dbs");
      Map<String, OResourcePool<String, ODatabaseDocumentTx>> dbPool =
          OSharedDocumentDatabase.getDatabasePools();
      for (Entry<String, OResourcePool<String, ODatabaseDocumentTx>> entry : dbPool.entrySet()) {
        for (ODatabaseDocumentTx db : entry.getValue().getResources()) {

          json.beginObject(2);
          writeField(json, 2, "db", db.getName());
          writeField(json, 2, "user", db.getUser() != null ? db.getUser().getName() : "-");
          writeField(json, 2, "status", db.isClosed() ? "closed" : "open");
          writeField(json, 2, "type", db.getType());
          writeField(json, 2, "storageType", db.getStorage().getType());
          json.endObject(2);
        }
      }
      json.endCollection(1, false);

      json.beginCollection(1, true, "storages");
      Collection<OStorage> storages = Orient.instance().getStorages();
      for (OStorage s : storages) {
        json.beginObject(2);
        writeField(json, 2, "name", s.getName());
        writeField(json, 2, "type", s.getClass().getSimpleName());
        writeField(
            json,
            2,
            "path",
            s instanceof OStorageLocalAbstract
                ? ((OStorageLocalAbstract) s).getStoragePath().replace('\\', '/')
                : "");
        writeField(json, 2, "activeUsers", s.getUsers());
        json.endObject(2);
      }
      json.endCollection(1, false);

      json.beginCollection(2, true, "properties");
      for (OServerEntryConfiguration entry : OServerMain.server().getConfiguration().properties) {
        json.beginObject(3, true, null);
        json.writeAttribute(4, false, "name", entry.name);
        json.writeAttribute(4, false, "value", entry.value);
        json.endObject(3, true);
      }
      json.endCollection(2, true);
      json.endObject();

      iResponse.send(
          OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, jsonBuffer.toString(), null);

    } finally {
    }
    return false;
  }