» JeaseCMS » Блог

Post process HTML with a Rewriter

Jease allows to post-process the resulting HTML for a page with a so called JEASE_SITE_REWRITER. A Rewriter is mostly a function which transforms a given input string into a modified output string.

Go to "CMS » System » Parameter" and create a new Parameter with key=JEASE_SITE_REWRITER and paste the following code into it:

import jfix.functor.Function;

public class Rewriter implements Function<String, String> {

  public String evaluate(String input) {
    return input.replace("Jease", "Cheese");
  }

}

This is a very simple solution to rebrand your website.

Another use of the JEASE_SITE_REWRITER is to rewrite all internal links into their final representation on server-side. Jease uses a special prefix (.​/​~) to store internal references which are resolved at request time via appropriate redirects. In order to avoid additional redirects, Jease uses a jQuery-Link-Rewriter at the client, which works fine, but if you have a good reason to rewrite all internal links at server side, you can do it with a JEASE_SITE_REWRITER like the following:

import jfix.functor.*;
import jease.cms.web.servlet.*;

public class LinkRewriter implements Function<String, String> { 
 public String evaluate(String input) { 
  return input.replace("."+"/"+"~", JeaseController.getContextPath()); 
 } 
}

Obfuscate email-addresses with JEASE_SITE_REWRITER

A more serious approach for using the JEASE_SITE_REWRITER is to parse your resulting HTML for special expressions with the following format and replace them via obfuscated mailto-links:

${[email protected]}

So putting an expression like above somewhere in your content (e.g. in a Text or Wiki) should result in a proper mailto link which is obfuscated against Spam-bots.

Here's the code which does the magic:

// Parse input for ${[email protected]}-expressions
// and replace them with Javascript obfuscated mailto-links.

import jfix.functor.Function;
import jfix.util.Regexps;

public class Rewriter implements Function<String, String> {

  public String evaluate(String content) {
    return Regexps.parseExpressions(content, PARSER);
  }

  private static final Function<String, String> PARSER = new Function<String, String>() {
    public String evaluate(String input) {
      try {
        int idx = input.indexOf("=");
        if (idx != -1) {
          String key = input.substring(0, idx).toLowerCase();
          String value = input.substring(idx + 1).trim();
          if ("mailto".equals(key)) {
            String address = value.replace("@", "&#64;").replace(".", "&#46;");
            String mailto = String.format("<a href=\"mailto:%1$s\">%1$s</a>", address);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mailto.length(); i++) {
              if (sb.length() != 0) {
                sb.append("+");
              }
              sb.append("'" + mailto.charAt(i) + "'");
            }
            return String.format("<script>document.write(%s);</script>", sb.toString());
          }
        }
      } catch (Throwable e) {
        // pass
      }
      return input;
    }
  };

}

The output in the final HTML will be as follows:

<script>
document.write(
'<'+'a'+' '+'h'+'r'+'e'+'f'+'='+'"'+'m'+'a'+'i'+'l'+'t'+'o'+':'+'e'+'m'+'a'+'i'+
'l'+'&'+'#'+'6'+'4'+';'+'s'+'o'+'m'+'e'+'d'+'o'+'m'+'a'+'i'+'n'+'&'+'#'+'4'+
'6'+';'+'o'+'r'+'g'+'"'+'>'+'e'+'m'+'a'+'i'+'l'+'&'+'#'+'6'+'4'+';'+'s'+'o'+
'm'+'e'+'d'+'o'+'m'+'a'+'i'+'n'+'&'+'#'+'4'+'6'+';'+'o'+'r'+'g'+'<'+'/'+'a'+'>'
);
</script>

And finally...

...the result will look like the following in your browser:

Maybe you can come up with more ideas what can be done with a rewriter. Just let us know...

Last modified on 2012-04-23 by Maik Jablonski