Class ContentViews

java.lang.Object
org.microproxy.contentviews.ContentViews

public final class ContentViews extends Object
A set of ContentViews, and the choice among them. In auto mode the view with the highest ContentView.priority(byte[], org.microproxy.contentviews.ContentView.Metadata) renders a body; if it cannot, the next one tries, and when none can, there is no rendering (callers show the body as they would without views).

 ContentViews views = ContentViews.defaults().withSchema(ProtoSchema.load(Path.of("api.desc")));
 views.render(body, ContentView.Metadata.forResponse(request, response))
         .ifPresent(r -> System.out.println(r.view() + ":\n" + r.text()));
 

The default views: protobuf, grpc, json, graphql, msgpack, urlencoded, multipart, query, socketio, dns (application/dns-message), and hex (only when named). Instances are immutable and safe to share; with(org.microproxy.contentviews.ContentView) and withSchema(org.microproxy.contentviews.ProtoSchema) return new ones.

  • Field Details

  • Method Details

    • defaults

      public static ContentViews defaults()
      Returns the default views, decoding protobuf without a schema.
      Returns:
      the default views, decoding protobuf without a schema
    • with

      public ContentViews with(ContentView view)
      These views with view added, replacing a view of the same name.
      Parameters:
      view - the view
      Returns:
      the new set
    • withSchema

      public ContentViews withSchema(ProtoSchema schema)
      These views with the protobuf and gRPC views decoding with schema.
      Parameters:
      schema - the schema
      Returns:
      the new set
    • schema

      public ProtoSchema schema()
      Returns the schema the protobuf and gRPC views decode with.
      Returns:
      the schema the protobuf and gRPC views decode with
    • names

      public List<String> names()
      Returns "auto" and the views' names, sorted.
      Returns:
      "auto" and the views' names, sorted
    • get

      public Optional<ContentView> get(String name)
      The view named name.
      Parameters:
      name - a view's name (any case)
      Returns:
      the view, or empty if there is none
    • select

      public Optional<ContentView> select(byte[] data, ContentView.Metadata metadata)
      The view auto mode would try first.
      Parameters:
      data - the body, decoded
      metadata - what is known about the message
      Returns:
      the view with the highest positive priority, or empty if none applies
    • render

      public Optional<ContentViews.Rendered> render(byte[] data, ContentView.Metadata metadata)
      Renders a body with the best view that can.
      Parameters:
      data - the body, decoded
      metadata - what is known about the message
      Returns:
      the rendering, or empty if no view applies or none could render the body
    • render

      public Optional<ContentViews.Rendered> render(String view, byte[] data, ContentView.Metadata metadata)
      Renders a body with the view named view, or the best one for "auto".
      Parameters:
      view - a view's name, or "auto"
      data - the body, decoded
      metadata - what is known about the message
      Returns:
      the rendering, or empty if the view could not render the body
      Throws:
      IllegalArgumentException - if there is no view named view
    • describeHeader

      public String describeHeader(String name, String value)
      What a header or trailer field means, for the fields gRPC defines: the name of a grpc-status code, the text of a percent-encoded grpc-message, and the message in a binary (-bin) field, grpc-status-details-bin as a google.rpc.Status.
      Parameters:
      name - the field's name
      value - the field's value
      Returns:
      a line (or YAML lines) explaining the value, or null for other fields
    • toString

      public String toString()
      Overrides:
      toString in class Object