jakartaee / jakartaee/rest

Supporting Streams API for bulk-processing of headers

Open
#482 3 comments 0 reactions 1 assignee Claimed by @glassfishrobot View on GitHub
Dominant language
Java
Stars
400
Forks
143
PR merge metrics
No merged PRs in 30d

Description

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 Headers.getRequestHeaders()
```

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

```
Stream>> 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 h: Headers.getRequestHeaders().entrySet())
if (h.getKey().startsWith("X-OTRS") {
final List 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]

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.