Search in sources :

Example 1 with LinkedCaseInsensitiveMap

use of org.springframework.util.LinkedCaseInsensitiveMap in project spring-framework by spring-projects.

the class UtilNamespaceHandlerTests method testMapWithTypes.

@Test
public void testMapWithTypes() {
    Map map = (Map) this.beanFactory.getBean("mapWithTypes");
    assertTrue(map instanceof LinkedCaseInsensitiveMap);
    assertEquals(this.beanFactory.getBean("testBean"), map.get("bean"));
}
Also used : LinkedCaseInsensitiveMap(org.springframework.util.LinkedCaseInsensitiveMap) LinkedCaseInsensitiveMap(org.springframework.util.LinkedCaseInsensitiveMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with LinkedCaseInsensitiveMap

use of org.springframework.util.LinkedCaseInsensitiveMap in project spring-framework by spring-projects.

the class ServletServerHttpRequest method getHeaders.

@Override
public HttpHeaders getHeaders() {
    if (this.headers == null) {
        this.headers = new HttpHeaders();
        for (Enumeration<?> headerNames = this.servletRequest.getHeaderNames(); headerNames.hasMoreElements(); ) {
            String headerName = (String) headerNames.nextElement();
            for (Enumeration<?> headerValues = this.servletRequest.getHeaders(headerName); headerValues.hasMoreElements(); ) {
                String headerValue = (String) headerValues.nextElement();
                this.headers.add(headerName, headerValue);
            }
        }
        // HttpServletRequest exposes some headers as properties: we should include those if not already present
        try {
            MediaType contentType = this.headers.getContentType();
            if (contentType == null) {
                String requestContentType = this.servletRequest.getContentType();
                if (StringUtils.hasLength(requestContentType)) {
                    contentType = MediaType.parseMediaType(requestContentType);
                    this.headers.setContentType(contentType);
                }
            }
            if (contentType != null && contentType.getCharset() == null) {
                String requestEncoding = this.servletRequest.getCharacterEncoding();
                if (StringUtils.hasLength(requestEncoding)) {
                    Charset charSet = Charset.forName(requestEncoding);
                    Map<String, String> params = new LinkedCaseInsensitiveMap<>();
                    params.putAll(contentType.getParameters());
                    params.put("charset", charSet.toString());
                    MediaType newContentType = new MediaType(contentType.getType(), contentType.getSubtype(), params);
                    this.headers.setContentType(newContentType);
                }
            }
        } catch (InvalidMediaTypeException ex) {
        // Ignore: simply not exposing an invalid content type in HttpHeaders...
        }
        if (this.headers.getContentLength() < 0) {
            int requestContentLength = this.servletRequest.getContentLength();
            if (requestContentLength != -1) {
                this.headers.setContentLength(requestContentLength);
            }
        }
    }
    return this.headers;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) LinkedCaseInsensitiveMap(org.springframework.util.LinkedCaseInsensitiveMap) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset) InvalidMediaTypeException(org.springframework.http.InvalidMediaTypeException)

Example 3 with LinkedCaseInsensitiveMap

use of org.springframework.util.LinkedCaseInsensitiveMap in project spring-framework by spring-projects.

the class ServletServerHttpRequest method initHeaders.

private static HttpHeaders initHeaders(HttpServletRequest request) {
    HttpHeaders headers = new HttpHeaders();
    for (Enumeration<?> names = request.getHeaderNames(); names.hasMoreElements(); ) {
        String name = (String) names.nextElement();
        for (Enumeration<?> values = request.getHeaders(name); values.hasMoreElements(); ) {
            headers.add(name, (String) values.nextElement());
        }
    }
    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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) LinkedCaseInsensitiveMap(org.springframework.util.LinkedCaseInsensitiveMap) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset)

Aggregations

LinkedCaseInsensitiveMap (org.springframework.util.LinkedCaseInsensitiveMap)3 Charset (java.nio.charset.Charset)2 HttpHeaders (org.springframework.http.HttpHeaders)2 MediaType (org.springframework.http.MediaType)2 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Test (org.junit.Test)1 InvalidMediaTypeException (org.springframework.http.InvalidMediaTypeException)1