/** * Select the body to use for the request * @param obj the body object * @param formParams the form parameters * @param contentType the content type of the request * @return Object the selected body */ protected Object selectBody(Object obj, MultiValueMap<String, Object> formParams, MediaType contentType) { boolean isForm = MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType) || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType); return isForm ? formParams : obj; }
@Override public boolean supportsWriteOnly(@Nullable MediaType mediaType) { return TEXT_HTML.isCompatibleWith(mediaType); }
@Override public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaType) { return (MultiValueMap.class.isAssignableFrom(elementType.toClass()) && (mediaType == null || this.supportedMediaTypes.stream().anyMatch(element -> element.isCompatibleWith(mediaType)))); }
@Override public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) { return Part.class.equals(elementType.toClass()) && (mediaType == null || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType)); }
@Override protected boolean matchMediaType(ServerWebExchange exchange) throws NotAcceptableStatusException { List<MediaType> acceptedMediaTypes = getAcceptedMediaTypes(exchange); for (MediaType acceptedMediaType : acceptedMediaTypes) { if (getMediaType().isCompatibleWith(acceptedMediaType)) { return true; } } return false; } }
@Override public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) { return MULTIPART_VALUE_TYPE.isAssignableFrom(elementType) && (mediaType == null || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType)); }
private boolean matchMediaType(List<MediaType> acceptedMediaTypes) { for (MediaType acceptedMediaType : acceptedMediaTypes) { if (getMediaType().isCompatibleWith(acceptedMediaType)) { return true; } } return false; } }
private boolean isStreamingMediaType(@Nullable MediaType contentType) { return (contentType != null && this.encoder instanceof HttpMessageEncoder && ((HttpMessageEncoder<?>) this.encoder).getStreamingMediaTypes().stream() .anyMatch(streamingMediaType -> contentType.isCompatibleWith(streamingMediaType) && contentType.getParameters().entrySet().containsAll(streamingMediaType.getParameters().keySet()))); }
@Override public void print(Message message, OutputStream output, MediaType contentType, Charset charset) throws IOException, HttpMessageConversionException { if (contentType.isCompatibleWith(APPLICATION_JSON)) { OutputStreamWriter writer = new OutputStreamWriter(output, charset); this.printer.appendTo(message, writer); writer.flush(); } else { throw new HttpMessageConversionException( "protobuf-java-util does not support printing " + contentType); } } }
@Override public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) { if (!MultiValueMap.class.isAssignableFrom(clazz)) { return false; } if (mediaType == null || MediaType.ALL.equals(mediaType)) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.isCompatibleWith(mediaType)) { return true; } } return false; }
@Override public void merge(InputStream input, Charset charset, MediaType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException, HttpMessageConversionException { if (contentType.isCompatibleWith(APPLICATION_JSON)) { InputStreamReader reader = new InputStreamReader(input, charset); this.parser.merge(reader, builder); } else { throw new HttpMessageConversionException( "protobuf-java-util does not support parsing " + contentType); } }
@Override public void merge(InputStream input, Charset charset, MediaType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException, HttpMessageConversionException { if (contentType.isCompatibleWith(APPLICATION_JSON)) { this.jsonFormatter.merge(input, charset, extensionRegistry, builder); } else if (contentType.isCompatibleWith(APPLICATION_XML)) { this.xmlFormatter.merge(input, charset, extensionRegistry, builder); } else { throw new HttpMessageConversionException( "protobuf-java-format does not support parsing " + contentType); } }
@Override public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaType) { if (!MultiValueMap.class.isAssignableFrom(elementType.toClass())) { return false; } if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)) { // Optimistically, any MultiValueMap with or without generics return true; } if (mediaType == null) { // Only String-based MultiValueMap return MULTIVALUE_TYPE.isAssignableFrom(elementType); } return false; }
@Override public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) { return ((MULTIVALUE_TYPE.isAssignableFrom(elementType) || (elementType.hasUnresolvableGenerics() && MultiValueMap.class.isAssignableFrom(elementType.toClass()))) && (mediaType == null || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType))); }
@Override protected ObjectWriter customizeWriter(ObjectWriter writer, @Nullable MimeType mimeType, ResolvableType elementType, @Nullable Map<String, Object> hints) { return (this.ssePrettyPrinter != null && MediaType.TEXT_EVENT_STREAM.isCompatibleWith(mimeType) && writer.getConfig().isEnabled(SerializationFeature.INDENT_OUTPUT) ? writer.with(this.ssePrettyPrinter) : writer); }
private Charset getContentTypeCharset(@Nullable MediaType contentType) { if (contentType != null && contentType.getCharset() != null) { return contentType.getCharset(); } else if (contentType != null && contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) { // Matching to AbstractJackson2HttpMessageConverter#DEFAULT_CHARSET return StandardCharsets.UTF_8; } else { Charset charset = getDefaultCharset(); Assert.state(charset != null, "No default charset"); return charset; } }
/** * Assert the request content type is compatible with the given * content type as defined by {@link MediaType#isCompatibleWith(MediaType)}. */ public RequestMatcher contentTypeCompatibleWith(final MediaType contentType) { return request -> { MediaType actualContentType = request.getHeaders().getContentType(); assertTrue("Content type not set", actualContentType != null); if (actualContentType != null) { assertTrue("Content type [" + actualContentType + "] is not compatible with [" + contentType + "]", actualContentType.isCompatibleWith(contentType)); } }; }
/** * Expect a "Content-Type" header compatible with the given value. */ public WebTestClient.ResponseSpec contentTypeCompatibleWith(MediaType mediaType) { MediaType actual = getHeaders().getContentType(); String message = getMessage("Content-Type") + "=[" + actual + "] is not compatible with [" + mediaType + "]"; this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertTrue(message, (actual != null && actual.isCompatibleWith(mediaType)))); return this.responseSpec; }
/** * Assert the ServletResponse content type is compatible with the given * content type as defined by {@link MediaType#isCompatibleWith(MediaType)}. */ public ResultMatcher contentTypeCompatibleWith(final MediaType contentType) { return result -> { String actual = result.getResponse().getContentType(); assertTrue("Content type not set", actual != null); if (actual != null) { MediaType actualContentType = MediaType.parseMediaType(actual); assertTrue("Content type [" + actual + "] is not compatible with [" + contentType + "]", actualContentType.isCompatibleWith(contentType)); } }; }
private MockResponse multipartRequest(RecordedRequest request) { MediaType mediaType = MediaType.parseMediaType(request.getHeader("Content-Type")); assertTrue(mediaType.isCompatibleWith(MediaType.MULTIPART_FORM_DATA)); String boundary = mediaType.getParameter("boundary"); Buffer body = request.getBody(); try { assertPart(body, "form-data", boundary, "name 1", "text/plain", "value 1"); assertPart(body, "form-data", boundary, "name 2", "text/plain", "value 2+1"); assertPart(body, "form-data", boundary, "name 2", "text/plain", "value 2+2"); assertFilePart(body, "form-data", boundary, "logo", "logo.jpg", "image/jpeg"); } catch (EOFException ex) { throw new IllegalStateException(ex); } return new MockResponse().setResponseCode(200); }