【问题标题】:AEM: links transform/rewrite in a pageAEM:链接在页面中转换/重写
【发布时间】:2016-09-07 02:24:07
【问题描述】:

在我当前的项目中,大坝资产正在移动到 CDN 服务器,我当前在 img 元素中的 src 属性应该引用相应的 CDN 服务器图像,而不是大坝中的那个。
例如:我当前的 img 元素如下所示:

我在 CDN 服务器中有对应的 123.jpg 图片。现在,应该更改 src 以引用 CDN 服务器映像。

我可以在组件级别更改它但我希望使用 OSGi 服务或其他东西在更全局的级别更改它!

任何输入?

【问题讨论】:

  • 嘿,你实现了吗?如果是,在哪个 AEM 版本中?我试图在 6.3 中实现相同的目标。任何信息将不胜感激

标签: java osgi aem


【解决方案1】:

您可以使用 Sling 重写器。 documents 可在 Apache Sling 网站上找到。我在下面包含了一个示例 Sling Transformer。您还需要添加一个配置节点。您可以在 ACS Commons Versioned Clientlibs 功能中找到其他示例。

添加这个 Transformer 并更新 StartElement 方法来处理你的路径更新:

import java.io.IOException;
import java.util.Map;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.rewriter.ProcessingComponentConfiguration;
import org.apache.sling.rewriter.ProcessingContext;
import org.apache.sling.rewriter.Transformer;
import org.apache.sling.rewriter.TransformerFactory;
import org.osgi.service.component.ComponentContext;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

@Component
@Service
@Property(name="pipeline.type", value="linkrewriter", propertyPrivate=true)
public class LinkRewriterTransformerFactory implements TransformerFactory {

    public Transformer createTransformer() {
        return new LinkRewriterTransformer();
    }

    @Activate
    protected void activate(Map<String, Object> properties) {
    }

    @Deactivate
    protected void deactivate(ComponentContext ctx) {
    }

    private class LinkRewriterTransformer implements Transformer {
        private ContentHandler contentHandler;
        private SlingHttpServletRequest request;

        public void characters(char[] ch, int start, int length) throws SAXException {
            contentHandler.characters(ch, start, length);
        }

        public void dispose() {
        }

        public void endDocument() throws SAXException {
            contentHandler.endDocument();
        }

        public void endElement(String uri, String localName, String qName) throws SAXException {
            contentHandler.endElement(uri, localName, qName);
        }

        public void endPrefixMapping(String prefix) throws SAXException {
            contentHandler.endPrefixMapping(prefix);
        }

        public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
            contentHandler.ignorableWhitespace(ch, start, length);
        }

        public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException {
            request = context.getRequest();
        }

        public void processingInstruction(String target, String data) throws SAXException {
            contentHandler.processingInstruction(target, data);
        }

        public void setContentHandler(ContentHandler handler) {
            this.contentHandler = handler;
        }

        public void setDocumentLocator(Locator locator) {
            contentHandler.setDocumentLocator(locator);
        }

        public void skippedEntity(String name) throws SAXException {
            contentHandler.skippedEntity(name);
        }

        public void startDocument() throws SAXException {
            contentHandler.startDocument();
        }

        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
            final AttributesImpl attributes = new AttributesImpl(atts);

            final String href = attributes.getValue("href");

            if (href != null) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    if ("href".equalsIgnoreCase(attributes.getQName(i))) {
                        String cdnPath = /* process your path here */;
                        attributes.setValue(i, cdnPath);
                        break;
                    }
                }
            }

            contentHandler.startElement(uri, localName, qName, attributes);
        }

        public void startPrefixMapping(String prefix, String uri) throws SAXException {
            contentHandler.startPrefixMapping(prefix, uri);
        }
    }
}

将此配置节点添加到/apps/myapp/config/rewriter/myapp.xml

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="nt:unstructured"
    contentTypes="[text/html]"
    enabled="{Boolean}true"
    generatorType="htmlparser"
    order="1"
    paths="[/content/myapp]"
    serializerType="htmlwriter"
    transformerTypes="[linkchecker,linkrewriter]">
    <generator-htmlparser
        jcr:primaryType="nt:unstructured"
        includeTags="[META,/META,A,/A,IMG,AREA,FORM,BASE,LINK,SCRIPT,BODY,/BODY,VIDEO,/VIDEO,ASIDE,/ASIDE,SECTION,/SECTION]"/>
</jcr:root>

【讨论】:

  • 这是否也支持style 标签内的路径,比如background:url
【解决方案2】:

我想实现与您描述的相同的目标。 你可能想看看这里:https://adobe-consulting-services.github.io/acs-aem-commons/features/utils-and-apis/static-reference-rewriter/index.html

步骤:

A.在您的实例上下载并安装 AEM ACS Commons 包(从上述网站获取 zip 包,安装在包管理器中)

B.转到apps/&lt;yourapp&gt;/config 并创建一个名为com.adobe.acs.commons.rewriter.impl.StaticReferenceRewriteTransformerFactory-&lt;your-custom-name&gt;.xml 的文件,其内容类似于:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"     xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="sling:OsgiConfig"
    pipeline.type="your-custom-pipeline-name"
    attributes="[img:src\,srcset,link:href,script:src]"
    matchingPatterns="[img:srcset;(\/content\/dam\/.+?[jpg|png]) .+?w]"
    host.pattern="your.domain.com"
    prefixes="[/etc/designs,/content/dam]"
/>

C.在apps/&lt;yourapp&gt;/config/rewriter 下创建一个包含以下内容的文件&lt;another-custom-name&gt;.xml

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="sling:Folder"
    contentTypes="[text/html]"
    enabled="{Boolean}true"
    generatorType="htmlparser"
    order="{Long}1"
    serializerType="htmlwriter"
    transformerTypes="[linkchecker,your-custom-pipeline-name]"
/>

在您的实例上重新安装捆绑包并进行测试。祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 2016-10-28
    • 2022-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多