gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:35:49 25 4
gpt4 key购买 nike

我正在将 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 。链接重写器需要重写。请使用此引用链接并再次编写重写器: https://helpx.adobe.com/experience-manager/using/aem63_link_rewriter.html

上面的这个特定错误可能是由于 bundle 未启动造成的。您可能需要尝试/系统/控制台/depfinder检查所有必需的包依赖项是否可用。较早的实例重新启动为我解决了同样的错误。

关于java - 无法获取类型为 'interface org.apache.sling.rewriter.Transformer' 的类 'linkrewriter' 的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48956220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com