Search in sources :

Example 1 with ReaderModel

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

the class MessageBodyFactory method processMessageBodyReadersForType.

private List<ReaderModel> processMessageBodyReadersForType(final Class<?> clazz) {
    final List<ReaderModel> suitableReaders = new ArrayList<>();
    final Class<?> wrapped = Primitives.wrap(clazz);
    for (final ReaderModel reader : readers) {
        if (reader.providedType() == null || reader.providedType() == clazz || reader.providedType().isAssignableFrom(wrapped)) {
            suitableReaders.add(reader);
        }
    }
    // Type -> Writer.
    Collections.sort(suitableReaders, WORKER_BY_TYPE_COMPARATOR);
    mbrTypeLookupCache.put(clazz, suitableReaders);
    // Type -> MediaType.
    typeToMediaTypeReadersCache.put(clazz, getMessageBodyWorkersMediaTypesByType(suitableReaders));
    return suitableReaders;
}
Also used : ArrayList(java.util.ArrayList) ReaderModel(org.glassfish.jersey.message.ReaderModel)

Example 2 with ReaderModel

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

the class MessageBodyFactory method addReaders.

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

Example 3 with ReaderModel

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

the class MessageBodyFactory method getMessageBodyReaderMediaTypes.

@Override
@SuppressWarnings("unchecked")
public List<MediaType> getMessageBodyReaderMediaTypes(final Class<?> type, final Type genericType, final Annotation[] annotations) {
    final Set<MediaType> readableMediaTypes = new LinkedHashSet<>();
    for (final ReaderModel model : readers) {
        boolean readableWorker = false;
        for (final MediaType mt : model.declaredTypes()) {
            if (model.isReadable(type, genericType, annotations, mt)) {
                readableMediaTypes.add(mt);
                readableWorker = true;
            }
            if (!readableMediaTypes.contains(MediaType.WILDCARD_TYPE) && readableWorker && model.declaredTypes().contains(MediaType.WILDCARD_TYPE)) {
                readableMediaTypes.add(MediaType.WILDCARD_TYPE);
            }
        }
    }
    final List<MediaType> mtl = new ArrayList<>(readableMediaTypes);
    Collections.sort(mtl, MediaTypes.PARTIAL_ORDER_COMPARATOR);
    return mtl;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) ReaderModel(org.glassfish.jersey.message.ReaderModel)

Example 4 with ReaderModel

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

the class MessageBodyFactory method _getMessageBodyReader.

@SuppressWarnings("unchecked")
private <T> MessageBodyReader<T> _getMessageBodyReader(final Class<T> c, final Type t, final Annotation[] as, final MediaType mediaType, final List<ReaderModel> models, final PropertiesDelegate propertiesDelegate) {
    // Ensure a parameter-less lookup type to prevent excessive memory consumption
    // reported in JERSEY-2297
    final MediaType lookupType = mediaType == null || mediaType.getParameters().isEmpty() ? mediaType : new MediaType(mediaType.getType(), mediaType.getSubtype());
    final ModelLookupKey lookupKey = new ModelLookupKey(c, lookupType);
    List<ReaderModel> readers = mbrLookupCache.get(lookupKey);
    if (readers == null) {
        readers = new ArrayList<>();
        for (final ReaderModel model : models) {
            if (isCompatible(model, c, mediaType)) {
                readers.add(model);
            }
        }
        Collections.sort(readers, new WorkerComparator<>(c, mediaType));
        mbrLookupCache.put(lookupKey, readers);
    }
    if (readers.isEmpty()) {
        return null;
    }
    final TracingLogger tracingLogger = TracingLogger.getInstance(propertiesDelegate);
    MessageBodyReader<T> selected = null;
    final Iterator<ReaderModel> iterator = readers.iterator();
    while (iterator.hasNext()) {
        final ReaderModel model = iterator.next();
        if (model.isReadable(c, t, as, mediaType)) {
            selected = (MessageBodyReader<T>) model.provider();
            tracingLogger.log(MsgTraceEvent.MBR_SELECTED, selected);
            break;
        }
        tracingLogger.log(MsgTraceEvent.MBR_NOT_READABLE, model.provider());
    }
    if (tracingLogger.isLogEnabled(MsgTraceEvent.MBR_SKIPPED)) {
        while (iterator.hasNext()) {
            final ReaderModel model = iterator.next();
            tracingLogger.log(MsgTraceEvent.MBR_SKIPPED, model.provider());
        }
    }
    return selected;
}
Also used : MediaType(javax.ws.rs.core.MediaType) ReaderModel(org.glassfish.jersey.message.ReaderModel)

Example 5 with ReaderModel

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

the class MethodSelectingRouter method isReadable.

private boolean isReadable(final RequestSpecificConsumesProducesAcceptor candidate) {
    final Invocable invocable = candidate.methodRouting.method.getInvocable();
    final Method handlingMethod = invocable.getHandlingMethod();
    final Parameter entityParam = getEntityParam(invocable);
    if (entityParam == null) {
        return true;
    } else {
        final Class<?> entityType = entityParam.getRawType();
        for (final ReaderModel model : workers.getReaderModelsForType(entityType)) {
            if (model.isReadable(entityType, entityParam.getType(), handlingMethod.getDeclaredAnnotations(), candidate.consumes.combinedType)) {
                return true;
            }
        }
    }
    return false;
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) Parameter(org.glassfish.jersey.server.model.Parameter) HttpMethod(javax.ws.rs.HttpMethod) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method) ReaderModel(org.glassfish.jersey.message.ReaderModel)

Aggregations

ReaderModel (org.glassfish.jersey.message.ReaderModel)5 MediaType (javax.ws.rs.core.MediaType)3 ArrayList (java.util.ArrayList)2 Method (java.lang.reflect.Method)1 LinkedHashSet (java.util.LinkedHashSet)1 Consumes (javax.ws.rs.Consumes)1 HttpMethod (javax.ws.rs.HttpMethod)1 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)1 Invocable (org.glassfish.jersey.server.model.Invocable)1 Parameter (org.glassfish.jersey.server.model.Parameter)1 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)1