Skip to content

Instantly share code, notes, and snippets.

@ryanlunka
Created April 8, 2013 12:35
Show Gist options
  • Save ryanlunka/5336471 to your computer and use it in GitHub Desktop.
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 "/".
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);
}
}
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);
}
}
@ehurtarte-xumak
Copy link

Hi Ryan,

How do I get the com.citytechinc.web.services.URLAdapter package?

@danvalencia
Copy link

Question, why do you iterate over the attributes even after null checking the value for href ? Wouldn't this be enough ?

      if (href != null && href.startsWith("/")) {
          LOG.info("Processing internal link href: " + href);
          attributes.setValue(i, enableRemoveExtensions ? removeExtension(href) : href);
      }

@jescobar-xumak
Copy link

Hi Ryan, as ehurtarte asked you, can you give us the URLAdparter code?

Thanks

Regards,

Jorge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment