Search in sources :

Example 31 with Attachment

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

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

the class MultipartProvider method writeTo.

public void writeTo(Object obj, Class<?> type, Type genericType, Annotation[] anns, MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException, WebApplicationException {
    List<Attachment> handlers = convertToDataHandlers(obj, type, genericType, anns, mt);
    if (mc.get(AttachmentUtils.OUT_FILTERS) != null) {
        List<MultipartOutputFilter> filters = CastUtils.cast((List<?>) mc.get(AttachmentUtils.OUT_FILTERS));
        for (MultipartOutputFilter filter : filters) {
            filter.filter(handlers);
        }
    }
    mc.put(MultipartBody.OUTBOUND_MESSAGE_ATTACHMENTS, handlers);
    handlers.get(0).getDataHandler().writeTo(os);
}
Also used : MultipartOutputFilter(org.apache.cxf.jaxrs.ext.multipart.MultipartOutputFilter) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment)

Example 33 with Attachment

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

the class MultipartProvider method readFrom.

public Object readFrom(Class<Object> c, Type t, Annotation[] anns, MediaType mt, MultivaluedMap<String, String> headers, InputStream is) throws IOException, WebApplicationException {
    checkContentLength();
    List<Attachment> infos = AttachmentUtils.getAttachments(mc, attachmentDir, attachmentThreshold, attachmentMaxSize);
    boolean collectionExpected = Collection.class.isAssignableFrom(c);
    if (collectionExpected && AnnotationUtils.getAnnotation(anns, Multipart.class) == null) {
        return getAttachmentCollection(t, infos, anns);
    }
    if (c.isAssignableFrom(Map.class)) {
        Map<String, Object> map = new LinkedHashMap<>(infos.size());
        Class<?> actual = getActualType(t, 1);
        for (Attachment a : infos) {
            map.put(a.getContentType().toString(), fromAttachment(a, actual, actual, anns));
        }
        return map;
    }
    if (MultipartBody.class.isAssignableFrom(c)) {
        return new MultipartBody(infos);
    }
    Multipart id = AnnotationUtils.getAnnotation(anns, Multipart.class);
    Attachment multipart = AttachmentUtils.getMultipart(id, mt, infos);
    if (multipart != null) {
        if (collectionExpected && !mediaTypeSupported(multipart.getContentType()) && !PropertyUtils.isTrue(mc.getContextualProperty(SINGLE_PART_IS_COLLECTION))) {
            List<Attachment> allMultiparts = AttachmentUtils.getMatchingAttachments(id, infos);
            return getAttachmentCollection(t, allMultiparts, anns);
        }
        return fromAttachment(multipart, c, t, anns);
    }
    if (id != null && !id.required()) {
        /*
             * Return default value for a missing optional part
             */
        Object defaultValue = null;
        if (c.isPrimitive()) {
            defaultValue = PrimitiveUtils.read((Class<?>) c == boolean.class ? "false" : "0", c);
        }
        return defaultValue;
    }
    throw ExceptionUtils.toBadRequestException(null, null);
}
Also used : Multipart(org.apache.cxf.jaxrs.ext.multipart.Multipart) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) LinkedHashMap(java.util.LinkedHashMap)

Example 34 with Attachment

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

the class AbstractJwsMultipartSignatureFilter method getAttachmentParts.

protected List<Object> getAttachmentParts(Object rootEntity) {
    final List<Object> parts;
    if (rootEntity instanceof MultipartBody) {
        parts = CastUtils.cast(((MultipartBody) rootEntity).getAllAttachments());
    } else {
        if (rootEntity instanceof List) {
            List<Object> entityList = CastUtils.cast((List<?>) rootEntity);
            parts = new ArrayList<>(entityList);
        } else {
            parts = new ArrayList<>(2);
            parts.add(rootEntity);
        }
    }
    JwsHeaders headers = new JwsHeaders();
    headers.setPayloadEncodingStatus(false);
    JwsSignatureProvider theSigProvider = sigProvider != null ? sigProvider : JwsUtils.loadSignatureProvider(headers, true);
    JwsSignature jwsSignature = theSigProvider.createJwsSignature(headers);
    String base64UrlEncodedHeaders = Base64UrlUtility.encode(writer.toJson(headers));
    byte[] headerBytesWithDot = StringUtils.toBytesASCII(base64UrlEncodedHeaders + '.');
    jwsSignature.update(headerBytesWithDot, 0, headerBytesWithDot.length);
    AttachmentUtils.addMultipartOutFilter(new JwsMultipartSignatureOutFilter(jwsSignature));
    JwsDetachedSignature jws = new JwsDetachedSignature(headers, base64UrlEncodedHeaders, jwsSignature, useJwsJsonSignatureFormat);
    Attachment jwsPart = new Attachment("signature", JoseConstants.MEDIA_TYPE_JOSE, jws);
    parts.add(jwsPart);
    return parts;
}
Also used : JwsSignature(org.apache.cxf.rs.security.jose.jws.JwsSignature) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) JwsHeaders(org.apache.cxf.rs.security.jose.jws.JwsHeaders) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) JwsDetachedSignature(org.apache.cxf.rs.security.jose.jws.JwsDetachedSignature) ArrayList(java.util.ArrayList) List(java.util.List) JwsSignatureProvider(org.apache.cxf.rs.security.jose.jws.JwsSignatureProvider)

Example 35 with Attachment

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

the class JwsMultipartSignatureOutFilter method filter.

@Override
public void filter(List<Attachment> parts) {
    for (int i = 0; i < parts.size() - 1; i++) {
        Attachment dataPart = parts.get(i);
        DataHandler handler = dataPart.getDataHandler();
        dataPart.setDataHandler(new JwsSignatureDataHandler(handler));
    }
}
Also used : Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) DataHandler(javax.activation.DataHandler)

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