Java Code Examples for org.springframework.http.MediaType#getCharset()
The following examples show how to use
org.springframework.http.MediaType#getCharset() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MockHttpServletRequest.java From java-technology-stack with MIT License | 6 votes |
public void setContentType(@Nullable String contentType) { this.contentType = contentType; if (contentType != null) { try { MediaType mediaType = MediaType.parseMediaType(contentType); if (mediaType.getCharset() != null) { this.characterEncoding = mediaType.getCharset().name(); } } catch (IllegalArgumentException ex) { // Try to get charset value anyway int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); if (charsetIndex != -1) { this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); } } updateContentTypeHeader(); } }
Example 2
Source File: JettyHttpHandlerAdapter.java From java-technology-stack with MIT License | 6 votes |
@Override protected void applyHeaders() { MediaType contentType = getHeaders().getContentType(); HttpServletResponse response = getNativeResponse(); 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); } }
Example 3
Source File: PropertiesHttpMessageConverter.java From SpringAll with MIT License | 6 votes |
@Override protected Properties readInternal(Class<? extends Properties> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { Properties properties = new Properties(); // 获取请求头 HttpHeaders headers = inputMessage.getHeaders(); // 获取 content-type MediaType contentType = headers.getContentType(); // 获取编码 Charset charset = null; if (contentType != null) { charset = contentType.getCharset(); } charset = charset == null ? Charset.forName("UTF-8") : charset; // 获取请求体 InputStream body = inputMessage.getBody(); InputStreamReader inputStreamReader = new InputStreamReader(body, charset); properties.load(inputStreamReader); return properties; }
Example 4
Source File: AbstractWireFeedHttpMessageConverter.java From lams with GNU General Public License v2.0 | 6 votes |
@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); } }
Example 5
Source File: TomcatHttpHandlerAdapter.java From java-technology-stack with MIT License | 6 votes |
@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); } }
Example 6
Source File: FormHttpMessageWriter.java From java-technology-stack with MIT License | 6 votes |
@Override public Mono<Void> write(Publisher<? extends MultiValueMap<String, String>> inputStream, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) { mediaType = getMediaType(mediaType); message.getHeaders().setContentType(mediaType); Charset charset = mediaType.getCharset(); Assert.notNull(charset, "No charset"); // should never occur return Mono.from(inputStream).flatMap(form -> { logFormData(form, hints); String value = serializeForm(form, charset); ByteBuffer byteBuffer = charset.encode(value); DataBuffer buffer = message.bufferFactory().wrap(byteBuffer); message.getHeaders().setContentLength(byteBuffer.remaining()); return message.writeWith(Mono.just(buffer)); }); }
Example 7
Source File: MockHttpServletResponse.java From spring-analysis-note with MIT License | 6 votes |
@Override public void setContentType(@Nullable String contentType) { this.contentType = contentType; if (contentType != null) { try { MediaType mediaType = MediaType.parseMediaType(contentType); if (mediaType.getCharset() != null) { this.characterEncoding = mediaType.getCharset().name(); this.charset = true; } } catch (Exception ex) { // Try to get charset value anyway int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); if (charsetIndex != -1) { this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); this.charset = true; } } updateContentTypeHeader(); } }
Example 8
Source File: MockHttpServletRequest.java From java-technology-stack with MIT License | 6 votes |
public void setContentType(@Nullable String contentType) { this.contentType = contentType; if (contentType != null) { try { MediaType mediaType = MediaType.parseMediaType(contentType); if (mediaType.getCharset() != null) { this.characterEncoding = mediaType.getCharset().name(); } } catch (IllegalArgumentException ex) { // Try to get charset value anyway int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); if (charsetIndex != -1) { this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); } } updateContentTypeHeader(); } }
Example 9
Source File: RequestPartServletServerHttpRequest.java From spring-analysis-note with MIT License | 5 votes |
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); }
Example 10
Source File: FormHttpMessageWriter.java From java-technology-stack with MIT License | 5 votes |
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; } }
Example 11
Source File: CommonsFileUploadSupport.java From lams with GNU General Public License v2.0 | 5 votes |
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); }
Example 12
Source File: ProtobufHttpMessageConverter.java From java-technology-stack with MIT License | 5 votes |
@Override protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { MediaType contentType = inputMessage.getHeaders().getContentType(); if (contentType == null) { contentType = PROTOBUF; } Charset charset = contentType.getCharset(); if (charset == null) { charset = DEFAULT_CHARSET; } Message.Builder builder = getMessageBuilder(clazz); if (PROTOBUF.isCompatibleWith(contentType)) { builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry); } else if (TEXT_PLAIN.isCompatibleWith(contentType)) { InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset); TextFormat.merge(reader, this.extensionRegistry, builder); } else if (this.protobufFormatSupport != null) { this.protobufFormatSupport.merge( inputMessage.getBody(), charset, contentType, this.extensionRegistry, builder); } return builder.build(); }
Example 13
Source File: ServletServerHttpRequest.java From spring-analysis-note with MIT License | 5 votes |
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; }
Example 14
Source File: MockClientHttpResponse.java From spring-analysis-note with MIT License | 5 votes |
private Charset getCharset() { Charset charset = null; MediaType contentType = getHeaders().getContentType(); if (contentType != null) { charset = contentType.getCharset(); } return (charset != null ? charset : StandardCharsets.UTF_8); }
Example 15
Source File: FormHttpMessageConverter.java From spring-analysis-note with MIT License | 5 votes |
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; } }
Example 16
Source File: MappingJacksonRPC2HttpMessageConverter.java From jsonrpc4j with MIT License | 5 votes |
/** * 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</code>) */ private JsonEncoding getJsonEncoding(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; }
Example 17
Source File: DefaultResponseErrorHandler.java From spring-analysis-note with MIT License | 5 votes |
/** * 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); }
Example 18
Source File: CommonsFileUploadSupport.java From spring-analysis-note with MIT License | 5 votes |
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); }
Example 19
Source File: CustomXMLHttpMessageConverter.java From GreenSummer with GNU Lesser General Public License v2.1 | 4 votes |
private void setCharset(MediaType contentType, Marshaller marshaller) throws PropertyException { if (contentType != null && contentType.getCharset() != null) { marshaller.setProperty(Marshaller.JAXB_ENCODING, contentType.getCharset().name()); } }
Example 20
Source File: Jaxb2RootElementHttpMessageConverter.java From java-technology-stack with MIT License | 4 votes |
private void setCharset(@Nullable MediaType contentType, Marshaller marshaller) throws PropertyException { if (contentType != null && contentType.getCharset() != null) { marshaller.setProperty(Marshaller.JAXB_ENCODING, contentType.getCharset().name()); } }