Created
April 8, 2013 12:35
-
-
Save ryanlunka/5336471 to your computer and use it in GitHub Desktop.
A Sling Rewriter Transformer that replaces all ".html" extensions in links within an HREF attribute of a requested page with "/".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.citytechinc.rewriter.linkchecker; | |
import java.io.IOException; | |
import org.apache.cocoon.xml.sax.AbstractSAXPipe; | |
import org.apache.sling.rewriter.ProcessingComponentConfiguration; | |
import org.apache.sling.rewriter.ProcessingContext; | |
import org.apache.sling.rewriter.Transformer; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.xml.sax.Attributes; | |
import org.xml.sax.SAXException; | |
import org.xml.sax.helpers.AttributesImpl; | |
import com.citytechinc.web.services.URLAdapter; | |
public class LinkTransformer extends AbstractSAXPipe implements Transformer { | |
private static final Logger LOG = LoggerFactory.getLogger(LinkTransformer.class); | |
private Boolean enableRemoveExtensions; | |
private URLAdapter adapter; | |
public LinkTransformer(final URLAdapter adapter) { | |
enableRemoveExtensions = adapter != null | |
&& adapter.getOldExtensions() != null | |
&& adapter.getOldExtensions().size() > 0 | |
&& adapter.getNewExtension() != null; | |
LOG.info("LinkTransformer created. Enabled = " + enableRemoveExtensions); | |
this.adapter = adapter; | |
} | |
@Override | |
public void dispose() { | |
} | |
@Override | |
public void init(final ProcessingContext context, final ProcessingComponentConfiguration config) | |
throws IOException { | |
} | |
@Override | |
public void startElement(final String uri, final String name, final String raw, final Attributes attrs) | |
throws SAXException { | |
final AttributesImpl attributes = new AttributesImpl(attrs); | |
LOG.info("Processing element: " + raw); | |
final String href = attributes.getValue("href"); | |
if (href != null && href.startsWith("/")) { | |
LOG.info("Processing internal link href: " + href); | |
for (int i = 0; i < attributes.getLength(); i++) { | |
if ("href".equalsIgnoreCase(attributes.getQName(i))) { | |
attributes.setValue(i, enableRemoveExtensions ? removeExtension(href) : href); | |
} | |
} | |
} | |
super.startElement(uri, name, raw, attributes); | |
} | |
private String removeExtension(final String url) { | |
return adapter.adaptUrl(url); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.citytechinc.rewriter.linkchecker; | |
import org.apache.felix.scr.annotations.Component; | |
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.rewriter.Transformer; | |
import org.apache.sling.rewriter.TransformerFactory; | |
import com.citytechinc.web.services.URLAdapter; | |
@Service(value = TransformerFactory.class) | |
@Component | |
public class LinkTransformerFactory implements TransformerFactory { | |
@Property( | |
value = "mylinktransformer", | |
propertyPrivate = true) | |
static final String PIPELINE_TYPE = "pipeline.type"; | |
@Reference | |
private URLAdapter urlAdapter; // A class with one method that replaces ".html" with "/", given a URL. | |
public final Transformer createTransformer() { | |
return new LinkTransformer(urlAdapter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Ryan, as ehurtarte asked you, can you give us the URLAdparter code?
Thanks
Regards,
Jorge