Skip to content

Commit 8857100

Browse files
authored
Adjust getRequestHeader method (#4893)
* Adjust getRequestHeader method (3.1 spec) Signed-off-by: Maxim Nesen <[email protected]>
1 parent 503e132 commit 8857100

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

core-client/src/main/java/org/glassfish/jersey/client/ClientRequest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,19 @@ ClientConfig getClientConfig() {
229229
return clientConfig;
230230
}
231231

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

237247
@Override

core-server/src/main/java/org/glassfish/jersey/server/ContainerRequest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -821,13 +821,13 @@ private static long roundDown(final long time) {
821821
}
822822

823823
/**
824-
* Get the values of a HTTP request header. The returned List is read-only.
825-
* This is a shortcut for {@code getRequestHeaders().get(name)}.
824+
* Get the values of an HTTP request header if the header exists on the current request. The returned value will be
825+
* a read-only List if the specified header exists or {@code null} if it does not. This is a shortcut for
826+
* {@code getRequestHeaders().get(name)}.
826827
*
827828
* @param name the header name, case insensitive.
828-
* @return a read-only list of header values.
829-
*
830-
* @throws IllegalStateException if called outside the scope of a request.
829+
* @return a read-only list of header values if the specified header exists, otherwise {@code null}.
830+
* @throws java.lang.IllegalStateException if called outside the scope of a request.
831831
*/
832832
@Override
833833
public List<String> getRequestHeader(final String name) {

docs/src/main/docbook/jaxrs-resources.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,11 @@ public String get(@Context HttpHeaders hh) {
480480
</example>
481481
</para>
482482

483+
<para>
484+
For quicker getting of values for a known header name there is a shortcut for <literal>hh.getRequestHeaders().get(name)</literal>
485+
which is <literal>hh.getRequestHeader(name)</literal>.
486+
</para>
487+
483488
<para>In general &jaxrs.core.Context; can be used to obtain contextual Java types related to the request or response.
484489
</para>
485490

docs/src/main/docbook/migration.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@
100100
<literal>true</literal>.
101101
</para>
102102
</listitem>
103+
<listitem>
104+
<para>
105+
Since Jersey 3.1.0+ the <literal>getRequestHeader(String name)</literal> method of the
106+
<literal>ClientRequest</literal> class returns NULL (instead of an empty List) in case if
107+
the specified header does not exist.
108+
</para>
109+
</listitem>
103110
</itemizedlist>
104111
</para>
105112
</section>

0 commit comments

Comments
 (0)