无法获取类型为 'linkrewriter' 的 class 'interface org.apache.sling.rewriter.Transformer' 的组件

Unable to get component of class 'interface org.apache.sling.rewriter.Transformer' with type 'linkrewriter'

我正在将 AEM 项目从 6.0 版升级到 6.3 版,但出现错误 Unable to get component of class 'interface org.apache.sling.rewriter.Transformer' with type 'linkrewriter'.

我的代码

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.cocoon.xml.sax.AbstractSAXPipe;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
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.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import java.io.IOException;

@Slf4j
@Properties({
        @Property(
                name = "pipeline.type",
                value = "linkrewriter",
                propertyPrivate = true
        ),
        @Property(
                name = "webconsole.configurationFactory.nameHint",
                value = "Pipeline: {pipeline.type}"
        )
})
@Component(metatype = true,
        label = "Clientlib Link Rewriter Transformer"
)
@Service(value = TransformerFactory.class)
public class LinkRewriterTransformer extends AbstractSAXPipe implements Transformer, TransformerFactory {

    private SlingHttpServletRequest request;

    private static final String SRC_ATTR = "src";
    private static final String JS_ELEM = "script";
    private static final String HREF_ATTR = "href";
    private static final String CSS_ELEM = "link";
    private static final String IMG_ELEM = "img";
    private static final String ANCHOR_ELEM = "a";

    private static final String DAM_PATH = "/content/dam/my-application";
    private static final String CONTENT_PATH = "/content/my-application";
    private static final String ASSET_PATH = "/etc";

    @Override
    public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException {
        this.request = context.getRequest();
        log.info("Transforming request {}", request.getRequestURI());
    }

    @Override
    public void dispose() {
        // Required for implementing interface; no action needed
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
        AttributesImpl linkAttrs = new AttributesImpl(attrs);

        String attrName = "";

        if (JS_ELEM.equalsIgnoreCase(localName) || IMG_ELEM.equalsIgnoreCase(localName)) {
            attrName = SRC_ATTR;
        }
        else if (CSS_ELEM.equalsIgnoreCase(localName) || ANCHOR_ELEM.equalsIgnoreCase(localName)) {
            attrName = HREF_ATTR;
        }

        if (attrName.length() > 0) {
            for (int i = 0; i < linkAttrs.getLength(); i++) {
                String mappedPath = getMappedAttributeUrl(attrName, linkAttrs, i);
                if (!mappedPath.isEmpty()) {
                    linkAttrs.setValue(i, mappedPath);
                }
            }

        }

        super.startElement(uri, localName, qName, linkAttrs);
    }

    private boolean pathMatches(String pathInLink) {
        return pathInLink.startsWith(ASSET_PATH) || pathInLink.startsWith(DAM_PATH) || pathInLink.startsWith
                (CONTENT_PATH);
    }

    private String getMappedAttributeUrl(String attrName, AttributesImpl linkAttrs, int index) {
        if (attrName.equalsIgnoreCase(linkAttrs.getLocalName(index))) {
            String pathInLink = linkAttrs.getValue(index);
            if (!pathInLink.isEmpty() && pathMatches(pathInLink)) {
                String mappedPath = request.getResourceResolver().map(request, pathInLink);
                log.info("Transforming {} to {}", pathInLink, mappedPath);
                return mappedPath;
            }
        }
        return "";
    }

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

我的配置节点放在 /apps/<application>/config/rewriter

<?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"
  serializerType="htmlwriter"
  resourceTypes="[my-application/pages/base]"
  paths="[/content/my-application]"
  transformerTypes="[linkchecker,mobile,mobiledebug,contentsync,linkrewriter]">
  <transformer-mobile jcr:primaryType="nt:unstructured" component-optional="{Boolean}true"/>
  <transformer-mobiledebug jcr:primaryType="nt:unstructured" component-optional="{Boolean}true"/>
  <transformer-contentsync jcr:primaryType="nt:unstructured" component-optional="{Boolean}true"/>
</jcr:root>

自 AEM 6.3 起,apache felix scr 注释已替换为 OSGI R6 annotations。 link重写器需要重写。请使用此参考 link 并再次编写重写器: https://helpx.adobe.com/experience-manager/using/aem63_link_rewriter.html

上述特定错误可能是由于未启动包造成的。您可能需要尝试 /system/console/depfinder 检查是否所有必需的包依赖项都可用。较早的实例重启为我解决了同样的错误。