Abnaxos / meldioc

A prototype of an IoC (Inversion of Control) library for Java
MIT License
5 stars 1 forks source link

Allow setting HTTP response headers #77

Open Abnaxos opened 4 years ago

Abnaxos commented 4 years ago

Add a way to set the HTTP status without throwing an exception. That's probably best done with a separate hardcoded interface that has to be implemented by "enriched" responses:

interface EnrichedHttpResponse<T> {
  int statusCode();
  Option<String> reasonPhrase();
  Seq<Tuple2<String, String>> headers();
  T entity();
}

An easy to use fluent default implementation should be provided.

Also add ways to enrich the response to the routing DSL for simple cases:

post().status(201, "Created").header("X-My-Header", "Foo").apply(...);

Consider adding a separate step to convert a response entity to an enriched response to the routing DSL.

post().enrich(entity -> new EnrichedHttpResponse<>(entity)).apply(() -> myEntity());

NOTE: Setting the status is supported now, add headers

Abnaxos commented 4 years ago

It may be better to add an action variant that takes some HTTP response manipulator as argument. The reason is type-safety. An additional interface makes it hard to keep the current level of type-safety up in the routing DSL.