public void write(OutputStream outputStream) throws IOException, WebApplicationException {
Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
BodyContentHandler body = new BodyContentHandler(new RichTextContentHandler(writer));
TikaInputStream tis = TikaInputStream.get(is);
try {
tis.getFile();
parser.parse(tis, body, metadata);
} catch (SAXException e) {
throw new WebApplicationException(e);
} catch (EncryptedDocumentException e) {
logger.warn(String.format(
"%s: Encrypted document",
info.getPath()
), e);
throw new WebApplicationException(e, Response.status(422).build());
} catch (TikaException e) {
logger.warn(String.format(
"%s: Text extraction failed",
info.getPath()
), e);
if (e.getCause()!=null && e.getCause() instanceof WebApplicationException) {
throw (WebApplicationException) e.getCause();
}
if (e.getCause()!=null && e.getCause() instanceof IllegalStateException) {
throw new WebApplicationException(Response.status(422).build());
}
if (e.getCause()!=null && e.getCause() instanceof OldWordFileFormatException) {
throw new WebApplicationException(Response.status(422).build());
}
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
} finally {
tis.close();
}
}
};
}