use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody in project tomee 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.MultipartBody in project tomee 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.MultipartBody in project tika by apache.
the class GrobidRESTParser method parse.
public void parse(String filePath, ContentHandler handler, Metadata metadata, ParseContext context) throws FileNotFoundException {
File pdfFile = new File(filePath);
ContentDisposition cd = new ContentDisposition("form-data; name=\"input\"; filename=\"" + pdfFile.getName() + "\"");
Attachment att = new Attachment("input", new FileInputStream(pdfFile), cd);
MultipartBody body = new MultipartBody(att);
Response response = WebClient.create(restHostUrlStr + GROBID_PROCESSHEADER_PATH).accept(MediaType.APPLICATION_XML).type(MediaType.MULTIPART_FORM_DATA).post(body);
try {
String resp = response.readEntity(String.class);
Metadata teiMet = new TEIParser().parse(resp);
for (String key : teiMet.names()) {
metadata.add("grobid:header_" + key, teiMet.get(key));
}
} catch (Exception e) {
LOG.warn("Couldn't read response", e);
}
}
use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody in project tomee 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 tomee by apache.
the class FormEncodingProvider method populateMap.
/**
* Retrieve map of parameters from the passed in message
*/
protected void populateMap(MultivaluedMap<String, String> params, Annotation[] anns, InputStream is, MediaType mt, boolean decode) {
if (mt.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)) {
MultipartBody body = AttachmentUtils.getMultipartBody(mc, attachmentDir, attachmentThreshold, attachmentMaxSize);
FormUtils.populateMapFromMultipart(params, body, PhaseInterceptorChain.getCurrentMessage(), decode);
} else {
String enc = HttpUtils.getEncoding(mt, StandardCharsets.UTF_8.name());
Object servletRequest = mc != null ? mc.getHttpServletRequest() : null;
if (servletRequest == null) {
FormUtils.populateMapFromString(params, PhaseInterceptorChain.getCurrentMessage(), FormUtils.readBody(is, enc), enc, decode);
} else {
FormUtils.populateMapFromString(params, PhaseInterceptorChain.getCurrentMessage(), FormUtils.readBody(is, enc), enc, decode, (javax.servlet.http.HttpServletRequest) servletRequest);
}
}
}
Aggregations