Search in sources :

Example 16 with MultipartBody

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

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

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

the class GrobidRESTParser method parse.

public void parse(String filePath, ContentHandler handler, Metadata metadata, ParseContext context) throws FileNotFoundException {
    File pdfFile = new File(filePath);
    ContentDisposition cd = new ContentDisposition("form-data; name=\"input\"; filename=\"" + pdfFile.getName() + "\"");
    Attachment att = new Attachment("input", new FileInputStream(pdfFile), cd);
    MultipartBody body = new MultipartBody(att);
    Response response = WebClient.create(restHostUrlStr + GROBID_PROCESSHEADER_PATH).accept(MediaType.APPLICATION_XML).type(MediaType.MULTIPART_FORM_DATA).post(body);
    try {
        String resp = response.readEntity(String.class);
        Metadata teiMet = new TEIParser().parse(resp);
        for (String key : teiMet.names()) {
            metadata.add("grobid:header_" + key, teiMet.get(key));
        }
    } catch (Exception e) {
        LOG.warn("Couldn't read response", e);
    }
}
Also used : Response(javax.ws.rs.core.Response) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Metadata(org.apache.tika.metadata.Metadata) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 19 with MultipartBody

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

the class FormEncodingProvider method readFrom.

public T readFrom(Class<T> clazz, Type genericType, Annotation[] annotations, MediaType mt, MultivaluedMap<String, String> headers, InputStream is) throws IOException {
    if (is == null) {
        return null;
    }
    try {
        if (mt.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)) {
            MultipartBody body = AttachmentUtils.getMultipartBody(mc);
            if (MultipartBody.class.isAssignableFrom(clazz)) {
                return clazz.cast(body);
            } else if (Attachment.class.isAssignableFrom(clazz)) {
                return clazz.cast(body.getRootAttachment());
            }
        }
        MultivaluedMap<String, String> params = createMap(clazz);
        populateMap(params, annotations, is, mt, !keepEncoded(annotations));
        validateMap(params);
        persistParamsOnMessage(params);
        return getFormObject(clazz, params);
    } catch (WebApplicationException e) {
        throw e;
    } catch (Exception e) {
        throw ExceptionUtils.toBadRequestException(e, null);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 20 with MultipartBody

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

the class FormEncodingProvider method populateMap.

/**
 * Retrieve map of parameters from the passed in message
 */
protected void populateMap(MultivaluedMap<String, String> params, Annotation[] anns, InputStream is, MediaType mt, boolean decode) {
    if (mt.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)) {
        MultipartBody body = AttachmentUtils.getMultipartBody(mc, attachmentDir, attachmentThreshold, attachmentMaxSize);
        FormUtils.populateMapFromMultipart(params, body, PhaseInterceptorChain.getCurrentMessage(), decode);
    } else {
        String enc = HttpUtils.getEncoding(mt, StandardCharsets.UTF_8.name());
        Object servletRequest = mc != null ? mc.getHttpServletRequest() : null;
        if (servletRequest == null) {
            FormUtils.populateMapFromString(params, PhaseInterceptorChain.getCurrentMessage(), FormUtils.readBody(is, enc), enc, decode);
        } else {
            FormUtils.populateMapFromString(params, PhaseInterceptorChain.getCurrentMessage(), FormUtils.readBody(is, enc), enc, decode, (javax.servlet.http.HttpServletRequest) servletRequest);
        }
    }
}
Also used : MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody)

Aggregations

MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)35 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)25 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)15 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 WebClient (org.apache.cxf.jaxrs.client.WebClient)7 CatalogFramework (ddf.catalog.CatalogFramework)6 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)5 Response (javax.ws.rs.core.Response)5 InputTransformer (ddf.catalog.transform.InputTransformer)4 LinkedHashMap (java.util.LinkedHashMap)4 HttpHeaders (javax.ws.rs.core.HttpHeaders)4 MediaType (javax.ws.rs.core.MediaType)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)3 Map (java.util.Map)3 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)3