Talespin_Kit
Talespin_Kit

Reputation: 21877

JAX-RS validating Content-Type and Accept header for generic upload and download API

The requirement is to implement a POST /v1/data and GET /v1/data API.

The upload API (POST) can have any Content-Type. This is not a problem as the content type is stored in database along with data.

The download API (GET) should:

  1. set the same Content-Type as the last upload call and
  2. validate with the Content-Type and the Accept headers received in the request.

The problem is in validating the Content-Type with the Accept header. The Accept header can be */*, text/* (partially concrete) or text/plain (completely concrete). If the last uploaded Content-Type is text/plain all the three above Accept headers are valid.

Is there a built in method such as bool validate(accept_header, content_type) which does the validation?

Upvotes: 1

Views: 484

Answers (1)

dur
dur

Reputation: 16969

You can convert a String to a MediaType object with MediaType#valueOf:

Creates a new instance of MediaType by parsing the supplied string.

and check it with MediaType#isCompatible:

Check if this media type is compatible with another media type. E.g. image/* is compatible with image/jpeg, image/png, etc. Media type parameters are ignored. The function is commutative.

Upvotes: 2

Related Questions