Search in sources :

Example 26 with MultipartBody

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

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

the class JAXRSUtils method processFormParam.

private static Object processFormParam(Message m, String key, Class<?> pClass, Type genericType, Annotation[] paramAnns, String defaultValue, boolean decode) {
    MessageContext mc = new MessageContextImpl(m);
    MediaType mt = mc.getHttpHeaders().getMediaType();
    @SuppressWarnings("unchecked") MultivaluedMap<String, String> params = (MultivaluedMap<String, String>) m.get(FormUtils.FORM_PARAM_MAP);
    String enc = HttpUtils.getEncoding(mt, StandardCharsets.UTF_8.name());
    if (params == null) {
        params = new MetadataMap<>();
        m.put(FormUtils.FORM_PARAM_MAP, params);
        if (mt == null || mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
            InputStream entityStream = copyAndGetEntityStream(m);
            String body = FormUtils.readBody(entityStream, enc);
            // Do not decode unless the key is empty value, fe @FormParam("")
            FormUtils.populateMapFromStringOrHttpRequest(params, m, body, enc, StringUtils.isEmpty(key) && decode);
        } else {
            if ("multipart".equalsIgnoreCase(mt.getType()) && MediaType.MULTIPART_FORM_DATA_TYPE.isCompatible(mt)) {
                MultipartBody body = AttachmentUtils.getMultipartBody(mc);
                FormUtils.populateMapFromMultipart(params, body, m, decode);
            } else {
                org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("WRONG_FORM_MEDIA_TYPE", BUNDLE, mt.toString());
                LOG.warning(errorMsg.toString());
                throw ExceptionUtils.toNotSupportedException(null, null);
            }
        }
    }
    if (decode && !MessageUtils.getContextualBoolean(m, FormUtils.FORM_PARAM_MAP_DECODED, false)) {
        List<String> values = params.get(key);
        if (values != null) {
            values = values.stream().map(value -> HttpUtils.urlDecode(value, enc)).collect(Collectors.toList());
            params.replace(key, values);
        }
    }
    if ("".equals(key)) {
        return InjectionUtils.handleBean(pClass, paramAnns, params, ParameterType.FORM, m, false);
    }
    List<String> results = params.get(key);
    return InjectionUtils.createParameterObject(results, pClass, genericType, paramAnns, defaultValue, false, ParameterType.FORM, m);
}
Also used : Message(org.apache.cxf.message.Message) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) InputStream(java.io.InputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) MediaType(javax.ws.rs.core.MediaType) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Example 28 with MultipartBody

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

the class AttachmentUtils method getMultipartBody.

public static MultipartBody getMultipartBody(MessageContext mc, String attachmentDir, String attachmentThreshold, String attachmentMaxSize) {
    if (attachmentDir != null) {
        mc.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, attachmentDir);
    }
    if (attachmentThreshold != null) {
        mc.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, attachmentThreshold);
    }
    if (attachmentMaxSize != null) {
        mc.put(AttachmentDeserializer.ATTACHMENT_MAX_SIZE, attachmentMaxSize);
    }
    boolean embeddedAttachment = mc.get("org.apache.cxf.multipart.embedded") != null;
    String propertyName = embeddedAttachment ? MultipartBody.INBOUND_MESSAGE_ATTACHMENTS + ".embedded" : MultipartBody.INBOUND_MESSAGE_ATTACHMENTS;
    MultipartBody body = (MultipartBody) mc.get(propertyName);
    if (!embeddedAttachment && mc.get(IN_FILTERS) != null) {
        List<MultipartInputFilter> filters = CastUtils.cast((List<?>) mc.get(IN_FILTERS));
        for (MultipartInputFilter filter : filters) {
            filter.filter(body.getAllAttachments());
        }
    }
    return body;
}
Also used : MultipartInputFilter(org.apache.cxf.jaxrs.ext.multipart.MultipartInputFilter) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody)

Example 29 with MultipartBody

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

the class JAXRSMultipartTest method testUploadFileWithSemicolonName.

@Test
public void testUploadFileWithSemicolonName() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/books/file/semicolon";
    WebClient client = WebClient.create(address);
    client.type("multipart/form-data").accept("text/plain");
    ContentDisposition cd = new ContentDisposition("attachment;name=\"a\";filename=\"a;txt\"");
    MultivaluedMap<String, String> headers = new MetadataMap<>();
    headers.putSingle("Content-Disposition", cd.toString());
    Attachment att = new Attachment(new ByteArrayInputStream("file name with semicolon".getBytes()), headers);
    MultipartBody body = new MultipartBody(att);
    String partContent = client.post(body, String.class);
    assertEquals("file name with semicolon, filename:" + "a;txt", partContent);
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 30 with MultipartBody

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

the class JAXRSMultipartTest method testUploadImageFromForm2.

@Test
public void testUploadImageFromForm2() throws Exception {
    File file = new File(getClass().getResource("/org/apache/cxf/systest/jaxrs/resources/java.jpg").toURI().getPath());
    String address = "http://localhost:" + PORT + "/bookstore/books/formimage2";
    WebClient client = WebClient.create(address);
    client.type("multipart/form-data").accept("multipart/form-data");
    WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart", "true");
    MultipartBody body2 = client.post(file, MultipartBody.class);
    InputStream is2 = body2.getRootAttachment().getDataHandler().getInputStream();
    byte[] image1 = IOUtils.readBytesFromStream(getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));
    byte[] image2 = IOUtils.readBytesFromStream(is2);
    assertArrayEquals(image1, image2);
    ContentDisposition cd2 = body2.getRootAttachment().getContentDisposition();
    assertEquals("form-data;name=file;filename=java.jpg", cd2.toString());
    assertEquals("java.jpg", cd2.getParameter("filename"));
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) File(java.io.File) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

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