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);
}
}
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);
}
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;
}
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);
}
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"));
}
Aggregations