|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * BroadleafCommerce CMS Module |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2013 Broadleaf Commerce |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package org.broadleafcommerce.core.web.processor; |
| 21 | + |
| 22 | +import org.broadleafcommerce.common.web.BroadleafRequestContext; |
| 23 | +import org.broadleafcommerce.core.catalog.domain.Category; |
| 24 | +import org.broadleafcommerce.core.catalog.domain.Product; |
| 25 | +import org.broadleafcommerce.core.catalog.service.CatalogURLService; |
| 26 | +import org.thymeleaf.Arguments; |
| 27 | +import org.thymeleaf.dom.Element; |
| 28 | +import org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor; |
| 29 | +import org.thymeleaf.standard.expression.Expression; |
| 30 | +import org.thymeleaf.standard.expression.StandardExpressions; |
| 31 | + |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import javax.annotation.Resource; |
| 36 | +import javax.servlet.http.HttpServletRequest; |
| 37 | + |
| 38 | +/** |
| 39 | + * For use with category and product entities. Creates a relative URL using the |
| 40 | + * current URI appended with the url-key (or last fragment of the url). |
| 41 | + * |
| 42 | + * Takes in a category or product object as a parameter. |
| 43 | + * |
| 44 | + * Uses the current request for the baseURI. |
| 45 | + * |
| 46 | + * This implementation will also a categoryId or productId to the end of the URL it generates. |
| 47 | + * |
| 48 | + * @author bpolster |
| 49 | + */ |
| 50 | +public class CatalogRelativeHrefProcessor extends AbstractAttributeModifierAttrProcessor { |
| 51 | + |
| 52 | + private static final String RHREF = "rhref"; |
| 53 | + private static final String HREF = "href"; |
| 54 | + |
| 55 | + @Resource(name = "blCatalogURLService") |
| 56 | + protected CatalogURLService catalogURLService; |
| 57 | + |
| 58 | + public CatalogRelativeHrefProcessor() { |
| 59 | + super(RHREF); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected Map<String, String> getModifiedAttributeValues(Arguments arguments, Element element, String attributeName) { |
| 64 | + Expression expression = (Expression) StandardExpressions.getExpressionParser(arguments.getConfiguration()) |
| 65 | + .parseExpression(arguments.getConfiguration(), arguments, element.getAttributeValue(attributeName)); |
| 66 | + HttpServletRequest request = BroadleafRequestContext.getBroadleafRequestContext().getRequest(); |
| 67 | + |
| 68 | + String relativeHref = buildRelativeHref(expression, arguments, request); |
| 69 | + |
| 70 | + Map<String, String> attrs = new HashMap<String, String>(); |
| 71 | + attrs.put(HREF, relativeHref); |
| 72 | + return attrs; |
| 73 | + } |
| 74 | + |
| 75 | + protected String buildRelativeHref(Expression expression, Arguments arguments, HttpServletRequest request) { |
| 76 | + Object result = expression.execute(arguments.getConfiguration(), arguments); |
| 77 | + String currentUrl = request.getRequestURI(); |
| 78 | + |
| 79 | + if (request.getQueryString() != null) { |
| 80 | + currentUrl = currentUrl + "?" + request.getQueryString(); |
| 81 | + } |
| 82 | + |
| 83 | + if (result instanceof Product) { |
| 84 | + return catalogURLService.buildRelativeProductURL(currentUrl, (Product) result); |
| 85 | + } else if (result instanceof Category) { |
| 86 | + return catalogURLService.buildRelativeCategoryURL(currentUrl, (Category) result); |
| 87 | + } |
| 88 | + return ""; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected ModificationType getModificationType(Arguments arguments, Element element, String attributeName, String newAttributeName) { |
| 93 | + return ModificationType.SUBSTITUTION; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected boolean removeAttributeIfEmpty(Arguments arguments, Element element, String attributeName, String newAttributeName) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + protected boolean recomputeProcessorsAfterExecution(Arguments arguments, Element element, String attributeName) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public int getPrecedence() { |
| 108 | + return 0; |
| 109 | + } |
| 110 | +} |
0 commit comments