final Encoding encoding = pageContext.getCharsetEncoding();
if (encoding == null) {
throw new PackagingException(
exceptionLocalizer.format("no-charset"));
}
final EnvironmentContext envContext =
pageContext.getEnvironmentContext();
// Get the body source written to the response writer
bodySource.write(new AbstractPackageBodyOutput() {
// Retrieve the output stream from the page context and
// filter up any errors
public OutputStream getRealOutputStream() {
try {
return envContext.getResponseOutputStream();
} catch (MarinerContextException mce) {
throw new RuntimeException(mce);
}
}
// Retrieve the output writer from the page context, convert it
// to a CharsetEncodingWriter and filter up any errors
public Writer getRealWriter() {
try {
return new CharsetEncodingWriter(
envContext.getResponseWriter(), encoding);
} catch (MarinerContextException mce) {
throw new RuntimeException(mce);
}
}
}, context, bodyContext);