diff --git a/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java b/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java index 7a5ad4a7..f29138fb 100644 --- a/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java +++ b/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java @@ -17,6 +17,7 @@ import cz.cvut.kbss.study.util.Constants; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import org.apache.commons.collections4.EnumerationUtils; import org.springframework.context.ApplicationEventPublisher; import org.springframework.core.io.InputStreamResource; import org.springframework.data.domain.Page; @@ -27,9 +28,7 @@ import org.springframework.web.util.UriComponentsBuilder; import java.io.InputStream; -import java.util.Comparator; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.stream.Stream; @RestController @@ -67,15 +66,21 @@ public ResponseEntity exportRecords( @RequestParam(name = "institution", required = false) String institutionKey, @RequestParam(required = false) MultiValueMap params, UriComponentsBuilder uriBuilder, HttpServletRequest request, HttpServletResponse response) { + MediaType exportType = Stream.of( - Optional.ofNullable(params).map(p -> p.getFirst(Constants.EXPORT_TYPE_PARAM)), - Optional.ofNullable(request.getHeader(HttpHeaders.ACCEPT)) - ).filter(Optional::isPresent) + Optional.ofNullable(params).map(p -> p.get(Constants.EXPORT_TYPE_PARAM)), + Optional.of(Collections.list(request.getHeaders(HttpHeaders.ACCEPT))) + ) + .map(o -> o.filter(l -> !l.isEmpty())) + .filter(Optional::isPresent) + .map(o -> o.map(l -> l.stream().flatMap(s -> + MediaType.parseMediaTypes(s).stream() + .filter(RestUtils::isSupportedExportType) + ).max(Comparator.comparing(MediaType::getQualityValue)).orElse(null))) + .filter(Optional::isPresent) + .map(o -> o.orElse(null)) .findFirst() - .orElse(Optional.empty()) - .flatMap(s -> MediaType.parseMediaTypes(s).stream() - .max(Comparator.comparing(MediaType::getQualityValue)) - ).orElse(MediaType.APPLICATION_JSON) + .orElse(MediaType.APPLICATION_JSON) .removeQualityValue(); return switch (exportType.toString()){ diff --git a/src/main/java/cz/cvut/kbss/study/rest/util/RestUtils.java b/src/main/java/cz/cvut/kbss/study/rest/util/RestUtils.java index 6848f18c..4d629afe 100644 --- a/src/main/java/cz/cvut/kbss/study/rest/util/RestUtils.java +++ b/src/main/java/cz/cvut/kbss/study/rest/util/RestUtils.java @@ -8,6 +8,7 @@ import org.springframework.data.domain.Sort; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.util.MultiValueMap; import org.springframework.web.server.ResponseStatusException; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; @@ -17,11 +18,18 @@ import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.format.DateTimeParseException; +import java.util.Arrays; +import java.util.List; import java.util.Optional; import java.util.stream.Collectors; public class RestUtils { + /** + * Supported export media types + */ + public static List SUPPORTED_EXPORT_MDEIA_TYPES = Arrays.asList(Constants.MEDIA_TYPE_EXCEL, MediaType.APPLICATION_JSON_VALUE); + /** * Prefix indicating ascending sort order. */ @@ -146,4 +154,8 @@ public static Pageable resolvePaging(MultiValueMap params) { } return PageRequest.of(page, size); } + + public static boolean isSupportedExportType(MediaType mt){ + return SUPPORTED_EXPORT_MDEIA_TYPES.stream().filter(s -> mt.toString().contains(s)).findAny().isPresent(); + } }