Search in sources :

Example 21 with Attachment

use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project tomee 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 22 with Attachment

use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project tomee 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 23 with Attachment

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

the class MultipartProvider method createDataHandler.

private <T> Attachment createDataHandler(T obj, Class<T> cls, Type genericType, Annotation[] anns, String mimeType, String mainMediaType, int id) throws IOException {
    final DataHandler dh;
    if (InputStream.class.isAssignableFrom(obj.getClass())) {
        dh = createInputStreamDH((InputStream) obj, mimeType);
    } else if (DataHandler.class.isAssignableFrom(obj.getClass())) {
        dh = (DataHandler) obj;
    } else if (DataSource.class.isAssignableFrom(obj.getClass())) {
        dh = new DataHandler((DataSource) obj);
    } else if (File.class.isAssignableFrom(obj.getClass())) {
        File f = (File) obj;
        ContentDisposition cd = mainMediaType.startsWith(MediaType.MULTIPART_FORM_DATA) ? new ContentDisposition("form-data;name=file;filename=" + f.getName()) : null;
        return new Attachment(AttachmentUtil.BODY_ATTACHMENT_ID, Files.newInputStream(f.toPath()), cd);
    } else if (Attachment.class.isAssignableFrom(obj.getClass())) {
        Attachment att = (Attachment) obj;
        if (att.getObject() == null) {
            return att;
        }
        dh = getHandlerForObject(att.getObject(), att.getObject().getClass(), new Annotation[] {}, att.getContentType().toString(), id);
        return new Attachment(att.getContentId(), dh, att.getHeaders());
    } else if (byte[].class.isAssignableFrom(obj.getClass())) {
        ByteDataSource source = new ByteDataSource((byte[]) obj);
        source.setContentType(mimeType);
        dh = new DataHandler(source);
    } else {
        dh = getHandlerForObject(obj, cls, genericType, anns, mimeType);
    }
    String contentId = getContentId(anns, id);
    MultivaluedMap<String, String> headers = new MetadataMap<>();
    headers.putSingle("Content-Type", mimeType);
    return new Attachment(contentId, dh, headers);
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) InputStream(java.io.InputStream) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) DataHandler(javax.activation.DataHandler) File(java.io.File) Annotation(java.lang.annotation.Annotation) InputStreamDataSource(org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) DataSource(javax.activation.DataSource)

Example 24 with Attachment

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

the class ClientProxyImpl method handleMultipart.

protected List<Attachment> handleMultipart(MultivaluedMap<ParameterType, Parameter> map, OperationResourceInfo ori, Object[] params) {
    List<Parameter> fm = getParameters(map, ParameterType.REQUEST_BODY);
    List<Attachment> atts = new ArrayList<>(fm.size());
    fm.forEach(p -> {
        Multipart part = getMultipart(ori, p.getIndex());
        if (part != null) {
            Object partObject = params[p.getIndex()];
            if (partObject != null) {
                atts.add(new Attachment(part.value(), part.type(), partObject));
            }
        }
    });
    return atts;
}
Also used : Multipart(org.apache.cxf.jaxrs.ext.multipart.Multipart) ArrayList(java.util.ArrayList) Parameter(org.apache.cxf.jaxrs.model.Parameter) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment)

Example 25 with Attachment

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

the class AbstractCatalogService method createMetacard.

@Override
public BinaryContent createMetacard(MultipartBody multipartBody, String transformerParam) throws CatalogServiceException {
    LOGGER.trace("ENTERING: createMetacard");
    String contentUri = multipartBody.getAttachmentObject("contentUri", String.class);
    LOGGER.debug("contentUri = {}", contentUri);
    InputStream stream = null;
    String contentType = null;
    Attachment contentPart = multipartBody.getAttachment(FILE_ATTACHMENT_CONTENT_ID);
    if (contentPart != null) {
        // Content-Type: application/json;id=geojson
        if (contentPart.getContentType() != null) {
            contentType = contentPart.getContentType().toString();
        }
        // at the beginning
        try {
            stream = contentPart.getDataHandler().getInputStream();
            if (stream != null && stream.available() == 0) {
                stream.reset();
            }
        } catch (IOException e) {
            LOGGER.info("IOException reading stream from file attachment in multipart body", e);
        }
    } else {
        LOGGER.debug(NO_FILE_CONTENTS_ATT_FOUND);
    }
    return createMetacard(stream, contentType, transformerParam);
}
Also used : BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException)

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