如何从 saxon TransformerFactory 获取 SlingRequest
How to get SlingRequest from saxon TransformerFactory
我想获得电流 SlingRequest.getRequestURI() 并将其传递给 new LinkTransformer(links, repository, <<requestedUrl>>)
。所以我试图添加一个注释 @Reference SlingRequest slingRequest
并且它抛出异常。这是我的代码:
package com.my.transformer;
import com.my.transformer.impl.LinkTransformer;
import org.apache.felix.scr.annotations.*;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.rewriter.Transformer;
import org.apache.sling.rewriter.TransformerFactory;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.RepositoryException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Dictionary;
@Service(value = TransformerFactory.class)
@Component(immediate = true, metatype = true,
label="My Link Rewriter",
name= "com.my.transformer.LinkTransformerFactory",
description="Rewrite certain links")
public class LinkTransformerFactory implements TransformerFactory {
private static final Logger log = LoggerFactory.getLogger(LinkTransformerFactory.class);
@Property(
value = "mylinktransformer",
propertyPrivate = true)
static final String PIPELINE_TYPE = "pipeline.type";
@Property(unbounded= PropertyUnbounded.ARRAY,description="....")
private static final String LINKS = "links";
private ArrayList<String> links;
@Reference
SlingRepository repository;
@Activate
protected void activate(ComponentContext context)
throws RepositoryException {
final Dictionary<?, ?> properties = context.getProperties();
String[] prop = (String[])properties.get(LINKS);
links = new ArrayList<String>(Arrays.asList(prop));
log.info("LinkTransformerFactory.activate");
}
@Override
public Transformer createTransformer() {
return new LinkTransformer(links, repository);
}
}
谢谢
你在TransformerFactory中获取不到,但你不需要它就可以给LinkTransformer。 Transformer 会在 init 方法中接收它。
Transformer receives a ProcessingContext的init方法,可以用ProcessingContext.getRequest()获取
我想获得电流 SlingRequest.getRequestURI() 并将其传递给 new LinkTransformer(links, repository, <<requestedUrl>>)
。所以我试图添加一个注释 @Reference SlingRequest slingRequest
并且它抛出异常。这是我的代码:
package com.my.transformer;
import com.my.transformer.impl.LinkTransformer;
import org.apache.felix.scr.annotations.*;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.rewriter.Transformer;
import org.apache.sling.rewriter.TransformerFactory;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.RepositoryException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Dictionary;
@Service(value = TransformerFactory.class)
@Component(immediate = true, metatype = true,
label="My Link Rewriter",
name= "com.my.transformer.LinkTransformerFactory",
description="Rewrite certain links")
public class LinkTransformerFactory implements TransformerFactory {
private static final Logger log = LoggerFactory.getLogger(LinkTransformerFactory.class);
@Property(
value = "mylinktransformer",
propertyPrivate = true)
static final String PIPELINE_TYPE = "pipeline.type";
@Property(unbounded= PropertyUnbounded.ARRAY,description="....")
private static final String LINKS = "links";
private ArrayList<String> links;
@Reference
SlingRepository repository;
@Activate
protected void activate(ComponentContext context)
throws RepositoryException {
final Dictionary<?, ?> properties = context.getProperties();
String[] prop = (String[])properties.get(LINKS);
links = new ArrayList<String>(Arrays.asList(prop));
log.info("LinkTransformerFactory.activate");
}
@Override
public Transformer createTransformer() {
return new LinkTransformer(links, repository);
}
}
谢谢
你在TransformerFactory中获取不到,但你不需要它就可以给LinkTransformer。 Transformer 会在 init 方法中接收它。
Transformer receives a ProcessingContext的init方法,可以用ProcessingContext.getRequest()获取