You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Nate Yolles <yo...@adobe.com> on 2015/07/28 17:46:19 UTC

Adding link rewrite to the pipeline throws exception

Hello,

I’m trying to add a link rewriter into the pipeline but I’m running into some trouble. I’ve done this in AEM/CQ a few times but this is the first time I’m attempting it in a pure Sling application. I’m using
“org.apache.sling.launchpad-7-standalone.jar” with Sightly. When I try to load a page I get the error “Unable to get component of class ‘interface org.apache.sling.rewriter.Generator’ with type ‘htmlparser’.”

Any guidance would be appreciated.

Thank you!


28.07.2015 08:07:26.341 *ERROR* [0:0:0:0:0:0:0:1 [1438096046286] GET /content/blog/index.html HTTP/1.1] org.apache.sling.servlets.resolver.internal.SlingServletResolver Original error class java.io.IOException

java.io.IOException: Unable to get component of class 'interface org.apache.sling.rewriter.Generator' with type 'htmlparser'.

at org.apache.sling.rewriter.impl.PipelineImpl.getPipelineComponent(PipelineImpl.java:160)

at org.apache.sling.rewriter.impl.PipelineImpl.init(PipelineImpl.java:85)

at org.apache.sling.rewriter.impl.ProcessorManagerImpl.getProcessor(ProcessorManagerImpl.java:445)

at org.apache.sling.rewriter.impl.RewriterResponse.getProcessor(RewriterResponse.java:172)

at org.apache.sling.rewriter.impl.RewriterResponse.getWriter(RewriterResponse.java:110)

at org.apache.sling.scripting.core.impl.helper.OnDemandWriter.getWriter(OnDemandWriter.java:38)

at org.apache.sling.scripting.core.impl.helper.OnDemandWriter.write(OnDemandWriter.java:75)

at java.io.PrintWriter.write(PrintWriter.java:456)

at java.io.PrintWriter.write(PrintWriter.java:473)

at libs.publick.pages.page.SightlyJava_html.render(SightlyJava_html.java:37)

I have a configuration node under /apps/mysite/config/rewriter/linkrewriter along with /apps/mysite/config/rewriter/linkrewriter/generator-htmlparser


{

    "jcr:primaryType": "sling:Folder",

    "rewriter" : {

        "jcr:primaryType": "sling:Folder",

        "linkrewriter" : {

            "jcr:primaryType" : "nt:unstructured",

            "contentTypes" : [

                "text/html"

            ],

            "enabled" : true,

            "generatorType" : "htmlparser",

            "order" : 1,

            "paths" : [

                "/content"

            ],

            "serializerType" : "htmlwriter",

            "transformerTypes" : [

                "linkrewriter"

            ],

            "generator-htmlparser" : {

                "jcr:primaryType" : "nt:unstructured",

                "includeTags" : [

                    "A",

                    "/A"

                ]

            }

        }

    }

}

Then I have the Transformer and TransformerFactory implementations:


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.Properties;

import org.apache.felix.scr.annotations.Property;

import org.apache.felix.scr.annotations.Reference;

import org.apache.felix.scr.annotations.Service;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.commons.osgi.PropertiesUtil;

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;


        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 {

        }


        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 {

            contentHandler.startElement(uri, localName, qName, atts);



//            final AttributesImpl attributes = new AttributesImpl(atts);

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

//            final boolean enableRewritePath = true;

//

//            if (href != null && href.startsWith("/")) {

//

//                for (int i = 0; i < attributes.getLength(); i++) {

//                    if ("href".equalsIgnoreCase(attributes.getQName(i))) {

//                        attributes.setValue(i, enableRewritePath ? rewritePath(href) : href);

//                    }

//                }

//            }

//

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

        }


        public void startPrefixMapping(String prefix, String uri) throws SAXException {

            contentHandler.startPrefixMapping(prefix, uri);

        }


        private String rewritePath(final String href) {

            return href.replace("/content", "");

        }

    }

}

Re: Adding link rewrite to the pipeline throws exception

Posted by Nate Yolles <yo...@adobe.com>.
My Sling rewriter changes all HTML elements to full uppercase.

Before: <div><h1 class="page-header">Welcome</h1></div>

After: <DIV><H1 class="page-header">Welcome</H1></DIV>

Is there a way to prevent this?

Thank you,

Nate






On 7/28/15, 10:59 AM, "Julian Sedding" <js...@gmail.com> wrote:

>Hi Nate
>
>You can look in the webconsole at /system/console/status-Services and
>use your browser search to find the string "pipeline.type". That will
>point you to all kind of implementations. To fully understand what
>each does, you may need to dive into the source.
>
>Regards
>Julian
>
>On Tue, Jul 28, 2015 at 6:59 PM, Nate Yolles <yo...@adobe.com> wrote:
>> Thank you Justin!
>>
>> After changing the generatorType to ³html-generator² and the
>> SerializerType to ³html-serializer² everything worked great.
>>
>> Are there other options for generator, serializer and transformer types?
>> If so, are there any docs that list all of the options?
>>
>> Thanks again,
>>
>> Nate
>>
>>
>>
>>
>> On 7/28/15, 9:11 AM, "Justin Edelson" <ju...@justinedelson.com> wrote:
>>
>>>Hi Nate,
>>>IIUC, in Sling, you need to use "html-generator". "htmlparser" refers to
>>>an
>>>AEM component.
>>>
>>>See
>>>https://github.com/apache/sling/blob/trunk/contrib/extensions/rewriter/s
>>>rc
>>>/main/java/org/apache/sling/rewriter/impl/components/HtmlGeneratorFactor
>>>y.
>>>java
>>>
>>>Regards,
>>>Justin
>>>
>>>On Tue, Jul 28, 2015 at 11:46 AM Nate Yolles <yo...@adobe.com> wrote:
>>>
>>>> Hello,
>>>>
>>>> I¹m trying to add a link rewriter into the pipeline but I¹m running
>>>>into
>>>> some trouble. I¹ve done this in AEM/CQ a few times but this is the
>>>>first
>>>> time I¹m attempting it in a pure Sling application. I¹m using
>>>> ³org.apache.sling.launchpad-7-standalone.jar² with Sightly. When I try
>>>>to
>>>> load a page I get the error ³Unable to get component of class
>>>>Œinterface
>>>> org.apache.sling.rewriter.Generator¹ with type Œhtmlparser¹.²
>>>>
>>>> Any guidance would be appreciated.
>>>>
>>>> Thank you!
>>>>
>>>>
>>>> 28.07.2015 08:07:26.341 *ERROR* [0:0:0:0:0:0:0:1 [1438096046286] GET
>>>> /content/blog/index.html HTTP/1.1]
>>>> org.apache.sling.servlets.resolver.internal.SlingServletResolver
>>>>Original
>>>> error class java.io.IOException
>>>>
>>>> java.io.IOException: Unable to get component of class 'interface
>>>> org.apache.sling.rewriter.Generator' with type 'htmlparser'.
>>>>
>>>> at
>>>>
>>>>org.apache.sling.rewriter.impl.PipelineImpl.getPipelineComponent(Pipeli
>>>>ne
>>>>Impl.java:160)
>>>>
>>>> at
>>>>org.apache.sling.rewriter.impl.PipelineImpl.init(PipelineImpl.java:85)
>>>>
>>>> at
>>>>
>>>>org.apache.sling.rewriter.impl.ProcessorManagerImpl.getProcessor(Proces
>>>>so
>>>>rManagerImpl.java:445)
>>>>
>>>> at
>>>>
>>>>org.apache.sling.rewriter.impl.RewriterResponse.getProcessor(RewriterRe
>>>>sp
>>>>onse.java:172)
>>>>
>>>> at
>>>>
>>>>org.apache.sling.rewriter.impl.RewriterResponse.getWriter(RewriterRespo
>>>>ns
>>>>e.java:110)
>>>>
>>>> at
>>>>
>>>>org.apache.sling.scripting.core.impl.helper.OnDemandWriter.getWriter(On
>>>>De
>>>>mandWriter.java:38)
>>>>
>>>> at
>>>>
>>>>org.apache.sling.scripting.core.impl.helper.OnDemandWriter.write(OnDema
>>>>nd
>>>>Writer.java:75)
>>>>
>>>> at java.io.PrintWriter.write(PrintWriter.java:456)
>>>>
>>>> at java.io.PrintWriter.write(PrintWriter.java:473)
>>>>
>>>> at
>>>>
>>>>libs.publick.pages.page.SightlyJava_html.render(SightlyJava_html.java:3
>>>>7)
>>>>
>>>> I have a configuration node under
>>>> /apps/mysite/config/rewriter/linkrewriter along with
>>>> /apps/mysite/config/rewriter/linkrewriter/generator-htmlparser
>>>>
>>>>
>>>> {
>>>>
>>>>     "jcr:primaryType": "sling:Folder",
>>>>
>>>>     "rewriter" : {
>>>>
>>>>         "jcr:primaryType": "sling:Folder",
>>>>
>>>>         "linkrewriter" : {
>>>>
>>>>             "jcr:primaryType" : "nt:unstructured",
>>>>
>>>>             "contentTypes" : [
>>>>
>>>>                 "text/html"
>>>>
>>>>             ],
>>>>
>>>>             "enabled" : true,
>>>>
>>>>             "generatorType" : "htmlparser",
>>>>
>>>>             "order" : 1,
>>>>
>>>>             "paths" : [
>>>>
>>>>                 "/content"
>>>>
>>>>             ],
>>>>
>>>>             "serializerType" : "htmlwriter",
>>>>
>>>>             "transformerTypes" : [
>>>>
>>>>                 "linkrewriter"
>>>>
>>>>             ],
>>>>
>>>>             "generator-htmlparser" : {
>>>>
>>>>                 "jcr:primaryType" : "nt:unstructured",
>>>>
>>>>                 "includeTags" : [
>>>>
>>>>                     "A",
>>>>
>>>>                     "/A"
>>>>
>>>>                 ]
>>>>
>>>>             }
>>>>
>>>>         }
>>>>
>>>>     }
>>>>
>>>> }
>>>>
>>>> Then I have the Transformer and TransformerFactory implementations:
>>>>
>>>>
>>>> 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.Properties;
>>>>
>>>> import org.apache.felix.scr.annotations.Property;
>>>>
>>>> import org.apache.felix.scr.annotations.Reference;
>>>>
>>>> import org.apache.felix.scr.annotations.Service;
>>>>
>>>> import org.apache.sling.api.SlingHttpServletRequest;
>>>>
>>>> import org.apache.sling.commons.osgi.PropertiesUtil;
>>>>
>>>> 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;
>>>>
>>>>
>>>>         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 {
>>>>
>>>>         }
>>>>
>>>>
>>>>         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 {
>>>>
>>>>             contentHandler.startElement(uri, localName, qName, atts);
>>>>
>>>>
>>>>
>>>> //            final AttributesImpl attributes = new
>>>>AttributesImpl(atts);
>>>>
>>>> //            final String href = attributes.getValue("href");
>>>>
>>>> //            final boolean enableRewritePath = true;
>>>>
>>>> //
>>>>
>>>> //            if (href != null && href.startsWith("/")) {
>>>>
>>>> //
>>>>
>>>> //                for (int i = 0; i < attributes.getLength(); i++) {
>>>>
>>>> //                    if
>>>>("href".equalsIgnoreCase(attributes.getQName(i)))
>>>> {
>>>>
>>>> //                        attributes.setValue(i, enableRewritePath ?
>>>> rewritePath(href) : href);
>>>>
>>>> //                    }
>>>>
>>>> //                }
>>>>
>>>> //            }
>>>>
>>>> //
>>>>
>>>> //            contentHandler.startElement(uri, localName, qName,
>>>> attributes);
>>>>
>>>>         }
>>>>
>>>>
>>>>         public void startPrefixMapping(String prefix, String uri)
>>>>throws
>>>> SAXException {
>>>>
>>>>             contentHandler.startPrefixMapping(prefix, uri);
>>>>
>>>>         }
>>>>
>>>>
>>>>         private String rewritePath(final String href) {
>>>>
>>>>             return href.replace("/content", "");
>>>>
>>>>         }
>>>>
>>>>     }
>>>>
>>>> }
>>>>
>>


Re: Adding link rewrite to the pipeline throws exception

Posted by Julian Sedding <js...@gmail.com>.
Hi Nate

You can look in the webconsole at /system/console/status-Services and
use your browser search to find the string "pipeline.type". That will
point you to all kind of implementations. To fully understand what
each does, you may need to dive into the source.

Regards
Julian

On Tue, Jul 28, 2015 at 6:59 PM, Nate Yolles <yo...@adobe.com> wrote:
> Thank you Justin!
>
> After changing the generatorType to ³html-generator² and the
> SerializerType to ³html-serializer² everything worked great.
>
> Are there other options for generator, serializer and transformer types?
> If so, are there any docs that list all of the options?
>
> Thanks again,
>
> Nate
>
>
>
>
> On 7/28/15, 9:11 AM, "Justin Edelson" <ju...@justinedelson.com> wrote:
>
>>Hi Nate,
>>IIUC, in Sling, you need to use "html-generator". "htmlparser" refers to
>>an
>>AEM component.
>>
>>See
>>https://github.com/apache/sling/blob/trunk/contrib/extensions/rewriter/src
>>/main/java/org/apache/sling/rewriter/impl/components/HtmlGeneratorFactory.
>>java
>>
>>Regards,
>>Justin
>>
>>On Tue, Jul 28, 2015 at 11:46 AM Nate Yolles <yo...@adobe.com> wrote:
>>
>>> Hello,
>>>
>>> I¹m trying to add a link rewriter into the pipeline but I¹m running into
>>> some trouble. I¹ve done this in AEM/CQ a few times but this is the first
>>> time I¹m attempting it in a pure Sling application. I¹m using
>>> ³org.apache.sling.launchpad-7-standalone.jar² with Sightly. When I try
>>>to
>>> load a page I get the error ³Unable to get component of class Œinterface
>>> org.apache.sling.rewriter.Generator¹ with type Œhtmlparser¹.²
>>>
>>> Any guidance would be appreciated.
>>>
>>> Thank you!
>>>
>>>
>>> 28.07.2015 08:07:26.341 *ERROR* [0:0:0:0:0:0:0:1 [1438096046286] GET
>>> /content/blog/index.html HTTP/1.1]
>>> org.apache.sling.servlets.resolver.internal.SlingServletResolver
>>>Original
>>> error class java.io.IOException
>>>
>>> java.io.IOException: Unable to get component of class 'interface
>>> org.apache.sling.rewriter.Generator' with type 'htmlparser'.
>>>
>>> at
>>>
>>>org.apache.sling.rewriter.impl.PipelineImpl.getPipelineComponent(Pipeline
>>>Impl.java:160)
>>>
>>> at
>>>org.apache.sling.rewriter.impl.PipelineImpl.init(PipelineImpl.java:85)
>>>
>>> at
>>>
>>>org.apache.sling.rewriter.impl.ProcessorManagerImpl.getProcessor(Processo
>>>rManagerImpl.java:445)
>>>
>>> at
>>>
>>>org.apache.sling.rewriter.impl.RewriterResponse.getProcessor(RewriterResp
>>>onse.java:172)
>>>
>>> at
>>>
>>>org.apache.sling.rewriter.impl.RewriterResponse.getWriter(RewriterRespons
>>>e.java:110)
>>>
>>> at
>>>
>>>org.apache.sling.scripting.core.impl.helper.OnDemandWriter.getWriter(OnDe
>>>mandWriter.java:38)
>>>
>>> at
>>>
>>>org.apache.sling.scripting.core.impl.helper.OnDemandWriter.write(OnDemand
>>>Writer.java:75)
>>>
>>> at java.io.PrintWriter.write(PrintWriter.java:456)
>>>
>>> at java.io.PrintWriter.write(PrintWriter.java:473)
>>>
>>> at
>>>
>>>libs.publick.pages.page.SightlyJava_html.render(SightlyJava_html.java:37)
>>>
>>> I have a configuration node under
>>> /apps/mysite/config/rewriter/linkrewriter along with
>>> /apps/mysite/config/rewriter/linkrewriter/generator-htmlparser
>>>
>>>
>>> {
>>>
>>>     "jcr:primaryType": "sling:Folder",
>>>
>>>     "rewriter" : {
>>>
>>>         "jcr:primaryType": "sling:Folder",
>>>
>>>         "linkrewriter" : {
>>>
>>>             "jcr:primaryType" : "nt:unstructured",
>>>
>>>             "contentTypes" : [
>>>
>>>                 "text/html"
>>>
>>>             ],
>>>
>>>             "enabled" : true,
>>>
>>>             "generatorType" : "htmlparser",
>>>
>>>             "order" : 1,
>>>
>>>             "paths" : [
>>>
>>>                 "/content"
>>>
>>>             ],
>>>
>>>             "serializerType" : "htmlwriter",
>>>
>>>             "transformerTypes" : [
>>>
>>>                 "linkrewriter"
>>>
>>>             ],
>>>
>>>             "generator-htmlparser" : {
>>>
>>>                 "jcr:primaryType" : "nt:unstructured",
>>>
>>>                 "includeTags" : [
>>>
>>>                     "A",
>>>
>>>                     "/A"
>>>
>>>                 ]
>>>
>>>             }
>>>
>>>         }
>>>
>>>     }
>>>
>>> }
>>>
>>> Then I have the Transformer and TransformerFactory implementations:
>>>
>>>
>>> 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.Properties;
>>>
>>> import org.apache.felix.scr.annotations.Property;
>>>
>>> import org.apache.felix.scr.annotations.Reference;
>>>
>>> import org.apache.felix.scr.annotations.Service;
>>>
>>> import org.apache.sling.api.SlingHttpServletRequest;
>>>
>>> import org.apache.sling.commons.osgi.PropertiesUtil;
>>>
>>> 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;
>>>
>>>
>>>         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 {
>>>
>>>         }
>>>
>>>
>>>         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 {
>>>
>>>             contentHandler.startElement(uri, localName, qName, atts);
>>>
>>>
>>>
>>> //            final AttributesImpl attributes = new
>>>AttributesImpl(atts);
>>>
>>> //            final String href = attributes.getValue("href");
>>>
>>> //            final boolean enableRewritePath = true;
>>>
>>> //
>>>
>>> //            if (href != null && href.startsWith("/")) {
>>>
>>> //
>>>
>>> //                for (int i = 0; i < attributes.getLength(); i++) {
>>>
>>> //                    if
>>>("href".equalsIgnoreCase(attributes.getQName(i)))
>>> {
>>>
>>> //                        attributes.setValue(i, enableRewritePath ?
>>> rewritePath(href) : href);
>>>
>>> //                    }
>>>
>>> //                }
>>>
>>> //            }
>>>
>>> //
>>>
>>> //            contentHandler.startElement(uri, localName, qName,
>>> attributes);
>>>
>>>         }
>>>
>>>
>>>         public void startPrefixMapping(String prefix, String uri) throws
>>> SAXException {
>>>
>>>             contentHandler.startPrefixMapping(prefix, uri);
>>>
>>>         }
>>>
>>>
>>>         private String rewritePath(final String href) {
>>>
>>>             return href.replace("/content", "");
>>>
>>>         }
>>>
>>>     }
>>>
>>> }
>>>
>

Re: Adding link rewrite to the pipeline throws exception

Posted by Nate Yolles <yo...@adobe.com>.
Thank you Justin!

After changing the generatorType to ³html-generator² and the
SerializerType to ³html-serializer² everything worked great.

Are there other options for generator, serializer and transformer types?
If so, are there any docs that list all of the options?

Thanks again,

Nate




On 7/28/15, 9:11 AM, "Justin Edelson" <ju...@justinedelson.com> wrote:

>Hi Nate,
>IIUC, in Sling, you need to use "html-generator". "htmlparser" refers to
>an
>AEM component.
>
>See
>https://github.com/apache/sling/blob/trunk/contrib/extensions/rewriter/src
>/main/java/org/apache/sling/rewriter/impl/components/HtmlGeneratorFactory.
>java
>
>Regards,
>Justin
>
>On Tue, Jul 28, 2015 at 11:46 AM Nate Yolles <yo...@adobe.com> wrote:
>
>> Hello,
>>
>> I¹m trying to add a link rewriter into the pipeline but I¹m running into
>> some trouble. I¹ve done this in AEM/CQ a few times but this is the first
>> time I¹m attempting it in a pure Sling application. I¹m using
>> ³org.apache.sling.launchpad-7-standalone.jar² with Sightly. When I try
>>to
>> load a page I get the error ³Unable to get component of class Œinterface
>> org.apache.sling.rewriter.Generator¹ with type Œhtmlparser¹.²
>>
>> Any guidance would be appreciated.
>>
>> Thank you!
>>
>>
>> 28.07.2015 08:07:26.341 *ERROR* [0:0:0:0:0:0:0:1 [1438096046286] GET
>> /content/blog/index.html HTTP/1.1]
>> org.apache.sling.servlets.resolver.internal.SlingServletResolver
>>Original
>> error class java.io.IOException
>>
>> java.io.IOException: Unable to get component of class 'interface
>> org.apache.sling.rewriter.Generator' with type 'htmlparser'.
>>
>> at
>> 
>>org.apache.sling.rewriter.impl.PipelineImpl.getPipelineComponent(Pipeline
>>Impl.java:160)
>>
>> at 
>>org.apache.sling.rewriter.impl.PipelineImpl.init(PipelineImpl.java:85)
>>
>> at
>> 
>>org.apache.sling.rewriter.impl.ProcessorManagerImpl.getProcessor(Processo
>>rManagerImpl.java:445)
>>
>> at
>> 
>>org.apache.sling.rewriter.impl.RewriterResponse.getProcessor(RewriterResp
>>onse.java:172)
>>
>> at
>> 
>>org.apache.sling.rewriter.impl.RewriterResponse.getWriter(RewriterRespons
>>e.java:110)
>>
>> at
>> 
>>org.apache.sling.scripting.core.impl.helper.OnDemandWriter.getWriter(OnDe
>>mandWriter.java:38)
>>
>> at
>> 
>>org.apache.sling.scripting.core.impl.helper.OnDemandWriter.write(OnDemand
>>Writer.java:75)
>>
>> at java.io.PrintWriter.write(PrintWriter.java:456)
>>
>> at java.io.PrintWriter.write(PrintWriter.java:473)
>>
>> at
>> 
>>libs.publick.pages.page.SightlyJava_html.render(SightlyJava_html.java:37)
>>
>> I have a configuration node under
>> /apps/mysite/config/rewriter/linkrewriter along with
>> /apps/mysite/config/rewriter/linkrewriter/generator-htmlparser
>>
>>
>> {
>>
>>     "jcr:primaryType": "sling:Folder",
>>
>>     "rewriter" : {
>>
>>         "jcr:primaryType": "sling:Folder",
>>
>>         "linkrewriter" : {
>>
>>             "jcr:primaryType" : "nt:unstructured",
>>
>>             "contentTypes" : [
>>
>>                 "text/html"
>>
>>             ],
>>
>>             "enabled" : true,
>>
>>             "generatorType" : "htmlparser",
>>
>>             "order" : 1,
>>
>>             "paths" : [
>>
>>                 "/content"
>>
>>             ],
>>
>>             "serializerType" : "htmlwriter",
>>
>>             "transformerTypes" : [
>>
>>                 "linkrewriter"
>>
>>             ],
>>
>>             "generator-htmlparser" : {
>>
>>                 "jcr:primaryType" : "nt:unstructured",
>>
>>                 "includeTags" : [
>>
>>                     "A",
>>
>>                     "/A"
>>
>>                 ]
>>
>>             }
>>
>>         }
>>
>>     }
>>
>> }
>>
>> Then I have the Transformer and TransformerFactory implementations:
>>
>>
>> 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.Properties;
>>
>> import org.apache.felix.scr.annotations.Property;
>>
>> import org.apache.felix.scr.annotations.Reference;
>>
>> import org.apache.felix.scr.annotations.Service;
>>
>> import org.apache.sling.api.SlingHttpServletRequest;
>>
>> import org.apache.sling.commons.osgi.PropertiesUtil;
>>
>> 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;
>>
>>
>>         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 {
>>
>>         }
>>
>>
>>         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 {
>>
>>             contentHandler.startElement(uri, localName, qName, atts);
>>
>>
>>
>> //            final AttributesImpl attributes = new
>>AttributesImpl(atts);
>>
>> //            final String href = attributes.getValue("href");
>>
>> //            final boolean enableRewritePath = true;
>>
>> //
>>
>> //            if (href != null && href.startsWith("/")) {
>>
>> //
>>
>> //                for (int i = 0; i < attributes.getLength(); i++) {
>>
>> //                    if
>>("href".equalsIgnoreCase(attributes.getQName(i)))
>> {
>>
>> //                        attributes.setValue(i, enableRewritePath ?
>> rewritePath(href) : href);
>>
>> //                    }
>>
>> //                }
>>
>> //            }
>>
>> //
>>
>> //            contentHandler.startElement(uri, localName, qName,
>> attributes);
>>
>>         }
>>
>>
>>         public void startPrefixMapping(String prefix, String uri) throws
>> SAXException {
>>
>>             contentHandler.startPrefixMapping(prefix, uri);
>>
>>         }
>>
>>
>>         private String rewritePath(final String href) {
>>
>>             return href.replace("/content", "");
>>
>>         }
>>
>>     }
>>
>> }
>>


Re: Adding link rewrite to the pipeline throws exception

Posted by Justin Edelson <ju...@justinedelson.com>.
Hi Nate,
IIUC, in Sling, you need to use "html-generator". "htmlparser" refers to an
AEM component.

See
https://github.com/apache/sling/blob/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/components/HtmlGeneratorFactory.java

Regards,
Justin

On Tue, Jul 28, 2015 at 11:46 AM Nate Yolles <yo...@adobe.com> wrote:

> Hello,
>
> I’m trying to add a link rewriter into the pipeline but I’m running into
> some trouble. I’ve done this in AEM/CQ a few times but this is the first
> time I’m attempting it in a pure Sling application. I’m using
> “org.apache.sling.launchpad-7-standalone.jar” with Sightly. When I try to
> load a page I get the error “Unable to get component of class ‘interface
> org.apache.sling.rewriter.Generator’ with type ‘htmlparser’.”
>
> Any guidance would be appreciated.
>
> Thank you!
>
>
> 28.07.2015 08:07:26.341 *ERROR* [0:0:0:0:0:0:0:1 [1438096046286] GET
> /content/blog/index.html HTTP/1.1]
> org.apache.sling.servlets.resolver.internal.SlingServletResolver Original
> error class java.io.IOException
>
> java.io.IOException: Unable to get component of class 'interface
> org.apache.sling.rewriter.Generator' with type 'htmlparser'.
>
> at
> org.apache.sling.rewriter.impl.PipelineImpl.getPipelineComponent(PipelineImpl.java:160)
>
> at org.apache.sling.rewriter.impl.PipelineImpl.init(PipelineImpl.java:85)
>
> at
> org.apache.sling.rewriter.impl.ProcessorManagerImpl.getProcessor(ProcessorManagerImpl.java:445)
>
> at
> org.apache.sling.rewriter.impl.RewriterResponse.getProcessor(RewriterResponse.java:172)
>
> at
> org.apache.sling.rewriter.impl.RewriterResponse.getWriter(RewriterResponse.java:110)
>
> at
> org.apache.sling.scripting.core.impl.helper.OnDemandWriter.getWriter(OnDemandWriter.java:38)
>
> at
> org.apache.sling.scripting.core.impl.helper.OnDemandWriter.write(OnDemandWriter.java:75)
>
> at java.io.PrintWriter.write(PrintWriter.java:456)
>
> at java.io.PrintWriter.write(PrintWriter.java:473)
>
> at
> libs.publick.pages.page.SightlyJava_html.render(SightlyJava_html.java:37)
>
> I have a configuration node under
> /apps/mysite/config/rewriter/linkrewriter along with
> /apps/mysite/config/rewriter/linkrewriter/generator-htmlparser
>
>
> {
>
>     "jcr:primaryType": "sling:Folder",
>
>     "rewriter" : {
>
>         "jcr:primaryType": "sling:Folder",
>
>         "linkrewriter" : {
>
>             "jcr:primaryType" : "nt:unstructured",
>
>             "contentTypes" : [
>
>                 "text/html"
>
>             ],
>
>             "enabled" : true,
>
>             "generatorType" : "htmlparser",
>
>             "order" : 1,
>
>             "paths" : [
>
>                 "/content"
>
>             ],
>
>             "serializerType" : "htmlwriter",
>
>             "transformerTypes" : [
>
>                 "linkrewriter"
>
>             ],
>
>             "generator-htmlparser" : {
>
>                 "jcr:primaryType" : "nt:unstructured",
>
>                 "includeTags" : [
>
>                     "A",
>
>                     "/A"
>
>                 ]
>
>             }
>
>         }
>
>     }
>
> }
>
> Then I have the Transformer and TransformerFactory implementations:
>
>
> 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.Properties;
>
> import org.apache.felix.scr.annotations.Property;
>
> import org.apache.felix.scr.annotations.Reference;
>
> import org.apache.felix.scr.annotations.Service;
>
> import org.apache.sling.api.SlingHttpServletRequest;
>
> import org.apache.sling.commons.osgi.PropertiesUtil;
>
> 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;
>
>
>         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 {
>
>         }
>
>
>         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 {
>
>             contentHandler.startElement(uri, localName, qName, atts);
>
>
>
> //            final AttributesImpl attributes = new AttributesImpl(atts);
>
> //            final String href = attributes.getValue("href");
>
> //            final boolean enableRewritePath = true;
>
> //
>
> //            if (href != null && href.startsWith("/")) {
>
> //
>
> //                for (int i = 0; i < attributes.getLength(); i++) {
>
> //                    if ("href".equalsIgnoreCase(attributes.getQName(i)))
> {
>
> //                        attributes.setValue(i, enableRewritePath ?
> rewritePath(href) : href);
>
> //                    }
>
> //                }
>
> //            }
>
> //
>
> //            contentHandler.startElement(uri, localName, qName,
> attributes);
>
>         }
>
>
>         public void startPrefixMapping(String prefix, String uri) throws
> SAXException {
>
>             contentHandler.startPrefixMapping(prefix, uri);
>
>         }
>
>
>         private String rewritePath(final String href) {
>
>             return href.replace("/content", "");
>
>         }
>
>     }
>
> }
>