* Under normal circumstances, the body of the request can only be read once, because it is
* backed by an {@code InputStream}, and thus is not easily consumed multiple times. This
* method gets the request content and resets it so it can be read again later if necessary.
*/
private byte[] safelyGetContent(HttpRequestContext request) {
ContainerRequest containerRequest = (ContainerRequest) request;
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = containerRequest.getEntityInputStream();
try {
byte[] content = null;
if (in.available() > 0) {
ReaderWriter.writeTo(in, out);
content = out.toByteArray();
// Reset the input stream so that it can be read again by another filter or resource
containerRequest.setEntityInputStream(new ByteArrayInputStream(content));
}
return content;
} catch (IOException ex) {
throw new ContainerException(ex);