diff --git a/akka-http-core/src/main/java/akka/http/javadsl/model/ContentType.java b/akka-http-core/src/main/java/akka/http/javadsl/model/ContentType.java deleted file mode 100644 index 9d7d2cf34a..0000000000 --- a/akka-http-core/src/main/java/akka/http/javadsl/model/ContentType.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright (C) 2009-2014 Typesafe Inc. - */ - -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 getCharsetOption(); - - interface Binary extends ContentType { - } - - interface NonBinary extends ContentType { - HttpCharset charset(); - } - - interface WithFixedCharset extends NonBinary { - } - - interface WithCharset extends NonBinary { - } -} diff --git a/akka-http-core/src/main/scala/akka/http/javadsl/model/ContentType.scala b/akka-http-core/src/main/scala/akka/http/javadsl/model/ContentType.scala new file mode 100644 index 0000000000..bc514fe201 --- /dev/null +++ b/akka-http-core/src/main/scala/akka/http/javadsl/model/ContentType.scala @@ -0,0 +1,45 @@ +/** + * Copyright (C) 2009-2014 Typesafe Inc. + */ +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] +} diff --git a/akka-http-core/src/main/scala/akka/http/javadsl/model/MediaType.scala b/akka-http-core/src/main/scala/akka/http/javadsl/model/MediaType.scala index 06101fb9f4..6fd07d4ed3 100644 --- a/akka-http-core/src/main/scala/akka/http/javadsl/model/MediaType.scala +++ b/akka-http-core/src/main/scala/akka/http/javadsl/model/MediaType.scala @@ -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 { diff --git a/akka-http-core/src/test/java/akka/http/javadsl/model/JavaApiTestCases.java b/akka-http-core/src/test/java/akka/http/javadsl/model/JavaApiTestCases.java index f33b9d0675..0a65c9b0d2 100644 --- a/akka-http-core/src/test/java/akka/http/javadsl/model/JavaApiTestCases.java +++ b/akka-http-core/src/test/java/akka/http/javadsl/model/JavaApiTestCases.java @@ -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; + } }