Merge pull request #16481 from 2beaucoup/minor-fixes

Minor changes to the Java API of the HTTP core model
This commit is contained in:
Konrad Malawski 2014-12-10 14:18:50 +01:00
commit 314040b95e
6 changed files with 30 additions and 32 deletions

View file

@ -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<HttpCharset> getDefinedCharset();
/**
* Creates a content-type from a media-type and a charset.

View file

@ -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<Parameter> parameters();
public abstract Iterable<Map.Entry<String, String>> 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<String, String> parameterMap();
public static interface Parameter {
String key();
String value();
}
/**
* Returns the fragment part of this Uri.
*/

View file

@ -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 {

View file

@ -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

View file

@ -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

View file

@ -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")