private static MediaType addDefaultCharset(MediaType main, @Nullable MediaType defaultType) { if (main.getCharset() == null && defaultType != null && defaultType.getCharset() != null) { return new MediaType(main, defaultType.getCharset()); } return main; }
private void setCharset(@Nullable MediaType contentType, Marshaller marshaller) throws PropertyException { if (contentType != null && contentType.getCharset() != null) { marshaller.setProperty(Marshaller.JAXB_ENCODING, contentType.getCharset().name()); } }
private Charset getMediaTypeCharset(@Nullable MediaType mediaType) { if (mediaType != null && mediaType.getCharset() != null) { return mediaType.getCharset(); } else { return getDefaultCharset(); } }
private String determineEncoding(String contentTypeHeader, String defaultEncoding) { if (!StringUtils.hasText(contentTypeHeader)) { return defaultEncoding; } MediaType contentType = MediaType.parseMediaType(contentTypeHeader); Charset charset = contentType.getCharset(); return (charset != null ? charset.name() : defaultEncoding); }
private MediaType getMediaType(@Nullable MediaType mediaType) { if (mediaType == null) { return DEFAULT_FORM_DATA_MEDIA_TYPE; } else if (mediaType.getCharset() == null) { return new MediaType(mediaType, this.charset); } else { return mediaType; } }
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; } }
private Mono<DataBuffer> encodeText(CharSequence text, MediaType mediaType, DataBufferFactory bufferFactory) { Assert.notNull(mediaType.getCharset(), "Expected MediaType with charset"); byte[] bytes = text.toString().getBytes(mediaType.getCharset()); return Mono.defer(() -> Mono.just(bufferFactory.allocateBuffer(bytes.length).write(bytes))); }
/** * Determine the JSON encoding to use for the given content type. * @param contentType the media type as requested by the caller * @return the JSON encoding to use (never {@code null}) */ protected JsonEncoding getJsonEncoding(@Nullable MediaType contentType) { if (contentType != null && contentType.getCharset() != null) { Charset charset = contentType.getCharset(); for (JsonEncoding encoding : JsonEncoding.values()) { if (charset.name().equals(encoding.getJavaName())) { return encoding; } } } return JsonEncoding.UTF8; }
private MediaType getMediaType(@Nullable MediaType mediaType) { if (mediaType == null) { return DEFAULT_FORM_DATA_MEDIA_TYPE; } else if (mediaType.getCharset() == null) { return new MediaType(mediaType, getDefaultCharset()); } else { return mediaType; } }
/** * Determine the charset of the response (for inclusion in a status exception). * @param response the response to inspect * @return the associated charset, or {@code null} if none * @since 4.3.8 */ @Nullable protected Charset getCharset(ClientHttpResponse response) { HttpHeaders headers = response.getHeaders(); MediaType contentType = headers.getContentType(); return (contentType != null ? contentType.getCharset() : null); }
private Charset getCharset() { Charset charset = null; MediaType contentType = getHeaders().getContentType(); if (contentType != null) { charset = contentType.getCharset(); } return (charset != null ? charset : StandardCharsets.UTF_8); }
private static Charset getCharset(HttpHeaders headers) { Charset charset = (headers.getContentType() != null ? headers.getContentType().getCharset() : null); return (charset != null ? charset : DEFAULT_CHARSET); }
@Override protected void applyHeaders() { getHeaders().forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { this.response.addHeader(headerName, headerValue); } }); MediaType contentType = getHeaders().getContentType(); if (this.response.getContentType() == null && contentType != null) { this.response.setContentType(contentType.toString()); } Charset charset = (contentType != null ? contentType.getCharset() : null); if (this.response.getCharacterEncoding() == null && charset != null) { this.response.setCharacterEncoding(charset.name()); } }
private Charset determineCharset() { MediaType contentType = getHeaders().getContentType(); if (contentType != null) { Charset charset = contentType.getCharset(); if (charset != null) { return charset; } } String encoding = this.multipartRequest.getCharacterEncoding(); return (encoding != null ? Charset.forName(encoding) : FORM_CHARSET); }
@Override public Mono<Void> write(Publisher<?> input, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) { mediaType = (mediaType != null && mediaType.getCharset() != null ? mediaType : DEFAULT_MEDIA_TYPE); DataBufferFactory bufferFactory = message.bufferFactory(); message.getHeaders().setContentType(mediaType); return message.writeAndFlushWith(encode(input, elementType, mediaType, bufferFactory, hints)); }
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; }); } }
@Override public String getCharacterEncoding() { MediaType type = this.outputMessage.getHeaders().getContentType(); return (type != null && type.getCharset() != null ? type.getCharset().name() : null); }
@Override protected void applyHeaders() { HttpServletResponse response = getNativeResponse(); MediaType contentType = getHeaders().getContentType(); if (response.getContentType() == null && contentType != null) { response.setContentType(contentType.toString()); } Charset charset = (contentType != null ? contentType.getCharset() : null); if (response.getCharacterEncoding() == null && charset != null) { response.setCharacterEncoding(charset.name()); } long contentLength = getHeaders().getContentLength(); if (contentLength != -1) { response.setContentLengthLong(contentLength); } }
@Override @SuppressWarnings("unchecked") protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { WireFeedInput feedInput = new WireFeedInput(); MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = (contentType != null && contentType.getCharset() != null ? contentType.getCharset() : DEFAULT_CHARSET); try { Reader reader = new InputStreamReader(inputMessage.getBody(), charset); return (T) feedInput.build(reader); } catch (FeedException ex) { throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex, inputMessage); } }
private Charset getCharset() { Charset charset = null; MediaType contentType = getHeaders().getContentType(); if (contentType != null) { charset = contentType.getCharset(); } return (charset != null ? charset : StandardCharsets.UTF_8); }