Search in sources :

Example 1 with InputStreamDataSource

use of org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource 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 2 with InputStreamDataSource

use of org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource in project cxf by apache.

the class JAXRSMultipartTest method testLargeHeader.

// The large Content Disposition header will be rejected here
@Test
public void testLargeHeader() throws Exception {
    InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
    String address = "http://localhost:" + PORT + "/bookstore/books/image";
    WebClient client = WebClient.create(address);
    client.type("multipart/mixed").accept("multipart/mixed");
    WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart", "true");
    StringBuilder sb = new StringBuilder();
    sb.append("form-data;");
    for (int i = 0; i < 10000; i++) {
        sb.append("aaaaaaaaaa");
    }
    MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
    headers.putSingle("Content-ID", "root");
    headers.putSingle("Content-Type", "application/octet-stream");
    headers.putSingle("Content-Disposition", sb.toString());
    DataHandler handler = new DataHandler(new InputStreamDataSource(is1, "application/octet-stream"));
    Attachment att = new Attachment(headers, handler, null);
    Response response = client.post(att);
    assertEquals(response.getStatus(), 413);
    client.close();
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InputStreamDataSource(org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) DataHandler(javax.activation.DataHandler) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 3 with InputStreamDataSource

use of org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource 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 4 with InputStreamDataSource

use of org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource in project cxf by apache.

the class DataSourceProviderTest method testWriteDataHandler.

@Test
public void testWriteDataHandler() throws Exception {
    DataSourceProvider<DataHandler> p = new DataSourceProvider<DataHandler>();
    DataHandler ds = new DataHandler(new InputStreamDataSource(new ByteArrayInputStream("image".getBytes()), "image/png"));
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    p.writeTo(ds, DataHandler.class, DataHandler.class, new Annotation[] {}, MediaType.valueOf("image/png"), new MetadataMap<String, Object>(), os);
    assertEquals("image", os.toString());
}
Also used : InputStreamDataSource(org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource) ByteArrayInputStream(java.io.ByteArrayInputStream) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 5 with InputStreamDataSource

use of org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource in project cxf by apache.

the class JAXRSMultipartTest method testLargerThanDefaultHeader.

// The Content Disposition header will be accepted here, even though it is larger than the default,
// as we have configured a larger value on the service side
@Test
public void testLargerThanDefaultHeader() throws Exception {
    InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
    String address = "http://localhost:" + PORT + "/bookstore/books/image";
    WebClient client = WebClient.create(address);
    client.type("multipart/mixed").accept("multipart/mixed");
    WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart", "true");
    StringBuilder sb = new StringBuilder();
    sb.append("form-data;");
    for (int i = 0; i < 35; i++) {
        sb.append("aaaaaaaaaa");
    }
    MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
    headers.putSingle("Content-ID", "root");
    headers.putSingle("Content-Type", "application/octet-stream");
    headers.putSingle("Content-Disposition", sb.toString());
    DataHandler handler = new DataHandler(new InputStreamDataSource(is1, "application/octet-stream"));
    Attachment att = new Attachment(headers, handler, null);
    Response response = client.post(att);
    assertEquals(response.getStatus(), 200);
    client.close();
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InputStreamDataSource(org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) DataHandler(javax.activation.DataHandler) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Aggregations

InputStreamDataSource (org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Test (org.junit.Test)5 DataHandler (javax.activation.DataHandler)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataSource (javax.activation.DataSource)3 InputStream (java.io.InputStream)2 PushbackInputStream (java.io.PushbackInputStream)2 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)2 Response (javax.ws.rs.core.Response)2 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)2 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 File (java.io.File)1 FileDataSource (javax.activation.FileDataSource)1