@Override
  protected void handleRequest(
      final RestRequest request, final RestChannel channel, final Client client) throws Exception {
    BytesRestResponse response = null;
    final XContentBuilder builder = channel.newBuilder();
    try {
      builder.startObject();
      final User user = ((User) request.getFromContext("_shield_user"));
      if (user != null) {
        builder.field("principal", user.principal());
        builder.field("roles", user.roles());
      } else {
        builder.nullField("principal");
      }

      builder.field("remote_address", request.getFromContext("_rest_remote_address"));
      builder.endObject();
      response = new BytesRestResponse(RestStatus.OK, builder);
    } catch (final Exception e1) {
      builder.startObject();
      builder.field("error", e1.toString());
      builder.endObject();
      response = new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, builder);
    }

    channel.sendResponse(response);
  }
コード例 #2
0
  public static RestResponse buildXContentBuilder(Table table, RestChannel channel)
      throws Exception {
    RestRequest request = channel.request();
    XContentBuilder builder = channel.newBuilder();
    List<DisplayHeader> displayHeaders = buildDisplayHeaders(table, request);

    builder.startArray();
    for (int row = 0; row < table.getRows().size(); row++) {
      builder.startObject();
      for (DisplayHeader header : displayHeaders) {
        builder.field(
            header.display, renderValue(request, table.getAsMap().get(header.name).get(row).value));
      }
      builder.endObject();
    }
    builder.endArray();
    return new BytesRestResponse(RestStatus.OK, builder);
  }