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 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()); } }
@Override public boolean supportsWriteOnly(@Nullable MediaType mediaType) { return TEXT_HTML.isCompatibleWith(mediaType); }
/** * Set the 'Content-Type' header of the request. * @param contentType the content type * @since 4.1.2 */ public MockHttpServletRequestBuilder contentType(String contentType) { this.contentType = MediaType.parseMediaType(contentType).toString(); return this; }
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 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); }
@Test public void writeOtherCharset() throws IOException, SAXException { Feed feed = new Feed("atom_1.0"); feed.setTitle("title"); String encoding = "ISO-8859-1"; feed.setEncoding(encoding); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(feed, null, outputMessage); assertEquals("Invalid content-type", new MediaType("application", "atom+xml", Charset.forName(encoding)), outputMessage.getHeaders().getContentType()); }
@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); } }
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 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); } }
if (HttpMethod.OPTIONS.matches(request.getMethod())) { HttpOptionsHandler handler = new HttpOptionsHandler(methods); return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD); throw new HttpRequestMethodNotSupportedException(request.getMethod(), methods); Set<MediaType> mediaTypes = helper.getConsumableMediaTypes(); MediaType contentType = null; if (StringUtils.hasLength(request.getContentType())) { try { contentType = MediaType.parseMediaType(request.getContentType());
@Override public MultiValueMap<String, String> read(@Nullable Class<? extends MultiValueMap<String, ?>> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = (contentType != null && contentType.getCharset() != null ? contentType.getCharset() : this.charset); String body = StreamUtils.copyToString(inputMessage.getBody(), charset); String[] pairs = StringUtils.tokenizeToStringArray(body, "&"); MultiValueMap<String, String> result = new LinkedMultiValueMap<>(pairs.length); for (String pair : pairs) { int idx = pair.indexOf('='); if (idx == -1) { result.add(URLDecoder.decode(pair, charset.name()), null); } else { String name = URLDecoder.decode(pair.substring(0, idx), charset.name()); String value = URLDecoder.decode(pair.substring(idx + 1), charset.name()); result.add(name, value); } } return result; }
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 boolean shouldParse(HttpServletRequest request) { if (!HTTP_METHODS.contains(request.getMethod())) { return false; } try { MediaType mediaType = MediaType.parseMediaType(request.getContentType()); return MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType); } catch (IllegalArgumentException ex) { return false; } }
private Mono<Void> writeMultipart( MultiValueMap<String, ?> map, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) { byte[] boundary = generateMultipartBoundary(); Map<String, String> params = new HashMap<>(2); params.put("boundary", new String(boundary, StandardCharsets.US_ASCII)); params.put("charset", getCharset().name()); outputMessage.getHeaders().setContentType(new MediaType(MediaType.MULTIPART_FORM_DATA, params)); LogFormatUtils.traceDebug(logger, traceOn -> Hints.getLogPrefix(hints) + "Encoding " + (isEnableLoggingRequestDetails() ? LogFormatUtils.formatValue(map, !traceOn) : "parts " + map.keySet() + " (content masked)")); Flux<DataBuffer> body = Flux.fromIterable(map.entrySet()) .concatMap(entry -> encodePartValues(boundary, entry.getKey(), entry.getValue())) .concatWith(Mono.just(generateLastLine(boundary))); return outputMessage.writeWith(body); }
@Override public String getCharacterEncoding() { MediaType type = this.outputMessage.getHeaders().getContentType(); return (type != null && type.getCharset() != null ? type.getCharset().name() : null); }
/** * Set the content type of the response to the configured * {@link #setContentType(String) content type} unless the * {@link View#SELECTED_CONTENT_TYPE} request attribute is present and set * to a concrete media type. */ protected void setResponseContentType(HttpServletRequest request, HttpServletResponse response) { MediaType mediaType = (MediaType) request.getAttribute(View.SELECTED_CONTENT_TYPE); if (mediaType != null && mediaType.isConcrete()) { response.setContentType(mediaType.toString()); } else { response.setContentType(getContentType()); } }
/** * Resolve file extension via {@link ServletContext#getMimeType(String)} * and also delegate to base class for a potential * {@link org.springframework.http.MediaTypeFactory} lookup. */ @Override @Nullable protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension) throws HttpMediaTypeNotAcceptableException { MediaType mediaType = null; String mimeType = this.servletContext.getMimeType("file." + extension); if (StringUtils.hasText(mimeType)) { mediaType = MediaType.parseMediaType(mimeType); } if (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) { MediaType superMediaType = super.handleNoMatch(webRequest, extension); if (superMediaType != null) { mediaType = superMediaType; } } return mediaType; }
public BufferedImageHttpMessageConverter() { String[] readerMediaTypes = ImageIO.getReaderMIMETypes(); for (String mediaType : readerMediaTypes) { if (StringUtils.hasText(mediaType)) { this.readableMediaTypes.add(MediaType.parseMediaType(mediaType)); } } String[] writerMediaTypes = ImageIO.getWriterMIMETypes(); for (String mediaType : writerMediaTypes) { if (StringUtils.hasText(mediaType)) { this.defaultContentType = MediaType.parseMediaType(mediaType); break; } } }
/** * Extends the base class * {@link PathExtensionContentNegotiationStrategy#getMediaTypeForResource} * with the ability to also look up through the ServletContext. * @param resource the resource to look up * @return the MediaType for the extension, or {@code null} if none found * @since 4.3 */ @Override public MediaType getMediaTypeForResource(Resource resource) { MediaType mediaType = null; String mimeType = this.servletContext.getMimeType(resource.getFilename()); if (StringUtils.hasText(mimeType)) { mediaType = MediaType.parseMediaType(mimeType); } if (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) { MediaType superMediaType = super.getMediaTypeForResource(resource); if (superMediaType != null) { mediaType = superMediaType; } } return mediaType; }