Search in sources :

Example 16 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class JAXRSUtilsTest method testCookieParameters.

@Test
public void testCookieParameters() throws Exception {
    Class<?>[] argType = { String.class, Set.class, String.class, Set.class };
    Method m = Customer.class.getMethod("testCookieParam", argType);
    Message messageImpl = createMessage();
    MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
    headers.add("Cookie", "c1=c1Value");
    messageImpl.put(Message.PROTOCOL_HEADERS, headers);
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(params.size(), 4);
    assertEquals("c1Value", params.get(0));
    Set<Cookie> set1 = CastUtils.cast((Set<?>) params.get(1));
    assertEquals(1, set1.size());
    assertTrue(set1.contains(Cookie.valueOf("c1=c1Value")));
    assertEquals("c2Value", params.get(2));
    Set<Cookie> set2 = CastUtils.cast((Set<?>) params.get(3));
    assertTrue(set2.contains((Object) "c2Value"));
    assertEquals(1, set2.size());
}
Also used : Cookie(javax.ws.rs.core.Cookie) Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Method(java.lang.reflect.Method) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Test(org.junit.Test)

Example 17 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class BinaryDataProviderTest method testReadFrom.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testReadFrom() throws Exception {
    MessageBodyReader p = new BinaryDataProvider();
    byte[] bytes = (byte[]) p.readFrom(byte[].class, byte[].class, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), new ByteArrayInputStream("hi".getBytes()));
    assertTrue(Arrays.equals(new String("hi").getBytes(), bytes));
    InputStream is = (InputStream) p.readFrom(InputStream.class, InputStream.class, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), new ByteArrayInputStream("hi".getBytes()));
    bytes = IOUtils.readBytesFromStream(is);
    assertTrue(Arrays.equals(new String("hi").getBytes(), bytes));
    Reader r = (Reader) p.readFrom(Reader.class, Reader.class, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), new ByteArrayInputStream("hi".getBytes()));
    assertEquals(IOUtils.toString(r), "hi");
    StreamingOutput so = (StreamingOutput) p.readFrom(StreamingOutput.class, StreamingOutput.class, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), new ByteArrayInputStream("hi".getBytes()));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    so.write(baos);
    bytes = baos.toByteArray();
    assertTrue(Arrays.equals(new String("hi").getBytes(), bytes));
}
Also used : MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) StringReader(java.io.StringReader) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) StreamingOutput(javax.ws.rs.core.StreamingOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 18 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class DataSourceProviderTest method testWriteDataSourceWithDiffCT.

@Test
public void testWriteDataSourceWithDiffCT() throws Exception {
    DataSourceProvider<DataSource> p = new DataSourceProvider<DataSource>();
    p.setUseDataSourceContentType(true);
    DataSource ds = new InputStreamDataSource(new ByteArrayInputStream("image".getBytes()), "image/png");
    MultivaluedMap<String, Object> outHeaders = new MetadataMap<String, Object>();
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        p.writeTo(ds, DataSource.class, DataSource.class, new Annotation[] {}, MediaType.valueOf("image/jpeg"), outHeaders, os);
        assertEquals("image", os.toString());
    }
    assertEquals("image/png", outHeaders.getFirst("Content-Type"));
}
Also used : InputStreamDataSource(org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamDataSource(org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource) DataSource(javax.activation.DataSource) Test(org.junit.Test)

Example 19 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class FormEncodingProviderTest method testWriteMultipleValues2.

@Test
public void testWriteMultipleValues2() throws Exception {
    MultivaluedMap<String, String> mvMap = new MetadataMap<String, String>();
    mvMap.add("a", "a1");
    mvMap.add("a", "a2");
    mvMap.add("b", "b1");
    FormEncodingProvider<MultivaluedMap<?, ?>> ferp = new FormEncodingProvider<MultivaluedMap<?, ?>>();
    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&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 20 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class FormEncodingProviderTest method testWrite.

@Test
public void testWrite() throws Exception {
    MultivaluedMap<String, String> mvMap = new MetadataMap<String, String>();
    mvMap.add("a", "a1");
    mvMap.add("b", "b1");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    FormEncodingProvider<MultivaluedMap<?, ?>> ferp = new FormEncodingProvider<MultivaluedMap<?, ?>>();
    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)

Aggregations

MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)80 Test (org.junit.Test)43 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)36 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)34 ByteArrayInputStream (java.io.ByteArrayInputStream)25 Message (org.apache.cxf.message.Message)25 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)15 List (java.util.List)13 Method (java.lang.reflect.Method)12 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 Endpoint (org.apache.cxf.endpoint.Endpoint)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 LinkedHashMap (java.util.LinkedHashMap)9 Customer (org.apache.cxf.jaxrs.Customer)9 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 Annotation (java.lang.annotation.Annotation)8 HashMap (java.util.HashMap)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)7