jakartaee / rest

Jakarta RESTful Web Services
Other
367 stars 121 forks source link

Supporting Streams API for bulk-processing of headers #482

Open glassfishrobot opened 10 years ago

glassfishrobot commented 10 years ago

The JAX-RS API 2.0 defines the Headers interface to access a list of all provided request headers. Typically this form of mass-provision is utilized by some kind of filtering and / or iteration algorithm, since obviously nobody really needs literally all headers (including even potentially unknown custom headers).

With the rise of Java 8 in the incarnation of both, the current Java SE 8 and the future Java EE 8 platforms, Streams API is the typical means to code bulk processing. Java SE 8 for example was overhauled to provide streams to process lots of mass-content like in the Collections API. The resulting code is often more concise and better readable, also supports potentially parallel execution by usage of parallel streams.

As Java SE 8 might be very popular at time of JAX-RS 2.1 publication, and as Java EE 8 is assumed to demand JAX-RS 2.1 and Java SE 8 as core technologies, it would be wise and beneficial to define additional APIs which allow to directly produce streams for header batch-processing.

The following serves as a proposal to discuss:

Wherever bulk access on headers is used in JAX-RS 2.0 (like the Headers interface), there should be an additional, similar method defined producing a stream of headers. To illustrate this, the following API...

MultivaluedMap<String, String> Headers.getRequestHeaders()

...could get extended by the following API...

Stream<Map.Entry<String, Stream<String>>> Headers.getRequestHeaders()

...to simplify processing using the Streams API.

While it is clear that the following code is possible without streams...

for  (final Map.Entry<String, String> h: Headers.getRequestHeaders().entrySet())
  if (h.getKey().startsWith("X-OTRS") {
    final List<String> v = h.getValue();
    if (v.contains("XYZ")
      System.out.println(v);
  }

...it is obvious that the following stream processing looks more concise to readers familiar with the Stream API...

Headers.stream().filter(h -> h.getKey().startsWith("X-OTRS")).map(h -> h.getValue()).filter(v -> v.contains("XYZ")).forEach(s -> System.out::println)

Environment

Java SE 8, Java EE 8

Affected Versions

[2.1]

glassfishrobot commented 6 years ago
glassfishrobot commented 10 years ago

@glassfishrobot Commented Reported by mkarg

glassfishrobot commented 7 years ago

@glassfishrobot Commented This issue was imported from java.net JIRA JAX_RS_SPEC-477