Search in sources :

Example 1 with WebRequest

use of org.springframework.web.context.request.WebRequest in project uhgroupings by uhawaii-system-its-ti-iam.

the class ErrorControllerAdviceTest method illegalArgumentTest.

@Test
public void illegalArgumentTest() {
    IllegalArgumentException IAE = new IllegalArgumentException();
    WebRequest Req = new WebRequest() {

        @Override
        public String getHeader(String s) {
            return null;
        }

        @Override
        public String[] getHeaderValues(String s) {
            return new String[0];
        }

        @Override
        public Iterator<String> getHeaderNames() {
            return null;
        }

        @Override
        public String getParameter(String s) {
            return null;
        }

        @Override
        public String[] getParameterValues(String s) {
            return new String[0];
        }

        @Override
        public Iterator<String> getParameterNames() {
            return null;
        }

        @Override
        public Map<String, String[]> getParameterMap() {
            return null;
        }

        @Override
        public Locale getLocale() {
            return null;
        }

        @Override
        public String getContextPath() {
            return null;
        }

        @Override
        public String getRemoteUser() {
            return null;
        }

        @Override
        public Principal getUserPrincipal() {
            return null;
        }

        @Override
        public boolean isUserInRole(String s) {
            return false;
        }

        @Override
        public boolean isSecure() {
            return false;
        }

        @Override
        public boolean checkNotModified(long l) {
            return false;
        }

        @Override
        public boolean checkNotModified(String s) {
            return false;
        }

        @Override
        public boolean checkNotModified(String s, long l) {
            return false;
        }

        @Override
        public String getDescription(boolean b) {
            return null;
        }

        @Override
        public Object getAttribute(String s, int i) {
            return null;
        }

        @Override
        public void setAttribute(String s, Object o, int i) {
        // intentionally empty
        }

        @Override
        public void removeAttribute(String s, int i) {
        // intentionally empty
        }

        @Override
        public String[] getAttributeNames(int i) {
            return new String[0];
        }

        @Override
        public void registerDestructionCallback(String s, Runnable runnable, int i) {
        // intentionally empty
        }

        @Override
        public Object resolveReference(String s) {
            return null;
        }

        @Override
        public String getSessionId() {
            return null;
        }

        @Override
        public Object getSessionMutex() {
            return null;
        }
    };
    errorControllerAdvice.handleIllegalArgumentException(IAE, Req);
    String IAException = "<404,edu.hawaii.its.api.type.GroupingsHTTPException: " + "Resource not available,[]>";
    assertThat(errorControllerAdvice.handleIllegalArgumentException(IAE, Req).toString(), equalTo(IAException));
}
Also used : WebRequest(org.springframework.web.context.request.WebRequest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with WebRequest

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

the class CustomerStateRefresher method onApplicationEvent.

/**
 * Removes the complete {@link Customer} stored in session and adds a new session variable for just the customer ID. This
 * should occur once the session-based {@link Customer} (all anonymous Customers start out this way) has been persisted.
 *
 * <p>Also updates {@link CustomerState} with the persisted {@link Customer} so that it will always represent the most
 * up-to-date version that is in the database</p>
 *
 * @param request
 * @param databaseCustomer
 */
@Override
public void onApplicationEvent(final CustomerPersistedEvent event) {
    Customer dbCustomer = event.getCustomer();
    // if there is an active request, remove the session-based customer if it exists and update CustomerState
    WebRequest request = BroadleafRequestContext.getBroadleafRequestContext().getWebRequest();
    if (request != null) {
        String customerAttribute = CustomerStateRequestProcessor.getAnonymousCustomerSessionAttributeName();
        String customerIdAttribute = CustomerStateRequestProcessor.getAnonymousCustomerIdSessionAttributeName();
        if (BLCRequestUtils.isOKtoUseSession(request)) {
            Customer sessionCustomer = (Customer) request.getAttribute(customerAttribute, WebRequest.SCOPE_GLOBAL_SESSION);
            // persisted
            if (sessionCustomer != null && sessionCustomer.getId().equals(dbCustomer.getId())) {
                request.removeAttribute(customerAttribute, WebRequest.SCOPE_GLOBAL_SESSION);
                request.setAttribute(customerIdAttribute, dbCustomer.getId(), WebRequest.SCOPE_GLOBAL_SESSION);
            }
        }
        // Update CustomerState if the persisted Customer ID is the same
        if (CustomerState.getCustomer() != null && CustomerState.getCustomer().getId().equals(dbCustomer.getId())) {
            // Copy transient fields from the customer that existed in CustomerState, prior to the DB refresh,
            // to the customer that has been saved (merged) in the DB....
            Customer preMergedCustomer = CustomerState.getCustomer();
            resetTransientFields(preMergedCustomer, dbCustomer);
            CustomerState.setCustomer(dbCustomer);
        }
    }
}
Also used : WebRequest(org.springframework.web.context.request.WebRequest) Customer(org.broadleafcommerce.profile.core.domain.Customer)

Example 3 with WebRequest

use of org.springframework.web.context.request.WebRequest 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 4 with WebRequest

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

the class BroadleafRequestCustomerResolverImpl method setCustomer.

@Override
public void setCustomer(Object customer) {
    WebRequest request = BroadleafRequestContext.getBroadleafRequestContext().getWebRequest();
    request.setAttribute(getCustomerRequestAttributeName(), customer, WebRequest.SCOPE_REQUEST);
}
Also used : ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) WebRequest(org.springframework.web.context.request.WebRequest)

Example 5 with WebRequest

use of org.springframework.web.context.request.WebRequest in project Flare-event-calendar by PollubCafe.

the class GlobalExceptionHandler method handleMethodArgumentNotValid.

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    BindingResult bindingResult = ex.getBindingResult();
    List<ApiFieldError> apiFieldErrors = bindingResult.getFieldErrors().stream().map(fieldError -> new ApiFieldError(fieldError.getField(), fieldError.getDefaultMessage())).collect(toList());
    ApiErrorsView apiErrorsView = new ApiErrorsView(apiFieldErrors);
    return new ResponseEntity<>(apiErrorsView, HttpStatus.UNPROCESSABLE_ENTITY);
}
Also used : RegistrationException(pl.pollub.cs.pentagoncafe.flare.exception.registration.RegistrationException) SendingEmailException(pl.pollub.cs.pentagoncafe.flare.exception.sendingEmail.SendingEmailException) ObjectNotFoundException(pl.pollub.cs.pentagoncafe.flare.exception.ObjectNotFoundException) WebRequest(org.springframework.web.context.request.WebRequest) Autowired(org.springframework.beans.factory.annotation.Autowired) BindingResult(org.springframework.validation.BindingResult) ApiErrorsView(pl.pollub.cs.pentagoncafe.flare.exception.handler.error.ApiErrorsView) TooManyLoginAttempts(pl.pollub.cs.pentagoncafe.flare.exception.auth.TooManyLoginAttempts) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) EventCalendarApplication(pl.pollub.cs.pentagoncafe.flare.EventCalendarApplication) ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) Messages(pl.pollub.cs.pentagoncafe.flare.component.message.Messages) HttpHeaders(org.springframework.http.HttpHeaders) ResetPasswordException(pl.pollub.cs.pentagoncafe.flare.exception.ResetPasswordException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ApiFieldError(pl.pollub.cs.pentagoncafe.flare.exception.handler.error.ApiFieldError) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Logger(org.apache.logging.log4j.Logger) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ResponseEntity(org.springframework.http.ResponseEntity) LogManager(org.apache.logging.log4j.LogManager) BindingResult(org.springframework.validation.BindingResult) ResponseEntity(org.springframework.http.ResponseEntity) ApiErrorsView(pl.pollub.cs.pentagoncafe.flare.exception.handler.error.ApiErrorsView) ApiFieldError(pl.pollub.cs.pentagoncafe.flare.exception.handler.error.ApiFieldError)

Aggregations

WebRequest (org.springframework.web.context.request.WebRequest)20 Test (org.testng.annotations.Test)6 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)5 List (java.util.List)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Locale (java.util.Locale)3 HttpHeaders (org.springframework.http.HttpHeaders)3 HttpStatus (org.springframework.http.HttpStatus)3 ResponseEntity (org.springframework.http.ResponseEntity)3 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)3 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)3 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)3 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)3 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.jupiter.api.Test)2 ErrorAttributeOptions (org.springframework.boot.web.error.ErrorAttributeOptions)2 DefaultErrorAttributes (org.springframework.boot.web.servlet.error.DefaultErrorAttributes)2 ErrorAttributes (org.springframework.boot.web.servlet.error.ErrorAttributes)2 MessageSource (org.springframework.context.MessageSource)2