private boolean isSupportedMediaType(MediaType mediaType) { if (MediaType.APPLICATION_JSON_TYPE.isCompatible(mediaType)) { return true; } if (MediaType.APPLICATION_XML_TYPE.isCompatible(mediaType)) { return xmlDecoder != null; } return false; }
@Override public boolean equals(final MediaType mt1, final MediaType mt2) { // treat compatible types as equal return mt1.isCompatible(mt2); }
@Override public boolean equals(final MediaType mt1, final MediaType mt2) { // treat compatible types as equal return mt1.isCompatible(mt2); }
/** * Returns {@code true} if specified {@link MediaType} is considered textual. * <p> * See {@link #READABLE_APP_MEDIA_TYPES}. * * @param mediaType the media type of the entity * @return {@code true} if specified {@link MediaType} is considered textual. */ static boolean isReadable(MediaType mediaType) { if (mediaType != null) { for (MediaType readableMediaType : READABLE_APP_MEDIA_TYPES) { if (readableMediaType.isCompatible(mediaType)) { return true; } } } return false; }
@Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return GenericError.class.isAssignableFrom(type) && MoreMediaTypes.TEXT_CSV_TYPE.isCompatible(mediaType); }
@Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return ScrollResult.ScrollChunk.class.equals(type) && MoreMediaTypes.TEXT_CSV_TYPE.isCompatible(mediaType); }
@Override public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) { return OutboundSseEvent.class.isAssignableFrom(type) && SseFeature.SERVER_SENT_EVENTS_TYPE.isCompatible(mediaType); }
@Override public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) { return InboundEvent.class.equals(type) && SseFeature.SERVER_SENT_EVENTS_TYPE.isCompatible(mediaType); }
@Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return List.class.isAssignableFrom(type) && TypeToken.of(genericType).resolveType(LIST_GENERIC_TOKEN).getRawType().equals(Page.class) && mediaType.isCompatible(PRESTO_PAGES_TYPE); }
/** * Determines whether this {@code ConsumesProducesAcceptor} router can process the {@code request}. * * @param requestContext The request to be tested. * @return True if the {@code request} can be processed by this router, false otherwise. */ boolean isConsumable(ContainerRequest requestContext) { MediaType contentType = requestContext.getMediaType(); return contentType == null || consumes.getMediaType().isCompatible(contentType); }
/** * Determines whether this {@code ConsumesProducesAcceptor} router can process the {@code request}. * * @param requestContext The request to be tested. * @return True if the {@code request} can be processed by this router, false otherwise. */ boolean isConsumable(ContainerRequest requestContext) { MediaType contentType = requestContext.getMediaType(); return contentType == null || consumes.getMediaType().isCompatible(contentType); }
@Override public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { if (responseContext.getMediaType() != null && responseContext.getMediaType().isCompatible(MediaType.TEXT_PLAIN_TYPE)) { String newContentType = "application/json" + responseContext.getMediaType().toString().substring(10); responseContext.getHeaders().putSingle("Content-Type", newContentType); } } }
@SuppressWarnings("unchecked") private <T> boolean isCompatible(final AbstractEntityProviderModel<T> model, final Class c, final MediaType mediaType) { if (model.providedType().equals(Object.class) || // looks weird. Could/(should?) be separated to Writer/Reader check model.providedType().isAssignableFrom(c) || c.isAssignableFrom(model.providedType()) ) { for (final MediaType mt : model.declaredTypes()) { if (mediaType == null) { return true; } if (mediaType.isCompatible(mt)) { return true; } } } return false; }
@Override public MediaType getAcceptableMediaType(List<MediaType> mediaTypes) { if (mediaTypes.isEmpty()) return getAcceptableMediaTypes().get(0); for (MediaType a : getAcceptableMediaTypes()) { if (a.getType().equals(MediaType.MEDIA_TYPE_WILDCARD)) return mediaTypes.get(0); for (MediaType m : mediaTypes) if (m.isCompatible(a) && !m.isWildcardType() && !m.isWildcardSubtype()) return m; } return null; }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { final Response.StatusType responseStatus = responseContext.getStatusInfo(); final String requestPath = requestContext.getUriInfo().getAbsolutePath().getPath(); final List<MediaType> acceptableMediaTypes = requestContext.getAcceptableMediaTypes(); final boolean acceptsHtml = acceptableMediaTypes.stream() .anyMatch(mediaType -> mediaType.isCompatible(MediaType.TEXT_HTML_TYPE) || mediaType.isCompatible(MediaType.APPLICATION_XHTML_XML_TYPE)); final boolean isGetRequest = "get".equalsIgnoreCase(requestContext.getMethod()); if (isGetRequest && responseStatus == Response.Status.NOT_FOUND && acceptsHtml && !requestPath.startsWith("/" + HttpConfiguration.PATH_API)) { final String entity = indexHtmlGenerator.get(requestContext.getHeaders()); responseContext.setStatusInfo(Response.Status.OK); responseContext.setEntity(entity, new Annotation[0], MediaType.TEXT_HTML_TYPE); responseContext.getHeaders().putSingle("X-UA-Compatible", "IE=edge"); } } }
@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 @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 RequestDispatcher create(AbstractResourceMethod method, JavaMethodInvoker invoker) { boolean found = false; for (MediaType m : method.getSupportedInputTypes()) { found = (!m.isWildcardSubtype() && m.isCompatible(MULTIPART_FORM_DATA)); if (found) break; } if (!found) return null; return super.create(method, invoker); }
@Override public int compare(final AbstractEntityProviderModel<T> modelA, final AbstractEntityProviderModel<T> modelB) { if (modelA.isCustom() ^ modelB.isCustom()) { return (modelA.isCustom()) ? -1 : 1; } final MediaType mtA = modelA.declaredTypes().get(0); final MediaType mtB = modelB.declaredTypes().get(0); final int mediaTypeComparison = MediaTypes.PARTIAL_ORDER_COMPARATOR.compare(mtA, mtB); if (mediaTypeComparison != 0 && !mtA.isCompatible(mtB)) { return mediaTypeComparison; } return distanceComparator.compare(modelA.provider(), modelB.provider()); } }
@Override public int compare(final AbstractEntityProviderModel<T> modelA, final AbstractEntityProviderModel<T> modelB) { if (modelA.isCustom() ^ modelB.isCustom()) { return (modelA.isCustom()) ? -1 : 1; } final MediaType mtA = modelA.declaredTypes().get(0); final MediaType mtB = modelB.declaredTypes().get(0); final int mediaTypeComparison = MediaTypes.PARTIAL_ORDER_COMPARATOR.compare(mtA, mtB); if (mediaTypeComparison != 0 && !mtA.isCompatible(mtB)) { return mediaTypeComparison; } return distanceComparator.compare(modelA.provider(), modelB.provider()); } }