Search in sources :

Example 16 with WebRequest

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

the class PortletMarketplaceController method setUpInitialView.

private void setUpInitialView(WebRequest webRequest, PortletRequest portletRequest, Model model, final String initialFilter) {
    // We'll track and potentially log the time it takes to perform this initialization
    final long timestamp = System.currentTimeMillis();
    final HttpServletRequest servletRequest = this.portalRequestUtils.getPortletHttpRequest(portletRequest);
    final PortletPreferences preferences = portletRequest.getPreferences();
    final boolean isLogLevelDebug = logger.isDebugEnabled();
    final IPerson user = personManager.getPerson(servletRequest);
    final Map<String, Set<?>> registry = getRegistry(user, portletRequest);
    @SuppressWarnings("unchecked") final Set<MarketplaceEntry> marketplaceEntries = (Set<MarketplaceEntry>) registry.get("portlets");
    model.addAttribute("marketplaceEntries", marketplaceEntries);
    @SuppressWarnings("unchecked") Set<PortletCategory> categoryList = (Set<PortletCategory>) registry.get("categories");
    @SuppressWarnings("unchecked") final Set<MarketplaceEntry> featuredPortlets = (Set<MarketplaceEntry>) registry.get("featured");
    model.addAttribute("featuredEntries", featuredPortlets);
    // Determine if the marketplace is going to show the root category
    String showRootCategoryPreferenceValue = preferences.getValue(SHOW_ROOT_CATEGORY_PREFERENCE, "false");
    boolean showRootCategory = Boolean.parseBoolean(showRootCategoryPreferenceValue);
    if (isLogLevelDebug) {
        logger.debug("Going to show Root Category?: {}", Boolean.toString(showRootCategory));
    }
    if (showRootCategory == false) {
        categoryList.remove(this.portletCategoryRegistry.getTopLevelPortletCategory());
    }
    logger.debug("initialFilter: {}", initialFilter);
    String filter = initialFilter == null ? null : categoryList.stream().parallel().map(PortletCategory::getName).filter(cat -> cat.equals(initialFilter)).findAny().orElse("");
    logger.debug("filter: {}", filter);
    model.addAttribute("categoryList", categoryList);
    model.addAttribute("initialFilter", filter);
    logger.debug("Marketplace took {}ms in setUpInitialView for user '{}'", System.currentTimeMillis() - timestamp, user.getUserName());
}
Also used : Arrays(java.util.Arrays) IUserLayoutStore(org.apereo.portal.layout.IUserLayoutStore) RequestParam(org.springframework.web.bind.annotation.RequestParam) XPathExpressionException(javax.xml.xpath.XPathExpressionException) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) WebRequest(org.springframework.web.context.request.WebRequest) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) IMarketplaceRatingDao(org.apereo.portal.portlet.dao.IMarketplaceRatingDao) ResourceResponse(javax.portlet.ResourceResponse) IAuthorizationPrincipal(org.apereo.portal.security.IAuthorizationPrincipal) Model(org.springframework.ui.Model) MarketplaceEntry(org.apereo.portal.rest.layout.MarketplaceEntry) Document(org.w3c.dom.Document) Map(java.util.Map) PermissionHelper(org.apereo.portal.security.PermissionHelper) RenderResponse(javax.portlet.RenderResponse) IMarketplaceRating(org.apereo.portal.portlet.marketplace.IMarketplaceRating) IPortletCategoryRegistry(org.apereo.portal.portlet.registry.IPortletCategoryRegistry) UserLayoutChannelDescription(org.apereo.portal.layout.node.UserLayoutChannelDescription) IPortletDefinitionRegistry(org.apereo.portal.portlet.registry.IPortletDefinitionRegistry) FavoritesUtils(org.apereo.portal.portlets.favorites.FavoritesUtils) Set(java.util.Set) RenderMapping(org.springframework.web.portlet.bind.annotation.RenderMapping) ResourceMapping(org.springframework.web.portlet.bind.annotation.ResourceMapping) String.format(java.lang.String.format) List(java.util.List) IUserInstance(org.apereo.portal.user.IUserInstance) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) IPortalRequestUtils(org.apereo.portal.url.IPortalRequestUtils) Cache(net.sf.ehcache.Cache) Validate(org.apache.commons.lang.Validate) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) IUserInstanceManager(org.apereo.portal.user.IUserInstanceManager) RenderRequest(javax.portlet.RenderRequest) TransformerException(javax.xml.transform.TransformerException) XPath(javax.xml.xpath.XPath) XPathConstants(javax.xml.xpath.XPathConstants) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) XPathExpression(javax.xml.xpath.XPathExpression) Controller(org.springframework.stereotype.Controller) IPortletDefinitionDao(org.apereo.portal.portlet.dao.IPortletDefinitionDao) AuthorizationPrincipalHelper(org.apereo.portal.security.AuthorizationPrincipalHelper) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ResourceRequest(javax.portlet.ResourceRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) IPersonManager(org.apereo.portal.security.IPersonManager) GroupService(org.apereo.portal.services.GroupService) Node(org.w3c.dom.Node) DistributedUserLayout(org.apereo.portal.layout.dlm.DistributedUserLayout) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PortletRequest(javax.portlet.PortletRequest) IPerson(org.apereo.portal.security.IPerson) IPermission(org.apereo.portal.security.IPermission) Logger(org.slf4j.Logger) NodeList(org.w3c.dom.NodeList) IGroupConstants(org.apereo.portal.groups.IGroupConstants) MarketplacePortletDefinition(org.apereo.portal.portlet.marketplace.MarketplacePortletDefinition) PortletPreferences(javax.portlet.PortletPreferences) IPersonalizer(org.apereo.portal.utils.personalize.IPersonalizer) XPathFactory(javax.xml.xpath.XPathFactory) PortletCategory(org.apereo.portal.portlet.om.PortletCategory) Element(org.w3c.dom.Element) TreeMap(java.util.TreeMap) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) EntityIdentifier(org.apereo.portal.EntityIdentifier) IUserLayout(org.apereo.portal.layout.IUserLayout) IMarketplaceService(org.apereo.portal.portlet.marketplace.IMarketplaceService) Collections(java.util.Collections) Set(java.util.Set) HashSet(java.util.HashSet) HttpServletRequest(javax.servlet.http.HttpServletRequest) IPerson(org.apereo.portal.security.IPerson) MarketplaceEntry(org.apereo.portal.rest.layout.MarketplaceEntry) PortletPreferences(javax.portlet.PortletPreferences) PortletCategory(org.apereo.portal.portlet.om.PortletCategory)

Example 17 with WebRequest

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

the class ManagementErrorEndpointTests method errorResponseWithDefaultErrorAttributesSubclassWithoutDelegation.

@Test
void errorResponseWithDefaultErrorAttributesSubclassWithoutDelegation() {
    ErrorAttributes attributes = new DefaultErrorAttributes() {

        @Override
        public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
            return Collections.singletonMap("error", "custom error");
        }
    };
    ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(attributes, this.errorProperties);
    Map<String, Object> response = endpoint.invoke(new ServletWebRequest(new MockHttpServletRequest()));
    assertThat(response).containsExactly(entry("error", "custom error"));
}
Also used : ErrorAttributes(org.springframework.boot.web.servlet.error.ErrorAttributes) DefaultErrorAttributes(org.springframework.boot.web.servlet.error.DefaultErrorAttributes) WebRequest(org.springframework.web.context.request.WebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ErrorAttributeOptions(org.springframework.boot.web.error.ErrorAttributeOptions) DefaultErrorAttributes(org.springframework.boot.web.servlet.error.DefaultErrorAttributes) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 18 with WebRequest

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

the class CartState method setCart.

/**
 * Sets the current cart on the current request
 *
 * @param cart the new cart to set
 */
public static void setCart(Order cart) {
    WebRequest request = BroadleafRequestContext.getBroadleafRequestContext().getWebRequest();
    request.setAttribute(CartStateRequestProcessor.getCartRequestAttributeName(), cart, WebRequest.SCOPE_REQUEST);
}
Also used : WebRequest(org.springframework.web.context.request.WebRequest)

Example 19 with WebRequest

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

the class CartStateRefresher method onApplicationEvent.

/**
 * <p>Resets {@link CartState} with the newly persisted Order. If {@link CartState} was empty, this will only update it if
 * the {@link Order} that has been persisted is the {@link OrderStatus#IN_PROCESS} {@link Order} for the active
 * {@link Customer} (as determined by {@link CustomerState#getCustomer()}. If {@link CartState} was <b>not</b> empty,
 * then it will be replaced only if this newly persisted {@link Order} has the same id.</p>
 *
 * <p>This ensures that whatever is returned from {@link CartState#getCart()} will always be the most up-to-date
 * database version (meaning, safe to write to the DB).</p>
 */
@Override
public void onApplicationEvent(final OrderPersistedEvent event) {
    WebRequest request = BroadleafRequestContext.getBroadleafRequestContext().getWebRequest();
    if (request != null) {
        Order dbOrder = event.getOrder();
        // Update the cart state ONLY IF the IDs of the newly persisted order and whatever is already in CartState match
        boolean emptyCartState = CartState.getCart() == null || CartState.getCart() instanceof NullOrderImpl;
        if (emptyCartState) {
            // If cart state is empty, set it to this newly persisted order if it's the active Customer's cart
            if (CustomerState.getCustomer() != null && CustomerState.getCustomer().getId().equals(dbOrder.getCustomer().getId()) && OrderStatus.IN_PROCESS.equals(dbOrder.getStatus())) {
                CartState.setCart(dbOrder);
            }
        } else if (CartState.getCart().getId().equals(dbOrder.getId())) {
            CartState.setCart(dbOrder);
        }
    }
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) WebRequest(org.springframework.web.context.request.WebRequest) NullOrderImpl(org.broadleafcommerce.core.order.domain.NullOrderImpl)

Example 20 with WebRequest

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

the class ServletRequestMethodArgumentResolverTests method setup.

@BeforeEach
public void setup() throws Exception {
    resolver = new ServletRequestMethodArgumentResolver();
    mavContainer = new ModelAndViewContainer();
    servletRequest = new MockHttpServletRequest("GET", "");
    webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
    method = getClass().getMethod("supportedParams", ServletRequest.class, MultipartRequest.class, HttpSession.class, Principal.class, Locale.class, InputStream.class, Reader.class, WebRequest.class, TimeZone.class, ZoneId.class, HttpMethod.class, PushBuilder.class);
}
Also used : Locale(java.util.Locale) ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ZoneId(java.time.ZoneId) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) InputStream(java.io.InputStream) Reader(java.io.Reader) TimeZone(java.util.TimeZone) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) WebRequest(org.springframework.web.context.request.WebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MultipartRequest(org.springframework.web.multipart.MultipartRequest) PushBuilder(jakarta.servlet.http.PushBuilder) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Principal(java.security.Principal) HttpMethod(org.springframework.http.HttpMethod) BeforeEach(org.junit.jupiter.api.BeforeEach)

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