Java Code Examples for javax.ws.rs.core.MediaType#isWildcardType()

The following examples show how to use javax.ws.rs.core.MediaType#isWildcardType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExceptionMapperUtils.java    From ameba with MIT License 6 votes vote down vote up
/**
 * <p>getResponseType.</p>
 *
 * @param request a {@link org.glassfish.jersey.server.ContainerRequest} object.
 * @param status  a {@link java.lang.Integer} object.
 * @return a {@link javax.ws.rs.core.MediaType} object.
 */
public static MediaType getResponseType(ContainerRequest request, Integer status) {
    if (status != null && status == 406) {
        return MediaType.TEXT_HTML_TYPE;
    }
    List<MediaType> accepts = request.getAcceptableMediaTypes();
    MediaType m;
    if (accepts != null && accepts.size() > 0) {
        m = accepts.get(0);
    } else {
        m = Requests.getMediaType();
    }
    if (m.isWildcardType() || m.equals(LOW_IE_DEFAULT_REQ_TYPE)) {
        m = MediaType.TEXT_HTML_TYPE;
    }
    return m;
}
 
Example 2
Source File: JAXRSOutInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: JAXRSUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static int compareMediaTypes(MediaType mt1, MediaType mt2, String qs) {

        boolean mt1TypeWildcard = mt1.isWildcardType();
        boolean mt2TypeWildcard = mt2.isWildcardType();
        if (mt1TypeWildcard && !mt2TypeWildcard) {
            return 1;
        }
        if (!mt1TypeWildcard && mt2TypeWildcard) {
            return -1;
        }

        boolean mt1SubTypeWildcard = mt1.getSubtype().contains(MediaType.MEDIA_TYPE_WILDCARD);
        boolean mt2SubTypeWildcard = mt2.getSubtype().contains(MediaType.MEDIA_TYPE_WILDCARD);
        if (mt1SubTypeWildcard && !mt2SubTypeWildcard) {
            return 1;
        }
        if (!mt1SubTypeWildcard && mt2SubTypeWildcard) {
            return -1;
        }

        return qs != null ? compareMediaTypesQualityFactors(mt1, mt2, qs) : 0;
    }
 
Example 4
Source File: ContentTypeNormaliserImpl.java    From cougar with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 5
Source File: PatchHandler.java    From trellis with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: BinaryProcessor.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supports(MediaType mediaType) {
    for (MediaType item : SUPPORTED_TYPES) {
        if (item.isCompatible(mediaType) && !mediaType.isWildcardType()) {
            return true;
        }
    }
    return false;
}
 
Example 7
Source File: JacksonProcessor.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supports(MediaType mediaType) {
    for (MediaType item : SUPPORTED_TYPES) {
        if (item.isCompatible(mediaType) && !mediaType.isWildcardType()) {
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: ContainerResponse.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: ContentTypeNormaliserImpl.java    From cougar with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 10
Source File: ToStringBodyWriter.java    From enkan with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
    return (Objects.equals(mediaType.getType(), "text")
            && Objects.equals(mediaType.getSubtype(), "plain"))
            || mediaType.isWildcardType();
}
 
Example 11
Source File: MediaTypeComparator.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
@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));
}
 
Example 12
Source File: MediaTypeHelper.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: MediaTypeHelper.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: ContainerResponse.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
private boolean isNullOrWildcard(MediaType contentType) {
    return contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype();
}