use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class MultipartProvider method getAttachments.
private List<Attachment> getAttachments(List<?> objects, String rootMediaType) throws IOException {
List<Attachment> handlers = new ArrayList<>(objects.size());
for (int i = 0; i < objects.size(); i++) {
Object value = objects.get(i);
Attachment handler = createDataHandler(value, value.getClass(), new Annotation[] {}, rootMediaType, rootMediaType, i);
handlers.add(handler);
}
return handlers;
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf 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);
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class MultipartProvider method getAttachmentCollection.
private Object getAttachmentCollection(Type t, List<Attachment> infos, Annotation[] anns) throws IOException {
Class<?> actual = getActualType(t, 0);
if (Attachment.class.isAssignableFrom(actual)) {
return infos;
}
Collection<Object> objects = new ArrayList<>();
for (Attachment a : infos) {
objects.add(fromAttachment(a, actual, actual, anns));
}
return objects;
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class AttachmentUtils method getMultipart.
public static Attachment getMultipart(Multipart id, MediaType mt, List<Attachment> infos) throws IOException {
if (id != null) {
for (Attachment a : infos) {
if (matchAttachmentId(a, id)) {
checkMediaTypes(a.getContentType(), id.type());
return a;
}
}
if (id.required()) {
org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("MULTTIPART_ID_NOT_FOUND", BUNDLE, id.value(), mt.toString());
LOG.warning(errorMsg.toString());
throw ExceptionUtils.toBadRequestException(new MultipartReadException(id.value(), id.type(), errorMsg.toString()), null);
}
return null;
}
return !infos.isEmpty() ? infos.get(0) : null;
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class AttachmentUtils method fromListToMap.
private static Map<String, Attachment> fromListToMap(List<Attachment> atts, boolean preferContentDisposition) {
Map<String, Attachment> map = new LinkedHashMap<>();
for (Attachment a : atts) {
String contentId = null;
if (preferContentDisposition) {
ContentDisposition cd = a.getContentDisposition();
if (cd != null) {
contentId = cd.getParameter("name");
}
}
if (contentId == null) {
contentId = a.getContentId();
}
map.put(contentId, a);
}
return map;
}
Aggregations