Class ContentTypeUtils

java.lang.Object
com.atlassian.oai.validator.util.ContentTypeUtils

public class ContentTypeUtils extends Object
  • Method Details

    • isJsonContentType

      public static boolean isJsonContentType(Request request)
      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

      public static boolean isJsonContentType(Response response)
      Returns:
      Whether the content-type of this response (defined in the Content-Type header) is a JSON type.
    • isJsonContentType

      public static boolean isJsonContentType(@Nullable String contentType)
      Returns:
      Whether the provided content-type is a JSON type (includes JSON suffix).
    • isFormDataContentType

      public static boolean isFormDataContentType(Request request)
      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

      public static boolean isFormDataContentType(Response response)
      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

      public static boolean isFormDataContentType(@Nullable String contentType)
      Returns:
      Whether the provided content-type is a form data type.
    • isMultipartFormDataContentType

      public static boolean isMultipartFormDataContentType(@Nullable String contentType)
      Returns:
      Whether the provided content-type is a multi-part form data type.
    • matches

      public static boolean matches(@Nullable String contentType, com.google.common.net.MediaType expected)
    • 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 request
      consumesContentType - content-type that the API consumes
    • extractMultipartBoundary

      @Nonnull public static Optional<String> extractMultipartBoundary(String multipartContentType)
      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

      public static boolean hasContentType(Request request)
      Determine whether a given request has a content-type header.
      Returns:
      Whether a content-type header is defined on the request
    • hasContentType

      public static boolean hasContentType(Response response)
      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/plain and the list of types is [text/*, */*, text/plain] (all of which could match), the most specific match text/plain will be returned.

      If there are no matches, will return empty.

      Parameters:
      response - The response to find a matching content type for
      apiContentTypes - The list of content types to search
      Returns:
      The most specific content type that matches the given request, or empty if none match.
    • findMostSpecificMatch

      public static Optional<String> findMostSpecificMatch(Request request, Set<String> apiContentTypes)
      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/plain and the list of types is [text/*, */*, text/plain] (all of which could match), the most specific match text/plain will be returned.

      If there are no matches, will return empty.

      Parameters:
      request - The request to find a matching content type for
      apiContentTypes - The list of content types to search
      Returns:
      The most specific content type that matches the given request, or empty if none match.
    • findMostSpecificMatch

      public static Optional<String> findMostSpecificMatch(String candidate, Set<String> apiContentTypes)
      Find the content-type that most specifically matches the given candidate content type.

      e.g. If the candidate is text/plain and the list of types is [text/*, */*, text/plain] (all of which could match), the most specific match text/plain will be returned.

      If there are no matches, will return empty.

      Parameters:
      candidate - The response to find a matching content type for
      apiContentTypes - The list of content types to search
      Returns:
      The most specific content type that matches the given request, or empty if none match.
    • matchesAny

      public static boolean matchesAny(String 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:
      true if 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:
      true if the candidate matches against any of the provided API-defined content types.
    • getCharsetFromContentType

      public static Optional<Charset> getCharsetFromContentType(@Nullable String contentType)
      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 empty if 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 empty if none is defined.
    • containsGlobalAccept

      public static boolean containsGlobalAccept(Collection<String> apiContentTypes)
      Return whether the given content types includes a "global match" wildcard
      Parameters:
      apiContentTypes - The content types to check
      Returns:
      true if at least one entry in the given content types is the global match