|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.security.web.util.MediaTypeRequestMatcher
public final class MediaTypeRequestMatcher
Allows matching HttpServletRequest
based upon the MediaType
's
resolved from a ContentNegotiationStrategy
.
By default, the matching process will perform the following:
ContentNegotiationStrategy
will resolve the MediaType
's for the current requestMediaType
instances resolved from the
ContentNegotiationStrategy
.MediaType
returned from the ContentNegotiationStrategy
, then
it returns trueGET / Accept: application/json ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); assert matcher.matches(request) == true // returns trueThe following will also return true
GET / Accept: */* ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); assert matcher.matches(request) == true // returns true
GET / Accept: */* ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); assert matcher.matches(request) == false // returns false
GET / Accept: application/json ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); assert matcher.matches(request) == true // returns true
MediaType
discovered by
ContentNegotiationStrategy
returns true for
MediaType.isCompatibleWith(MediaType)
on the matchingMediaTypes, the
result of the match is true. However, sometimes you may want to perform an
exact match. This can be done with the following examples:
GET / Accept: application/json ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setUseEquals(true); assert matcher.matches(request) == true // returns true
GET / Accept: application/* ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setUseEquals(true); assert matcher.matches(request) == false // returns false
GET / Accept: */* ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setUseEquals(true); assert matcher.matches(request) == false // returns false
Constructor Summary | |
---|---|
MediaTypeRequestMatcher(org.springframework.web.accept.ContentNegotiationStrategy contentNegotiationStrategy,
Collection<MediaType> matchingMediaTypes)
Creates an instance |
|
MediaTypeRequestMatcher(org.springframework.web.accept.ContentNegotiationStrategy contentNegotiationStrategy,
MediaType... matchingMediaTypes)
Creates an instance |
Method Summary | |
---|---|
boolean |
matches(javax.servlet.http.HttpServletRequest request)
Decides whether the rule implemented by the strategy matches the supplied request. |
void |
setIgnoredMediaTypes(Set<MediaType> ignoredMediaTypes)
Set the MediaType to ignore from the
ContentNegotiationStrategy . |
void |
setUseEquals(boolean useEquals)
If set to true, matches on exact MediaType , else uses
MediaType.isCompatibleWith(MediaType) . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public MediaTypeRequestMatcher(org.springframework.web.accept.ContentNegotiationStrategy contentNegotiationStrategy, MediaType... matchingMediaTypes)
contentNegotiationStrategy
- the ContentNegotiationStrategy
to usematchingMediaTypes
- the MediaType
that will make the RequestMatcher
return truepublic MediaTypeRequestMatcher(org.springframework.web.accept.ContentNegotiationStrategy contentNegotiationStrategy, Collection<MediaType> matchingMediaTypes)
contentNegotiationStrategy
- the ContentNegotiationStrategy
to usematchingMediaTypes
- the MediaType
that will make the RequestMatcher
return trueMethod Detail |
---|
public boolean matches(javax.servlet.http.HttpServletRequest request)
RequestMatcher
matches
in interface RequestMatcher
request
- the request to check for a match
public void setUseEquals(boolean useEquals)
MediaType
, else uses
MediaType.isCompatibleWith(MediaType)
.
useEquals
- specify if equals comparison should be used.public void setIgnoredMediaTypes(Set<MediaType> ignoredMediaTypes)
MediaType
to ignore from the
ContentNegotiationStrategy
. This is useful if for example, you
want to match on MediaType.APPLICATION_JSON
but want to ignore
MediaType.ALL
.
ignoredMediaTypes
- the MediaType
's to ignore from the
ContentNegotiationStrategy
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |