[OLINGO-328] improve multi valued headers of ODataRequest

This commit is contained in:
Stephan Klevenz 2014-06-26 08:53:18 +02:00
parent 97222fad31
commit af3f998b7f
2 changed files with 45 additions and 11 deletions

View File

@ -19,6 +19,7 @@
package org.apache.olingo.server.api; package org.apache.olingo.server.api;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -44,14 +45,26 @@ public class ODataRequest {
} }
/** /**
* Add header to request where name handled as case insensitive key. * Add header to request where name handled as case insensitive key. If a header already exists then the list of
* values will just be extended.
* @param name case insensitive header name * @param name case insensitive header name
* @param values * @param values
*/ */
public void addHeader(String name, List<String> values) { public void addHeader(String name, List<String> values) {
headers.put(name.toUpperCase(), values); String key = name.toUpperCase();
if (headers.containsKey(key)) {
List<String> oldValues = headers.get(key);
List<String> newValues = new ArrayList<String>();
newValues.addAll(oldValues);
newValues.addAll(values);
headers.put(name.toUpperCase(), newValues);
} else {
headers.put(name.toUpperCase(), values);
}
} }
/** /**
* Returns header value for name where name is a case insensitive key. * Returns header value for name where name is a case insensitive key.
* @return the header value or null if not found * @return the header value or null if not found
@ -59,7 +72,7 @@ public class ODataRequest {
public List<String> getHeader(String name) { public List<String> getHeader(String name) {
return headers.get(name.toUpperCase()); return headers.get(name.toUpperCase());
} }
public InputStream getBody() { public InputStream getBody() {
return body; return body;
} }
@ -94,7 +107,7 @@ public class ODataRequest {
public void setRawODataPath(String rawODataPath) { public void setRawODataPath(String rawODataPath) {
this.rawODataPath = rawODataPath; this.rawODataPath = rawODataPath;
} }
public void setRawBaseUri(String rawBaseUri) { public void setRawBaseUri(String rawBaseUri) {

View File

@ -25,21 +25,42 @@ import java.util.Arrays;
import org.junit.Test; import org.junit.Test;
public class ODataRequestTest { public class ODataRequestTest {
@Test @Test
public void testHeader() { public void testHeader() {
ODataRequest r = new ODataRequest(); ODataRequest r = new ODataRequest();
r.addHeader("aa", Arrays.asList("cc")); r.addHeader("aa", Arrays.asList("cc"));
assertEquals("cc", r.getHeader("aa").get(0)); assertEquals("cc", r.getHeader("aa").get(0));
assertEquals("cc", r.getHeader("aA").get(0)); assertEquals("cc", r.getHeader("aA").get(0));
assertEquals("cc", r.getHeader("AA").get(0)); assertEquals("cc", r.getHeader("AA").get(0));
}
@Test
public void testHeader2() {
ODataRequest r = new ODataRequest();
r.addHeader("AA", Arrays.asList("dd")); r.addHeader("AA", Arrays.asList("dd"));
assertEquals("dd", r.getHeader("aa").get(0)); assertEquals("dd", r.getHeader("aa").get(0));
assertEquals("dd", r.getHeader("aA").get(0)); assertEquals("dd", r.getHeader("aA").get(0));
assertEquals("dd", r.getHeader("AA").get(0)); assertEquals("dd", r.getHeader("AA").get(0));
}
@Test
public void testMultiValueHeader() {
ODataRequest r = new ODataRequest();
r.addHeader("aa", Arrays.asList("a", "b"));
assertEquals("a", r.getHeader("aa").get(0));
assertEquals("b", r.getHeader("aA").get(1));
r.addHeader("Aa", Arrays.asList("c"));
assertEquals("a", r.getHeader("aa").get(0));
assertEquals("b", r.getHeader("aA").get(1));
assertEquals("c", r.getHeader("aA").get(2));
} }
} }