Search in sources :

Example 11 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class AbstractRequestLoggingFilter method createMessage.

/**
	 * Create a log message for the given request, prefix and suffix.
	 * <p>If {@code includeQueryString} is {@code true}, then the inner part
	 * of the log message will take the form {@code request_uri?query_string};
	 * otherwise the message will simply be of the form {@code request_uri}.
	 * <p>The final message is composed of the inner part as described and
	 * the supplied prefix and suffix.
	 */
protected String createMessage(HttpServletRequest request, String prefix, String suffix) {
    StringBuilder msg = new StringBuilder();
    msg.append(prefix);
    msg.append("uri=").append(request.getRequestURI());
    if (isIncludeQueryString()) {
        String queryString = request.getQueryString();
        if (queryString != null) {
            msg.append('?').append(queryString);
        }
    }
    if (isIncludeClientInfo()) {
        String client = request.getRemoteAddr();
        if (StringUtils.hasLength(client)) {
            msg.append(";client=").append(client);
        }
        HttpSession session = request.getSession(false);
        if (session != null) {
            msg.append(";session=").append(session.getId());
        }
        String user = request.getRemoteUser();
        if (user != null) {
            msg.append(";user=").append(user);
        }
    }
    if (isIncludeHeaders()) {
        msg.append(";headers=").append(new ServletServerHttpRequest(request).getHeaders());
    }
    if (isIncludePayload()) {
        ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
        if (wrapper != null) {
            byte[] buf = wrapper.getContentAsByteArray();
            if (buf.length > 0) {
                int length = Math.min(buf.length, getMaxPayloadLength());
                String payload;
                try {
                    payload = new String(buf, 0, length, wrapper.getCharacterEncoding());
                } catch (UnsupportedEncodingException ex) {
                    payload = "[unknown]";
                }
                msg.append(";payload=").append(payload);
            }
        }
    }
    msg.append(suffix);
    return msg.toString();
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HttpSession(javax.servlet.http.HttpSession) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper)

Example 12 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class WebUtils method isSameOrigin.

/**
	 * Check if the request is a same-origin one, based on {@code Origin}, {@code Host},
	 * {@code Forwarded} and {@code X-Forwarded-Host} headers.
	 * @return {@code true} if the request is a same-origin one, {@code false} in case
	 * of cross-origin request.
	 * @since 4.2
	 */
public static boolean isSameOrigin(HttpRequest request) {
    String origin = request.getHeaders().getOrigin();
    if (origin == null) {
        return true;
    }
    UriComponentsBuilder urlBuilder;
    if (request instanceof ServletServerHttpRequest) {
        // Build more efficiently if we can: we only need scheme, host, port for origin comparison
        HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
        urlBuilder = new UriComponentsBuilder().scheme(servletRequest.getScheme()).host(servletRequest.getServerName()).port(servletRequest.getServerPort()).adaptFromForwardedHeaders(request.getHeaders());
    } else {
        urlBuilder = UriComponentsBuilder.fromHttpRequest(request);
    }
    UriComponents actualUrl = urlBuilder.build();
    UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
    return (actualUrl.getHost().equals(originUrl.getHost()) && getPort(actualUrl) == getPort(originUrl));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest)

Example 13 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class ObjectToStringHttpMessageConverterTests method read.

@Test
public void read() throws IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContentType(MediaType.TEXT_PLAIN_VALUE);
    Short shortValue = Short.valueOf((short) 781);
    request.setContent(shortValue.toString().getBytes(StringHttpMessageConverter.DEFAULT_CHARSET));
    assertEquals(shortValue, this.converter.read(Short.class, new ServletServerHttpRequest(request)));
    Float floatValue = Float.valueOf(123);
    request.setCharacterEncoding("UTF-16");
    request.setContent(floatValue.toString().getBytes("UTF-16"));
    assertEquals(floatValue, this.converter.read(Float.class, new ServletServerHttpRequest(request)));
    Long longValue = Long.valueOf(55819182821331L);
    request.setCharacterEncoding("UTF-8");
    request.setContent(longValue.toString().getBytes("UTF-8"));
    assertEquals(longValue, this.converter.read(Long.class, new ServletServerHttpRequest(request)));
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 14 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class UriComponentsBuilderTests method fromHttpRequestForwardedHeader.

// SPR-11856
@Test
public void fromHttpRequestForwardedHeader() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Forwarded", "proto=https; host=84.198.58.199");
    request.setScheme("http");
    request.setServerName("example.com");
    request.setRequestURI("/rest/mobile/users/1");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("https", result.getScheme());
    assertEquals("84.198.58.199", result.getHost());
    assertEquals("/rest/mobile/users/1", result.getPath());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 15 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedPortMultiValueHeader.

// SPR-12813
@Test
public void fromHttpRequestWithForwardedPortMultiValueHeader() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(9090);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("X-Forwarded-Host", "a.example.org");
    request.addHeader("X-Forwarded-Port", "80,52022");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("http://a.example.org/mvc-showcase", result.toString());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)64 Test (org.junit.Test)33 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)29 HttpRequest (org.springframework.http.HttpRequest)20 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 Before (org.junit.Before)8 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 HttpInputMessage (org.springframework.http.HttpInputMessage)5 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)4 MediaType (org.springframework.http.MediaType)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpEntity (org.springframework.http.HttpEntity)2 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)2 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1