private int getMediaTypeDistance(final MediaType wanted, final List<MediaType> mtl) { if (wanted == null) { return 0; } int distance = 2; for (final MediaType mt : mtl) { if (MediaTypes.typeEqual(wanted, mt)) { return 0; } if (distance > 1 && MediaTypes.typeEqual(MediaTypes.getTypeWildCart(wanted), mt)) { distance = 1; } } return distance; }
/** * Creates enhancing methods for given resource. * * @param resourceClass resource class for which enhancing methods should be created. * @param resourceInstance resource instance for which enhancing methods should be created. May be {@code null}. * @param newMethods list to store new methods into. */ private void createEnhancingMethods(final Class<?> resourceClass, final Object resourceInstance, final List<ModelProcessorUtil.Method> newMethods) { final Template template = resourceClass.getAnnotation(Template.class); if (template != null) { final Class<?> annotatedResourceClass = ModelHelper.getAnnotatedResourceClass(resourceClass); final List<MediaType> produces = MediaTypes .createQualitySourceMediaTypes(annotatedResourceClass.getAnnotation(Produces.class)); final List<MediaType> consumes = MediaTypes.createFrom(annotatedResourceClass.getAnnotation(Consumes.class)); final TemplateInflectorImpl inflector = new TemplateInflectorImpl(template.name(), resourceClass, resourceInstance); newMethods.add(new ModelProcessorUtil.Method(HttpMethod.GET, consumes, produces, inflector)); newMethods.add(new ModelProcessorUtil.Method(IMPLICIT_VIEW_PATH_PARAMETER_TEMPLATE, HttpMethod.GET, consumes, produces, inflector)); } } }
/** * Create combined client/server media type. * * if the two types are not compatible, {@link #NO_MATCH} is returned. * * @param clientType client-side media type. * @param serverType server-side media type. * @return combined client/server media type. */ static CombinedMediaType create(MediaType clientType, EffectiveMediaType serverType) { if (!clientType.isCompatible(serverType.getMediaType())) { return NO_MATCH; } final MediaType strippedClientType = MediaTypes.stripQualityParams(clientType); final MediaType strippedServerType = MediaTypes.stripQualityParams(serverType.getMediaType()); return new CombinedMediaType( MediaTypes.mostSpecific(strippedClientType, strippedServerType), MediaTypes.getQuality(clientType), QualitySourceMediaType.getQualitySource(serverType.getMediaType()), matchedWildcards(clientType, serverType)); }
/** * Determine if the two list of media types share a common * {@link #typeEqual(javax.ws.rs.core.MediaType, javax.ws.rs.core.MediaType) type-equal} * sub-list. * * @param ml1 first media type list. * @param ml2 second media type list. * @return {@code true} if the two media type lists intersect by sharing a * common type-equal sub-list, {@code false} otherwise. */ public static boolean intersect(List<? extends MediaType> ml1, List<? extends MediaType> ml2) { for (MediaType m1 : ml1) { for (MediaType m2 : ml2) { if (MediaTypes.typeEqual(m1, m2)) { return true; } } } return false; }
/** * Add consumed media types supported by the component. * * @param types consumed media types. * @return updated builder object. */ public Builder consumes(String... types) { return consumes(MediaTypes.createFrom(types)); }
private static boolean usePreSelectedMediaType(final RequestSpecificConsumesProducesAcceptor selectedMethod, final List<AcceptableMediaType> acceptableMediaTypes) { // Resource method is annotated with @Produces and this annotation contains only one MediaType. if (!selectedMethod.producesFromProviders && selectedMethod.methodRouting.method.getProducedTypes().size() == 1) { return true; } // There is only one (non-wildcard) acceptable media type - at this point the pre-selected method has to be chosen so // there are compatible writers (not necessarily writeable ones). return acceptableMediaTypes.size() == 1 && !MediaTypes.isWildcard(acceptableMediaTypes.get(0)); }
/** * Create a list of quality source media type from the Produces annotation. * <p> * * @param mime the Produces annotation. * @return the list of {@link QualitySourceMediaType}, ordered according to * {@link org.glassfish.jersey.message.internal.QualitySourceMediaType#COMPARATOR}. */ public static List<MediaType> createQualitySourceMediaTypes(Produces mime) { if (mime == null || mime.value().length == 0) { return WILDCARD_QS_TYPE_SINGLETON_LIST; } return new ArrayList<MediaType>(createQualitySourceMediaTypes(mime.value())); }
@Override @SuppressWarnings("unchecked") public MediaType getMessageBodyWriterMediaType( final Class<?> c, final Type t, final Annotation[] as, final List<MediaType> acceptableMediaTypes) { for (final MediaType acceptable : acceptableMediaTypes) { for (final WriterModel model : writers) { for (final MediaType mt : model.declaredTypes()) { if (mt.isCompatible(acceptable) && model.isWriteable(c, t, as, acceptable)) { return MediaTypes.mostSpecific(mt, acceptable); } } } } return null; }
@Override public String getProducesMediaType() { return MediaTypes.convertToString(resourceMethod.getProducedTypes()); }
/** * Get media types for which the {@link org.glassfish.jersey.server.mvc.spi.ResolvedViewable resolved viewable} could be * produced. * * @param containerRequest request to obtain acceptable media types. * @param extendedUriInfo uri info to obtain resource method from and its producible media types. * @param varyHeaderValue Vary header reference. * @return list of producible media types. */ public static List<MediaType> getProducibleMediaTypes(final ContainerRequest containerRequest, final ExtendedUriInfo extendedUriInfo, final Ref<String> varyHeaderValue) { final List<MediaType> producedTypes = getResourceMethodProducibleTypes(extendedUriInfo); final MediaType[] mediaTypes = producedTypes.toArray(new MediaType[producedTypes.size()]); final List<Variant> variants = VariantSelector.selectVariants( containerRequest, Variant.mediaTypes(mediaTypes).build(), varyHeaderValue == null ? Refs.<String>emptyRef() : varyHeaderValue); return variants.stream() .map(variant -> MediaTypes.stripQualityParams(variant.getMediaType())) .collect(Collectors.toList()); }
consumesFails = inputTypes1.equals(inputTypes2); if (!consumesFails) { consumesOnlyIntersects = MediaTypes.intersect(inputTypes1, inputTypes2); consumesFails = MediaTypes.intersect(inputTypes1, inputTypes2); producesFails = outputTypes1.equals(outputTypes2); if (!producesFails) { producesOnlyIntersects = MediaTypes.intersect(outputTypes1, outputTypes2); producesFails = MediaTypes.intersect(outputTypes1, outputTypes2);
@Override public <T> MessageBodyWriter<T> getMessageBodyWriter(final Class<T> c, final Type t, final Annotation[] as, final MediaType mediaType, final PropertiesDelegate propertiesDelegate) { MessageBodyWriter<T> p = null; if (legacyProviderOrdering) { if (mediaType != null) { p = _getMessageBodyWriter(c, t, as, mediaType, mediaType, propertiesDelegate); if (p == null) { p = _getMessageBodyWriter(c, t, as, mediaType, MediaTypes.getTypeWildCart(mediaType), propertiesDelegate); } } if (p == null) { p = _getMessageBodyWriter(c, t, as, mediaType, MediaType.WILDCARD_TYPE, propertiesDelegate); } } else { p = _getMessageBodyWriter(c, t, as, mediaType, writers, propertiesDelegate); } return p; }
/** * Determine if the two list of media types share a common * {@link #typeEqual(javax.ws.rs.core.MediaType, javax.ws.rs.core.MediaType) type-equal} * sub-list. * * @param ml1 first media type list. * @param ml2 second media type list. * @return {@code true} if the two media type lists intersect by sharing a * common type-equal sub-list, {@code false} otherwise. */ public static boolean intersect(List<? extends MediaType> ml1, List<? extends MediaType> ml2) { for (MediaType m1 : ml1) { for (MediaType m2 : ml2) { if (MediaTypes.typeEqual(m1, m2)) { return true; } } } return false; }
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)); } }
private static boolean usePreSelectedMediaType(final RequestSpecificConsumesProducesAcceptor selectedMethod, final List<AcceptableMediaType> acceptableMediaTypes) { // Resource method is annotated with @Produces and this annotation contains only one MediaType. if (!selectedMethod.producesFromProviders && selectedMethod.methodRouting.method.getProducedTypes().size() == 1) { return true; } // There is only one (non-wildcard) acceptable media type - at this point the pre-selected method has to be chosen so // there are compatible writers (not necessarily writeable ones). return acceptableMediaTypes.size() == 1 && !MediaTypes.isWildcard(acceptableMediaTypes.get(0)); }
/** * Create a list of quality source media type from the Produces annotation. * <p> * * @param mime the Produces annotation. * @return the list of {@link QualitySourceMediaType}, ordered according to * {@link org.glassfish.jersey.message.internal.QualitySourceMediaType#COMPARATOR}. */ public static List<MediaType> createQualitySourceMediaTypes(Produces mime) { if (mime == null || mime.value().length == 0) { return WILDCARD_QS_TYPE_SINGLETON_LIST; } return new ArrayList<MediaType>(createQualitySourceMediaTypes(mime.value())); }
@Override @SuppressWarnings("unchecked") public MediaType getMessageBodyWriterMediaType( final Class<?> c, final Type t, final Annotation[] as, final List<MediaType> acceptableMediaTypes) { for (final MediaType acceptable : acceptableMediaTypes) { for (final WriterModel model : writers) { for (final MediaType mt : model.declaredTypes()) { if (mt.isCompatible(acceptable) && model.isWriteable(c, t, as, acceptable)) { return MediaTypes.mostSpecific(mt, acceptable); } } } } return null; }
@Override public String getConsumesMediaType() { return MediaTypes.convertToString(resourceMethod.getConsumedTypes()); }
@Override public MediaType apply(final Variant variant) { return MediaTypes.stripQualityParams(variant.getMediaType()); } });
consumesFails = inputTypes1.equals(inputTypes2); if (!consumesFails) { consumesOnlyIntersects = MediaTypes.intersect(inputTypes1, inputTypes2); consumesFails = MediaTypes.intersect(inputTypes1, inputTypes2); producesFails = outputTypes1.equals(outputTypes2); if (!producesFails) { producesOnlyIntersects = MediaTypes.intersect(outputTypes1, outputTypes2); producesFails = MediaTypes.intersect(outputTypes1, outputTypes2);