Search in sources :

Example 6 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project engine by craftercms.

the class StaticAssetsRequestHandler method handleRequest.

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    checkAndPrepare(request, response, true);
    SiteContext siteContext = SiteContext.getCurrent();
    String path = getPath(request, siteContext);
    if (siteContext == null) {
        throw new IllegalStateException("No current site context found");
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Trying to get content for static asset at [context=" + siteContext + ", path='" + path + "']");
    }
    Content content = getContent(siteContext, path);
    if (content == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("No static asset found at [context=" + siteContext + ", path='" + path + "'] - returning 404");
        }
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    MediaType mediaType = getMediaType(path);
    if (mediaType != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Determined media type '" + mediaType + "' for static asset at [context=" + siteContext + ", path='" + path + "']");
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("No media type found for static asset at [context=" + siteContext + ", path='" + path + "'] - not sending a content-type header");
        }
    }
    if ((new ServletWebRequest(request, response)).checkNotModified(content.getLastModified())) {
        if (logger.isDebugEnabled()) {
            logger.debug("Static asset not modified - returning 304");
        }
        return;
    }
    setHeaders(response, content, mediaType);
    if (disableCaching) {
        if (logger.isDebugEnabled()) {
            logger.debug("Caching disabled on client");
        }
        HttpUtils.disableCaching(response);
    }
    if (METHOD_HEAD.equals(request.getMethod())) {
        logger.trace("HEAD request - skipping content");
        return;
    }
    writeContent(response, content);
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Content(org.craftercms.core.service.Content) MediaType(org.springframework.http.MediaType) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 7 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project alf.io by alfio-event.

the class ReservationFlowIntegrationTest method reserveTicket.

private String reserveTicket(String eventName) {
    ReservationForm reservationForm = new ReservationForm();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    ServletWebRequest servletWebRequest = new ServletWebRequest(request);
    BindingResult bindingResult = new BeanPropertyBindingResult(reservationForm, "reservation");
    Model model = new BindingAwareModelMap();
    RedirectAttributes redirectAttributes = new RedirectAttributesModelMap();
    TicketReservationModification ticketReservation = new TicketReservationModification();
    ticketReservation.setAmount(1);
    ticketReservation.setTicketCategoryId(ticketCategoryRepository.findByEventId(event.getId()).stream().findFirst().map(TicketCategory::getId).orElseThrow(IllegalStateException::new));
    reservationForm.setReservation(Collections.singletonList(ticketReservation));
    return eventController.reserveTicket(eventName, reservationForm, bindingResult, model, servletWebRequest, redirectAttributes, Locale.ENGLISH);
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Model(org.springframework.ui.Model) BindingAwareModelMap(org.springframework.validation.support.BindingAwareModelMap) ReservationForm(alfio.controller.form.ReservationForm) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) TicketReservationModification(alfio.model.modification.TicketReservationModification)

Example 8 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project dubion by valsamiq.

the class CustomSignInAdapter method signIn.

@Override
public String signIn(String userId, Connection<?> connection, NativeWebRequest request) {
    try {
        UserDetails user = userDetailsService.loadUserByUsername(userId);
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        String jwt = tokenProvider.createToken(authenticationToken, false);
        ServletWebRequest servletWebRequest = (ServletWebRequest) request;
        servletWebRequest.getResponse().addCookie(getSocialAuthenticationCookie(jwt));
    } catch (AuthenticationException ae) {
        log.error("Social authentication error");
        log.trace("Authentication exception trace: {}", ae);
    }
    return jHipsterProperties.getSocial().getRedirectAfterSignIn();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) AuthenticationException(org.springframework.security.core.AuthenticationException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 9 with ServletWebRequest

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

the class SessionFixationProtectionFilter method abortUser.

protected void abortUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
    SecurityContextHolder.clearContext();
    cookieUtils.invalidateCookie(response, SessionFixationProtectionCookie.COOKIE_NAME);
    if (BLCRequestUtils.isOKtoUseSession(new ServletWebRequest(request))) {
        request.getSession().invalidate();
    }
    response.sendRedirect("/");
}
Also used : ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 10 with ServletWebRequest

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

the class RepeatSubmitProtectionFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    boolean useSession = true;
    if (BroadleafRequestContext.getBroadleafRequestContext() != null && BroadleafRequestContext.getBroadleafRequestContext().getWebRequest() != null) {
        if (!BLCRequestUtils.isOKtoUseSession(BroadleafRequestContext.getBroadleafRequestContext().getWebRequest())) {
            useSession = false;
        }
    } else if (!BLCRequestUtils.isOKtoUseSession(new ServletWebRequest((HttpServletRequest) request))) {
        useSession = false;
    }
    if (useSession) {
        String sessionId;
        String requestURI;
        synchronized (requests) {
            sessionId = ((HttpServletRequest) request).getSession().getId();
            requestURI = ((HttpServletRequest) request).getRequestURI();
            if (requests.containsKey(sessionId) && requests.get(sessionId).contains(requestURI)) {
                // we are currently already processing this request
                ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_NO_CONTENT);
                return;
            }
            List<String> myRequests = requests.get(sessionId);
            if (myRequests == null) {
                myRequests = new ArrayList<String>();
                requests.put(sessionId, myRequests);
            }
            myRequests.add(requestURI);
        }
        try {
            chain.doFilter(request, response);
        } finally {
            synchronized (requests) {
                List<String> myRequests = requests.get(sessionId);
                myRequests.remove(requestURI);
                if (myRequests.isEmpty()) {
                    requests.remove(sessionId);
                }
            }
        }
    } else {
        chain.doFilter(request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Aggregations

ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)224 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)80 Test (org.junit.jupiter.api.Test)79 MethodParameter (org.springframework.core.MethodParameter)50 BeforeEach (org.junit.jupiter.api.BeforeEach)41 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)32 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)30 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)28 Method (java.lang.reflect.Method)21 Test (org.junit.Test)21 MockMultipartHttpServletRequest (org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest)21 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)18 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)14 TestBean (org.springframework.beans.testfixture.beans.TestBean)14 RequestParam (org.springframework.web.bind.annotation.RequestParam)14 MockPart (org.springframework.web.testfixture.servlet.MockPart)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 MultipartFile (org.springframework.web.multipart.MultipartFile)13 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)11 IOException (java.io.IOException)10