Search in sources :

Example 6 with MediaType

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);
}
Also used : Charset(java.nio.charset.Charset) MediaType(org.springframework.http.MediaType)

Example 7 with MediaType

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();
    }
}
Also used : MediaType(org.springframework.http.MediaType) IOException(java.io.IOException) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with MediaType

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;
}
Also used : Cookie(javax.servlet.http.Cookie) FlashMap(org.springframework.web.servlet.FlashMap) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) FlashMapManager(org.springframework.web.servlet.FlashMapManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MediaType(org.springframework.http.MediaType) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 9 with MediaType

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));
        }
    };
}
Also used : MediaType(org.springframework.http.MediaType) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 10 with MediaType

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);
}
Also used : ZeroCopyHttpOutputMessage(org.springframework.http.ZeroCopyHttpOutputMessage) MediaType(org.springframework.http.MediaType) File(java.io.File) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

MediaType (org.springframework.http.MediaType)253 Test (org.junit.Test)157 HttpHeaders (org.springframework.http.HttpHeaders)49 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)31 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)24 Charset (java.nio.charset.Charset)21 ArrayList (java.util.ArrayList)21 URI (java.net.URI)20 ByteArrayInputStream (java.io.ByteArrayInputStream)15 IOException (java.io.IOException)15 ServerWebExchange (org.springframework.web.server.ServerWebExchange)15 List (java.util.List)14 HttpInputMessage (org.springframework.http.HttpInputMessage)14 HttpStatus (org.springframework.http.HttpStatus)11 Resource (org.springframework.core.io.Resource)10 HttpEntity (org.springframework.http.HttpEntity)10 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)9 MediaTypeRequestMatcher (org.springframework.security.web.util.matcher.MediaTypeRequestMatcher)9 HttpMediaTypeNotAcceptableException (org.springframework.web.HttpMediaTypeNotAcceptableException)9 InputStream (java.io.InputStream)8