Search in sources :

Example 46 with Attachment

use of org.apache.cxf.jaxrs.ext.multipart.Attachment 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)

Example 47 with Attachment

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

the class MessageContextImpl method createAttachments.

private MultipartBody createAttachments(String propertyName) {
    Message inMessage = m.getExchange().getInMessage();
    boolean embeddedAttachment = inMessage.get("org.apache.cxf.multipart.embedded") != null;
    Object o = inMessage.get(propertyName);
    if (o != null) {
        return (MultipartBody) o;
    }
    if (embeddedAttachment) {
        inMessage = new MessageImpl();
        inMessage.setExchange(new ExchangeImpl());
        inMessage.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_DIRECTORY));
        inMessage.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD));
        inMessage.put(AttachmentDeserializer.ATTACHMENT_MAX_SIZE, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MAX_SIZE));
        inMessage.put(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE));
        inMessage.setContent(InputStream.class, m.getExchange().getInMessage().get("org.apache.cxf.multipart.embedded.input"));
        inMessage.put(Message.CONTENT_TYPE, m.getExchange().getInMessage().get("org.apache.cxf.multipart.embedded.ctype").toString());
    }
    new AttachmentInputInterceptor().handleMessage(inMessage);
    List<Attachment> newAttachments = new LinkedList<Attachment>();
    try {
        Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>) inMessage.get(AttachmentDeserializer.ATTACHMENT_PART_HEADERS));
        Attachment first = new Attachment(AttachmentUtil.createAttachment(inMessage.getContent(InputStream.class), headers), new ProvidersImpl(inMessage));
        newAttachments.add(first);
    } catch (IOException ex) {
        throw ExceptionUtils.toInternalServerErrorException(ex, null);
    }
    Collection<org.apache.cxf.message.Attachment> childAttachments = inMessage.getAttachments();
    if (childAttachments == null) {
        childAttachments = Collections.emptyList();
    }
    childAttachments.size();
    for (org.apache.cxf.message.Attachment a : childAttachments) {
        newAttachments.add(new Attachment(a, new ProvidersImpl(inMessage)));
    }
    MediaType mt = embeddedAttachment ? (MediaType) inMessage.get("org.apache.cxf.multipart.embedded.ctype") : getHttpHeaders().getMediaType();
    MultipartBody body = new MultipartBody(newAttachments, mt, false);
    inMessage.put(propertyName, body);
    return body;
}
Also used : Message(org.apache.cxf.message.Message) AttachmentInputInterceptor(org.apache.cxf.jaxrs.interceptor.AttachmentInputInterceptor) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) MediaType(javax.ws.rs.core.MediaType) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 48 with Attachment

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

the class MultipartProvider method getAttachments.

private List<Attachment> getAttachments(List<?> objects, String rootMediaType) throws IOException {
    List<Attachment> handlers = new ArrayList<>(objects.size());
    for (int i = 0; i < objects.size(); i++) {
        Object value = objects.get(i);
        Attachment handler = createDataHandler(value, value.getClass(), new Annotation[] {}, rootMediaType, rootMediaType, i);
        handlers.add(handler);
    }
    return handlers;
}
Also used : ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment)

Example 49 with Attachment

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

the class MultipartProvider method getAttachmentCollection.

private Object getAttachmentCollection(Type t, List<Attachment> infos, Annotation[] anns) throws IOException {
    Class<?> actual = getActualType(t, 0);
    if (Attachment.class.isAssignableFrom(actual)) {
        return infos;
    }
    Collection<Object> objects = new ArrayList<>();
    for (Attachment a : infos) {
        objects.add(fromAttachment(a, actual, actual, anns));
    }
    return objects;
}
Also used : ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment)

Example 50 with Attachment

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

the class MultipartProvider method convertToDataHandlers.

private List<Attachment> convertToDataHandlers(Object obj, Class<?> type, Type genericType, Annotation[] anns, MediaType mt) throws IOException {
    if (Map.class.isAssignableFrom(obj.getClass())) {
        Map<Object, Object> objects = CastUtils.cast((Map<?, ?>) obj);
        List<Attachment> handlers = new ArrayList<>(objects.size());
        int i = 0;
        for (Iterator<Map.Entry<Object, Object>> iter = objects.entrySet().iterator(); iter.hasNext(); ) {
            Map.Entry<Object, Object> entry = iter.next();
            Object value = entry.getValue();
            Attachment handler = createDataHandler(value, value.getClass(), new Annotation[] {}, entry.getKey().toString(), mt.toString(), i++);
            handlers.add(handler);
        }
        return handlers;
    }
    String rootMediaType = getRootMediaType(anns, mt);
    if (List.class.isAssignableFrom(obj.getClass())) {
        return getAttachments((List<?>) obj, rootMediaType);
    }
    if (MultipartBody.class.isAssignableFrom(type)) {
        List<Attachment> atts = ((MultipartBody) obj).getAllAttachments();
        // these attachments may have no DataHandlers, but objects only
        return getAttachments(atts, rootMediaType);
    }
    Attachment handler = createDataHandler(obj, genericType, anns, rootMediaType, mt.toString(), 1);
    return Collections.singletonList(handler);
}
Also used : MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Aggregations

Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)56 Test (org.junit.Test)20 InputStream (java.io.InputStream)19 Response (javax.ws.rs.core.Response)17 ArrayList (java.util.ArrayList)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)15 WebClient (org.apache.cxf.jaxrs.client.WebClient)14 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)14 IOException (java.io.IOException)10 PushbackInputStream (java.io.PushbackInputStream)8 POST (javax.ws.rs.POST)8 LinkedList (java.util.LinkedList)7 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)7 LinkedHashMap (java.util.LinkedHashMap)6 DataHandler (javax.activation.DataHandler)6 Path (javax.ws.rs.Path)6 QueryResponse (ddf.catalog.operation.QueryResponse)5 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)5 Consumes (javax.ws.rs.Consumes)5