Merge pull request #17686 from spray/w/17663-fix-HttpEntity.fromFile
=htc #17663 implement HttpEntity.apply(File)
This commit is contained in:
commit
a01eade9c0
12 changed files with 61 additions and 40 deletions
|
|
@ -6,6 +6,7 @@ package akka.http.javadsl.model;
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import akka.http.impl.util.JavaAccessors;
|
||||
import akka.util.ByteString;
|
||||
import akka.stream.javadsl.Source;
|
||||
import akka.http.scaladsl.model.HttpEntity$;
|
||||
|
|
@ -39,7 +40,11 @@ public final class HttpEntities {
|
|||
}
|
||||
|
||||
public static UniversalEntity create(ContentType contentType, File file) {
|
||||
return HttpEntity$.MODULE$.apply((akka.http.scaladsl.model.ContentType) contentType, file);
|
||||
return JavaAccessors.HttpEntity(contentType, file);
|
||||
}
|
||||
|
||||
public static UniversalEntity create(ContentType contentType, File file, int chunkSize) {
|
||||
return HttpEntity$.MODULE$.apply((akka.http.scaladsl.model.ContentType) contentType, file, chunkSize);
|
||||
}
|
||||
|
||||
public static HttpEntityDefault create(ContentType contentType, long contentLength, Source<ByteString, Object> data) {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
package akka.http.javadsl.model;
|
||||
|
||||
import akka.http.impl.util.JavaAccessors;
|
||||
|
||||
/**
|
||||
* Represents an Http request.
|
||||
*/
|
||||
|
|
@ -47,14 +49,14 @@ public abstract class HttpRequest implements HttpMessage, HttpMessage.MessageTra
|
|||
* Returns a default request to be modified using the `withX` methods.
|
||||
*/
|
||||
public static HttpRequest create() {
|
||||
return Accessors.HttpRequest();
|
||||
return JavaAccessors.HttpRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a default request to the specified URI to be modified using the `withX` methods.
|
||||
*/
|
||||
public static HttpRequest create(String uri) {
|
||||
return Accessors.HttpRequest(uri);
|
||||
return JavaAccessors.HttpRequest(uri);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
package akka.http.javadsl.model;
|
||||
|
||||
import akka.http.impl.util.JavaAccessors;
|
||||
|
||||
/**
|
||||
* Represents an Http response.
|
||||
*/
|
||||
|
|
@ -37,6 +39,6 @@ public abstract class HttpResponse implements HttpMessage, HttpMessage.MessageTr
|
|||
* Returns a default response to be changed using the `withX` methods.
|
||||
*/
|
||||
public static HttpResponse create() {
|
||||
return Accessors.HttpResponse();
|
||||
return JavaAccessors.HttpResponse();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package akka.http.javadsl.model;
|
||||
|
||||
import akka.http.impl.util.JavaAccessors$;
|
||||
import akka.http.scaladsl.model.UriJavaAccessor;
|
||||
import akka.japi.Option;
|
||||
import akka.parboiled2.ParserInput$;
|
||||
|
|
@ -161,28 +162,28 @@ public abstract class Uri {
|
|||
* Creates a default Uri to be modified using the modification methods.
|
||||
*/
|
||||
public static Uri create() {
|
||||
return Accessors$.MODULE$.Uri(akka.http.scaladsl.model.Uri.Empty$.MODULE$);
|
||||
return JavaAccessors$.MODULE$.Uri(akka.http.scaladsl.model.Uri.Empty$.MODULE$);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Uri created by parsing the given string representation.
|
||||
*/
|
||||
public static Uri create(String uri) {
|
||||
return Accessors$.MODULE$.Uri(akka.http.scaladsl.model.Uri.apply(uri));
|
||||
return JavaAccessors$.MODULE$.Uri(akka.http.scaladsl.model.Uri.apply(uri));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Uri created by parsing the given string representation and parsing-mode.
|
||||
*/
|
||||
public static Uri create(String uri, akka.http.scaladsl.model.Uri.ParsingMode parsingMode) {
|
||||
return Accessors$.MODULE$.Uri(akka.http.scaladsl.model.Uri.apply(ParserInput$.MODULE$.apply(uri), parsingMode));
|
||||
return JavaAccessors$.MODULE$.Uri(akka.http.scaladsl.model.Uri.apply(ParserInput$.MODULE$.apply(uri), parsingMode));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Uri created by parsing the given string representation, charset, and parsing-mode.
|
||||
*/
|
||||
public static Uri create(String uri, Charset charset, akka.http.scaladsl.model.Uri.ParsingMode parsingMode) {
|
||||
return Accessors$.MODULE$.Uri(akka.http.scaladsl.model.Uri.apply(ParserInput$.MODULE$.apply(uri), charset, parsingMode));
|
||||
return JavaAccessors$.MODULE$.Uri(akka.http.scaladsl.model.Uri.apply(ParserInput$.MODULE$.apply(uri), charset, parsingMode));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,13 @@
|
|||
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.http.javadsl.model
|
||||
package akka.http.impl.util
|
||||
|
||||
import java.io.File
|
||||
|
||||
import JavaMapping.Implicits._
|
||||
import akka.http.impl.model.JavaUri
|
||||
import akka.http.javadsl.model._
|
||||
import akka.http.scaladsl.model
|
||||
|
||||
/**
|
||||
|
|
@ -12,7 +16,7 @@ import akka.http.scaladsl.model
|
|||
*
|
||||
* Accessors for constructors with default arguments to be used from the Java implementation
|
||||
*/
|
||||
object Accessors {
|
||||
object JavaAccessors {
|
||||
/** INTERNAL API */
|
||||
def HttpRequest(): HttpRequest = model.HttpRequest()
|
||||
|
||||
|
|
@ -24,4 +28,8 @@ object Accessors {
|
|||
|
||||
/** INTERNAL API */
|
||||
def Uri(uri: model.Uri): Uri = JavaUri(uri)
|
||||
|
||||
/** INTERNAL API */
|
||||
def HttpEntity(contentType: ContentType, file: File): UniversalEntity =
|
||||
model.HttpEntity(contentType.asScala, file)
|
||||
}
|
||||
|
|
@ -10,7 +10,6 @@ import scala.collection.immutable
|
|||
import scala.reflect.ClassTag
|
||||
import akka.japi
|
||||
import akka.http.impl.model.JavaUri
|
||||
import akka.http.javadsl.model.Accessors
|
||||
import akka.http.javadsl.{ model ⇒ jm }
|
||||
import akka.http.scaladsl.{ model ⇒ sm }
|
||||
|
||||
|
|
@ -147,7 +146,7 @@ object JavaMapping {
|
|||
|
||||
implicit object Uri extends JavaMapping[jm.Uri, sm.Uri] {
|
||||
def toScala(javaObject: jm.Uri): Uri.S = cast[JavaUri](javaObject).uri
|
||||
def toJava(scalaObject: sm.Uri): Uri.J = Accessors.Uri(scalaObject)
|
||||
def toJava(scalaObject: sm.Uri): Uri.J = JavaAccessors.Uri(scalaObject)
|
||||
}
|
||||
|
||||
private def cast[T](obj: AnyRef)(implicit classTag: ClassTag[T]): T =
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import akka.util.ByteString
|
|||
import akka.stream.FlowMaterializer
|
||||
import akka.stream.scaladsl._
|
||||
import akka.stream
|
||||
import akka.stream.io.SynchronousFileSource
|
||||
import akka.stream.TimerTransformer
|
||||
import akka.http.scaladsl.util.FastFuture
|
||||
import akka.http.javadsl.{ model ⇒ jm }
|
||||
|
|
@ -131,9 +132,9 @@ object HttpEntity {
|
|||
def apply(contentType: ContentType, contentLength: Long, data: Source[ByteString, Any]): UniversalEntity =
|
||||
if (contentLength == 0) empty(contentType) else Default(contentType, contentLength, data)
|
||||
|
||||
def apply(contentType: ContentType, file: File): UniversalEntity = {
|
||||
def apply(contentType: ContentType, file: File, chunkSize: Int = SynchronousFileSource.DefaultChunkSize): UniversalEntity = {
|
||||
val fileLength = file.length
|
||||
if (fileLength > 0) Default(contentType, fileLength, ???) // FIXME: attach from-file-Publisher
|
||||
if (fileLength > 0) Default(contentType, fileLength, SynchronousFileSource(file, chunkSize))
|
||||
else empty(contentType)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
package akka.http.scaladsl.model
|
||||
|
||||
import java.lang.{ Iterable ⇒ JIterable }
|
||||
import akka.http.impl.util
|
||||
|
||||
import scala.concurrent.duration.FiniteDuration
|
||||
import scala.concurrent.{ Future, ExecutionContext }
|
||||
import scala.collection.immutable
|
||||
|
|
@ -266,7 +268,7 @@ final case class HttpRequest(method: HttpMethod = HttpMethods.GET,
|
|||
def withUri(uri: Uri): HttpRequest = copy(uri = uri)
|
||||
|
||||
/** Java API */
|
||||
override def getUri: jm.Uri = jm.Accessors.Uri(uri)
|
||||
override def getUri: jm.Uri = util.JavaAccessors.Uri(uri)
|
||||
/** Java API */
|
||||
override def withUri(relativeUri: akka.http.javadsl.model.Uri): HttpRequest = copy(uri = relativeUri.asInstanceOf[JavaUri].uri)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,44 +4,45 @@
|
|||
|
||||
package akka.http.javadsl.model
|
||||
|
||||
import org.scalatest.{ MustMatchers, FreeSpec }
|
||||
import org.scalatest.{ FreeSpec, MustMatchers }
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
|
||||
class JavaApiSpec extends FreeSpec with MustMatchers {
|
||||
"The Java API should work for" - {
|
||||
"work with Uris" - {
|
||||
"addParameter" in {
|
||||
Accessors.Uri("/abc")
|
||||
.addParameter("name", "paul") must be(Accessors.Uri("/abc?name=paul"))
|
||||
Uri.create("/abc")
|
||||
.addParameter("name", "paul") must be(Uri.create("/abc?name=paul"))
|
||||
}
|
||||
"addSegment" in {
|
||||
Accessors.Uri("/abc")
|
||||
.addPathSegment("def") must be(Accessors.Uri("/abc/def"))
|
||||
Uri.create("/abc")
|
||||
.addPathSegment("def") must be(Uri.create("/abc/def"))
|
||||
|
||||
Accessors.Uri("/abc/")
|
||||
.addPathSegment("def") must be(Accessors.Uri("/abc/def"))
|
||||
Uri.create("/abc/")
|
||||
.addPathSegment("def") must be(Uri.create("/abc/def"))
|
||||
}
|
||||
"scheme/host/port" in {
|
||||
Accessors.Uri("/abc")
|
||||
Uri.create("/abc")
|
||||
.scheme("http")
|
||||
.host("example.com")
|
||||
.port(8258) must be(Accessors.Uri("http://example.com:8258/abc"))
|
||||
.port(8258) must be(Uri.create("http://example.com:8258/abc"))
|
||||
}
|
||||
"toRelative" in {
|
||||
Accessors.Uri("http://example.com/abc")
|
||||
.toRelative must be(Accessors.Uri("/abc"))
|
||||
Uri.create("http://example.com/abc")
|
||||
.toRelative must be(Uri.create("/abc"))
|
||||
}
|
||||
"pathSegments" in {
|
||||
Accessors.Uri("/abc/def/ghi/jkl")
|
||||
Uri.create("/abc/def/ghi/jkl")
|
||||
.pathSegments().asScala.toSeq must contain inOrderOnly ("abc", "def", "ghi", "jkl")
|
||||
}
|
||||
"access parameterMap" in {
|
||||
Accessors.Uri("/abc?name=blub&age=28")
|
||||
Uri.create("/abc?name=blub&age=28")
|
||||
.parameterMap().asScala must contain allOf ("name" -> "blub", "age" -> "28")
|
||||
}
|
||||
"access parameters" in {
|
||||
val Seq(param1, param2, param3) =
|
||||
Accessors.Uri("/abc?name=blub&age=28&name=blub2")
|
||||
Uri.create("/abc?name=blub&age=28&name=blub2")
|
||||
.parameters.asScala.toSeq
|
||||
|
||||
param1.getKey must be("name")
|
||||
|
|
@ -54,16 +55,16 @@ class JavaApiSpec extends FreeSpec with MustMatchers {
|
|||
param3.getValue must be("blub2")
|
||||
}
|
||||
"containsParameter" in {
|
||||
val uri = Accessors.Uri("/abc?name=blub")
|
||||
val uri = Uri.create("/abc?name=blub")
|
||||
uri.containsParameter("name") must be(true)
|
||||
uri.containsParameter("age") must be(false)
|
||||
}
|
||||
"access single parameter" in {
|
||||
val uri = Accessors.Uri("/abc?name=blub")
|
||||
val uri = Uri.create("/abc?name=blub")
|
||||
uri.parameter("name") must be(akka.japi.Option.some("blub"))
|
||||
uri.parameter("age") must be(akka.japi.Option.none)
|
||||
|
||||
Accessors.Uri("/abc?name=blub&name=blib").parameter("name") must be(akka.japi.Option.some("blub"))
|
||||
Uri.create("/abc?name=blub&name=blib").parameter("name") must be(akka.japi.Option.some("blub"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,11 +5,11 @@
|
|||
package akka.http.javadsl.model
|
||||
|
||||
import akka.http.javadsl.model.headers.Cookie
|
||||
import akka.http.scaladsl.model
|
||||
import akka.http.scaladsl.model.headers.BasicHttpCredentials
|
||||
import org.scalatest.{ FreeSpec, MustMatchers }
|
||||
|
||||
import scala.collection.immutable
|
||||
import org.scalatest.{ MustMatchers, FreeSpec }
|
||||
import akka.http.scaladsl.model.headers.BasicHttpCredentials
|
||||
import akka.http.scaladsl.model
|
||||
|
||||
class JavaApiTestCaseSpecs extends FreeSpec with MustMatchers {
|
||||
"JavaApiTestCases should work as intended" - {
|
||||
|
|
@ -50,11 +50,11 @@ class JavaApiTestCaseSpecs extends FreeSpec with MustMatchers {
|
|||
}
|
||||
"createUriForOrder" in {
|
||||
JavaApiTestCases.createUriForOrder("123", "149", "42") must be(
|
||||
Accessors.Uri(model.Uri("/order?orderId=123&price=149&amount=42")))
|
||||
Uri.create("/order?orderId=123&price=149&amount=42"))
|
||||
}
|
||||
"addSessionId" in {
|
||||
val origin = Accessors.Uri("/order?orderId=123")
|
||||
JavaApiTestCases.addSessionId(origin) must be(Accessors.Uri("/order?orderId=123&session=abcdefghijkl"))
|
||||
val origin = Uri.create("/order?orderId=123")
|
||||
JavaApiTestCases.addSessionId(origin) must be(Uri.create("/order?orderId=123&session=abcdefghijkl"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -98,7 +98,7 @@ private[akka] class SynchronousFilePublisher(f: File, bytesReadPromise: Promise[
|
|||
case -1 ⇒
|
||||
// had nothing to read into this chunk
|
||||
eofReachedAtOffset = chan.position
|
||||
log.debug("No more bytes available to read (got `-1` or `0` from `read`), marking final bytes of file @ " + eofReachedAtOffset)
|
||||
log.debug("No more bytes available to read (got `-1` from `read`), marking final bytes of file @ " + eofReachedAtOffset)
|
||||
|
||||
case _ ⇒
|
||||
readBytesTotal += readBytes
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ object SynchronousFileSource {
|
|||
|
||||
/**
|
||||
* Creates a synchronous (Java 6 compatible) Source from a Files contents.
|
||||
* Emitted elements are [[ByteString]] elements, chubnked by default by [[DefaultChunkSize]] bytes.
|
||||
* Emitted elements are [[ByteString]] elements, chunked by default by [[DefaultChunkSize]] bytes.
|
||||
*
|
||||
* This source is backed by an Actor which will use the dedicated thread-pool base dispatcher.
|
||||
* You can configure the default dispatcher for this Source by changing the `akka.stream.file-io-dispatcher` or
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue