@Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        if (!requestContext.getUriInfo().getPath().endsWith(targetPath)) {
            return;
        }
        final List<MediaType> acceptedFormats = requestContext.getAcceptableMediaTypes();

        final Map<MediaType, ExportFormat> exportFormatCandidates = supportedFormats.entrySet()
                .stream()
                .filter(entry -> acceptedFormats.stream().anyMatch(acceptedFormat -> entry.getKey().isCompatible(acceptedFormat)))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

        if (exportFormatCandidates.isEmpty()) {
            requestContext.abortWith(Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).build());
            return;
        }

        final Map<MediaType, Optional<String>> candidateErrors = exportFormatCandidates.entrySet()
                .stream()
                .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().hasError()));

        if (candidateErrors.values().stream().allMatch(Optional::isPresent)) {
            final String errorMessage = candidateErrors.values().stream()
                    .map(optionalError -> optionalError.orElse(""))
                    .collect(Collectors.joining("\n"));
            requestContext.abortWith(Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
                    .entity(errorMessage)
                    .type(MoreMediaTypes.TEXT_PLAIN_TYPE)
                    .build());
            return;
        }

        final List<String> allowedMediaTypes = candidateErrors.entrySet().stream()
                .filter(entry -> !entry.getValue().isPresent())
                .map(Map.Entry::getKey)
                .map(MediaType::toString)
                .collect(Collectors.toList());

        requestContext.getHeaders().put(HttpHeaders.ACCEPT, allowedMediaTypes);
    }
@Test
    void doesNothingIfRequestPathDoesNotMatch() throws Exception {
        final ContainerRequestFilter filter = new MessageExportFormatFilter(Collections.emptySet());
        final ContainerRequestContext requestContext = mockRequestContextForNonMatchingPath();

        filter.filter(requestContext);

        verifyRequestNotAborted(requestContext);
    }