Spring content negotiation - one mime type to extend another

228 Views Asked by At

I'm using Spring Boot with header based content negotiation. I have one set of endpoints that are only available to a subset of clients, and the rest are public. My public endpoints are annotated like this:

@PostMapping(consumes = "application/vnd.com.foo+json", produces = "application/vnd.com.foo+json")

and my private ones like this:

@PostMapping(consumes = "application/vnd.com.foo.private+json", produces = "application/vnd.com.foo.private+json")

I want the public endpoints to also consume and produce the private mime types, so that my private clients can just set that mime type on all their requests. Obviously, I can do that by explicitly specifying it on all my public endpoints:

@PostMapping(consumes = {"application/vnd.com.foo+json", "application/vnd.com.foo.private+json"}, 
    produces = {"application/vnd.com.foo+json", "application/vnd.com.foo.private+json"})

but I'd like a neater way to do this.

Is there some way to configure Spring to treat the private mime type as if it 'extends' the public one?

2

There are 2 best solutions below

0
On

I can configure content negotiation in my WebMvcConfigurer bean:

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.strategies(List.of(new HeaderContentNegotiationStrategy() {
        @Override
        public List<MediaType> resolveMediaTypes(NativeWebRequest request) throws HttpMediaTypeNotAcceptableException {
            List<MediaType> mediaTypes = super.resolveMediaTypes(request);
            List<MediaType> publicisedInternalMediaTypes = mediaTypes.stream()
                    .filter(m -> m.getSubtype().startsWith("vnd.com.foo.private"))
                    .map(m -> new MediaType(
                            m.getType(),
                            m.getSubtype().replace("vnd.com.foo.private", "vnd.com.foo"),
                            m.getParameters()))
                    .collect(toList());
            mediaTypes.addAll(publicisedInternalMediaTypes);
            return mediaTypes;
        }
    }));
}

That seems to work so far, but I'd be interested to see a more standard or elegant way to achieve this.

Edit: This worked for the Accept header, but not Content-Type.

0
On

In my DelegatingWebMvcConfiguration, I can override createRequestMappingHandlerMapping() to add private consumes and produces mappings to add the public mappings.

@Override
protected RequestMappingInfo createRequestMappingInfo(
        RequestMapping original, RequestCondition<?> customCondition) {
    ExpandedRequestMapping expanded = expandRequestMapping(original);
    return super.createRequestMappingInfo(expanded, customCondition);
}

/**
 * If the produces or consume fields include a public media type, 
 * add the corresponding private media type, too.
 */
private ExpandedRequestMapping expandRequestMapping(RequestMapping original) {
    return new ExpandedRequestMapping(original);
}

private static String[] expandedMediaTypes(String[] originalArray) {
    // If we ever have a controller method that produces or consumes multiple public types,
    // then we'll need to change this.
    return Arrays.stream(originalArray)
            .filter(mediaType -> mediaType.contains(VND_PUBLIC))
            .findAny()
            .map(publicMediaType -> publicMediaType.replace(VND_PUBLIC, VND_PRIVATE))
            .map(privateMediaType -> ArrayUtils.addAll(originalArray, privateMediaType))
            .orElse(originalArray);
}

static class ExpandedRequestMapping implements RequestMapping {

    private final RequestMapping delegate;

    private ExpandedRequestMapping(RequestMapping delegate) {
        this.delegate = delegate;
    }

    @Override
    public String[] consumes() {
        return expandedMediaTypes(delegate.consumes());
    }

    @Override
    public String[] produces() {
        return expandedMediaTypes(delegate.produces());
    }

    // delegate other methods
}