/** * Check whether the type is supported by JSON Processing. * * @param type media type to check * @return true if the media type checked is supported by JSON Processing */ public static boolean isSupported(MediaType type) { for (MediaType supportedMediaType : SUPPORTED_MEDIA_TYPES) { if (supportedMediaType.test(type)) { return true; } } return false; }
private static MediaType createMediaType(String type, String subtype) { MediaType mediaType = MediaType.create(type, subtype); KNOWN_TYPES.put(mediaType, mediaType); return mediaType; }
/** * Checks if the subtype is a wildcard. * * @return true if the subtype is a wildcard. */ public boolean isWildcardSubtype() { return this.subtype().equals(AcceptPredicate.WILDCARD_VALUE); }
.stream() .map(type -> { if (type.test(MediaType.APPLICATION_JSON)) { return MediaType.APPLICATION_JSON; } else if (type.test(JsonProcessing.APPLICATION_JAVASCRIPT)) { return JsonProcessing.APPLICATION_JAVASCRIPT; } else if (type.test(JsonProcessing.TEXT_JAVASCRIPT)) { return JsonProcessing.TEXT_JAVASCRIPT; } else if (type.hasSuffix("json")) { return MediaType.create(type.type(), type.subtype()); } else { return null;
@Override public Optional<MediaType> bestAccepted(MediaType... mediaTypes) { if (mediaTypes == null || mediaTypes.length == 0) { return Optional.empty(); } List<MediaType> accepts = acceptedTypes(); if (accepts == null || accepts.isEmpty()) { return Optional.ofNullable(mediaTypes[0]); } double best = 0; MediaType result = null; for (MediaType mt : mediaTypes) { if (mt != null) { for (MediaType acc : accepts) { double q = acc.qualityFactor(); if (q > best && acc.test(mt)) { if (q == 1) { return Optional.of(mt); } else { best = q; result = mt; } } } } } return Optional.ofNullable(result); }
private static MediaType create( String type, String subtype, Map<String, String> parameters) { Objects.requireNonNull(type, "Parameter 'type' is null!"); Objects.requireNonNull(subtype, "Parameter 'subtype' is null!"); Objects.requireNonNull(parameters, "Parameter 'parameters' is null!"); String normalizedType = normalizeToken(type); String normalizedSubtype = normalizeToken(subtype); checkState( !WILDCARD.type.equals(normalizedType) || WILDCARD.type.equals(normalizedSubtype), "A wildcard type cannot be used with a non-wildcard subtype"); Map<String, String> builder = new HashMap<>(); for (Map.Entry<String, String> entry : parameters.entrySet()) { String attribute = normalizeToken(entry.getKey()); builder.put(attribute, normalizeParameterValue(attribute, entry.getValue())); } MediaType mediaType = MediaType.builder() .type(normalizedType) .subtype(normalizedSubtype) .parameters(builder) .build(); // Return one of the constants if the media type is a known type. //TODO or else get? return Optional.ofNullable(KNOWN_TYPES.get(mediaType)).orElse(mediaType); }
/** * Only accept requests with any of the given content types. * * @param contentType Content type * @return composed predicate representing the logical expression between * this predicate <b>AND</b> the provided predicate * @throws NullPointerException if the specified content type array is null */ public RequestPredicate hasContentType(final String... contentType) { Objects.requireNonNull(contentType, "accepted media types"); return and((req) -> { Optional<MediaType> actualContentType = req.headers().contentType(); return actualContentType.isPresent() && Stream.of(contentType) .anyMatch((mt) -> actualContentType.get() .equals(MediaType.parse(mt))); }); }
@Override public void addAcceptPatches(MediaType... acceptableMediaTypes) { if (acceptableMediaTypes == null) { return; } for (MediaType mt : acceptableMediaTypes) { add(Http.Header.ACCEPT_PATCH, mt.toString()); } }
/** * Check if the passed headers (specifically Accept) prefer * data in JSON format. * * @return true if passed headers prefer data in JSON format. */ static boolean requestsJsonData(RequestHeaders headers) { Optional<MediaType> mediaType = headers.bestAccepted(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON); boolean requestsJson = mediaType.isPresent() && mediaType.get().equals(MediaType.APPLICATION_JSON); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Generating metrics for media type " + mediaType.toString() + ". requestsJson=" + requestsJson); } return requestsJson; }
/** * Checks if the primary type is a wildcard. * * @return true if the primary type is a wildcard. */ public boolean isWildcardType() { return this.type().equals(AcceptPredicate.WILDCARD_VALUE); }
String charset = mediaType.charset().orElse(StandardCharsets.UTF_8.name()); headers.contentType(mediaType.withCharset(charset)); return ContentWriters.charSequenceWriter(Charset.forName(charset)).apply(s); });
private static MediaType createMediaType() { MediaType mediaType = MediaType.builder().build(); KNOWN_TYPES.put(mediaType, mediaType); return mediaType; }
@Override public MediaType build() { return new MediaType(this); }
.stream() .map(type -> { if (type.test(MediaType.APPLICATION_JSON)) { return MediaType.APPLICATION_JSON; } else if (type.test(JsonProcessing.APPLICATION_JAVASCRIPT)) { return JsonProcessing.APPLICATION_JAVASCRIPT; } else if (type.test(JsonProcessing.TEXT_JAVASCRIPT)) { return JsonProcessing.TEXT_JAVASCRIPT; } else if (type.hasSuffix("json")) { return MediaType.create(type.type(), type.subtype()); } else { return null;
@Override public void contentType(MediaType contentType) { if (contentType == null) { remove(Http.Header.CONTENT_TYPE); } else { put(Http.Header.CONTENT_TYPE, contentType.toString()); } }
/** * Only accept requests with any of the given content types. * * @param contentType Content type * @return composed predicate representing the logical expression between * this predicate <b>AND</b> the provided predicate * @throws NullPointerException if the specified content type array is null */ public RequestPredicate hasContentType(final MediaType... contentType) { Objects.requireNonNull(contentType, "content types"); return and((req) -> { Optional<MediaType> actualContentType = req.headers().contentType(); return actualContentType.isPresent() && Stream.of(contentType) .anyMatch((mt) -> actualContentType.get() .equals(mt)); }); }
/** * Creates a new instance of {@code MediaType} with the supplied type and subtype. * * @param type the primary type, {@code null} is equivalent to * {@link #WILDCARD_VALUE} * @param subtype the subtype, {@code null} is equivalent to * {@link #WILDCARD_VALUE} * @return a new media type for the specified type and subtype */ public static MediaType create(String type, String subtype) { return builder() .type(type) .subtype(subtype) .build(); }
/** * Check whether the type is supported by JSON Processing. * * @param type media type to check * @return true if the media type checked is supported by JSON Processing */ public static boolean isSupported(MediaType type) { for (MediaType supportedMediaType : SUPPORTED_MEDIA_TYPES) { if (supportedMediaType.test(type)) { return true; } } return false; }
/** * Calls using specified HTTP method with body. * * @param method an HTTP method. * @param mediaPublisher a request body publisher. * @return a response to read. * @throws InterruptedException if thread is interrupted. * @throws TimeoutException if request timeout is reached. */ public TestResponse call(Http.RequestMethod method, MediaPublisher mediaPublisher) throws InterruptedException, TimeoutException { if (mediaPublisher != null && !headers.containsKey(Http.Header.CONTENT_TYPE) && mediaPublisher.mediaType() != null) { header(Http.Header.CONTENT_TYPE, mediaPublisher.mediaType().toString()); } return testClient.call(method, version, uri(), headers, mediaPublisher); }
return create(type, subtype, parameters); } catch (IllegalStateException e) { throw new IllegalArgumentException("Could not parse '" + input + "'", e);