Search in sources :

Example 56 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class FormEncodingProviderTest method testWriteMultipleValues.

@Test
public void testWriteMultipleValues() throws Exception {
    MultivaluedMap<String, String> mvMap = new MetadataMap<>();
    mvMap.add("a", "a1");
    mvMap.add("a", "a2");
    FormEncodingProvider<MultivaluedMap<?, ?>> ferp = new FormEncodingProvider<>();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ferp.writeTo(mvMap, MultivaluedMap.class, MultivaluedMap.class, new Annotation[0], MediaType.APPLICATION_FORM_URLENCODED_TYPE, new MetadataMap<String, Object>(), bos);
    String result = bos.toString();
    assertEquals("Wrong value", "a=a1&a=a2", result);
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 57 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class FormEncodingProviderTest method testReadFromMultiples.

@SuppressWarnings("unchecked")
@Test
public void testReadFromMultiples() throws Exception {
    InputStream is = getClass().getResourceAsStream("multiValPostBody.txt");
    @SuppressWarnings("rawtypes") FormEncodingProvider<MultivaluedMap> ferp = new FormEncodingProvider<>();
    MultivaluedMap<String, String> mvMap = ferp.readFrom(MultivaluedMap.class, null, new Annotation[] {}, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, is);
    List<String> vals = mvMap.get("foo");
    assertEquals("Wrong size for foo params", 2, vals.size());
    assertEquals("Wrong size for foo params", 1, mvMap.get("boo").size());
    assertEquals("Wrong entry for foo 0", "bar", vals.get(0));
    assertEquals("Wrong entry for foo 1", "bar2", vals.get(1));
    assertEquals("Wrong entry for boo", "far", mvMap.getFirst("boo"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 58 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class FormEncodingProviderTest method testReadFromISO.

@SuppressWarnings("unchecked")
@Test
public void testReadFromISO() throws Exception {
    String eWithAcute = "\u00E9";
    String helloStringUTF16 = "name=F" + eWithAcute + "lix";
    byte[] iso88591bytes = helloStringUTF16.getBytes("ISO-8859-1");
    String helloStringISO88591 = new String(iso88591bytes, "ISO-8859-1");
    @SuppressWarnings("rawtypes") FormEncodingProvider<MultivaluedMap> ferp = new FormEncodingProvider<>();
    MultivaluedMap<String, String> mvMap = ferp.readFrom(MultivaluedMap.class, null, new Annotation[] {}, MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED + ";charset=ISO-8859-1"), null, new ByteArrayInputStream(iso88591bytes));
    String value = mvMap.getFirst("name");
    assertEquals(helloStringISO88591, "name=" + value);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 59 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class FormEncodingProviderTest method testWrite.

@Test
public void testWrite() throws Exception {
    MultivaluedMap<String, String> mvMap = new MetadataMap<>();
    mvMap.add("a", "a1");
    mvMap.add("b", "b1");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    FormEncodingProvider<MultivaluedMap<?, ?>> ferp = new FormEncodingProvider<>();
    ferp.writeTo(mvMap, MultivaluedMap.class, MultivaluedMap.class, new Annotation[0], MediaType.APPLICATION_FORM_URLENCODED_TYPE, new MetadataMap<String, Object>(), bos);
    String result = bos.toString();
    assertEquals("Wrong value", "a=a1&b=b1", result);
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 60 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class FormEncodingProviderTest method testEncoded.

@SuppressWarnings("unchecked")
@Test
public void testEncoded() throws Exception {
    String values = "foo=1+2&bar=1+3";
    @SuppressWarnings("rawtypes") FormEncodingProvider<MultivaluedMap> ferp = new FormEncodingProvider<>();
    MultivaluedMap<String, String> mvMap = ferp.readFrom(MultivaluedMap.class, null, new Annotation[] { CustomMap.class.getAnnotations()[0] }, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, new ByteArrayInputStream(values.getBytes()));
    assertEquals("Wrong entry for foo", "1+2", mvMap.getFirst("foo"));
    assertEquals("Wrong entry for boo", "1+3", mvMap.getFirst("bar"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Aggregations

MultivaluedMap (javax.ws.rs.core.MultivaluedMap)135 Map (java.util.Map)67 List (java.util.List)51 HashMap (java.util.HashMap)45 MediaType (javax.ws.rs.core.MediaType)35 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)28 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)27 ArrayList (java.util.ArrayList)24 IOException (java.io.IOException)23 Test (org.junit.Test)21 WebApplicationException (javax.ws.rs.WebApplicationException)19 LinkedHashMap (java.util.LinkedHashMap)18 Type (java.lang.reflect.Type)16 Response (javax.ws.rs.core.Response)16 InputStream (java.io.InputStream)14 OutputStream (java.io.OutputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)13 Method (java.lang.reflect.Method)11 Optional (java.util.Optional)11