Search in sources :

Example 66 with MetadataMap

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

the class DataSourceProviderTest method testWriteDataSource.

@Test
public void testWriteDataSource() throws Exception {
    DataSourceProvider<DataSource> p = new DataSourceProvider<DataSource>();
    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/png"), outHeaders, os);
        assertEquals("image", os.toString());
    }
    assertEquals(0, outHeaders.size());
}
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 67 with MetadataMap

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

the class JAXBElementProviderTest method doTestObjectFactoryExtraClass.

private void doTestObjectFactoryExtraClass(JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider) throws Exception {
    org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3 b = new org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3("CXF in Action", 123L, 124L);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(b, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider2 = new JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book>();
    provider2.setExtraClass(new Class[] { org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3.class });
    ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());
    org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3 book = (org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3) provider2.readFrom(org.apache.cxf.jaxrs.fortest.jaxb.Book.class, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), is);
    assertEquals(124L, book.getSuperId());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book)

Example 68 with MetadataMap

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

the class PrimitiveTextProviderTest method testWriteStringISO.

@Test
public void testWriteStringISO() throws Exception {
    MessageBodyWriter<String> p = new PrimitiveTextProvider<String>();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    MultivaluedMap<String, Object> headers = new MetadataMap<String, Object>();
    String eWithAcute = "\u00E9";
    String helloStringUTF16 = "Hello, my name is F" + eWithAcute + "lix Agn" + eWithAcute + "s";
    p.writeTo(helloStringUTF16, String.class, String.class, null, MediaType.valueOf("text/plain;charset=ISO-8859-1"), headers, os);
    byte[] iso88591bytes = helloStringUTF16.getBytes("ISO-8859-1");
    String helloStringISO88591 = new String(iso88591bytes, "ISO-8859-1");
    assertEquals(helloStringISO88591, os.toString("ISO-8859-1"));
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 69 with MetadataMap

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

the class CustomOutInterceptor method handleMessage.

@SuppressWarnings("unchecked")
public void handleMessage(Message message) throws Fault {
    String requestUri = (String) message.getExchange().getInMessage().get(Message.REQUEST_URI);
    if (requestUri.endsWith("/outfault")) {
        throw new WebApplicationException(403);
    }
    HttpHeaders requestHeaders = new HttpHeadersImpl(message.getExchange().getInMessage());
    if (requestHeaders.getHeaderString("PLAIN-MAP") != null) {
        Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS);
        if (headers == null) {
            headers = new HashMap<>();
            message.put(Message.PROTOCOL_HEADERS, headers);
        }
        headers.put("BookId", Arrays.asList("321"));
        headers.put("MAP-NAME", Arrays.asList(Map.class.getName()));
        message.put(Message.PROTOCOL_HEADERS, headers);
    } else {
        MultivaluedMap<String, Object> headers = new MetadataMap<String, Object>();
        headers.putSingle("BookId", "123");
        headers.putSingle("MAP-NAME", MultivaluedMap.class.getName());
        message.put(Message.PROTOCOL_HEADERS, headers);
    }
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) WebApplicationException(javax.ws.rs.WebApplicationException) List(java.util.List) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map) HttpHeadersImpl(org.apache.cxf.jaxrs.impl.HttpHeadersImpl)

Example 70 with MetadataMap

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

the class JAXRSMultipartTest method testUploadImageFromForm.

@Test
public void testUploadImageFromForm() throws Exception {
    InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
    String address = "http://localhost:" + PORT + "/bookstore/books/formimage";
    WebClient client = WebClient.create(address);
    HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
    conduit.getClient().setReceiveTimeout(1000000);
    conduit.getClient().setConnectionTimeout(1000000);
    client.type("multipart/form-data").accept("multipart/form-data");
    ContentDisposition cd = new ContentDisposition("attachment;filename=java.jpg");
    MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
    headers.putSingle("Content-ID", "image");
    headers.putSingle("Content-Disposition", cd.toString());
    headers.putSingle("Content-Location", "http://host/bar");
    headers.putSingle("custom-header", "custom");
    Attachment att = new Attachment(is1, headers);
    MultipartBody body = new MultipartBody(att);
    MultipartBody body2 = client.post(body, MultipartBody.class);
    InputStream is2 = body2.getRootAttachment().getDataHandler().getInputStream();
    byte[] image1 = IOUtils.readBytesFromStream(getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));
    byte[] image2 = IOUtils.readBytesFromStream(is2);
    assertTrue(Arrays.equals(image1, image2));
    ContentDisposition cd2 = body2.getRootAttachment().getContentDisposition();
    assertEquals("attachment;filename=java.jpg", cd2.toString());
    assertEquals("java.jpg", cd2.getParameter("filename"));
    assertEquals("http://host/location", body2.getRootAttachment().getHeader("Content-Location"));
}
Also used : HTTPConduit(org.apache.cxf.transport.http.HTTPConduit) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) WebClient(org.apache.cxf.jaxrs.client.WebClient) 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