Search in sources :

Example 1 with TypedEntity

use of org.broadleafcommerce.common.admin.domain.TypedEntity 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)

Example 2 with TypedEntity

use of org.broadleafcommerce.common.admin.domain.TypedEntity in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafAdminTypedEntityRequestFilter method getTypeFieldName.

protected String getTypeFieldName(AdminSection adminSection) {
    try {
        DynamicEntityDao dynamicEntityDao = getDynamicEntityDao(adminSection.getCeilingEntity());
        Class<?> implClass = dynamicEntityDao.getCeilingImplClass(adminSection.getCeilingEntity());
        return ((TypedEntity) implClass.newInstance()).getTypeFieldName();
    } catch (Exception e) {
        return null;
    }
}
Also used : TypedEntity(org.broadleafcommerce.common.admin.domain.TypedEntity) DynamicEntityDao(org.broadleafcommerce.openadmin.server.dao.DynamicEntityDao) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

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