file.delete();
try {
file.createNewFile();
}
catch (IOException e) {
throw new RewriteException("Could not create file for Stream operation", e);
}
}
Response.withOutputStreamWrappedBy(new ResponseStreamWrapper() {
@Override
public OutputStream wrap(HttpServletRewrite rewrite, OutputStream outputStream)
{
try {
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file));
rewrite.getRequest().setAttribute(STREAM_KEY, stream);
log.debug("Cloning response OutputStream to file [" + file + "]");
return new MultiOutputStream(stream, outputStream);
}
catch (FileNotFoundException e) {
throw new RewriteException("Could not wrap stream", e);
}
}
@Override
public void finish(HttpServletRewrite rewrite)
{
try {
OutputStream stream = (OutputStream) rewrite.getRequest().getAttribute(STREAM_KEY);
if (stream != null)
{
log.debug("Closing cloned file [" + file + "] OutputStream");
stream.flush();
stream.close();
}
}
catch (Exception e) {
throw new RewriteException("Could not close stream", e);
}
}
}).perform(event, context);
}