Exemplo n.º 1
0
  public static RestResponse buildTextPlainResponse(
      Table table, RestRequest request, RestChannel channel) {
    boolean verbose = request.paramAsBoolean("v", false);

    List<DisplayHeader> headers = buildDisplayHeaders(table, request);
    int[] width = buildWidths(table, request, verbose, headers);

    StringBuilder out = new StringBuilder();
    if (verbose) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        pad(
            new Table.Cell(header.display, table.findHeaderByName(header.name)),
            width[col],
            request,
            out);
        out.append(" ");
      }
      out.append("\n");
    }

    for (int row = 0; row < table.getRows().size(); row++) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        pad(table.getAsMap().get(header.name).get(row), width[col], request, out);
        out.append(" ");
      }
      out.append("\n");
    }

    return new StringRestResponse(RestStatus.OK, out.toString());
  }
Exemplo n.º 2
0
  public static RestResponse buildTextPlainResponse(
      Table table, RestRequest request, RestChannel channel) {
    boolean verbose = request.paramAsBoolean("v", true);
    int[] width = buildWidths(table, request, verbose);
    Set<String> displayHeaders = buildDisplayHeaders(table, request);

    StringBuilder out = new StringBuilder();
    if (verbose) {
      // print the headers
      for (int i = 0; i < width.length; i++) {
        String headerName = table.getHeaders().get(i).value.toString();
        if (displayHeaders.contains(headerName)) {
          pad(table.getHeaders().get(i), width[i], request, out);
          out.append(" ");
        }
      }
      out.append("\n");
    }
    for (List<Table.Cell> row : table.getRows()) {
      for (int i = 0; i < width.length; i++) {
        String headerName = table.getHeaders().get(i).value.toString();
        if (displayHeaders.contains(headerName)) {
          pad(row.get(i), width[i], request, out);
          out.append(" ");
        }
      }
      out.append("\n");
    }

    return new StringRestResponse(RestStatus.OK, out.toString());
  }
Exemplo n.º 3
0
  @Test
  public void testSimple() {
    Table table = this.getTableWithHeaders();
    table.startRow();
    table.addCell("foo1");
    table.addCell("bar1");
    table.endRow();
    table.startRow();
    table.addCell("foo2");
    table.addCell("bar2");
    table.endRow();

    // Check headers
    List<Table.Cell> headers = table.getHeaders();
    assertEquals(2, headers.size());
    assertEquals("foo", headers.get(0).value.toString());
    assertEquals(2, headers.get(0).attr.size());
    assertEquals("f", headers.get(0).attr.get("alias"));
    assertEquals("foo", headers.get(0).attr.get("desc"));
    assertEquals("bar", headers.get(1).value.toString());
    assertEquals(2, headers.get(1).attr.size());
    assertEquals("b", headers.get(1).attr.get("alias"));
    assertEquals("bar", headers.get(1).attr.get("desc"));

    // Check rows
    List<List<Table.Cell>> rows = table.getRows();
    assertEquals(2, rows.size());
    List<Table.Cell> row = rows.get(0);
    assertEquals("foo1", row.get(0).value.toString());
    assertEquals("bar1", row.get(1).value.toString());
    row = rows.get(1);
    assertEquals("foo2", row.get(0).value.toString());
    assertEquals("bar2", row.get(1).value.toString());

    // Check getAsMap
    Map<String, List<Table.Cell>> map = table.getAsMap();
    assertEquals(2, map.size());
    row = map.get("foo");
    assertEquals("foo1", row.get(0).value.toString());
    assertEquals("foo2", row.get(1).value.toString());
    row = map.get("bar");
    assertEquals("bar1", row.get(0).value.toString());
    assertEquals("bar2", row.get(1).value.toString());

    // Check getHeaderMap
    Map<String, Table.Cell> headerMap = table.getHeaderMap();
    assertEquals(2, headerMap.size());
    Table.Cell cell = headerMap.get("foo");
    assertEquals("foo", cell.value.toString());
    cell = headerMap.get("bar");
    assertEquals("bar", cell.value.toString());

    // Check findHeaderByName
    cell = table.findHeaderByName("foo");
    assertEquals("foo", cell.value.toString());
    cell = table.findHeaderByName("missing");
    assertNull(cell);
  }
Exemplo n.º 4
0
  public static RestResponse buildTextPlainResponse(
      Table table, RestRequest request, RestChannel channel) {
    boolean verbose = request.paramAsBoolean("v", false);
    boolean help = request.paramAsBoolean("h", false);
    if (help) {
      int[] width = buildHelpWidths(table, request, verbose);
      StringBuilder out = new StringBuilder();
      for (Table.Cell cell : table.getHeaders()) {
        // need to do left-align always, so create new cells
        pad(new Table.Cell(cell.value), width[0], request, out);
        out.append(" ");
        pad(
            new Table.Cell(cell.attr.containsKey("desc") ? cell.attr.get("desc") : "not available"),
            width[1],
            request,
            out);
        out.append("\n");
      }
      return new StringRestResponse(RestStatus.OK, out.toString());
    }

    int[] width = buildWidths(table, request, verbose);
    Set<String> displayHeaders = buildDisplayHeaders(table, request);

    StringBuilder out = new StringBuilder();
    if (verbose) {
      // print the headers
      for (int i = 0; i < width.length; i++) {
        String headerName = table.getHeaders().get(i).value.toString();
        if (displayHeaders.contains(headerName)) {
          pad(table.getHeaders().get(i), width[i], request, out);
          out.append(" ");
        }
      }
      out.append("\n");
    }
    for (List<Table.Cell> row : table.getRows()) {
      for (int i = 0; i < width.length; i++) {
        String headerName = table.getHeaders().get(i).value.toString();
        if (displayHeaders.contains(headerName)) {
          pad(row.get(i), width[i], request, out);
          out.append(" ");
        }
      }
      out.append("\n");
    }

    return new StringRestResponse(RestStatus.OK, out.toString());
  }
Exemplo n.º 5
0
  public static RestResponse buildXContentBuilder(
      Table table, RestRequest request, RestChannel channel) throws Exception {
    XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
    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 XContentRestResponse(request, RestStatus.OK, builder);
  }
Exemplo n.º 6
0
  public static RestResponse buildTextPlainResponse(Table table, RestChannel channel)
      throws IOException {
    RestRequest request = channel.request();
    boolean verbose = request.paramAsBoolean("v", false);

    List<DisplayHeader> headers = buildDisplayHeaders(table, request);
    int[] width = buildWidths(table, request, verbose, headers);

    BytesStreamOutput bytesOut = channel.bytesOutput();
    UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOut);
    int lastHeader = headers.size() - 1;
    if (verbose) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        boolean isLastColumn = col == lastHeader;
        pad(
            new Table.Cell(header.display, table.findHeaderByName(header.name)),
            width[col],
            request,
            out,
            isLastColumn);
        if (!isLastColumn) {
          out.append(" ");
        }
      }
      out.append("\n");
    }
    for (int row = 0; row < table.getRows().size(); row++) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        boolean isLastColumn = col == lastHeader;
        pad(table.getAsMap().get(header.name).get(row), width[col], request, out, isLastColumn);
        if (!isLastColumn) {
          out.append(" ");
        }
      }
      out.append("\n");
    }
    out.close();
    return new BytesRestResponse(
        RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOut.bytes());
  }
Exemplo n.º 7
0
  public static RestResponse buildXContentBuilder(
      Table table, RestRequest request, RestChannel channel) throws Exception {
    XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
    Set<String> displayHeaders = buildDisplayHeaders(table, request);

    List<Table.Cell> headers = table.getHeaders();
    builder.startArray();
    for (List<Table.Cell> row : table.getRows()) {
      builder.startObject();
      for (int i = 0; i < headers.size(); i++) {
        String headerName = headers.get(i).value.toString();
        if (displayHeaders.contains(headerName)) {
          builder.field(headerName, renderValue(request, row.get(i).value));
        }
      }
      builder.endObject();
    }
    builder.endArray();
    return new XContentRestResponse(request, RestStatus.OK, builder);
  }
Exemplo n.º 8
0
  private static int[] buildWidths(Table table, RestRequest request, boolean verbose) {
    int[] width = new int[table.getHeaders().size()];

    if (verbose) {
      for (int col = 0; col < width.length; col++) {
        String v = renderValue(request, table.getHeaders().get(col).value);
        int vWidth = v == null ? 0 : v.length();
        if (width[col] < vWidth) {
          width[col] = vWidth;
        }
      }
    }

    for (List<Table.Cell> row : table.getRows()) {
      for (int col = 0; col < width.length; col++) {
        String v = renderValue(request, row.get(col).value);
        int vWidth = v == null ? 0 : v.length();
        if (width[col] < vWidth) {
          width[col] = vWidth;
        }
      }
    }
    return width;
  }