Search in sources :

Example 1 with IteratorEnumeration

use of org.apache.commons.collections4.iterators.IteratorEnumeration in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafAdminRequestFilter method forwardToConflictDestination.

/**
 * Forward the user to the conflict error page.
 *
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public void forwardToConflictDestination(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    response.setStatus(HttpServletResponse.SC_CONFLICT);
    final Map reducedMap = new LinkedHashMap(request.getParameterMap());
    reducedMap.remove(BroadleafAdminRequestProcessor.CATALOG_REQ_PARAM);
    reducedMap.remove(BroadleafAdminRequestProcessor.PROFILE_REQ_PARAM);
    reducedMap.remove(BroadleafAdminRequestProcessor.SANDBOX_REQ_PARAM);
    reducedMap.remove(StaleStateProtectionServiceImpl.STATEVERSIONTOKENPARAMETER);
    reducedMap.remove(BroadleafSiteResolver.SELECTED_SITE_URL_PARAM);
    final HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(request) {

        @Override
        public String getParameter(String name) {
            Object temp = reducedMap.get(name);
            Object[] response = new Object[0];
            if (temp != null) {
                ArrayUtils.addAll(response, temp);
            }
            if (ArrayUtils.isEmpty(response)) {
                return null;
            } else {
                return (String) response[0];
            }
        }

        @Override
        public Map getParameterMap() {
            return reducedMap;
        }

        @Override
        public Enumeration getParameterNames() {
            return new IteratorEnumeration(reducedMap.keySet().iterator());
        }

        @Override
        public String[] getParameterValues(String name) {
            return (String[]) reducedMap.get(name);
        }
    };
    requestProcessor.process(new ServletWebRequest(wrapper, response));
    wrapper.getRequestDispatcher("/sc_conflict").forward(wrapper, response);
}
Also used : HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) IteratorEnumeration(org.apache.commons.collections4.iterators.IteratorEnumeration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with IteratorEnumeration

use of org.apache.commons.collections4.iterators.IteratorEnumeration in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafAdminTypedEntityRequestFilter method isRequestForTypedEntity.

@SuppressWarnings("unchecked")
public boolean isRequestForTypedEntity(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    String servletPath = request.getServletPath();
    if (!servletPath.contains(":")) {
        return false;
    }
    // Find the Admin Section for the typed entity.
    String sectionKey = getSectionKeyFromRequest(request);
    AdminSection typedEntitySection = adminNavigationService.findAdminSectionByURI(sectionKey);
    // If the Typed Entity Section does not exist, continue with the filter chain.
    if (typedEntitySection == null) {
        return false;
    }
    // Check if the item requested matches the item section
    TypedEntity typedEntity = getTypedEntityFromServletPathId(servletPath, typedEntitySection.getCeilingEntity());
    if (typedEntity != null && !typeMatchesAdminSection(typedEntity, sectionKey)) {
        String redirectUrl = getTypeAdminSectionMismatchUrl(typedEntity, typedEntitySection.getCeilingEntity(), request.getRequestURI(), sectionKey);
        response.sendRedirect(redirectUrl);
        return true;
    }
    // Check if admin user has access to this section.
    if (!adminUserHasAccess(typedEntitySection)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access is denied");
        return true;
    }
    // Add the typed entity admin section to the request.
    request.setAttribute("typedEntitySection", typedEntitySection);
    // Find the type and build the new path.
    String type = getEntityTypeFromRequest(request);
    final String forwardPath = servletPath.replace(type, "");
    // Get the type field name on the Entity for the given section.
    String typedFieldName = getTypeFieldName(typedEntitySection);
    // Build out the new parameter map to be forwarded.
    final Map parameters = new LinkedHashMap(request.getParameterMap());
    if (typedFieldName != null) {
        parameters.put(typedFieldName, new String[] { type.substring(1).toUpperCase() });
    }
    // Build our request wrapper.
    final HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(request) {

        @Override
        public String getParameter(String name) {
            String[] response = (String[]) parameters.get(name);
            if (ArrayUtils.isEmpty(response)) {
                return null;
            } else {
                return response[0];
            }
        }

        @Override
        public Map getParameterMap() {
            return parameters;
        }

        @Override
        public Enumeration getParameterNames() {
            return new IteratorEnumeration(parameters.keySet().iterator());
        }

        @Override
        public String[] getParameterValues(String name) {
            return (String[]) parameters.get(name);
        }

        @Override
        public String getServletPath() {
            return forwardPath;
        }
    };
    requestProcessor.process(new ServletWebRequest(wrapper, response));
    // Forward the wrapper to the appropriate path
    wrapper.getRequestDispatcher(wrapper.getServletPath()).forward(wrapper, response);
    return true;
}
Also used : HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) IteratorEnumeration(org.apache.commons.collections4.iterators.IteratorEnumeration) AdminSection(org.broadleafcommerce.openadmin.server.security.domain.AdminSection) TypedEntity(org.broadleafcommerce.common.admin.domain.TypedEntity) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)2 IteratorEnumeration (org.apache.commons.collections4.iterators.IteratorEnumeration)2 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)2 HashMap (java.util.HashMap)1 TypedEntity (org.broadleafcommerce.common.admin.domain.TypedEntity)1 AdminSection (org.broadleafcommerce.openadmin.server.security.domain.AdminSection)1