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;
}
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));
}
}
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;
}
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;
}
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;
}
Aggregations