private boolean safeMediaType(MediaType mediaType) { return (WHITELISTED_MEDIA_BASE_TYPES.contains(mediaType.getType()) || mediaType.getSubtype().endsWith("+xml")); }
/** * Copy-constructor that copies the type and subtype of the given {@code MediaType}, * and allows for different parameter. * @param other the other media type * @param parameters the parameters, may be {@code null} * @throws IllegalArgumentException if any of the parameters contain illegal characters */ public MediaType(MediaType other, @Nullable Map<String, String> parameters) { super(other.getType(), other.getSubtype(), parameters); }
private int indexOfEqualMediaType(MediaType mediaType) { for (int i = 0; i < getExpressionsToCompare().size(); i++) { MediaType currentMediaType = getExpressionsToCompare().get(i).getMediaType(); if (mediaType.getType().equalsIgnoreCase(currentMediaType.getType()) && mediaType.getSubtype().equalsIgnoreCase(currentMediaType.getSubtype())) { return i; } } return -1; }
private int indexOfEqualMediaType(MediaType mediaType) { for (int i = 0; i < getExpressionsToCompare().size(); i++) { MediaType currentMediaType = getExpressionsToCompare().get(i).getMediaType(); if (mediaType.getType().equalsIgnoreCase(currentMediaType.getType()) && mediaType.getSubtype().equalsIgnoreCase(currentMediaType.getSubtype())) { return i; } } return -1; }
/** * Copy-constructor that copies the type and subtype of the given {@code MediaType}, * and allows for different parameter. * @param other the other media type * @param parameters the parameters, may be {@code null} * @throws IllegalArgumentException if any of the parameters contain illegal characters */ public MediaType(MediaType other, @Nullable Map<String, String> parameters) { super(other.getType(), other.getSubtype(), parameters); }
private boolean safeMediaType(MediaType mediaType) { return (WHITELISTED_MEDIA_BASE_TYPES.contains(mediaType.getType()) || mediaType.getSubtype().endsWith("+xml")); }
private Stream<MediaType> getSupportedMediaTypes(HttpMessageConverter<?> messageConverter) { return messageConverter.getSupportedMediaTypes() .stream() .map(mediaType -> { if (mediaType.getCharset() != null) { return new MediaType(mediaType.getType(), mediaType.getSubtype()); } return mediaType; }); } }
private int indexOfEqualMediaType(MediaType mediaType) { for (int i = 0; i < getExpressionsToCompare().size(); i++) { MediaType currentMediaType = getExpressionsToCompare().get(i).getMediaType(); if (mediaType.getType().equalsIgnoreCase(currentMediaType.getType()) && mediaType.getSubtype().equalsIgnoreCase(currentMediaType.getSubtype())) { return i; } } return -1; }
params.putAll(contentType.getParameters()); params.put("charset", charSet.toString()); MediaType mediaType = new MediaType(contentType.getType(), contentType.getSubtype(), params); this.headers.setContentType(mediaType);
private static HttpHeaders initHeaders(HttpHeaders headers, HttpServletRequest request) { MediaType contentType = headers.getContentType(); if (contentType == null) { String requestContentType = request.getContentType(); if (StringUtils.hasLength(requestContentType)) { contentType = MediaType.parseMediaType(requestContentType); headers.setContentType(contentType); } } if (contentType != null && contentType.getCharset() == null) { String encoding = request.getCharacterEncoding(); if (StringUtils.hasLength(encoding)) { Charset charset = Charset.forName(encoding); Map<String, String> params = new LinkedCaseInsensitiveMap<>(); params.putAll(contentType.getParameters()); params.put("charset", charset.toString()); headers.setContentType( new MediaType(contentType.getType(), contentType.getSubtype(), params)); } } if (headers.getContentLength() == -1) { int contentLength = request.getContentLength(); if (contentLength != -1) { headers.setContentLength(contentLength); } } return headers; }
private Stream<MediaType> getSupportedMediaTypes(HttpMessageConverter<?> messageConverter) { return messageConverter.getSupportedMediaTypes() .stream() .map(mediaType -> { if (mediaType.getCharset() != null) { return new MediaType(mediaType.getType(), mediaType.getSubtype()); } return mediaType; }); } }
@SuppressWarnings("unchecked") @Override public Mono<Void> write(Publisher<? extends Message> inputStream, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) { try { Message.Builder builder = getMessageBuilder(elementType.toClass()); Descriptors.Descriptor descriptor = builder.getDescriptorForType(); message.getHeaders().add(X_PROTOBUF_SCHEMA_HEADER, descriptor.getFile().getName()); message.getHeaders().add(X_PROTOBUF_MESSAGE_HEADER, descriptor.getFullName()); if (inputStream instanceof Flux) { if (mediaType == null) { message.getHeaders().setContentType(((HttpMessageEncoder<?>)getEncoder()).getStreamingMediaTypes().get(0)); } else if (!ProtobufEncoder.DELIMITED_VALUE.equals(mediaType.getParameters().get(ProtobufEncoder.DELIMITED_KEY))) { Map<String, String> parameters = new HashMap<>(mediaType.getParameters()); parameters.put(ProtobufEncoder.DELIMITED_KEY, ProtobufEncoder.DELIMITED_VALUE); message.getHeaders().setContentType(new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters)); } } return super.write(inputStream, elementType, mediaType, message, hints); } catch (Exception ex) { return Mono.error(new DecodingException("Could not read Protobuf message: " + ex.getMessage(), ex)); } }
params.putAll(contentType.getParameters()); params.put("charset", charSet.toString()); MediaType mediaType = new MediaType(contentType.getType(), contentType.getSubtype(), params); this.headers.setContentType(mediaType);
private static HttpHeaders initHeaders(HttpHeaders headers, HttpServletRequest request) { MediaType contentType = headers.getContentType(); if (contentType == null) { String requestContentType = request.getContentType(); if (StringUtils.hasLength(requestContentType)) { contentType = MediaType.parseMediaType(requestContentType); headers.setContentType(contentType); } } if (contentType != null && contentType.getCharset() == null) { String encoding = request.getCharacterEncoding(); if (StringUtils.hasLength(encoding)) { Charset charset = Charset.forName(encoding); Map<String, String> params = new LinkedCaseInsensitiveMap<>(); params.putAll(contentType.getParameters()); params.put("charset", charset.toString()); headers.setContentType( new MediaType(contentType.getType(), contentType.getSubtype(), params)); } } if (headers.getContentLength() == -1) { int contentLength = request.getContentLength(); if (contentLength != -1) { headers.setContentLength(contentLength); } } return headers; }
@Override protected void writeInternal(T wireFeed, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { Charset charset = (StringUtils.hasLength(wireFeed.getEncoding()) ? Charset.forName(wireFeed.getEncoding()) : DEFAULT_CHARSET); MediaType contentType = outputMessage.getHeaders().getContentType(); if (contentType != null) { contentType = new MediaType(contentType.getType(), contentType.getSubtype(), charset); outputMessage.getHeaders().setContentType(contentType); } WireFeedOutput feedOutput = new WireFeedOutput(); try { Writer writer = new OutputStreamWriter(outputMessage.getBody(), charset); feedOutput.output(wireFeed, writer); } catch (FeedException ex) { throw new HttpMessageNotWritableException("Could not write WireFeed: " + ex.getMessage(), ex); } }
@SuppressWarnings("unchecked") @Override public Mono<Void> write(Publisher<? extends Message> inputStream, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) { try { Message.Builder builder = getMessageBuilder(elementType.toClass()); Descriptors.Descriptor descriptor = builder.getDescriptorForType(); message.getHeaders().add(X_PROTOBUF_SCHEMA_HEADER, descriptor.getFile().getName()); message.getHeaders().add(X_PROTOBUF_MESSAGE_HEADER, descriptor.getFullName()); if (inputStream instanceof Flux) { if (mediaType == null) { message.getHeaders().setContentType(((HttpMessageEncoder<?>)getEncoder()).getStreamingMediaTypes().get(0)); } else if (!ProtobufEncoder.DELIMITED_VALUE.equals(mediaType.getParameters().get(ProtobufEncoder.DELIMITED_KEY))) { Map<String, String> parameters = new HashMap<>(mediaType.getParameters()); parameters.put(ProtobufEncoder.DELIMITED_KEY, ProtobufEncoder.DELIMITED_VALUE); message.getHeaders().setContentType(new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters)); } } return super.write(inputStream, elementType, mediaType, message, hints); } catch (Exception ex) { return Mono.error(new DecodingException("Could not read Protobuf message: " + ex.getMessage(), ex)); } }
@Override protected void writeInternal(T wireFeed, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { Charset charset = (StringUtils.hasLength(wireFeed.getEncoding()) ? Charset.forName(wireFeed.getEncoding()) : DEFAULT_CHARSET); MediaType contentType = outputMessage.getHeaders().getContentType(); if (contentType != null) { contentType = new MediaType(contentType.getType(), contentType.getSubtype(), charset); outputMessage.getHeaders().setContentType(contentType); } WireFeedOutput feedOutput = new WireFeedOutput(); try { Writer writer = new OutputStreamWriter(outputMessage.getBody(), charset); feedOutput.output(wireFeed, writer); } catch (FeedException ex) { throw new HttpMessageNotWritableException("Could not write WireFeed: " + ex.getMessage(), ex); } }
@Test public void parseMediaType() throws Exception { String s = "audio/*; q=0.2"; MediaType mediaType = MediaType.parseMediaType(s); assertEquals("Invalid type", "audio", mediaType.getType()); assertEquals("Invalid subtype", "*", mediaType.getSubtype()); assertEquals("Invalid quality factor", 0.2D, mediaType.getQualityValue(), 0D); }
@Test public void parseURLConnectionMediaType() throws Exception { String s = "*; q=.2"; MediaType mediaType = MediaType.parseMediaType(s); assertEquals("Invalid type", "*", mediaType.getType()); assertEquals("Invalid subtype", "*", mediaType.getSubtype()); assertEquals("Invalid quality factor", 0.2D, mediaType.getQualityValue(), 0D); }
@Test public void validateContentTypeAsString() { HeaderMapper<HttpHeaders> mapper = DefaultHttpHeaderMapper.outboundMapper(); Map<String, Object> messageHeaders = new HashMap<String, Object>(); messageHeaders.put("Content-Type", "text/html"); HttpHeaders headers = new HttpHeaders(); mapper.fromHeaders(new MessageHeaders(messageHeaders), headers); assertEquals("text", headers.getContentType().getType()); assertEquals("html", headers.getContentType().getSubtype()); }