Java Code Examples for org.apache.cxf.jaxrs.utils.JAXRSUtils#sortMediaTypes()

The following examples show how to use org.apache.cxf.jaxrs.utils.JAXRSUtils#sortMediaTypes() . 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: 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 2
Source File: ProviderFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
public int compare(ProviderInfo<MessageBodyReader<?>> p1,
                   ProviderInfo<MessageBodyReader<?>> p2) {
    MessageBodyReader<?> e1 = p1.getProvider();
    MessageBodyReader<?> e2 = p2.getProvider();

    List<MediaType> types1 = JAXRSUtils.getProviderConsumeTypes(e1);
    types1 = JAXRSUtils.sortMediaTypes(types1, null);
    List<MediaType> types2 = JAXRSUtils.getProviderConsumeTypes(e2);
    types2 = JAXRSUtils.sortMediaTypes(types2, null);

    int result = JAXRSUtils.compareSortedMediaTypes(types1, types2, null);
    if (result != 0) {
        return result;
    }
    result = compareClasses(e1, e2);
    if (result != 0) {
        return result;
    }
    result = compareCustomStatus(p1, p2);
    if (result != 0) {
        return result;
    }
    return comparePriorityStatus(p1.getProvider().getClass(), p2.getProvider().getClass());
}
 
Example 3
Source File: ProviderFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
public int compare(ProviderInfo<MessageBodyWriter<?>> p1,
                   ProviderInfo<MessageBodyWriter<?>> p2) {
    MessageBodyWriter<?> e1 = p1.getProvider();
    MessageBodyWriter<?> e2 = p2.getProvider();

    int result = compareClasses(e1, e2);
    if (result != 0) {
        return result;
    }
    List<MediaType> types1 =
        JAXRSUtils.sortMediaTypes(JAXRSUtils.getProviderProduceTypes(e1), JAXRSUtils.MEDIA_TYPE_QS_PARAM);
    List<MediaType> types2 =
        JAXRSUtils.sortMediaTypes(JAXRSUtils.getProviderProduceTypes(e2), JAXRSUtils.MEDIA_TYPE_QS_PARAM);

    result = JAXRSUtils.compareSortedMediaTypes(types1, types2, JAXRSUtils.MEDIA_TYPE_QS_PARAM);
    if (result != 0) {
        return result;
    }
    result = compareCustomStatus(p1, p2);
    if (result != 0) {
        return result;
    }

    return comparePriorityStatus(p1.getProvider().getClass(), p2.getProvider().getClass());
}
 
Example 4
Source File: OperationResourceInfo.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void checkMediaTypes(String consumeMediaTypes,
                             String produceMediaTypes) {
    if (consumeMediaTypes != null) {
        consumeMimes = JAXRSUtils.sortMediaTypes(consumeMediaTypes, null);
    } else {
        Consumes cm =
            AnnotationUtils.getMethodAnnotation(annotatedMethod, Consumes.class);
        if (cm != null) {
            consumeMimes = JAXRSUtils.sortMediaTypes(JAXRSUtils.getMediaTypes(cm.value()), null);
        } else if (classResourceInfo != null) {
            consumeMimes = JAXRSUtils.sortMediaTypes(classResourceInfo.getConsumeMime(), null);
        }
    }
    if (produceMediaTypes != null) {
        produceMimes = JAXRSUtils.sortMediaTypes(produceMediaTypes, JAXRSUtils.MEDIA_TYPE_QS_PARAM);
    } else {
        Produces pm =
            AnnotationUtils.getMethodAnnotation(annotatedMethod, Produces.class);
        if (pm != null) {
            produceMimes = JAXRSUtils.sortMediaTypes(JAXRSUtils.getMediaTypes(pm.value()),
                                                     JAXRSUtils.MEDIA_TYPE_QS_PARAM);
        } else if (classResourceInfo != null) {
            produceMimes = JAXRSUtils.sortMediaTypes(classResourceInfo.getProduceMime(),
                                                     JAXRSUtils.MEDIA_TYPE_QS_PARAM);
        }
    }
}
 
Example 5
Source File: ProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
public int compare(ProviderInfo<ContextResolver<?>> p1,
                   ProviderInfo<ContextResolver<?>> p2) {
    ContextResolver<?> e1 = p1.getProvider();
    ContextResolver<?> e2 = p2.getProvider();

    List<MediaType> types1 =
        JAXRSUtils.sortMediaTypes(JAXRSUtils.getProduceTypes(
             e1.getClass().getAnnotation(Produces.class)), JAXRSUtils.MEDIA_TYPE_QS_PARAM);
    List<MediaType> types2 =
        JAXRSUtils.sortMediaTypes(JAXRSUtils.getProduceTypes(
             e2.getClass().getAnnotation(Produces.class)), JAXRSUtils.MEDIA_TYPE_QS_PARAM);

    return JAXRSUtils.compareSortedMediaTypes(types1, types2, JAXRSUtils.MEDIA_TYPE_QS_PARAM);
}
 
Example 6
Source File: SelectMethodCandidatesTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static List<MediaType> sortMediaTypes(String mediaTypes) {
    return JAXRSUtils.sortMediaTypes(mediaTypes, JAXRSUtils.MEDIA_TYPE_Q_PARAM);
}