Skip to content

Commit 39ffced

Browse files
jansupolsenivam
authored andcommitted
AbortResponse headers case insensitive header names
Signed-off-by: jansupol <[email protected]>
1 parent ba65258 commit 39ffced

File tree

2 files changed

+108
-0
lines changed
  • core-client/src/test/java/org/glassfish/jersey/client
  • core-common/src/main/java/org/glassfish/jersey/internal/util/collection

2 files changed

+108
-0
lines changed

core-client/src/test/java/org/glassfish/jersey/client/AbortTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.util.Collections;
5454
import java.util.Iterator;
5555
import java.util.List;
56+
import java.util.Locale;
5657
import java.util.Map;
5758
import java.util.concurrent.CompletionStage;
5859
import java.util.concurrent.atomic.AtomicReference;
@@ -225,6 +226,43 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re
225226
}
226227
}
227228

229+
@Test
230+
public void testResponseContextCaseInsensitiveKeys() {
231+
try (Response response = ClientBuilder.newClient()
232+
.register(new ClientRequestFilter() {
233+
@Override
234+
public void filter(ClientRequestContext requestContext) throws IOException {
235+
requestContext.abortWith(Response.ok()
236+
.header("header1", "value")
237+
.header("header1", "value1 , value2")
238+
.header("header1", "Value3,white space ")
239+
.header("header2", "Value4;;Value5")
240+
.build());
241+
}
242+
})
243+
.register(new ClientResponseFilter() {
244+
@Override
245+
public void filter(ClientRequestContext requestContext, ClientResponseContext context) {
246+
Assertions.assertTrue(context.getHeaderString("header1").contains("value"));
247+
Assertions.assertTrue(context.getHeaderString("HEADER1").contains("value2"));
248+
//White space in value not trimmed
249+
Assertions.assertFalse(context.getHeaderString("header1").contains("whitespace"));
250+
//Multiple character separator
251+
Assertions.assertTrue(context.getHeaderString("header2").contains("Value5"));
252+
253+
Assertions.assertTrue(context.getHeaders().containsKey("HEADer1"));
254+
Assertions.assertFalse(context.getHeaders().get("HEADer1").isEmpty());
255+
Assertions.assertFalse(context.getHeaders().remove("HeAdEr1").isEmpty());
256+
Assertions.assertFalse(context.getHeaders().containsKey("header1"));
257+
}
258+
})
259+
.target("http://localhost:8080")
260+
.request()
261+
.get()) {
262+
Assertions.assertEquals(200, response.getStatus());
263+
}
264+
}
265+
228266
private static class StringHeader extends AtomicReference<String> {
229267

230268
}

core-common/src/main/java/org/glassfish/jersey/internal/util/collection/Views.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,76 @@ public List<String> put(String key, List<String> value) {
230230
final List<Object> old = originalMap.put(key, (List<Object>) (List<?>) value);
231231
return valuesTransformer.apply(old);
232232
}
233+
234+
@Override
235+
public boolean containsKey(Object key) {
236+
Iterator<Entry<String, List<String>>> i = entrySet().iterator();
237+
if (key == null) {
238+
while (i.hasNext()) {
239+
Entry<String, List<String>> e = i.next();
240+
if (e.getKey() == null) {
241+
return true;
242+
}
243+
}
244+
} else {
245+
while (i.hasNext()) {
246+
Entry<String, List<String>> e = i.next();
247+
if (((String) key).equalsIgnoreCase(e.getKey())) {
248+
return true;
249+
}
250+
}
251+
}
252+
return false;
253+
}
254+
255+
@Override
256+
public List<String> get(Object key) {
257+
Iterator<Entry<String, List<String>>> i = entrySet().iterator();
258+
if (key == null) {
259+
while (i.hasNext()) {
260+
Entry<String, List<String>> e = i.next();
261+
if (e.getKey() == null) {
262+
return e.getValue();
263+
}
264+
}
265+
} else {
266+
while (i.hasNext()) {
267+
Entry<String, List<String>> e = i.next();
268+
if (((String) key).equalsIgnoreCase(e.getKey())) {
269+
return e.getValue();
270+
}
271+
}
272+
}
273+
return null;
274+
}
275+
276+
@Override
277+
public List<String> remove(Object key) {
278+
Iterator<Entry<String, List<String>>> i = entrySet().iterator();
279+
Entry<String, List<String>> correctEntry = null;
280+
if (key == null) {
281+
while (correctEntry == null && i.hasNext()) {
282+
Entry<String, List<String>> e = i.next();
283+
if (e.getKey() == null) {
284+
correctEntry = e;
285+
}
286+
}
287+
} else {
288+
while (correctEntry == null && i.hasNext()) {
289+
Entry<String, List<String>> e = i.next();
290+
if (((String) key).equalsIgnoreCase(e.getKey())) {
291+
correctEntry = e;
292+
}
293+
}
294+
}
295+
296+
List<String> oldValue = null;
297+
if (correctEntry != null) {
298+
oldValue = correctEntry.getValue();
299+
i.remove();
300+
}
301+
return oldValue;
302+
}
233303
}
234304

235305
/**

0 commit comments

Comments
 (0)