Search in sources :

Example 1 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project fastjson by alibaba.

the class FastJsonpResponseBodyAdvice method beforeBodyWriteInternal.

/**
     * Invoked only if the converter type is {@code FastJsonpHttpMessageConverter4}.
     */
public void beforeBodyWriteInternal(MappingFastJsonValue bodyContainer, MediaType contentType, MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    for (String name : this.jsonpQueryParamNames) {
        String value = servletRequest.getParameter(name);
        if (value != null) {
            if (!isValidJsonpQueryParam(value)) {
                continue;
            }
            MediaType contentTypeToUse = getContentType(contentType, request, response);
            response.getHeaders().setContentType(contentTypeToUse);
            bodyContainer.setJsonpFunction(value);
            break;
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MediaType(org.springframework.http.MediaType)

Example 2 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 3 with ServletServerHttpRequest

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

the class AbstractJsonpResponseBodyAdvice method beforeBodyWriteInternal.

@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType, MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    for (String name : this.jsonpQueryParamNames) {
        String value = servletRequest.getParameter(name);
        if (value != null) {
            if (!isValidJsonpQueryParam(value)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Ignoring invalid jsonp parameter value: " + value);
                }
                continue;
            }
            MediaType contentTypeToUse = getContentType(contentType, request, response);
            response.getHeaders().setContentType(contentTypeToUse);
            bodyContainer.setJsonpFunction(value);
            break;
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MediaType(org.springframework.http.MediaType)

Example 4 with ServletServerHttpRequest

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

the class ServletUriComponentsBuilder method initFromRequest.

/**
	 * Initialize a builder with a scheme, host,and port (but not path and query).
	 */
private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest request) {
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    String scheme = uriComponents.getScheme();
    String host = uriComponents.getHost();
    int port = uriComponents.getPort();
    ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
    builder.scheme(scheme);
    builder.host(host);
    if (("http".equals(scheme) && port != 80) || ("https".equals(scheme) && port != 443)) {
        builder.port(port);
    }
    return builder;
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) UriComponents(org.springframework.web.util.UriComponents)

Example 5 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project uavstack by uavorg.

the class JSONPResponseBodyAdvice method beforeBodyWrite.

public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    ResponseJSONP responseJsonp = returnType.getMethodAnnotation(ResponseJSONP.class);
    if (responseJsonp == null) {
        responseJsonp = returnType.getContainingClass().getAnnotation(ResponseJSONP.class);
    }
    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    String callbackMethodName = servletRequest.getParameter(responseJsonp.callback());
    if (!IOUtils.isValidJsonpQueryParam(callbackMethodName)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Invalid jsonp parameter value:" + callbackMethodName);
        }
        callbackMethodName = null;
    }
    JSONPObject jsonpObject = new JSONPObject(callbackMethodName);
    jsonpObject.addParameter(body);
    beforeBodyWriteInternal(jsonpObject, selectedContentType, returnType, request, response);
    return jsonpObject;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) JSONPObject(com.alibaba.fastjson.JSONPObject) ResponseJSONP(com.alibaba.fastjson.support.spring.annotation.ResponseJSONP)

Aggregations

ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)111 Test (org.junit.jupiter.api.Test)39 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)39 HttpRequest (org.springframework.http.HttpRequest)31 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)22 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)13 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)10 IOException (java.io.IOException)8 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)8 BeforeEach (org.junit.jupiter.api.BeforeEach)7 HttpHeaders (org.springframework.http.HttpHeaders)7 MediaType (org.springframework.http.MediaType)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 HttpInputMessage (org.springframework.http.HttpInputMessage)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 URI (java.net.URI)4 ResponseEntity (org.springframework.http.ResponseEntity)4