Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust getRequestHeader method #4893

Merged
merged 2 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,19 @@ ClientConfig getClientConfig() {
return clientConfig;
}

/**
* Get the values of an HTTP request header if the header exists on the current request. The returned value will be
* a read-only List if the specified header exists or {@code null} if it does not. This is a shortcut for
* {@code getRequestHeaders().get(name)}.
*
* @param name the header name, case insensitive.
* @return a read-only list of header values if the specified header exists, otherwise {@code null}.
* @throws java.lang.IllegalStateException if called outside the scope of a request.
*/
@Override
public List<String> getRequestHeader(String name) {
return HeaderUtils.asStringList(getHeaders().get(name), clientConfig.getConfiguration());
final List<Object> values = getHeaders().get(name);
return values == null ? null : HeaderUtils.asStringList(values, clientConfig.getConfiguration());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,13 +821,13 @@ private static long roundDown(final long time) {
}

/**
* Get the values of a HTTP request header. The returned List is read-only.
* This is a shortcut for {@code getRequestHeaders().get(name)}.
* Get the values of an HTTP request header if the header exists on the current request. The returned value will be
* a read-only List if the specified header exists or {@code null} if it does not. This is a shortcut for
* {@code getRequestHeaders().get(name)}.
*
* @param name the header name, case insensitive.
* @return a read-only list of header values.
*
* @throws IllegalStateException if called outside the scope of a request.
* @return a read-only list of header values if the specified header exists, otherwise {@code null}.
* @throws java.lang.IllegalStateException if called outside the scope of a request.
*/
@Override
public List<String> getRequestHeader(final String name) {
Expand Down
5 changes: 5 additions & 0 deletions docs/src/main/docbook/jaxrs-resources.xml
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,11 @@ public String get(@Context HttpHeaders hh) {
</example>
</para>

<para>
For quicker getting of values for a known header name there is a shortcut for <literal>hh.getRequestHeaders().get(name)</literal>
which is <literal>hh.getRequestHeader(name)</literal>.
</para>

<para>In general &jaxrs.core.Context; can be used to obtain contextual Java types related to the request or response.
</para>

Expand Down
7 changes: 7 additions & 0 deletions docs/src/main/docbook/migration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
<literal>true</literal>.
</para>
</listitem>
<listitem>
<para>
Since Jersey 3.1.0+ the <literal>getRequestHeader(String name)</literal> method of the
<literal>ClientRequest</literal> class returns NULL (instead of an empty List) in case if
the specified header does not exist.
</para>
</listitem>
</itemizedlist>
</para>
</section>
Expand Down