diff --git a/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java b/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java index 0917d1d72a4..3ea312238e1 100644 --- a/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java +++ b/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java @@ -412,14 +412,16 @@ public static Attachment createAttachment(InputStream stream, Map> e : headers.entrySet()) { @@ -447,14 +449,32 @@ public static Attachment createAttachment(InputStream stream, Map> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + if (cd != null) { + headers.put("Content-Disposition", Collections.singletonList(cd)); + } + Message message = new MessageImpl(); + if (defaultCt != null) { + message.put(AttachmentUtil.ATTACHMENT_CONTENT_TYPE, defaultCt); + } + Attachment att = AttachmentUtil.createAttachment( + new ByteArrayInputStream("3".getBytes(StandardCharsets.UTF_8)), headers, message); + return att.getDataHandler().getContentType(); + } } diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MultipartProviderTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MultipartProviderTest.java index e3712fb6b31..35499c4ea5a 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MultipartProviderTest.java +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MultipartProviderTest.java @@ -28,7 +28,9 @@ import jakarta.ws.rs.core.MediaType; import org.apache.cxf.attachment.AttachmentDeserializer; +import org.apache.cxf.endpoint.Endpoint; import org.apache.cxf.jaxrs.ext.MessageContextImpl; +import org.apache.cxf.jaxrs.ext.multipart.Multipart; import org.apache.cxf.jaxrs.impl.MetadataMap; import org.apache.cxf.message.Exchange; import org.apache.cxf.message.ExchangeImpl; @@ -39,6 +41,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class MultipartProviderTest { @Test @@ -130,4 +134,51 @@ public void testManyAttachmentHeaders() throws Exception { new MetadataMap(), msg.getContent(InputStream.class))); } + + @Test + public void testFormDataFieldWithoutContentTypeAsInteger() throws Exception { + assertEquals(Integer.valueOf(3), readFormDataInteger(null)); + } + + @Test + public void testFormDataFieldWithTextPlainContentTypeAsInteger() throws Exception { + assertEquals(Integer.valueOf(3), readFormDataInteger("text/plain")); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + private Object readFormDataInteger(String partContentType) throws Exception { + StringBuilder sb = new StringBuilder() + .append("--bound\r\n") + .append("Content-Disposition: form-data; name=\"dzchunkindex\"\r\n"); + if (partContentType != null) { + sb.append("Content-Type: ").append(partContentType).append("\r\n"); + } + sb.append("\r\n") + .append("3\r\n") + .append("--bound--\r\n"); + + final Exchange exchange = new ExchangeImpl(); + final Endpoint endpoint = mock(Endpoint.class); + when(endpoint.get(ServerProviderFactory.class.getName())).thenReturn(ServerProviderFactory.getInstance()); + exchange.put(Endpoint.class, endpoint); + final Message msg = new MessageImpl(); + msg.setExchange(exchange); + exchange.setInMessage(msg); + msg.put(Message.CONTENT_TYPE, "multipart/form-data; boundary=bound"); + InputStream is = new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8)); + msg.setContent(InputStream.class, is); + + final MultipartProvider p = new MultipartProvider(); + p.setMessageContext(new MessageContextImpl(msg)); + Annotation[] anns = FormDataResource.class.getMethod("upload", Integer.class) + .getParameterAnnotations()[0]; + return p.readFrom((Class)Integer.class, Integer.class, anns, + MediaType.valueOf("multipart/form-data; boundary=bound"), new MetadataMap(), is); + } + + public static class FormDataResource { + public void upload(@Multipart(value = "dzchunkindex", required = false) Integer index) { + // complete + } + } }