use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class MultipartProvider method createDataHandler.
private <T> Attachment createDataHandler(T obj, Class<T> cls, Type genericType, Annotation[] anns, String mimeType, String mainMediaType, int id) throws IOException {
final DataHandler dh;
if (InputStream.class.isAssignableFrom(obj.getClass())) {
dh = createInputStreamDH((InputStream) obj, mimeType);
} else if (DataHandler.class.isAssignableFrom(obj.getClass())) {
dh = (DataHandler) obj;
} else if (DataSource.class.isAssignableFrom(obj.getClass())) {
dh = new DataHandler((DataSource) obj);
} else if (File.class.isAssignableFrom(obj.getClass())) {
File f = (File) obj;
ContentDisposition cd = mainMediaType.startsWith(MediaType.MULTIPART_FORM_DATA) ? new ContentDisposition("form-data;name=file;filename=" + f.getName()) : null;
return new Attachment(AttachmentUtil.BODY_ATTACHMENT_ID, Files.newInputStream(f.toPath()), cd);
} else if (Attachment.class.isAssignableFrom(obj.getClass())) {
Attachment att = (Attachment) obj;
if (att.getObject() == null) {
return att;
}
dh = getHandlerForObject(att.getObject(), att.getObject().getClass(), new Annotation[] {}, att.getContentType().toString(), id);
return new Attachment(att.getContentId(), dh, att.getHeaders());
} else if (byte[].class.isAssignableFrom(obj.getClass())) {
ByteDataSource source = new ByteDataSource((byte[]) obj);
source.setContentType(mimeType);
dh = new DataHandler(source);
} else {
dh = getHandlerForObject(obj, cls, genericType, anns, mimeType);
}
String contentId = getContentId(anns, id);
MultivaluedMap<String, String> headers = new MetadataMap<>();
headers.putSingle("Content-Type", mimeType);
return new Attachment(contentId, dh, headers);
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class MultipartProvider method writeTo.
public void writeTo(Object obj, Class<?> type, Type genericType, Annotation[] anns, MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException, WebApplicationException {
List<Attachment> handlers = convertToDataHandlers(obj, type, genericType, anns, mt);
if (mc.get(AttachmentUtils.OUT_FILTERS) != null) {
List<MultipartOutputFilter> filters = CastUtils.cast((List<?>) mc.get(AttachmentUtils.OUT_FILTERS));
for (MultipartOutputFilter filter : filters) {
filter.filter(handlers);
}
}
mc.put(MultipartBody.OUTBOUND_MESSAGE_ATTACHMENTS, handlers);
handlers.get(0).getDataHandler().writeTo(os);
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf 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);
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class AbstractJwsMultipartSignatureFilter method getAttachmentParts.
protected List<Object> getAttachmentParts(Object rootEntity) {
final List<Object> parts;
if (rootEntity instanceof MultipartBody) {
parts = CastUtils.cast(((MultipartBody) rootEntity).getAllAttachments());
} else {
if (rootEntity instanceof List) {
List<Object> entityList = CastUtils.cast((List<?>) rootEntity);
parts = new ArrayList<>(entityList);
} else {
parts = new ArrayList<>(2);
parts.add(rootEntity);
}
}
JwsHeaders headers = new JwsHeaders();
headers.setPayloadEncodingStatus(false);
JwsSignatureProvider theSigProvider = sigProvider != null ? sigProvider : JwsUtils.loadSignatureProvider(headers, true);
JwsSignature jwsSignature = theSigProvider.createJwsSignature(headers);
String base64UrlEncodedHeaders = Base64UrlUtility.encode(writer.toJson(headers));
byte[] headerBytesWithDot = StringUtils.toBytesASCII(base64UrlEncodedHeaders + '.');
jwsSignature.update(headerBytesWithDot, 0, headerBytesWithDot.length);
AttachmentUtils.addMultipartOutFilter(new JwsMultipartSignatureOutFilter(jwsSignature));
JwsDetachedSignature jws = new JwsDetachedSignature(headers, base64UrlEncodedHeaders, jwsSignature, useJwsJsonSignatureFormat);
Attachment jwsPart = new Attachment("signature", JoseConstants.MEDIA_TYPE_JOSE, jws);
parts.add(jwsPart);
return parts;
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class JwsMultipartSignatureOutFilter method filter.
@Override
public void filter(List<Attachment> parts) {
for (int i = 0; i < parts.size() - 1; i++) {
Attachment dataPart = parts.get(i);
DataHandler handler = dataPart.getDataHandler();
dataPart.setDataHandler(new JwsSignatureDataHandler(handler));
}
}
Aggregations