How to get SlingRequest from saxon TransformerFactory

339 views Asked by At

I want to get a current SlingRequest.getRequestURI() and pass it to new LinkTransformer(links, repository, <<requestedUrl>>). So I tried to add an annotation @Reference SlingRequest slingRequest and it throws me exception. Here is my code:

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);
        }
    }

Thank you

1

There are 1 answers

0
santiagozky On BEST ANSWER

You cannot obtain it in the TransformerFactory, but you don't need it to give it to the LinkTransformer. The Transformer will receive it in the init method.

The init method of Transformer receives a ProcessingContext, you can use ProcessingContext.getRequest() to get it