diff --git a/akka-http-core/src/main/java/akka/http/javadsl/model/HttpEntity.java b/akka-http-core/src/main/java/akka/http/javadsl/model/HttpEntity.java index 5ee656bf76..70b8b4fc25 100644 --- a/akka-http-core/src/main/java/akka/http/javadsl/model/HttpEntity.java +++ b/akka-http-core/src/main/java/akka/http/javadsl/model/HttpEntity.java @@ -6,8 +6,10 @@ package akka.http.javadsl.model; import akka.http.scaladsl.model.HttpEntity$; import akka.japi.Option; +import akka.stream.Materializer; import akka.stream.javadsl.Source; import akka.util.ByteString; +import scala.concurrent.Future; /** * Represents the entity of an Http message. An entity consists of the content-type of the data @@ -80,4 +82,15 @@ public interface HttpEntity { * Returns a stream of data bytes this entity consists of. */ public abstract Source getDataBytes(); + + /** + * Returns a future of a strict entity that contains the same data as this entity + * which is only completed when the complete entity has been collected. As the + * duration of receiving the complete entity cannot be predicted, a timeout needs to + * be specified to guard the process against running and keeping resources infinitely. + * + * Use getDataBytes and stream processing instead if the expected data is big or + * is likely to take a long time. + */ + public abstract Future toStrict(long timeoutMillis, Materializer materializer); } diff --git a/akka-http-core/src/main/scala/akka/http/scaladsl/model/HttpEntity.scala b/akka-http-core/src/main/scala/akka/http/scaladsl/model/HttpEntity.scala index fed567e1f0..10e46759c3 100644 --- a/akka-http-core/src/main/scala/akka/http/scaladsl/model/HttpEntity.scala +++ b/akka-http-core/src/main/scala/akka/http/scaladsl/model/HttpEntity.scala @@ -4,11 +4,13 @@ package akka.http.scaladsl.model +import akka.http.javadsl.model.HttpEntityStrict + import language.implicitConversions import java.io.File import java.lang.{ Iterable ⇒ JIterable, Long ⇒ JLong } import scala.concurrent.Future -import scala.concurrent.duration.FiniteDuration +import scala.concurrent.duration._ import scala.collection.immutable import akka.util.ByteString import akka.stream.Materializer @@ -103,6 +105,10 @@ sealed trait HttpEntity extends jm.HttpEntity { def isIndefiniteLength: Boolean = false def isDefault: Boolean = false def isChunked: Boolean = false + + /** Java API */ + def toStrict(timeoutMillis: Long, materializer: Materializer): Future[HttpEntityStrict] = + toStrict(timeoutMillis.millis)(materializer) } /* An entity that can be used for body parts */