diff --git a/akka-http-core/src/main/java/akka/http/model/japi/ContentType.java b/akka-http-core/src/main/java/akka/http/model/japi/ContentType.java index 0660944255..c8b3bbb5c0 100644 --- a/akka-http-core/src/main/java/akka/http/model/japi/ContentType.java +++ b/akka-http-core/src/main/java/akka/http/model/japi/ContentType.java @@ -4,25 +4,26 @@ package akka.http.model.japi; +import akka.japi.Option; + /** * Represents an Http content-type. A content-type consists of a media-type and an optional charset. */ public abstract class ContentType { /** - * Returns the media-type of the this content-type. + * Returns the media-type of this content-type. */ public abstract MediaType mediaType(); /** - * Returns if this content-type defines a charset. + * Returns the charset of this content-type. */ - public abstract boolean isCharsetDefined(); + public abstract HttpCharset charset(); /** - * Returns the charset of this content-type if there is one or throws an exception - * otherwise. + * Returns the optionally defined charset of this content-type. */ - public abstract HttpCharset getDefinedCharset(); + public abstract Option getDefinedCharset(); /** * Creates a content-type from a media-type and a charset. diff --git a/akka-http-core/src/main/java/akka/http/model/japi/Uri.java b/akka-http-core/src/main/java/akka/http/model/japi/Uri.java index 3a66574493..474131eb10 100644 --- a/akka-http-core/src/main/java/akka/http/model/japi/Uri.java +++ b/akka-http-core/src/main/java/akka/http/model/japi/Uri.java @@ -12,7 +12,7 @@ import java.nio.charset.Charset; import java.util.Map; /** - * Represents a Uri. Use the `withX` methods to create modified copies of a given instance. + * Represents an Uri. Use the `withX` methods to create modified copies of a given instance. */ public abstract class Uri { /** @@ -76,22 +76,18 @@ public abstract class Uri { public abstract boolean containsParameter(String key); /** - * Returns an Iterable of all query parameters of this Uri. + * Returns an `Iterable` of all query parameters of this Uri. Use the `parameterMap()` + * method to filter out entries with duplicated keys. */ - public abstract Iterable parameters(); + public abstract Iterable> parameters(); /** * Returns a key/value map of the query parameters of this Uri. Use - * the `parameters()` method to returns all parameters if keys may occur + * the `parameters()` method to return all parameters if keys may occur * multiple times. */ public abstract Map parameterMap(); - public static interface Parameter { - String key(); - String value(); - } - /** * Returns the fragment part of this Uri. */ diff --git a/akka-http-core/src/main/scala/akka/http/model/ContentType.scala b/akka-http-core/src/main/scala/akka/http/model/ContentType.scala index ec1c672fdb..92c93b3d78 100644 --- a/akka-http-core/src/main/scala/akka/http/model/ContentType.scala +++ b/akka-http-core/src/main/scala/akka/http/model/ContentType.scala @@ -6,6 +6,9 @@ package akka.http.model import language.implicitConversions import akka.http.util._ +import akka.japi.{ Option ⇒ JOption } + +import akka.http.model.japi.JavaMapping.Implicits._ final case class ContentTypeRange(mediaRange: MediaRange, charsetRange: HttpCharsetRange) extends ValueRenderable { def matches(contentType: ContentType) = @@ -41,18 +44,15 @@ final case class ContentType(mediaType: MediaType, definedCharset: Option[HttpCh } def charset: HttpCharset = definedCharset getOrElse HttpCharsets.`UTF-8` - def isCharsetDefined = definedCharset.isDefined - def noCharsetDefined = definedCharset.isEmpty - def withMediaType(mediaType: MediaType) = if (mediaType != this.mediaType) copy(mediaType = mediaType) else this def withCharset(charset: HttpCharset) = - if (noCharsetDefined || charset != definedCharset.get) copy(definedCharset = Some(charset)) else this + if (definedCharset.isEmpty || charset != definedCharset.get) copy(definedCharset = Some(charset)) else this def withoutDefinedCharset = - if (isCharsetDefined) copy(definedCharset = None) else this + if (definedCharset.isDefined) copy(definedCharset = None) else this /** Java API */ - def getDefinedCharset: japi.HttpCharset = definedCharset.orNull + def getDefinedCharset: JOption[japi.HttpCharset] = definedCharset.asJava } object ContentType { diff --git a/akka-http-core/src/main/scala/akka/http/model/headers/HttpCookie.scala b/akka-http-core/src/main/scala/akka/http/model/headers/HttpCookie.scala index e1d6431604..e58fa33935 100644 --- a/akka-http-core/src/main/scala/akka/http/model/headers/HttpCookie.scala +++ b/akka-http-core/src/main/scala/akka/http/model/headers/HttpCookie.scala @@ -28,9 +28,9 @@ final case class HttpCookie( // TODO: suppress running these requires for cookies created from our header parser require(nameChars.matchesAll(name), s"'${nameChars.firstMismatch(name).get}' not allowed in cookie name ('$name')") require(contentChars.matchesAll(content), s"'${contentChars.firstMismatch(content).get}' not allowed in cookie content ('$content')") - require(domain.isEmpty || domainChars.matchesAll(domain.get), s"'${domainChars.firstMismatch(domain.get).get}' not allowed in cookie domain ('${domain.get}')") - require(path.isEmpty || pathOrExtChars.matchesAll(path.get), s"'${pathOrExtChars.firstMismatch(path.get).get}' not allowed in cookie path ('${path.get}')") - require(extension.isEmpty || pathOrExtChars.matchesAll(extension.get), s"'${pathOrExtChars.firstMismatch(extension.get).get}' not allowed in cookie extension ('${extension.get}')") + require(domain.forall(domainChars.matchesAll), s"'${domainChars.firstMismatch(domain.get).get}' not allowed in cookie domain ('${domain.get}')") + require(path.forall(pathOrExtChars.matchesAll), s"'${pathOrExtChars.firstMismatch(path.get).get}' not allowed in cookie path ('${path.get}')") + require(extension.forall(pathOrExtChars.matchesAll), s"'${pathOrExtChars.firstMismatch(extension.get).get}' not allowed in cookie extension ('${extension.get}')") def render[R <: Rendering](r: R): r.type = { r ~~ name ~~ '=' ~~ content diff --git a/akka-http-core/src/main/scala/akka/http/model/japi/JavaUri.scala b/akka-http-core/src/main/scala/akka/http/model/japi/JavaUri.scala index 050668f2b6..43a770761e 100644 --- a/akka-http-core/src/main/scala/akka/http/model/japi/JavaUri.scala +++ b/akka-http-core/src/main/scala/akka/http/model/japi/JavaUri.scala @@ -40,11 +40,12 @@ protected[model] case class JavaUri(uri: model.Uri) extends Uri { def queryString(): String = uri.query.toString def parameterMap(): ju.Map[String, String] = uri.query.toMap.asJava - def parameters(): jl.Iterable[Uri.Parameter] = uri.query.map(t ⇒ Param(t._1, t._2): Uri.Parameter).toIterable.asJava + def parameters(): jl.Iterable[Param] = + uri.query.map { case (k, v) ⇒ new ju.AbstractMap.SimpleImmutableEntry(k, v): Param }.toIterable.asJava def containsParameter(key: String): Boolean = uri.query.get(key).isDefined def parameter(key: String): Option[String] = uri.query.get(key) - case class Param(key: String, value: String) extends Uri.Parameter + type Param = ju.Map.Entry[String, String] def fragment: Option[String] = uri.fragment diff --git a/akka-http-core/src/test/scala/akka/http/model/japi/JavaApiSpec.scala b/akka-http-core/src/test/scala/akka/http/model/japi/JavaApiSpec.scala index 5a6f769a36..6c366b28cd 100644 --- a/akka-http-core/src/test/scala/akka/http/model/japi/JavaApiSpec.scala +++ b/akka-http-core/src/test/scala/akka/http/model/japi/JavaApiSpec.scala @@ -44,14 +44,14 @@ class JavaApiSpec extends FreeSpec with MustMatchers { Accessors.Uri("/abc?name=blub&age=28&name=blub2") .parameters.asScala.toSeq - param1.key() must be("name") - param1.value() must be("blub") + param1.getKey must be("name") + param1.getValue must be("blub") - param2.key() must be("age") - param2.value() must be("28") + param2.getKey must be("age") + param2.getValue must be("28") - param3.key() must be("name") - param3.value() must be("blub2") + param3.getKey must be("name") + param3.getValue must be("blub2") } "containsParameter" in { val uri = Accessors.Uri("/abc?name=blub")