/** * 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 #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 materialiser 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 Status code of this Result value. */ public static int status(Result result) { return result.toScala().header().status(); }
/** * Extracts the content as a String. * * @param result The result to extract the content from. * @param mat The materialiser 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 String. */ public static String contentAsString(Result result, Materializer mat, long timeout) { return contentAsBytes(result, mat, timeout).decodeString(result.charset().orElse("utf-8")); }
/** * Extracts the content as a String. * * @param result The result to extract the content from. * @param mat The materialiser to use to extract the body from the result stream. * @return The content of the result as a String. */ public static String contentAsString(Result result, Materializer mat) { return contentAsBytes(result, mat, DEFAULT_TIMEOUT) .decodeString(result.charset().orElse("utf-8")); }
/** * Extracts the content as a String. * * <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 #contentAsString(Result, * Materializer)}. * * @param result The result to extract the content from. * @return The content of the result as a String. * @throws UnsupportedOperationException if the result does not have a strict entity. */ public static String contentAsString(Result result) { return contentAsBytes(result).decodeString(result.charset().orElse("utf-8")); }