use of org.springframework.http.MediaType in project spring-framework by spring-projects.
the class MockClientHttpResponse method getCharset.
private Charset getCharset() {
Charset charset = null;
MediaType contentType = getHeaders().getContentType();
if (contentType != null) {
charset = contentType.getCharset();
}
return (charset != null ? charset : StandardCharsets.UTF_8);
}
use of org.springframework.http.MediaType in project spring-framework by spring-projects.
the class MockHttpServletResponse method setContentType.
@Override
public void setContentType(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();
}
}
use of org.springframework.http.MediaType in project spring-framework by spring-projects.
the class MockHttpServletRequestBuilder method buildRequest.
/**
* Build a {@link MockHttpServletRequest}.
*/
@Override
public final MockHttpServletRequest buildRequest(ServletContext servletContext) {
MockHttpServletRequest request = createServletRequest(servletContext);
request.setAsyncSupported(true);
request.setMethod(this.method);
String requestUri = this.url.getRawPath();
request.setRequestURI(requestUri);
if (this.url.getScheme() != null) {
request.setScheme(this.url.getScheme());
}
if (this.url.getHost() != null) {
request.setServerName(this.url.getHost());
}
if (this.url.getPort() != -1) {
request.setServerPort(this.url.getPort());
}
updatePathRequestProperties(request, requestUri);
if (this.secure != null) {
request.setSecure(this.secure);
}
if (this.principal != null) {
request.setUserPrincipal(this.principal);
}
if (this.session != null) {
request.setSession(this.session);
}
request.setCharacterEncoding(this.characterEncoding);
request.setContent(this.content);
request.setContentType(this.contentType);
for (String name : this.headers.keySet()) {
for (Object value : this.headers.get(name)) {
request.addHeader(name, value);
}
}
if (this.url.getRawQuery() != null) {
request.setQueryString(this.url.getRawQuery());
}
addRequestParams(request, UriComponentsBuilder.fromUri(this.url).build().getQueryParams());
for (String name : this.parameters.keySet()) {
for (String value : this.parameters.get(name)) {
request.addParameter(name, value);
}
}
if (this.content != null && this.content.length > 0) {
String requestContentType = request.getContentType();
if (requestContentType != null) {
MediaType mediaType = MediaType.parseMediaType(requestContentType);
if (MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType)) {
addRequestParams(request, parseFormData(mediaType));
}
}
}
if (!ObjectUtils.isEmpty(this.cookies)) {
request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
}
if (!ObjectUtils.isEmpty(this.locales)) {
request.setPreferredLocales(this.locales);
}
for (String name : this.requestAttributes.keySet()) {
request.setAttribute(name, this.requestAttributes.get(name));
}
for (String name : this.sessionAttributes.keySet()) {
request.getSession().setAttribute(name, this.sessionAttributes.get(name));
}
FlashMap flashMap = new FlashMap();
flashMap.putAll(this.flashAttributes);
FlashMapManager flashMapManager = getFlashMapManager(request);
flashMapManager.saveOutputFlashMap(flashMap, request, new MockHttpServletResponse());
return request;
}
use of org.springframework.http.MediaType in project spring-framework by spring-projects.
the class ContentResultMatchers method contentTypeCompatibleWith.
/**
* 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 new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
String actual = result.getResponse().getContentType();
assertTrue("Content type not set", actual != null);
MediaType actualContentType = MediaType.parseMediaType(actual);
assertTrue("Content type [" + actual + "] is not compatible with [" + contentType + "]", actualContentType.isCompatibleWith(contentType));
}
};
}
use of org.springframework.http.MediaType in project spring-framework by spring-projects.
the class ResourceRegionHttpMessageWriter method writeResourceRegion.
private Mono<Void> writeResourceRegion(ResourceRegion region, ReactiveHttpOutputMessage outputMessage) {
if (outputMessage instanceof ZeroCopyHttpOutputMessage) {
Optional<File> file = getFile(region.getResource());
if (file.isPresent()) {
ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) outputMessage;
return zeroCopyResponse.writeWith(file.get(), region.getPosition(), region.getCount());
}
}
// non-zero copy fallback, using ResourceRegionEncoder
DataBufferFactory bufferFactory = outputMessage.bufferFactory();
MediaType contentType = outputMessage.getHeaders().getContentType();
Map<String, Object> hints = Collections.emptyMap();
Flux<DataBuffer> body = this.encoder.encode(Mono.just(region), bufferFactory, TYPE, contentType, hints);
return outputMessage.writeWith(body);
}
Aggregations