Merge pull request #19460 from ktoso/wip-contettype-ktoso

=htc #19418 avoid LUB problem in javadsl.model.ContentType, see SI-9621
This commit is contained in:
Konrad Malawski 2016-01-14 18:33:26 +01:00
commit 2925beace9
4 changed files with 126 additions and 91 deletions

View file

@ -1,41 +0,0 @@
/**
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.http.javadsl.model;
import akka.japi.Option;
/**
* Represents an Http content-type. A content-type consists of a media-type and an optional charset.
*/
public interface ContentType {
/**
* The media-type of this content-type.
*/
MediaType mediaType();
/**
* True if this ContentType is non-textual.
*/
boolean binary();
/**
* Returns the charset if this ContentType is non-binary.
*/
Option<HttpCharset> getCharsetOption();
interface Binary extends ContentType {
}
interface NonBinary extends ContentType {
HttpCharset charset();
}
interface WithFixedCharset extends NonBinary {
}
interface WithCharset extends NonBinary {
}
}

View file

@ -0,0 +1,45 @@
/**
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.http.javadsl.model
import akka.japi.Option
/**
* Represents an Http content-type. A content-type consists of a media-type and an optional charset.
*/
// Has to be defined in Scala even though it's JavaDSL because of:
// https://issues.scala-lang.org/browse/SI-9621
object ContentType {
trait Binary extends ContentType {
}
trait NonBinary extends ContentType {
def charset: HttpCharset
}
trait WithFixedCharset extends NonBinary {
}
trait WithCharset extends NonBinary {
}
}
trait ContentType {
/**
* The media-type of this content-type.
*/
def mediaType: MediaType
/**
* True if this ContentType is non-textual.
*/
def binary: Boolean
/**
* Returns the charset if this ContentType is non-binary.
*/
def getCharsetOption: Option[HttpCharset]
}

View file

@ -6,8 +6,9 @@ package akka.http.javadsl.model
/**
* Represents an Http media-type. A media-type consists of a main-type and a sub-type.
*
* Has to be defined in Scala even though it's JavaDSL because of: https://issues.scala-lang.org/browse/SI-9621
*/
// Has to be defined in Scala even though it's JavaDSL because of:
// https://issues.scala-lang.org/browse/SI-9621
object MediaType {
trait Binary extends MediaType {

View file

@ -5,66 +5,96 @@
package akka.http.javadsl.model;
import akka.http.impl.util.Util;
import akka.http.javadsl.model.headers.Authorization;
import akka.http.javadsl.model.headers.HttpCredentials;
import akka.http.javadsl.model.headers.*;
import akka.japi.Pair;
public class JavaApiTestCases {
/** Builds a request for use on the client side */
public static HttpRequest buildRequest() {
/**
* Builds a request for use on the client side
*/
public static HttpRequest buildRequest() {
return
HttpRequest.create()
.withMethod(HttpMethods.POST)
.withUri("/send");
}
/**
* A simple handler for an Http server
*/
public static HttpResponse handleRequest(HttpRequest request) {
if (request.method() == HttpMethods.GET) {
Uri uri = request.getUri();
if (uri.path().equals("/hello")) {
String name = Util.getOrElse(uri.query().get("name"), "Mister X");
return
HttpRequest.create()
.withMethod(HttpMethods.POST)
.withUri("/send");
}
/** A simple handler for an Http server */
public static HttpResponse handleRequest(HttpRequest request) {
if (request.method() == HttpMethods.GET) {
Uri uri = request.getUri();
if (uri.path().equals("/hello")) {
String name = Util.getOrElse(uri.query().get("name"), "Mister X");
return
HttpResponse.create()
.withEntity("Hello " + name + "!");
} else
return
HttpResponse.create()
.withStatus(404)
.withEntity("Not found");
} else
return
HttpResponse.create()
HttpResponse.create()
.withEntity("Hello " + name + "!");
} else {
return
HttpResponse.create()
.withStatus(404)
.withEntity("Not found");
}
} else {
return
HttpResponse.create()
.withStatus(StatusCodes.METHOD_NOT_ALLOWED)
.withEntity("Unsupported method");
}
}
/** Adds authentication to an existing request */
public static HttpRequest addAuthentication(HttpRequest request) {
// unused here but just to show the shortcut
request.addHeader(Authorization.basic("username", "password"));
/**
* Adds authentication to an existing request
*/
public static HttpRequest addAuthentication(HttpRequest request) {
// unused here but just to show the shortcut
request.addHeader(Authorization.basic("username", "password"));
return request
.addHeader(Authorization.create(HttpCredentials.createBasicHttpCredentials("username", "password")));
return request
.addHeader(Authorization.create(HttpCredentials.createBasicHttpCredentials("username", "password")));
}
}
/** Removes cookies from an existing request */
public static HttpRequest removeCookies(HttpRequest request) {
return request.removeHeader("Cookie");
}
/**
* Removes cookies from an existing request
*/
public static HttpRequest removeCookies(HttpRequest request) {
return request.removeHeader("Cookie");
}
/** Build a uri to send a form */
public static Uri createUriForOrder(String orderId, String price, String amount) {
return Uri.create("/order").query(
Query.create(
Pair.create("orderId", orderId),
Pair.create("price", price),
Pair.create("amount", amount)));
}
/**
* Build a uri to send a form
*/
public static Uri createUriForOrder(String orderId, String price, String amount) {
return Uri.create("/order").query(
Query.create(
Pair.create("orderId", orderId),
Pair.create("price", price),
Pair.create("amount", amount)));
}
public static Query addSessionId(Query query) {
return query.withParam("session", "abcdefghijkl");
}
public static Query addSessionId(Query query) {
return query.withParam("session", "abcdefghijkl");
}
public static Object accessScalaDefinedJavadslContentTypeAndMediaType(ContentType type) {
Object anything = null;
akka.http.javadsl.model.MediaType mediaType = type.mediaType();
// just for the sake of explicitly touching the interfaces
if (mediaType.binary()) anything = (akka.http.javadsl.model.MediaType.Binary) mediaType;
if (1 == 2) anything = (akka.http.javadsl.model.MediaType.Multipart) mediaType;
if (1 == 2) anything = (akka.http.javadsl.model.MediaType.WithOpenCharset) mediaType;
if (1 == 2) anything = (akka.http.javadsl.model.MediaType.WithFixedCharset) mediaType;
if (type.binary()) anything = (akka.http.javadsl.model.ContentType.Binary) type;
if (1 == 2) anything = (akka.http.javadsl.model.ContentType.NonBinary) type;
if (1 == 2) anything = (akka.http.javadsl.model.ContentType.WithCharset) type;
if (1 == 2) anything = (akka.http.javadsl.model.ContentType.WithFixedCharset) type;
return anything;
}
}