July 25th, 2012 by Lincoln Baxter III

Rewrite 1.1.0.Final introduces the OutputBuffer – What Servlet always wanted but could never have

Output buffering is a feature often desired but rarely properly implemented – Properly resetting the Response stream, preserving headers correctly, writing to disk so you don’t overflow the JVM memory space – in Rewrite, we hope to make this attainable for anyone seeking to do things like:
  • Dynamic minification of HTML, JavaScript, and/or CSS files
  • Modification of output HTML or content before it is sent to the client.
  • Reduction of build and development complexity by transforming LESS or SASS files into CSS on the Server
  • Got more ideas?… tell us.?
Now, using the Rewrite’s “OutputBuffer” API introduced in the new version 1.1.0.Final, you can finally achieve this in just a few lines of code, either by wrapping a stream, or simply modifying the contents directly! Want to make every first word in your application bold or remove all punctuation? You probably won’t make too many friends, but you could do it if you wanted to. Maybe we should start by changing all links in the app to this educational video. Or maybe we’ll just make everything lowercase:
Create an OutputBuffer
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.ocpsoft.common.util.Streams;

public class OutputToLowercase implements OutputBuffer
{
   @Override
   public InputStream execute(InputStream input)
   {
      String contents = Streams.toString(input);
      return new ByteArrayInputStream(contents.toLowerCase().getBytes());
   }
}
Use it in your ConfigurationProviderSource
public Configuration getConfiguration(final ServletContext context)
   {
      return ConfigurationBuilder.begin()
               .addRule()
               .when(Path.matches("/index.html"))
               .perform(Response.withOutputBufferedBy(new OutputToLowercase()));
   }
Combine that with a little bit of Rewrite-style configuration, and you’ve got a simple recipe for output modification. Some of the features to come in Rewrite 2.0, including a simplified output Transform API, are based on this technology. So if you want to get a head start, you can try a 2.0.0-SNAPSHOT, or just wait a few weeks 🙂
Get Rewrite version 1.1.0.Final, or see everything that’s new.

VERSION 1.1.0

Features & Enhancements:

  • Enabled fluent chaining of additional Condition and Operation instances when adding single/custom rules by calling ConfigurationBuilder.addRule(new CustomRule()).when(...).perform(...);
  • Added Response output buffering support
  • JAAS support with JAASRoles condition
  • Content Delivery Network support with CDN rule
  • Typesafe method invocation support
  • Issue #30 – Tasks may now be performed in a SubFlow without affecting the Flow of the entire Rewrite event.
  • Preview of Transformer APIs
  • Preview of Annotation Configuration API

Regression Impact:

  • Configuration strings are now literal. Regular expressions must be configured through a parameter such as: .defineRule().when(Path.matches("/{*}").where("*").matches(".*"))
  • Join no longer matches Forwarded requests.
  • EL API is now a separate module and the Java package name has changed to ‘org.ocpsoft.rewrite.el’

Bug Fixes:

  • Parameter binding now works on Join rules.
  • Issue #67 – Keep order of fluent rules when building configuration.
  • Fixed NumberFormatException on EncodeQuery operation when hashCode of checksum is modified
  • Fixed ArrayIndexOutOfBounds exception for parsing query-strings with a separator but no content “?”
  • Fixed showcase applications
  • Issue #51 – Join no longer matches its own Forwarded requests.
  • Fixed GWT integration issue where context path was not correctly transmitted when client application is hosted under the root URL “/”
  • TypeBasedExpression no longer requires the expression to be surrounded by “#{…}”
  • Fixed bug where container would swallow certain resource requests, resulting in false 404 errors
  • Issue #32 – PhaseBinding defers validation and conversion until within the JSF lifecycle.
  • Issue #33 – ClassVisitorImpl now supports adding rules to the ConfigurationBuilder if necessary
  • Fixed some bugs with URLEncoding/URLDecoding

Lincoln Baxter, III

About the author:

Lincoln Baxter, III is the Chief Editor of Red Hat Developers, and has worked extensively on JBoss open-source projects; most notably as creator & project lead of JBoss Forge, author of Errai UI, and Project Lead of JBoss Windup. This content represents his personal opinions, not those of his employer.

He is a founder of OCPsoft, the author of PrettyFaces and Rewrite, the leading URL-rewriting extensions for Servlet, Java EE, and Java web frameworks; he is also the author of PrettyTime, social-style date and timestamp formatting for Java. When he is not swimming, running, or playing competitive Magic: The Gathering, Lincoln is focused on promoting open-source software and making technology more accessible for everyone.

Posted in Releases

Leave a Comment




Please note: In order to submit code or special characters, wrap it in

[code lang="xml"][/code]
(for your language) - or your tags will be eaten.

Please note: Comment moderation is enabled and may delay your comment from appearing. There is no need to resubmit your comment.