public void logRequest(ServerRequest request, MultiValueMap<String, ?> map) { if (configHolder.getConfig().getLogging().isHeaders()) { logRequestHeaders(request); } if (configHolder.getConfig().getLogging().isBody()) { map.forEach((key, values) -> values.forEach(value -> { if (value instanceof Part) { val part = (Part) value; part.headers().forEach((headerKey, headerValues) -> headerValues.forEach(headerValue -> log.info("{} [{}] {}: {}", RECEIVED, key, headerKey, headerValue))); } else { log.info("{} {}={}", RECEIVED, key, value); } })); } }
@PostMapping(value = "", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) Mono<String> requestBodyFlux(@RequestBody Flux<Part> parts) { return partFluxDescription(parts); }
@RequestMapping(value = "/post", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public Mono<Map<String, Object>> postFormData(@RequestBody Mono<MultiValueMap<String, Part>> parts) { // StringDecoder decoder = StringDecoder.allMimeTypes(true); return parts.flux().flatMap(map -> Flux.fromIterable(map.values())) .flatMap(map -> Flux.fromIterable(map)) .filter(part -> part instanceof FilePart) .reduce(new HashMap<String, Object>(), (files, part) -> { MediaType contentType = part.headers().getContentType(); long contentLength = part.headers().getContentLength(); files.put(part.name(), "data:"+contentType+";base64,"+contentLength); //TODO: get part data return files; }).map(files -> Collections.singletonMap("files", files)); }
private static String partMapDescription(MultiValueMap<String, Part> partsMap) { return partsMap.keySet().stream().sorted() .map(key -> partListDescription(partsMap.get(key))) .collect(Collectors.joining(",", "Map[", "]")); }
private static Mono<String> partFluxDescription(Flux<? extends Part> partsFlux) { return partsFlux.log().collectList().map(MultipartController::partListDescription); }
private static String partListDescription(List<? extends Part> parts) { return parts.stream().map(MultipartController::partDescription) .collect(Collectors.joining(",", "[", "]")); }
private static String partDescription(Part part) { return part instanceof FilePart ? part.name() + ":" + ((FilePart) part).filename() : part.name(); }