Skip to content

Commit d1e3e11

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

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
@@ -51,6 +51,7 @@
5151
import java.util.Collections;
5252
import java.util.Iterator;
5353
import java.util.List;
54+
import java.util.Locale;
5455
import java.util.Map;
5556
import java.util.concurrent.atomic.AtomicReference;
5657

@@ -222,6 +223,43 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re
222223
}
223224
}
224225

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

227265
}

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)