+htc,java #18600 Add missing convenience ContentTypes class

This commit is contained in:
Konrad Malawski 2015-09-29 20:58:47 +02:00
parent 239b4ca268
commit 2f2e07666e
4 changed files with 25 additions and 2 deletions

View file

@ -6,6 +6,7 @@ package docs.http.javadsl.server;
//#high-level-server-example
import akka.actor.ActorSystem;
import akka.http.javadsl.model.ContentTypes;
import akka.http.javadsl.model.MediaTypes;
import akka.http.javadsl.server.*;
import akka.http.javadsl.server.values.Parameters;
@ -50,7 +51,7 @@ public class HighLevelServerExample extends HttpApp {
// matches the empty path
pathSingleSlash().route(
// return a constant string with a certain content type
complete(MediaTypes.TEXT_HTML.toContentType(),
complete(ContentTypes.TEXT_HTML,
"<html><body>Hello world!</body></html>")
),
path("ping").route(

View file

@ -72,7 +72,7 @@ public class HttpServerExampleDocTest {
if (uri.path().equals("/"))
return
HttpResponse.create()
.withEntity(MediaTypes.TEXT_HTML.toContentType(),
.withEntity(ContentTypes.TEXT_HTML,
"<html><body>Hello world!</body></html>");
else if (uri.path().equals("/hello")) {
String name = Util.getOrElse(uri.parameter("name"), "Mister X");

View file

@ -0,0 +1,21 @@
/**
* Copyright (C) 2009-2015 Typesafe Inc. <http:__www.typesafe.com>
*/
package akka.http.javadsl.model;
/**
* Contains the set of predefined content-types.
* <p>
* If the {@link ContentType} you're looking for is not pre-defined here,
* you can obtain it from a {@link MediaType} by using:
* <p>
* {@code MediaTypes.TEXT_HTML.toContentType()}
*/
public final class ContentTypes {
public static final ContentType APPLICATION_JSON = MediaTypes.APPLICATION_JSON.toContentType();
public static final ContentType TEXT_PLAIN = MediaTypes.TEXT_PLAIN.toContentType();
public static final ContentType TEXT_PLAIN_UTF8 = akka.http.scaladsl.model.ContentTypes.text$divplain$u0028UTF$minus8$u0029();
public static final ContentType TEXT_HTML = MediaTypes.TEXT_HTML.toContentType();
public static final ContentType APPLICATION_OCTET_STREAM = MediaTypes.APPLICATION_OCTET_STREAM.toContentType();
}

View file

@ -92,6 +92,7 @@ object ContentTypes {
val `application/json` = ContentType(MediaTypes.`application/json`)
val `text/plain` = ContentType(MediaTypes.`text/plain`)
val `text/plain(UTF-8)` = ContentType(MediaTypes.`text/plain`, HttpCharsets.`UTF-8`)
val `text/html` = ContentType(MediaTypes.`text/html`)
val `application/octet-stream` = ContentType(MediaTypes.`application/octet-stream`)
// used for explicitly suppressing the rendering of Content-Type headers on requests and responses