/** * Extracts the content as a {@link akka.util.ByteString}. * <p> * This method is only capable of extracting the content of results with strict entities. To extract the content of * results with streamed entities, use {@link Helpers#contentAsBytes(Result, Materializer)}. * * @param result The result to extract the content from. * @return The content of the result as a ByteString. * @throws UnsupportedOperationException if the result does not have a strict entity. */ public static ByteString contentAsBytes(Result result) { if (result.body() instanceof HttpEntity.Strict) { return ((HttpEntity.Strict) result.body()).data(); } else { throw new UnsupportedOperationException("Tried to extract body from a non strict HTTP entity without a materializer, use the version of this method that accepts a materializer instead"); } }
/** * Extracts the content as a {@link akka.util.ByteString}. * <p> * This method is only capable of extracting the content of results with strict entities. To extract the content of * results with streamed entities, use {@link Helpers#contentAsBytes(Result, Materializer)}. * * @param result The result to extract the content from. * @return The content of the result as a ByteString. * @throws UnsupportedOperationException if the result does not have a strict entity. */ public static ByteString contentAsBytes(Result result) { if (result.body() instanceof HttpEntity.Strict) { return ((HttpEntity.Strict) result.body()).data(); } else { throw new UnsupportedOperationException("Tried to extract body from a non strict HTTP entity without a materializer, use the version of this method that accepts a materializer instead"); } }
/** * Extracts the content as a {@link akka.util.ByteString}. * * @param result The result to extract the content from. * @param mat The materializer to use to extract the body from the result stream. * @param timeout The amount of time, in milliseconds, to wait for the body to be produced. * @return The content of the result as a ByteString. */ public static ByteString contentAsBytes(Result result, Materializer mat, long timeout) { try { return result.body().consumeData(mat).thenApply(Function.identity()).toCompletableFuture().get(timeout, TimeUnit.MILLISECONDS); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
/** * Extracts the content as a {@link akka.util.ByteString}. * * @param result The result to extract the content from. * @param mat The materializer to use to extract the body from the result stream. * @param timeout The amount of time, in milliseconds, to wait for the body to be produced. * @return The content of the result as a ByteString. */ public static ByteString contentAsBytes(Result result, Materializer mat, long timeout) { try { return result.body().consumeData(mat).thenApply(Function.identity()).toCompletableFuture().get(timeout, TimeUnit.MILLISECONDS); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }