Search in sources :

Example 1 with WriterModel

use of org.glassfish.jersey.message.WriterModel in project jersey by jersey.

the class MessageBodyFactory method getMessageBodyWriterMediaTypes.

@Override
@SuppressWarnings("unchecked")
public List<MediaType> getMessageBodyWriterMediaTypes(final Class<?> c, final Type t, final Annotation[] as) {
    final Set<MediaType> writeableMediaTypes = new LinkedHashSet<>();
    for (final WriterModel model : writers) {
        boolean writeableWorker = false;
        for (final MediaType mt : model.declaredTypes()) {
            if (model.isWriteable(c, t, as, mt)) {
                writeableMediaTypes.add(mt);
                writeableWorker = true;
            }
            if (!writeableMediaTypes.contains(MediaType.WILDCARD_TYPE) && writeableWorker && model.declaredTypes().contains(MediaType.WILDCARD_TYPE)) {
                writeableMediaTypes.add(MediaType.WILDCARD_TYPE);
            }
        }
    }
    final List<MediaType> mtl = new ArrayList<>(writeableMediaTypes);
    Collections.sort(mtl, MediaTypes.PARTIAL_ORDER_COMPARATOR);
    return mtl;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WriterModel(org.glassfish.jersey.message.WriterModel) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType)

Example 2 with WriterModel

use of org.glassfish.jersey.message.WriterModel in project jersey by jersey.

the class MessageBodyFactory method addWriters.

private static void addWriters(final List<WriterModel> models, final Set<MessageBodyWriter> writers, final boolean custom) {
    for (final MessageBodyWriter provider : writers) {
        final List<MediaType> values = MediaTypes.createFrom(provider.getClass().getAnnotation(Produces.class));
        models.add(new WriterModel(provider, values, custom));
    }
}
Also used : WriterModel(org.glassfish.jersey.message.WriterModel) Produces(javax.ws.rs.Produces) MediaType(javax.ws.rs.core.MediaType) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter)

Example 3 with WriterModel

use of org.glassfish.jersey.message.WriterModel in project jersey by jersey.

the class MethodSelectingRouter method isWriteable.

private boolean isWriteable(final RequestSpecificConsumesProducesAcceptor candidate) {
    final Invocable invocable = candidate.methodRouting.method.getInvocable();
    final Class<?> responseType = Primitives.wrap(invocable.getRawRoutingResponseType());
    if (Response.class.isAssignableFrom(responseType) || Void.class.isAssignableFrom(responseType)) {
        return true;
    }
    final Type genericType = invocable.getRoutingResponseType();
    final Type genericReturnType = genericType instanceof GenericType ? ((GenericType) genericType).getType() : genericType;
    for (final WriterModel model : workers.getWritersModelsForType(responseType)) {
        if (model.isWriteable(responseType, genericReturnType, invocable.getHandlingMethod().getDeclaredAnnotations(), candidate.produces.combinedType)) {
            return true;
        }
    }
    return false;
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(javax.ws.rs.core.Response) Invocable(org.glassfish.jersey.server.model.Invocable) MediaType(javax.ws.rs.core.MediaType) AcceptableMediaType(org.glassfish.jersey.message.internal.AcceptableMediaType) GenericType(javax.ws.rs.core.GenericType) Type(java.lang.reflect.Type) GenericType(javax.ws.rs.core.GenericType) WriterModel(org.glassfish.jersey.message.WriterModel)

Example 4 with WriterModel

use of org.glassfish.jersey.message.WriterModel in project jersey by jersey.

the class MethodSelectingRouter method determineResponseMediaType.

/**
     * Determine the {@link MediaType} of the {@link Response} based on writers suitable for the given entity class,
     * pre-selected method and acceptable media types.
     *
     * @param entityClass          entity class to determine the media type for.
     * @param entityType           entity type for writers.
     * @param selectedMethod       pre-selected (invoked) method.
     * @param acceptableMediaTypes acceptable media types from request.
     * @return media type of the response.
     */
private MediaType determineResponseMediaType(final Class<?> entityClass, final Type entityType, final RequestSpecificConsumesProducesAcceptor selectedMethod, final List<AcceptableMediaType> acceptableMediaTypes) {
    // Return pre-selected MediaType.
    if (usePreSelectedMediaType(selectedMethod, acceptableMediaTypes)) {
        return selectedMethod.produces.combinedType;
    }
    final ResourceMethod resourceMethod = selectedMethod.methodRouting.method;
    final Invocable invocable = resourceMethod.getInvocable();
    // Entity class can be null when considering HEAD method || empty entity.
    final Class<?> responseEntityClass = entityClass == null ? invocable.getRawRoutingResponseType() : entityClass;
    final Method handlingMethod = invocable.getHandlingMethod();
    // Media types producible by method.
    final List<MediaType> methodProducesTypes = !resourceMethod.getProducedTypes().isEmpty() ? resourceMethod.getProducedTypes() : Collections.singletonList(MediaType.WILDCARD_TYPE);
    // Applicable entity providers
    final List<WriterModel> writersForEntityType = workers.getWritersModelsForType(responseEntityClass);
    CombinedMediaType selected = null;
    for (final MediaType acceptableMediaType : acceptableMediaTypes) {
        for (final MediaType methodProducesType : methodProducesTypes) {
            if (!acceptableMediaType.isCompatible(methodProducesType)) {
                // no need to go deeper if acceptable and method produces type are incompatible
                continue;
            }
            // Use writers suitable for entity class to determine the media type.
            for (final WriterModel model : writersForEntityType) {
                for (final MediaType writerProduces : model.declaredTypes()) {
                    if (!writerProduces.isCompatible(acceptableMediaType) || !methodProducesType.isCompatible(writerProduces)) {
                        continue;
                    }
                    final CombinedMediaType.EffectiveMediaType effectiveProduces = new CombinedMediaType.EffectiveMediaType(MediaTypes.mostSpecific(methodProducesType, writerProduces), false);
                    final CombinedMediaType candidate = CombinedMediaType.create(acceptableMediaType, effectiveProduces);
                    if (candidate != CombinedMediaType.NO_MATCH) {
                        // Look for a better compatible worker.
                        if (selected == null || CombinedMediaType.COMPARATOR.compare(candidate, selected) < 0) {
                            if (model.isWriteable(responseEntityClass, entityType, handlingMethod.getDeclaredAnnotations(), candidate.combinedType)) {
                                selected = candidate;
                            }
                        }
                    }
                }
            }
        }
    }
    // Found media type for current writer.
    if (selected != null) {
        return selected.combinedType;
    }
    // so it can be written.
    return selectedMethod.produces.combinedType;
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) WriterModel(org.glassfish.jersey.message.WriterModel) MediaType(javax.ws.rs.core.MediaType) AcceptableMediaType(org.glassfish.jersey.message.internal.AcceptableMediaType) HttpMethod(javax.ws.rs.HttpMethod) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 5 with WriterModel

use of org.glassfish.jersey.message.WriterModel in project jersey by jersey.

the class MessageBodyFactory method processMessageBodyWritersForType.

private List<WriterModel> processMessageBodyWritersForType(final Class<?> clazz) {
    final List<WriterModel> suitableWriters = new ArrayList<>();
    if (Response.class.isAssignableFrom(clazz)) {
        suitableWriters.addAll(writers);
    } else {
        final Class<?> wrapped = Primitives.wrap(clazz);
        for (final WriterModel model : writers) {
            if (model.providedType() == null || model.providedType() == clazz || model.providedType().isAssignableFrom(wrapped)) {
                suitableWriters.add(model);
            }
        }
    }
    // Type -> Writer.
    Collections.sort(suitableWriters, WORKER_BY_TYPE_COMPARATOR);
    mbwTypeLookupCache.put(clazz, suitableWriters);
    // Type -> MediaType.
    typeToMediaTypeWritersCache.put(clazz, getMessageBodyWorkersMediaTypesByType(suitableWriters));
    return suitableWriters;
}
Also used : WriterModel(org.glassfish.jersey.message.WriterModel) ArrayList(java.util.ArrayList)

Aggregations

WriterModel (org.glassfish.jersey.message.WriterModel)6 MediaType (javax.ws.rs.core.MediaType)5 ArrayList (java.util.ArrayList)2 AcceptableMediaType (org.glassfish.jersey.message.internal.AcceptableMediaType)2 Invocable (org.glassfish.jersey.server.model.Invocable)2 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 LinkedHashSet (java.util.LinkedHashSet)1 HttpMethod (javax.ws.rs.HttpMethod)1 Produces (javax.ws.rs.Produces)1 GenericType (javax.ws.rs.core.GenericType)1 Response (javax.ws.rs.core.Response)1 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)1 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)1 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)1