Search in sources :

Example 31 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project irida by phac-nml.

the class UnitTestListener method testRunStarted.

/**
 * {@inheritDoc}
 */
public void testRunStarted(Description description) throws Exception {
    logger.debug("Configuring Spring MockHTTPServletRequest.");
    // fake out the servlet response so that the URI builder will work.
    RequestAttributes ra = new ServletRequestAttributes(new MockHttpServletRequest());
    RequestContextHolder.setRequestAttributes(ra);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 32 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project irida by phac-nml.

the class RootControllerTest method setUp.

@Before
public void setUp() {
    // fake out the servlet response so that the URI builder will work.
    RequestAttributes ra = new ServletRequestAttributes(new MockHttpServletRequest());
    RequestContextHolder.setRequestAttributes(ra);
    controller.initLinks();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) Before(org.junit.Before)

Example 33 with ServletRequestAttributes

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

the class FrameworkMvcUriComponentsBuilder method getWebApplicationContext.

private static WebApplicationContext getWebApplicationContext() {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes == null) {
        logger.debug("No request bound to the current thread: not in a DispatcherServlet request?");
        return null;
    }
    HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
    WebApplicationContext wac = (WebApplicationContext) request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (wac == null) {
        logger.debug("No WebApplicationContext found: not in a DispatcherServlet request?");
        return null;
    }
    return wac;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 34 with ServletRequestAttributes

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

the class BroadleafContextUtil method establishThinRequestContextInternal.

/**
 * Adds request and site to the BroadleafRequestContext
 *
 * If includeTheme is true then also adds the Theme.
 * If includeSandBox is true then also adds the SandBox.
 *
 * @param includeTheme
 * @param includeSandBox
 */
protected void establishThinRequestContextInternal(boolean includeTheme, boolean includeSandBox) {
    BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
    if (brc.getRequest() == null) {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = req.getSession(false);
        SecurityContext ctx = readSecurityContextFromSession(session);
        if (ctx != null) {
            SecurityContextHolder.setContext(ctx);
        }
        brc.setRequest(req);
    }
    WebRequest wr = brc.getWebRequest();
    if (brc.getNonPersistentSite() == null) {
        brc.setNonPersistentSite(siteResolver.resolveSite(wr, true));
        if (includeSandBox) {
            brc.setSandBox(sbResolver.resolveSandBox(wr, brc.getNonPersistentSite()));
        }
        brc.setDeployBehavior(deployBehaviorUtil.isProductionSandBoxMode() ? DeployBehavior.CLONE_PARENT : DeployBehavior.OVERWRITE_PARENT);
    }
    if (includeTheme) {
        if (brc.getTheme() == null) {
            brc.setTheme(themeResolver.resolveTheme(wr));
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebRequest(org.springframework.web.context.request.WebRequest) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) HttpSession(javax.servlet.http.HttpSession) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) SecurityContext(org.springframework.security.core.context.SecurityContext)

Example 35 with ServletRequestAttributes

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

the class BLCJSResourceResolver method convertResource.

protected Resource convertResource(Resource origResource, String resourceFileName) throws IOException {
    byte[] bytes = FileCopyUtils.copyToByteArray(origResource.getInputStream());
    String content = new String(bytes, DEFAULT_CHARSET);
    String newContent = content;
    if (!StringUtils.isEmpty(content)) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        newContent = newContent.replace("//BLC-SERVLET-CONTEXT", request.getContextPath());
        String siteBaseUrl = urlResolver.getSiteBaseUrl();
        if (!StringUtils.isEmpty(siteBaseUrl)) {
            newContent = newContent.replace("//BLC-SITE-BASEURL", siteBaseUrl);
        }
    }
    return new GeneratedResource(newContent.getBytes(), resourceFileName);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) GeneratedResource(org.broadleafcommerce.common.resource.GeneratedResource)

Aggregations

ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)205 HttpServletRequest (javax.servlet.http.HttpServletRequest)92 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)62 RequestAttributes (org.springframework.web.context.request.RequestAttributes)50 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)32 Test (org.junit.jupiter.api.Test)28 lombok.val (lombok.val)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)22 Test (org.junit.Test)18 AuthChecker (com.code.server.login.anotation.AuthChecker)17 HttpSession (javax.servlet.http.HttpSession)12 Before (org.junit.Before)12 HashMap (java.util.HashMap)11 Method (java.lang.reflect.Method)8 Date (java.util.Date)7 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