Skip to content

Instantly share code, notes, and snippets.

@mrg
Last active August 29, 2015 13:56
Show Gist options
  • Save mrg/8943683 to your computer and use it in GitHub Desktop.
Save mrg/8943683 to your computer and use it in GitHub Desktop.
Tapestry 5 service/filter to automatically trim all form inputs of leading/trailing whitespace and replace smart quotes with normal quotes.
In your AppModule:
public static void bind(ServiceBinder binder)
{
...
binder.bind(FormPreProcessorFilter.class, FormPreProcessorFilterImplementation.class);
...
}
/**
* Contributes custom request handlers to the pipeline.
*
* @param configuration The ordered request handler configuration.
* @param formInputTrimmerFilter A custom filter/service to trim leading/trailing whitespace from form inputs.
*/
public void contributeHttpServletRequestHandler(OrderedConfiguration<HttpServletRequestFilter> configuration,
@InjectService("FormPreProcessorFilter") HttpServletRequestFilter formPreProcessorFilter)
{
configuration.add("FormPreProcessorFilter", formPreProcessorFilter);
}
Put this in your src/main/java/.../services folder:
import org.apache.tapestry5.services.HttpServletRequestFilter;
/**
* Tapestry HttpServletRequestFilter service designed to be used inside a
* contributeHttpServletRequestHandler startup contribution:
*
* public void contributeHttpServletRequestHandler(OrderedConfiguration<HttpServletRequestFilter> configuration,
* @InjectService("FormPreProcessorFilter") HttpServletRequestFilter formPreProcessorFilter)
* {
* configuration.add("FormPreProcessorFilter", formPreProcessorFilter);
* }
*
* This service will then filter all form inputs by trimming leading/trailing
* spaces and replacing smart quotes.
*/
public interface FormPreProcessorFilter extends HttpServletRequestFilter
{
}
Put this in your src/main/java/.../services folder (or subfolder):
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import ....services.FormPreProcessorFilter;
import org.apache.tapestry5.services.HttpServletRequestHandler;
/**
* Tapestry service to trim leading and trailing spaces from form inputs and
* also replace smart quotes with normal quotes.
*
* See the interface for how to contribute the service as a
* HttpServletRequestFilter.
*/
public class FormPreProcessorFilterImplementation implements FormPreProcessorFilter
{
/*
* Service the HTTP request by passing it through our custom request wrapper.
*
* @see org.apache.tapestry5.services.HttpServletRequestFilter#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.tapestry5.services.HttpServletRequestHandler)
*/
public boolean service(HttpServletRequest request, HttpServletResponse response, HttpServletRequestHandler handler) throws IOException
{
return handler.service(new FormPreProcessorRequestWrapper(request), response);
}
/**
* Custom request wrapper which trims all calls to get the parameter value.
* Java Servlets do not allow you to set the parameter values, so this is
* the best that can be done.
*/
static class FormPreProcessorRequestWrapper extends HttpServletRequestWrapper
{
public FormPreProcessorRequestWrapper(HttpServletRequest request)
{
super(request);
}
@Override
public String getParameter(String parameterName)
{
String parameter = super.getParameter(parameterName);
// Trim the input parameter and then replace smart quotes with
// normal quotes, per:
// http://stackoverflow.com/questions/2826191/converting-ms-word-quotes-and-apostrophes
if (parameter != null)
{
parameter = parameter.trim();
parameter = parameter.replaceAll("[\\u2018\\u2019]", "'");
parameter = parameter.replaceAll("[\\u201C\\u201D]", "\"");
}
return parameter;
}
// TODO: For completeness, add getParameterValues and getParameterMap.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment