Top Description Inners Fields Constructors Methods
org.springframework.web.servlet.mvc.condition

public final Class ProducesRequestCondition

extends AbstractRequestCondition<ProducesRequestCondition>
Class Inheritance
Imports
java.util.ArrayList, .Collections, .LinkedHashSet, .List, .Set, jakarta.servlet.http.HttpServletRequest, org.springframework.http.MediaType, org.springframework.lang.Nullable, org.springframework.util.CollectionUtils, .MimeType, .ObjectUtils, org.springframework.web.HttpMediaTypeException, .HttpMediaTypeNotAcceptableException, org.springframework.web.accept.ContentNegotiationManager, org.springframework.web.bind.annotation.RequestMapping, org.springframework.web.context.request.ServletWebRequest, org.springframework.web.cors.CorsUtils, org.springframework.web.servlet.mvc.condition.HeadersRequestCondition.HeaderExpression

A logical disjunction (' || ') request condition to match a request's 'Accept' header to a list of media type expressions. Two kinds of media type expressions are supported, which are described in RequestMapping#produces() and RequestMapping#headers() where the header name is 'Accept'. Regardless of which syntax is used, the semantics are the same.
Authors
Arjen Poutsma, Rossen Stoyanchev
Since
3.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
ProducesRequestCondition.ProduceMediaTypeExpression

Parses and matches a single media type expression to a request's 'Accept' header.

Field Summary

Modifier and TypeField and Description
private final ContentNegotiationManager
private static final ContentNegotiationManager
private static final ProducesRequestCondition
private final List<ProducesRequestCondition.ProduceMediaTypeExpression>
private static final List<ProducesRequestCondition.ProduceMediaTypeExpression>
private static final String

Constructor Summary

AccessConstructor and Description
public
ProducesRequestCondition(String...
expressions with syntax defined by RequestMapping#produces()
produces
)

Creates a new instance from "produces" expressions.

public
ProducesRequestCondition(String[]
expressions with syntax defined by RequestMapping#produces()
produces
,
String[]
expressions with syntax defined by RequestMapping#headers()
headers
)

Creates a new instance with "produces" and "header" expressions.

public
ProducesRequestCondition(String[]
expressions with syntax defined by RequestMapping#produces()
produces
,
String[]
expressions with syntax defined by RequestMapping#headers()
headers
,
ContentNegotiationManager
used to determine requested media types
manager
)

Same as ProducesRequestCondition(String[], String[]) but also accepting a ContentNegotiationManager.

private
ProducesRequestCondition(List<ProducesRequestCondition.ProduceMediaTypeExpression> expressions, ProducesRequestCondition other)

Private constructor for internal use to create matching conditions.

Method Summary

Modifier and TypeMethod and Description
public static void
clearMediaTypesAttribute(HttpServletRequest
the current request
request
)

Use this to clear MEDIA_TYPES_ATTRIBUTE that contains the parsed, requested media types.

public ProducesRequestCondition
combine(ProducesRequestCondition
the condition to combine with.
other
)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

Returns the "other" instance if it has any expressions; returns "this" instance otherwise.

private int
compareMatchingMediaTypes(ProducesRequestCondition condition1, int index1, ProducesRequestCondition condition2, int index2)

public int
compareTo(ProducesRequestCondition other, HttpServletRequest request)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.compareTo.

Compares this and another "produces" condition as follows:

  1. Sort 'Accept' header media types by quality value via org.springframework.util.MimeTypeUtils#sortBySpecificity(List) and iterate the list.
  2. Get the first index of matching media types in each "produces" condition first matching with MediaType#equals(Object) and then with MediaType#includes(MediaType).
  3. If a lower index is found, the condition at that index wins.
  4. If both indexes are equal, the media types at the index are compared further with MediaType#isMoreSpecific(MimeType).
private List<MediaType>
protected List<ProducesRequestCondition.ProduceMediaTypeExpression>
getContent()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getContent.

Return the discrete items a request condition is composed of.

public Set<MediaTypeExpression>
getExpressions()

Return the contained "produces" expressions.

private List<ProducesRequestCondition.ProduceMediaTypeExpression>
getExpressionsToCompare()

Return the contained "produces" expressions or if that's empty, a list with a */* expression.

public ProducesRequestCondition

Returns:

the same instance if there are no expressions; or a new condition with matching expressions; or null if no expressions match.
getMatchingCondition
(HttpServletRequest
the current request
request
)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.getMatchingCondition.

Checks if any of the contained media type expressions match the given request 'Content-Type' header and returns an instance that is guaranteed to contain matching expressions only.

private List<ProducesRequestCondition.ProduceMediaTypeExpression>
getMatchingExpressions(List<MediaType> acceptedMediaTypes)

public Set<MediaType>
getProducibleMediaTypes()

Return the contained producible media types excluding negated expressions.

protected String
getToStringInfix()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getToStringInfix.

The notation to use when printing discrete items of content.

private int
private int
public boolean
isEmpty()

Overrides org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.isEmpty.

Whether the condition has any media type expressions.

private List<ProducesRequestCondition.ProduceMediaTypeExpression>
parseExpressions(String[] produces, String[] headers)

Inherited from org.springframework.web.servlet.mvc.condition.AbstractRequestCondition:
equalshashCodetoString

Field Detail

contentNegotiationManagerback to summary
private final ContentNegotiationManager contentNegotiationManager
DEFAULT_CONTENT_NEGOTIATION_MANAGERback to summary
private static final ContentNegotiationManager DEFAULT_CONTENT_NEGOTIATION_MANAGER
EMPTY_CONDITIONback to summary
private static final ProducesRequestCondition EMPTY_CONDITION
expressionsback to summary
private final List<ProducesRequestCondition.ProduceMediaTypeExpression> expressions
MEDIA_TYPE_ALL_LISTback to summary
private static final List<ProducesRequestCondition.ProduceMediaTypeExpression> MEDIA_TYPE_ALL_LIST
MEDIA_TYPES_ATTRIBUTEback to summary
private static final String MEDIA_TYPES_ATTRIBUTE

Constructor Detail

ProducesRequestConditionback to summary
public ProducesRequestCondition(String... produces)

Creates a new instance from "produces" expressions. If 0 expressions are provided in total, this condition will match to any request.

Parameters
produces:String[]

expressions with syntax defined by RequestMapping#produces()

ProducesRequestConditionback to summary
public ProducesRequestCondition(String[] produces, String[] headers)

Creates a new instance with "produces" and "header" expressions. "Header" expressions where the header name is not 'Accept' or have no header value defined are ignored. If 0 expressions are provided in total, this condition will match to any request.

Parameters
produces:String[]

expressions with syntax defined by RequestMapping#produces()

headers:String[]

expressions with syntax defined by RequestMapping#headers()

ProducesRequestConditionback to summary
public ProducesRequestCondition(String[] produces, String[] headers, ContentNegotiationManager manager)

Same as ProducesRequestCondition(String[], String[]) but also accepting a ContentNegotiationManager.

Parameters
produces:String[]

expressions with syntax defined by RequestMapping#produces()

headers:String[]

expressions with syntax defined by RequestMapping#headers()

manager:ContentNegotiationManager

used to determine requested media types

ProducesRequestConditionback to summary
private ProducesRequestCondition(List<ProducesRequestCondition.ProduceMediaTypeExpression> expressions, ProducesRequestCondition other)

Private constructor for internal use to create matching conditions. Note the expressions List is neither sorted nor deep copied.

Method Detail

clearMediaTypesAttributeback to summary
public static void clearMediaTypesAttribute(HttpServletRequest request)

Use this to clear MEDIA_TYPES_ATTRIBUTE that contains the parsed, requested media types.

Parameters
request:HttpServletRequest

the current request

Since
5.2
combineback to summary
public ProducesRequestCondition combine(ProducesRequestCondition other)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

Returns the "other" instance if it has any expressions; returns "this" instance otherwise. Practically that means a method-level "produces" overrides a type-level "produces" condition.

Parameters
other:ProducesRequestCondition

Doc from org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

the condition to combine with.

Returns:ProducesRequestCondition

Doc from org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

a request condition instance that is the result of combining the two condition instances.

Annotations
@Override
compareMatchingMediaTypesback to summary
private int compareMatchingMediaTypes(ProducesRequestCondition condition1, int index1, ProducesRequestCondition condition2, int index2)
compareToback to summary
public int compareTo(ProducesRequestCondition other, HttpServletRequest request)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.compareTo.

Compares this and another "produces" condition as follows:

  1. Sort 'Accept' header media types by quality value via org.springframework.util.MimeTypeUtils#sortBySpecificity(List) and iterate the list.
  2. Get the first index of matching media types in each "produces" condition first matching with MediaType#equals(Object) and then with MediaType#includes(MediaType).
  3. If a lower index is found, the condition at that index wins.
  4. If both indexes are equal, the media types at the index are compared further with MediaType#isMoreSpecific(MimeType).

It is assumed that both instances have been obtained via getMatchingCondition(HttpServletRequest) and each instance contains the matching producible media type expression only or is otherwise empty.

Annotations
@Override
getAcceptedMediaTypesback to summary
private List<MediaType> getAcceptedMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException
Annotations
@SuppressWarnings:unchecked
getContentback to summary
protected List<ProducesRequestCondition.ProduceMediaTypeExpression> getContent()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getContent.

Doc from org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getContent.

Return the discrete items a request condition is composed of.

For example URL patterns, HTTP request methods, param expressions, etc.

Returns:List<ProducesRequestCondition.ProduceMediaTypeExpression>

a collection of objects (never null)

Annotations
@Override
getExpressionsback to summary
public Set<MediaTypeExpression> getExpressions()

Return the contained "produces" expressions.

getExpressionsToCompareback to summary
private List<ProducesRequestCondition.ProduceMediaTypeExpression> getExpressionsToCompare()

Return the contained "produces" expressions or if that's empty, a list with a */* expression.

getMatchingConditionback to summary
public ProducesRequestCondition getMatchingCondition(HttpServletRequest request)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.getMatchingCondition.

Checks if any of the contained media type expressions match the given request 'Content-Type' header and returns an instance that is guaranteed to contain matching expressions only. The match is performed via MediaType#isCompatibleWith(MediaType).

Parameters
request:HttpServletRequest

the current request

Returns:ProducesRequestCondition

the same instance if there are no expressions; or a new condition with matching expressions; or null if no expressions match.

Annotations
@Override
@Nullable
getMatchingExpressionsback to summary
private List<ProducesRequestCondition.ProduceMediaTypeExpression> getMatchingExpressions(List<MediaType> acceptedMediaTypes)
Annotations
@Nullable
getProducibleMediaTypesback to summary
public Set<MediaType> getProducibleMediaTypes()

Return the contained producible media types excluding negated expressions.

getToStringInfixback to summary
protected String getToStringInfix()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getToStringInfix.

Doc from org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getToStringInfix.

The notation to use when printing discrete items of content.

For example " || " for URL patterns or " && " for param expressions.

Annotations
@Override
indexOfEqualMediaTypeback to summary
private int indexOfEqualMediaType(MediaType mediaType)
indexOfIncludedMediaTypeback to summary
private int indexOfIncludedMediaType(MediaType mediaType)
isEmptyback to summary
public boolean isEmpty()

Overrides org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.isEmpty.

Whether the condition has any media type expressions.

Returns:boolean

Doc from org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.isEmpty.

true if empty; false otherwise

Annotations
@Override
parseExpressionsback to summary
private List<ProducesRequestCondition.ProduceMediaTypeExpression> parseExpressions(String[] produces, String[] headers)
org.springframework.web.servlet.mvc.condition back to summary

pack-priv Class ProducesRequestCondition.ProduceMediaTypeExpression

extends AbstractMediaTypeExpression
Class Inheritance

Parses and matches a single media type expression to a request's 'Accept' header.

Constructor Summary

AccessConstructor and Description
pack-priv
ProduceMediaTypeExpression(MediaType mediaType, boolean negated)

pack-priv

Method Summary

Modifier and TypeMethod and Description
public final boolean
match(List<MediaType> acceptedMediaTypes)

private boolean
matchMediaType(List<MediaType> acceptedMediaTypes)

Inherited from org.springframework.web.servlet.mvc.condition.AbstractMediaTypeExpression:
compareToequalsgetMediaTypehashCodeisNegatedmatchParameterstoString

Constructor Detail

ProduceMediaTypeExpressionback to summary
pack-priv ProduceMediaTypeExpression(MediaType mediaType, boolean negated)
ProduceMediaTypeExpressionback to summary
pack-priv ProduceMediaTypeExpression(String expression)

Method Detail

matchback to summary
public final boolean match(List<MediaType> acceptedMediaTypes)
matchMediaTypeback to summary
private boolean matchMediaType(List<MediaType> acceptedMediaTypes)