/*
* #%L
* BroadleafCommerce CMS Module
* %%
* Copyright (C) 2009 - 2013 Broadleaf Commerce
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.broadleafcommerce.cms.web.processor;
import org.broadleafcommerce.cms.file.service.StaticAssetService;
import org.broadleafcommerce.common.file.service.StaticAssetPathService;
import org.broadleafcommerce.common.web.BroadleafRequestContext;
import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor;
import org.thymeleaf.standard.expression.Expression;
import org.thymeleaf.standard.expression.StandardExpressions;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
* A Thymeleaf processor that processes the given url through the StaticAssetService's
* {@link StaticAssetService#convertAssetPath(String, String, boolean)} method to determine
* the appropriate URL for the asset to be served from.
*
* @author apazzolini
*/
public class UrlRewriteProcessor extends AbstractAttributeModifierAttrProcessor {
@Resource(name = "blStaticAssetPathService")
protected StaticAssetPathService staticAssetPathService;
/**
* Sets the name of this processor to be used in Thymeleaf template
*/
public UrlRewriteProcessor() {
this("src");
}
protected UrlRewriteProcessor(final String attributeName) {
super(attributeName);
}
@Override
public int getPrecedence() {
return 1000;
}
/**
* @return true if the current request.scheme = HTTPS or if the request.isSecure value is true.
*/
protected boolean isRequestSecure(HttpServletRequest request) {
return ("HTTPS".equalsIgnoreCase(request.getScheme()) || request.isSecure());
}
@Override
protected Map<String, String> getModifiedAttributeValues(Arguments arguments, Element element, String attributeName) {
Map<String, String> attrs = new HashMap<String, String>();
HttpServletRequest request = BroadleafRequestContext.getBroadleafRequestContext().getRequest();
boolean secureRequest = true;
if (request != null) {
secureRequest = isRequestSecure(request);
}
String elementValue = element.getAttributeValue(attributeName);
if (elementValue.startsWith("/")) {
elementValue = "@{ " + elementValue + " }";
}
Expression expression = (Expression) StandardExpressions.getExpressionParser(arguments.getConfiguration())
.parseExpression(arguments.getConfiguration(), arguments, elementValue);
String assetPath = (String) expression.execute(arguments.getConfiguration(), arguments);
// We are forcing an evaluation of @{} from Thymeleaf above which will automatically add a contextPath, no need to
// add it twice
assetPath = staticAssetPathService.convertAssetPath(assetPath, null, secureRequest);
attrs.put("src", assetPath);
return attrs;
}
@Override
protected ModificationType getModificationType(Arguments arguments, Element element, String attributeName, String newAttributeName) {
return ModificationType.SUBSTITUTION;
}
@Override
protected boolean removeAttributeIfEmpty(Arguments arguments, Element element, String attributeName, String newAttributeName) {
return true;
}
@Override
protected boolean recomputeProcessorsAfterExecution(Arguments arguments, Element element, String attributeName) {
return false;
}
}