Search in sources :

Example 66 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 67 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)

Example 68 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 69 with Attachment

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

the class AttachmentUtils method getMultipart.

public static Attachment getMultipart(Multipart id, MediaType mt, List<Attachment> infos) throws IOException {
    if (id != null) {
        for (Attachment a : infos) {
            if (matchAttachmentId(a, id)) {
                checkMediaTypes(a.getContentType(), id.type());
                return a;
            }
        }
        if (id.required()) {
            org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("MULTTIPART_ID_NOT_FOUND", BUNDLE, id.value(), mt.toString());
            LOG.warning(errorMsg.toString());
            throw ExceptionUtils.toBadRequestException(new MultipartReadException(id.value(), id.type(), errorMsg.toString()), null);
        }
        return null;
    }
    return !infos.isEmpty() ? infos.get(0) : null;
}
Also used : Message(org.apache.cxf.message.Message) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment)

Example 70 with Attachment

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

the class AttachmentUtils method fromListToMap.

private static Map<String, Attachment> fromListToMap(List<Attachment> atts, boolean preferContentDisposition) {
    Map<String, Attachment> map = new LinkedHashMap<>();
    for (Attachment a : atts) {
        String contentId = null;
        if (preferContentDisposition) {
            ContentDisposition cd = a.getContentDisposition();
            if (cd != null) {
                contentId = cd.getParameter("name");
            }
        }
        if (contentId == null) {
            contentId = a.getContentId();
        }
        map.put(contentId, a);
    }
    return map;
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)79 ArrayList (java.util.ArrayList)31 Test (org.junit.Test)29 InputStream (java.io.InputStream)25 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)25 ByteArrayInputStream (java.io.ByteArrayInputStream)23 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)23 Response (javax.ws.rs.core.Response)17 IOException (java.io.IOException)15 WebClient (org.apache.cxf.jaxrs.client.WebClient)15 CatalogFramework (ddf.catalog.CatalogFramework)10 LinkedHashMap (java.util.LinkedHashMap)9 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)9 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)8 PushbackInputStream (java.io.PushbackInputStream)8 POST (javax.ws.rs.POST)8 Path (javax.ws.rs.Path)8 Metacard (ddf.catalog.data.Metacard)7 InputTransformer (ddf.catalog.transform.InputTransformer)7 LinkedList (java.util.LinkedList)7