Search in sources :

Example 1 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project ORCID-Source by ORCID.

the class ValidationManagerForLegacyApiVersionsImpl method doSchemaValidation.

@Override
protected void doSchemaValidation(OrcidMessage orcidMessage) {
    // Hack to support legacy API versions
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes instanceof ServletRequestAttributes) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        String contentType = servletRequestAttributes.getRequest().getContentType();
        if (!StringUtils.containsIgnoreCase(contentType, "json")) {
            super.doSchemaValidation(orcidMessage);
        }
    } else {
        super.doSchemaValidation(orcidMessage);
    }
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 2 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project geode by apache.

the class ShellCommandsControllerJUnitTest method getControllerWebServiceEndpoints.

private List<String> getControllerWebServiceEndpoints() {
    RequestAttributes requestAttrs = RequestContextHolder.getRequestAttributes();
    HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttrs).getRequest();
    String scheme = servletRequest.getScheme();
    try {
        Set<Class<?>> controllerClasses = scanPackageForClassesExtending("org.apache.geode.management.internal.web.controllers", AbstractCommandsController.class);
        List<String> controllerWebServiceEndpoints = new ArrayList<>(controllerClasses.size());
        for (Class<?> controllerClass : controllerClasses) {
            if (!AbstractCommandsController.class.equals(controllerClass)) {
                for (Method method : controllerClass.getMethods()) {
                    if (method.isAnnotationPresent(RequestMapping.class)) {
                        RequestMapping requestMappingAnnotation = method.getAnnotation(RequestMapping.class);
                        String webServiceEndpoint = String.format("%1$s %2$s", requestMappingAnnotation.method()[0], UriUtils.decode(controller.toUri(requestMappingAnnotation.value()[0], scheme).toString()));
                        String[] requestParameters = requestMappingAnnotation.params();
                        if (requestParameters.length > 0) {
                            webServiceEndpoint += "?".concat(StringUtils.join(requestParameters, "&amp;"));
                        }
                        controllerWebServiceEndpoints.add(webServiceEndpoint);
                    }
                }
            }
        }
        return controllerWebServiceEndpoints;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ArrayList(java.util.ArrayList) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Method(java.lang.reflect.Method) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass)

Example 3 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class FrameworkServlet method processRequest.

/**
	 * Process this request, publishing an event regardless of the outcome.
	 * <p>The actual event handling is performed by the abstract
	 * {@link #doService} template method.
	 */
protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    long startTime = System.currentTimeMillis();
    Throwable failureCause = null;
    LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
    LocaleContext localeContext = buildLocaleContext(request);
    RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
    ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());
    initContextHolders(request, localeContext, requestAttributes);
    try {
        doService(request, response);
    } catch (ServletException ex) {
        failureCause = ex;
        throw ex;
    } catch (IOException ex) {
        failureCause = ex;
        throw ex;
    } catch (Throwable ex) {
        failureCause = ex;
        throw new NestedServletException("Request processing failed", ex);
    } finally {
        resetContextHolders(request, previousLocaleContext, previousAttributes);
        if (requestAttributes != null) {
            requestAttributes.requestCompleted();
        }
        if (logger.isDebugEnabled()) {
            if (failureCause != null) {
                this.logger.debug("Could not complete request", failureCause);
            } else {
                if (asyncManager.isConcurrentHandlingStarted()) {
                    logger.debug("Leaving response open for concurrent processing");
                } else {
                    this.logger.debug("Successfully completed request");
                }
            }
        }
        publishRequestHandledEvent(request, response, startTime, failureCause);
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) IOException(java.io.IOException)

Example 4 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-boot by spring-projects.

the class LinksEnhancerTests method setup.

@Before
public void setup() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Before(org.junit.Before)

Example 5 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerScopeIntegrationTests method setUp.

@Before
public void setUp() {
    MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
    oldRequestWithSession.setSession(new MockHttpSession());
    this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);
    MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
    newRequestWithSession.setSession(new MockHttpSession());
    this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) Before(org.junit.Before)

Aggregations

ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)171 HttpServletRequest (javax.servlet.http.HttpServletRequest)64 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)61 RequestAttributes (org.springframework.web.context.request.RequestAttributes)44 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)32 Test (org.junit.jupiter.api.Test)28 lombok.val (lombok.val)25 Test (org.junit.Test)18 HttpSession (javax.servlet.http.HttpSession)11 Before (org.junit.Before)11 Method (java.lang.reflect.Method)8 AbstractTracingSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan)7 TraceSegment (org.apache.skywalking.apm.agent.core.context.trace.TraceSegment)7 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)6 Map (java.util.Map)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 UserSession (com.haulmont.cuba.security.global.UserSession)5 HashMap (java.util.HashMap)4 Locale (java.util.Locale)4 URIBuilder (org.apache.http.client.utils.URIBuilder)4