public static boolean contains(MultipartInput input, String key) { for (InputPart p : input.getParts()) { String name = MultipartUtils.extractName(p); if (key.equals(name)) { return true; } } return false; }
@SuppressWarnings("unchecked") public static Map<String, Object> getMap(MultipartInput input, String key) { try { for (InputPart p : input.getParts()) { String n = MultipartUtils.extractName(p); if (key.equalsIgnoreCase(n)) { String v = p.getBodyAsString().trim(); if (v.isEmpty()) { return Collections.emptyMap(); } else { return objectMapper.readValue(v, Map.class); } } } } catch (IOException e) { throw new ConcordApplicationException("Error parsing the request", e); } return null; }
private static Map<String, String> getTextFormFields(final String format, final MultipartInput multipartInput, final Set<String> assetNames) throws IOException { Map<String, String> fields = new HashMap<>(); List<InputPart> fieldsParts = multipartInput.getParts().stream() .filter(part -> TEXT_PLAIN_TYPE.isCompatible(part.getMediaType())) .collect(toList()); for (InputPart inputPart : fieldsParts) { Optional<String> maybeContentDisposition = getContentDisposition(inputPart); if (maybeContentDisposition.isPresent()) { Optional<String> maybeFieldName = extractFieldName(format, maybeContentDisposition.get()); if (maybeFieldName.isPresent() && !assetNames.contains(maybeFieldName.get())) { fields.put(maybeFieldName.get(), inputPart.getBodyAsString()); } } } return fields; }
public static String getString(MultipartInput input, String key) { try { for (InputPart p : input.getParts()) { String n = MultipartUtils.extractName(p); if (key.equalsIgnoreCase(n)) { String result = p.getBodyAsString().trim(); if (result.isEmpty()) { return null; } else { return result; } } } } catch (IOException e) { throw new ConcordApplicationException("Error parsing the request", e); } return null; }
public static InputStream getStream(MultipartInput input, String key) { try { for (InputPart p : input.getParts()) { String name = MultipartUtils.extractName(p); if (key.equals(name)) { return p.getBody(InputStream.class, null); } } } catch (IOException e) { throw new ConcordApplicationException("Error parsing the request", e); } return null; }
private static Map<String, InputStreamPartPayload> getAssetsPayloads(final String format, final MultipartInput multipartInput) throws IOException { Map<String, InputStreamPartPayload> payloads = new HashMap<>(); for (InputPart inputPart : multipartInput.getParts()) { Optional<String> maybeContentDisposition = getContentDisposition(inputPart); if (maybeContentDisposition.isPresent()) { String contentDisposition = maybeContentDisposition.get(); Optional<String> maybeFieldName = extractFieldName(format, contentDisposition); if (maybeFieldName.isPresent()) { String fieldName = maybeFieldName.get(); boolean isAsset = fieldName.matches(ASSET_NAME_REGEX); if (isAsset) { Optional<String> maybeFilename = extractFilename(contentDisposition); InputStream inputStream = inputPart.getBody(InputStream.class, null); String filename = maybeFilename.orElse(null); String contentType = inputPart.getMediaType().toString(); InputStreamPartPayload payload = new InputStreamPartPayload(filename, fieldName, inputStream, contentType); payloads.put(fieldName, payload); } } } } return payloads; }
public static Map<String, Object> toMap(MultipartInput input) { Map<String, Object> result = new HashMap<>(); try { for (InputPart p : input.getParts()) { String name = MultipartUtils.extractName(p); if (name == null || name.startsWith("/") || name.contains("..")) { throw new ConcordApplicationException("Invalid attachment name: " + name); } if (name.endsWith(JSON_FIELD_SUFFIX)) { name = name.substring(0, name.length() - JSON_FIELD_SUFFIX.length()); Object v = objectMapper.readValue(p.getBodyAsString(), Object.class); result.put(name, v); } else if (p.getMediaType().isCompatible(MediaType.TEXT_PLAIN_TYPE)) { String currentValue = p.getBodyAsString().trim(); result.compute(name, (k, oldValue) -> computeStringMultipartEntry(oldValue, currentValue)); } else { result.put(name, p.getBody(InputStream.class, null)); } } return result; } catch (IOException e) { throw new ConcordApplicationException("Error parsing the request", e); } }
Path baseDir = ensureBaseDir(); for (InputPart p : input.getParts()) { String name = MultipartUtils.extractName(p); if (name == null || name.startsWith("/") || name.contains("..")) {
List<InputPart> parts = response.getParts();
List<InputPart> parts = response.getParts();