Class ContentTypeUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontainsGlobalAccept(Collection<String> apiContentTypes) Return whether the given content types includes a "global match" wildcardextractMultipartBoundary(String multipartContentType) Extracts boundary from multipart/form-data content typefindMostSpecificMatch(Request request, Set<String> apiContentTypes) Find the content-type that most specifically matches the content-type defined on the given request.findMostSpecificMatch(Response response, Set<String> apiContentTypes) Find the content-type that most specifically matches the content-type defined on the given response.findMostSpecificMatch(String candidate, Set<String> apiContentTypes) Find the content-type that most specifically matches the given candidate content type.getCharsetFromContentType(com.google.common.collect.Multimap<String, String> headers) Resolves the content-type of the given headers, if it is defined.getCharsetFromContentType(String contentType) Extract and return the charset from the given content-type, if it is defined.static booleanhasContentType(Request request) Determine whether a given request has a content-type header.static booleanhasContentType(Response response) Determine whether a given request has a content-type header.static booleanisFormDataContentType(Request request) Determine whether a given request has a formdata content-type.static booleanisFormDataContentType(Response response) Determine whether a given response has a formdata content-type.static booleanisFormDataContentType(String contentType) static booleanisJsonContentType(Request request) Determine whether a given request has a JSON content-type.static booleanisJsonContentType(Response response) static booleanisJsonContentType(String contentType) static booleanisMultipartContentTypeAcceptedByConsumer(String requestContentType, String consumesContentType) Checks if the content type of a multipart/form-data request matches the consumer's content type.static booleanisMultipartFormDataContentType(String contentType) static booleanstatic booleanmatchesAny(com.google.common.net.MediaType candidate, Collection<String> apiContentTypes) Returns whether the candidate media type matches any of the applied API content type expressions.static booleanmatchesAny(String candidate, Collection<String> apiContentTypes) Returns whether the candidate media type matches any of the applied API content type expressions.
-
Method Details
-
isJsonContentType
Determine whether a given request has a JSON content-type.- Returns:
- Whether the content-type of the request (defined in the Content-Type header) is a JSON type.
-
isJsonContentType
- Returns:
- Whether the content-type of this response (defined in the Content-Type header) is a JSON type.
-
isJsonContentType
- Returns:
- Whether the provided content-type is a JSON type (includes JSON suffix).
-
isFormDataContentType
Determine whether a given request has a formdata content-type.- Returns:
- Whether the content-type of the request (defined in the Content-Type header) is a FORM_DATA type.
-
isFormDataContentType
Determine whether a given response has a formdata content-type.- Returns:
- Whether the content-type of the response (defined in the Content-Type header) is a FORM_DATA type.
-
isFormDataContentType
- Returns:
- Whether the provided content-type is a form data type.
-
isMultipartFormDataContentType
- Returns:
- Whether the provided content-type is a multi-part form data type.
-
matches
-
isMultipartContentTypeAcceptedByConsumer
public static boolean isMultipartContentTypeAcceptedByConsumer(@Nullable String requestContentType, @Nullable String consumesContentType) Checks if the content type of a multipart/form-data request matches the consumer's content type.- Parameters:
requestContentType- content-type of a requestconsumesContentType- content-type that the API consumes
-
extractMultipartBoundary
Extracts boundary from multipart/form-data content type- Parameters:
multipartContentType- a multipart form data content type, e.g. "multipart/form-data; boundary=blah"- Returns:
- the boundary value (blah from the example above) or Optional.empty() if absent
-
hasContentType
Determine whether a given request has a content-type header.- Returns:
- Whether a content-type header is defined on the request
-
hasContentType
Determine whether a given request has a content-type header.- Returns:
- Whether a content-type header is defined on the response
-
findMostSpecificMatch
public static Optional<String> findMostSpecificMatch(Response response, Set<String> apiContentTypes) Find the content-type that most specifically matches the content-type defined on the given response.e.g. If the response has
Content-Type=text/plainand the list of types is[text/*, */*, text/plain](all of which could match), the most specific matchtext/plainwill be returned.If there are no matches, will return empty.
- Parameters:
response- The response to find a matching content type forapiContentTypes- The list of content types to search- Returns:
- The most specific content type that matches the given request, or empty if none match.
-
findMostSpecificMatch
Find the content-type that most specifically matches the content-type defined on the given request.e.g. If the response has
Content-Type=text/plainand the list of types is[text/*, */*, text/plain](all of which could match), the most specific matchtext/plainwill be returned.If there are no matches, will return empty.
- Parameters:
request- The request to find a matching content type forapiContentTypes- The list of content types to search- Returns:
- The most specific content type that matches the given request, or empty if none match.
-
findMostSpecificMatch
Find the content-type that most specifically matches the given candidate content type.e.g. If the candidate is
text/plainand the list of types is[text/*, */*, text/plain](all of which could match), the most specific matchtext/plainwill be returned.If there are no matches, will return empty.
- Parameters:
candidate- The response to find a matching content type forapiContentTypes- The list of content types to search- Returns:
- The most specific content type that matches the given request, or empty if none match.
-
matchesAny
Returns whether the candidate media type matches any of the applied API content type expressions.Supports matching against media type ranges e.g. "text/*". Note that if the global match "*/*" is included will always return
true.- Parameters:
candidate- The candidate type to match (e.g. from the request or response header)apiContentTypes- The content types defined in the API to match against. Can be media type ranges e.g. "text/*".- Returns:
trueif the candidate matches against any of the provided API-defined content types.
-
matchesAny
public static boolean matchesAny(com.google.common.net.MediaType candidate, Collection<String> apiContentTypes) Returns whether the candidate media type matches any of the applied API content type expressions.Supports matching against media type ranges e.g. "text/*". Note that if the global match "*/*" is included will always return
true.- Parameters:
candidate- The candidate type to match (e.g. from the request or response header)apiContentTypes- The content types defined in the API to match against. Can be media type ranges e.g. "text/*".- Returns:
trueif the candidate matches against any of the provided API-defined content types.
-
getCharsetFromContentType
Extract and return the charset from the given content-type, if it is defined.If no content-type is provided, or no charset is defined in the content-type, will return
empty- Parameters:
contentType- The content-type value to extract the charset from- Returns:
- The charset of the content-type, or
emptyif none is defined.
-
getCharsetFromContentType
public static Optional<Charset> getCharsetFromContentType(@Nullable com.google.common.collect.Multimap<String, String> headers) Resolves the content-type of the given headers, if it is defined. And then extracts and returns the charset from the found content-type header.If no content-type is provided, or no charset is defined in the content-type, will return
empty- Parameters:
headers- the headers that contain the content-type header- Returns:
- The charset of the content-type, or
emptyif none is defined.
-
containsGlobalAccept
Return whether the given content types includes a "global match" wildcard- Parameters:
apiContentTypes- The content types to check- Returns:
trueif at least one entry in the given content types is the global match
-