下面列出了javax.ws.rs.core.MediaType#isWildcardSubtype() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private MediaType checkFinalContentType(MediaType mt, List<WriterInterceptor> writers, boolean checkWriters) {
if (checkWriters) {
int mbwIndex = writers.size() == 1 ? 0 : writers.size() - 1;
MessageBodyWriter<Object> writer = ((WriterInterceptorMBW)writers.get(mbwIndex)).getMBW();
Produces pm = writer.getClass().getAnnotation(Produces.class);
if (pm != null) {
List<MediaType> sorted =
JAXRSUtils.sortMediaTypes(JAXRSUtils.getMediaTypes(pm.value()), JAXRSUtils.MEDIA_TYPE_QS_PARAM);
mt = JAXRSUtils.intersectMimeTypes(sorted, mt).get(0);
}
}
if (mt.isWildcardType() || mt.isWildcardSubtype()) {
if ("application".equals(mt.getType()) || mt.isWildcardType()) {
mt = MediaType.APPLICATION_OCTET_STREAM_TYPE;
} else {
throw ExceptionUtils.toNotAcceptableException(null, null);
}
}
return mt;
}
@Override
public MediaType getNormalisedResponseMediaType(HttpServletRequest request) {
// Negotiate Response format
MediaType responseMediaType;
String responseFormat = getResponseFormat(request);
try {
List<MediaType> acceptMT = MediaTypeUtils.parseMediaTypes(responseFormat);
responseMediaType = MediaTypeUtils.getResponseMediaType(allContentTypes, acceptMT);
if (responseMediaType == null) {
throw new CougarValidationException(ServerFaultCode.AcceptTypeNotValid, "Could not agree a response media type");
} else if (responseMediaType.isWildcardType() || responseMediaType.isWildcardSubtype()) {
throw new CougarServiceException(ServerFaultCode.ResponseContentTypeNotValid,
"Service configuration error - response media type must not be a wildcard - " + responseMediaType);
}
} catch (IllegalArgumentException e) {
throw new CougarValidationException(ServerFaultCode.MediaTypeParseFailure, "Unable to parse supplied media types (" + responseFormat + ")",e);
}
return responseMediaType;
}
static boolean supportsContentType(final RDFSyntax syntax, final String contentType) {
if (contentType != null) {
final MediaType mediaType = MediaType.valueOf(contentType);
return mediaType.isCompatible(MediaType.valueOf(syntax.mediaType())) &&
!mediaType.isWildcardSubtype() && !mediaType.isWildcardType();
}
return false;
}
private boolean isEmptyOrContainsSingleWildcardMediaType(List<MediaType> acceptableWriterMediaTypes) {
if (acceptableWriterMediaTypes.isEmpty()) {
return true;
}
if (acceptableWriterMediaTypes.size() == 1) {
MediaType mediaType = acceptableWriterMediaTypes.get(0);
if (mediaType.isWildcardType() && mediaType.isWildcardSubtype()) {
return true;
}
}
return false;
}
@Override
public MediaType getNormalisedRequestMediaType(HttpServletRequest request) {
String contentType = request.getContentType();
if (request.getMethod().equals("POST")) {
if (contentType == null) {
throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type was not specified for deserialisable response");
}
MediaType requestMT;
try {
requestMT = MediaType.valueOf(contentType);
} catch (Exception e) {
throw new CougarValidationException(ServerFaultCode.MediaTypeParseFailure, "Input content type cannot be parsed: " + contentType,e);
}
if (requestMT.isWildcardType() || requestMT.isWildcardSubtype()) {
throw new CougarValidationException(ServerFaultCode.InvalidInputMediaType, "Input content type may not be wildcard: " + requestMT);
}
if (!MediaTypeUtils.isValid(allContentTypes, requestMT)) {
throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type is not valid: " + requestMT);
}
String candidateContentType = requestMT.getType() + "/" + requestMT.getSubtype();
MediaType normalizedMediaType = validContentTypes.get(candidateContentType);
if (normalizedMediaType == null) {
throw new CougarValidationException(ServerFaultCode.FrameworkError, "Input content type " + contentType + " failed to find a normalized type using key " + candidateContentType);
}
return normalizedMediaType;
}
return null;
}
@Override
public int compare(MediaType mediaTypeOne, MediaType mediaTypeTwo) {
if (mediaTypeOne.isWildcardType() && !mediaTypeTwo.isWildcardType()) {
return 1;
}
if (mediaTypeOne.isWildcardSubtype() && !mediaTypeTwo.isWildcardSubtype()) {
return 1;
}
if (!mediaTypeOne.isWildcardType() && mediaTypeTwo.isWildcardType()) {
return -1;
}
if (!mediaTypeOne.isWildcardSubtype() && mediaTypeTwo.isWildcardSubtype()) {
return -1;
}
Matcher extSubtypeMatcherOne = MediaTypeHelper.EXT_SUBTYPE_PATTERN.matcher(mediaTypeOne.getSubtype());
Matcher extSubtypeMatcherTwo = MediaTypeHelper.EXT_SUBTYPE_PATTERN.matcher(mediaTypeTwo.getSubtype());
boolean extSubtypeMatcherOneMatches = extSubtypeMatcherOne.matches();
boolean extSubtypeMatcherTwoMatches = extSubtypeMatcherTwo.matches();
if (extSubtypeMatcherOneMatches && !extSubtypeMatcherTwoMatches) {
return 1;
}
if (!extSubtypeMatcherOneMatches && extSubtypeMatcherTwoMatches) {
return -1;
}
extSubtypeMatcherOne = MediaTypeHelper.EXT_PREFIX_SUBTYPE_PATTERN.matcher(mediaTypeOne.getSubtype());
extSubtypeMatcherTwo = MediaTypeHelper.EXT_PREFIX_SUBTYPE_PATTERN.matcher(mediaTypeTwo.getSubtype());
extSubtypeMatcherOneMatches = extSubtypeMatcherOne.matches();
extSubtypeMatcherTwoMatches = extSubtypeMatcherTwo.matches();
if (extSubtypeMatcherOneMatches && !extSubtypeMatcherTwoMatches) {
return 1;
}
if (!extSubtypeMatcherOneMatches && extSubtypeMatcherTwoMatches) {
return -1;
}
extSubtypeMatcherOne = MediaTypeHelper.EXT_SUFFIX_SUBTYPE_PATTERN.matcher(mediaTypeOne.getSubtype());
extSubtypeMatcherTwo = MediaTypeHelper.EXT_SUFFIX_SUBTYPE_PATTERN.matcher(mediaTypeTwo.getSubtype());
extSubtypeMatcherOneMatches = extSubtypeMatcherOne.matches();
extSubtypeMatcherTwoMatches = extSubtypeMatcherTwo.matches();
if (extSubtypeMatcherOneMatches && !extSubtypeMatcherTwoMatches) {
return 1;
}
if (!extSubtypeMatcherOneMatches && extSubtypeMatcherTwoMatches) {
return -1;
}
return toString(mediaTypeOne).compareToIgnoreCase(toString(mediaTypeTwo));
}
/**
* Checks that types {@code mediaTypeOne} and type {@code mediaTypeTwo} are compatible. The operation is commutative.
* <p>
* Examples:
* <ul>
* <li><i>text/plain</i> and <i>text/*</i> are compatible</li>
* <li><i>application/atom+xml</i> and <i>application/atom+*</i> are compatible</li>
* </ul>
* </p>
*
* @param mediaTypeOne
* media type
* @param mediaTypeTwo
* media type
* @return {@code true} if types compatible and {@code false} otherwise
*/
public static boolean isCompatible(MediaType mediaTypeOne, MediaType mediaTypeTwo) {
if (mediaTypeOne == null || mediaTypeTwo == null) {
throw new IllegalArgumentException("Null arguments are not allowed");
}
if (mediaTypeOne.isWildcardType() || mediaTypeTwo.isWildcardType()) {
return true;
}
if (mediaTypeOne.getType().equalsIgnoreCase(mediaTypeTwo.getType())) {
if (mediaTypeOne.isWildcardSubtype()
|| mediaTypeTwo.isWildcardSubtype()
|| mediaTypeOne.getSubtype().equalsIgnoreCase(mediaTypeTwo.getSubtype())) {
return true;
}
Matcher extSubtypeMatcherOne = EXT_SUBTYPE_PATTERN.matcher(mediaTypeOne.getSubtype());
Matcher extSubtypeMatcherTwo = EXT_SUBTYPE_PATTERN.matcher(mediaTypeTwo.getSubtype());
boolean extSubtypeMatcherOneMatches = extSubtypeMatcherOne.matches();
boolean extSubtypeMatcherTwoMatches = extSubtypeMatcherTwo.matches();
if (!extSubtypeMatcherOneMatches && extSubtypeMatcherTwoMatches) {
// one is type such as application/xml
// two is type such as application/atom+xml, application/*+xml, application/xml+*
return mediaTypeOne.getSubtype().equalsIgnoreCase(extSubtypeMatcherTwo.group(1))
|| mediaTypeOne.getSubtype().equalsIgnoreCase(extSubtypeMatcherTwo.group(2));
} else if (extSubtypeMatcherOneMatches && !extSubtypeMatcherTwoMatches) {
// one is type such as application/atom+xml, application/*+xml, application/xml+*
// two is type such as application/xml
return mediaTypeTwo.getSubtype().equalsIgnoreCase(extSubtypeMatcherOne.group(1))
|| mediaTypeTwo.getSubtype().equalsIgnoreCase(extSubtypeMatcherOne.group(2));
} else if (extSubtypeMatcherOneMatches && extSubtypeMatcherTwoMatches) {
// both types are extended types
String subtypePrefixOne = extSubtypeMatcherOne.group(1);
String subTypeSuffixOne = extSubtypeMatcherOne.group(2);
String subTypePrefixTwo = extSubtypeMatcherTwo.group(1);
String subtypeSuffixTwo = extSubtypeMatcherTwo.group(2);
if (subtypePrefixOne.equalsIgnoreCase(subTypePrefixTwo)
&& (MEDIA_TYPE_WILDCARD.equals(subTypeSuffixOne) || MEDIA_TYPE_WILDCARD.equals(subtypeSuffixTwo))) {
// parts before '+' are the same and one of after '+' is wildcard '*'
// For example two sub-types: atom+* and atom+xml
return true;
}
if (subTypeSuffixOne.equalsIgnoreCase(subtypeSuffixTwo)
&& (MEDIA_TYPE_WILDCARD.equals(subtypePrefixOne) || MEDIA_TYPE_WILDCARD.equals(subTypePrefixTwo))) {
// parts after '+' are the same and one of before '+' is wildcard '*'
// For example two sub-types: *+xml and atom+xml
return true;
}
}
}
return false;
}
/**
* Checks that type {@code checkMe} is matched to type {@code pattern}. NOTE The operation is NOT commutative,
* e.g. matching of type {@code checkMe} to {@code pattern} does not guaranty that {@code pattern} is matched to {@code checkMe}.
* <p>
* Examples:
* <ul>
* <li><i>text/plain</i> is matched to <i>text/*</i> but type <i>text/*</i> is not matched to <i>text/plain</i></li>
* <li><i>application/atom+xml</i> is matched to <i>application/atom+*</i> but type <i>application/atom+*</i> is not
* matched to <i>application/atom+xml</i></li>
* </ul>
* </p>
*
* @param pattern
* pattern type
* @param checkMe
* type to be checked
* @return {@code true} if type {@code checkMe} is matched to {@code pattern} and {@code false} otherwise
*/
public static boolean isMatched(MediaType pattern, MediaType checkMe) {
if (pattern == null || checkMe == null) {
throw new IllegalArgumentException("Null arguments are not allowed");
}
if (pattern.isWildcardType()) {
return true;
}
if (pattern.getType().equalsIgnoreCase(checkMe.getType())) {
if (pattern.isWildcardSubtype() || pattern.getSubtype().equalsIgnoreCase(checkMe.getSubtype())) {
return true;
}
Matcher patternMatcher = EXT_SUBTYPE_PATTERN.matcher(pattern.getSubtype());
Matcher checkMeMatcher = EXT_SUBTYPE_PATTERN.matcher(checkMe.getSubtype());
if (patternMatcher.matches()) {
String patternPrefix = patternMatcher.group(1);
String patternSuffix = patternMatcher.group(2);
if (!checkMeMatcher.matches()) {
// pattern is type such as application/atom+xml, application/*+xml, application/xml+*
// checkMe is type such as application/xml
return checkMe.getSubtype().equalsIgnoreCase(patternPrefix)
|| checkMe.getSubtype().equalsIgnoreCase(patternSuffix);
}
// both types are extended types
String checkMePrefix = checkMeMatcher.group(1);
String checkMeSuffix = checkMeMatcher.group(2);
if (MEDIA_TYPE_WILDCARD.equals(patternSuffix) && patternPrefix.equalsIgnoreCase(checkMePrefix)) {
// parts before '+' are the same and pattern after '+' is wildcard '*'
// For example two sub-types: atom+* and atom+xml
return true;
}
if (MEDIA_TYPE_WILDCARD.equals(patternPrefix) && patternSuffix.equalsIgnoreCase(checkMeSuffix)) {
// parts after '+' are the same and pattern before '+' is wildcard '*'
// For example two sub-types: *+xml and atom+xml
return true;
}
}
}
return false;
}
private boolean isNullOrWildcard(MediaType contentType) {
return contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype();
}