+htc add popular ContentType instances for convenience

This commit is contained in:
Konrad Malawski 2015-09-30 14:06:23 +02:00
parent 573a69e2b9
commit 9bd11620f0
5 changed files with 60 additions and 33 deletions

View file

@ -31,7 +31,7 @@ public class HighLevelServerBindFailureExample {
System.err.println("Something very bad happened! " + failure.getMessage());
system.shutdown();
}
});
}, system.dispatcher());
system.shutdown();
}

View file

@ -13,9 +13,12 @@ import akka.http.javadsl.IncomingConnection;
import akka.http.javadsl.ServerBinding;
import akka.http.javadsl.model.*;
import akka.http.javadsl.model.ContentTypes;
import akka.http.javadsl.model.HttpMethods;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.model.Uri;
import akka.http.scaladsl.model.*;
import akka.http.scaladsl.model.HttpEntity;
import akka.japi.JavaPartialFunction;
import akka.japi.function.Function;
import akka.japi.function.Procedure;
@ -156,10 +159,11 @@ public class HttpServerExampleDocTest {
Flow.of(HttpRequest.class)
.via(failureDetection)
.map(request -> {
Source<ByteString, ?> bytes = request.entity().getDataBytes();
HttpEntity entity = HttpEntities.create(ContentTypes.TEXT_PLAIN, (Source<ByteString, Object>) bytes);
return HttpResponse.create()
.withEntity(entity);
Source<ByteString, ?> bytes = request.entity().getDataBytes();
HttpEntity.Chunked entity = HttpEntities.create(ContentTypes.TEXT_PLAIN, (Source<ByteString, Object>) bytes);
return HttpResponse.create()
.withEntity(entity);
});
Future<ServerBinding> serverBindingFuture =

View file

@ -12,7 +12,9 @@ import akka.http.scaladsl.model._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{ Flow, Sink }
import akka.stream.stage.{ Context, PushStage }
import akka.testkit.TestActors
import org.scalatest.{ Matchers, WordSpec }
import scala.language.postfixOps
import scala.concurrent.{ ExecutionContext, Future }
@ -21,7 +23,9 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
// never actually called
val log: LoggingAdapter = null
"binding-example" in {
def compileOnlySpec(body: => Unit) = ()
"binding-example" in compileOnlySpec {
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import akka.stream.scaladsl._
@ -39,7 +43,7 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
}).run()
}
"binding-failure-high-level-example" in {
"binding-failure-high-level-example" in compileOnlySpec {
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
@ -68,7 +72,7 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
val handleConnections: Sink[Http.IncomingConnection, Future[Http.ServerBinding]] =
Sink.ignore.mapMaterializedValue(_ => Future.failed(new Exception("")))
"binding-failure-handling" in {
"binding-failure-handling" in compileOnlySpec {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
@ -87,7 +91,11 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
}
}
"incoming-connections-source-failure-handling" in {
object MyExampleMonitoringActor {
def props = TestActors.echoActorProps
}
"incoming-connections-source-failure-handling" in compileOnlySpec {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
@ -96,7 +104,7 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
val (host, port) = ("localhost", 8080)
val serverSource = Http().bind(host, port)
val failureMonitor: ActorRef = ???
val failureMonitor: ActorRef = system.actorOf(MyExampleMonitoringActor.props)
val reactToTopLevelFailures = Flow[IncomingConnection]
.transform { () =>
@ -117,7 +125,7 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
.run()
}
"connection-stream-failure-handling" in {
"connection-stream-failure-handling" in compileOnlySpec {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
@ -151,7 +159,7 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
}
}
"full-server-example" in {
"full-server-example" in compileOnlySpec {
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model._
@ -188,7 +196,7 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
}).run()
}
"low-level-server-example" in {
"low-level-server-example" in compileOnlySpec {
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model._
@ -217,7 +225,7 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
// format: OFF
"high-level-server-example" in {
"high-level-server-example" in compileOnlySpec {
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._
import akka.http.scaladsl.server.Directives._
@ -247,7 +255,7 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
Http().bindAndHandle(route, "localhost", 8080)
}
"minimal-routing-example" in {
"minimal-routing-example" in compileOnlySpec {
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._
import akka.http.scaladsl.server.Directives._
@ -277,12 +285,16 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
}
}
"long-routing-example" in {
"long-routing-example" in compileOnlySpec {
//#long-routing-example
import akka.actor.ActorRef
import akka.http.scaladsl.coding.Deflate
import akka.http.scaladsl.marshalling.ToResponseMarshaller
import akka.http.scaladsl.model.StatusCodes.MovedPermanently
import akka.http.scaladsl.server.Directives._
// TODO: these explicit imports are only needed in complex cases, like below; Also, not needed on Scala 2.11
import akka.http.scaladsl.server.directives.ParameterDirectives.ParamMagnet
import akka.http.scaladsl.server.directives.FormFieldDirectives.FieldMagnet
import akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller
import akka.pattern.ask
import akka.util.Timeout
@ -294,13 +306,22 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
case class Order(email: String, amount: Money)
case class Update(order: Order)
case class OrderItem(i: Int, os: Option[String], s: String)
// marshalling would usually be derived automatically using libraries
implicit val orderUM: FromRequestUnmarshaller[Order] = ???
implicit val orderM: ToResponseMarshaller[Seq[Order]] = ???
implicit val orderM: ToResponseMarshaller[Order] = ???
implicit val orderSeqM: ToResponseMarshaller[Seq[Order]] = ???
implicit val timeout: Timeout = ??? // for actor asks
implicit val ec: ExecutionContext = ???
implicit val mat: ActorMaterializer = ???
implicit val sys: ActorSystem = ???
// backend entry points
def myAuthenticator: Authenticator[User] = ???
def retrieveOrdersFromDB: Seq[Order] = ???
def myDbActor: ActorRef = ???
def processOrderRequest(id: Int, complete: Order => Unit): Unit = ???
val route = {
path("orders") {
authenticateBasic(realm = "admin area", myAuthenticator) { user =>
@ -373,11 +394,5 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
redirect("http://oldapi.example.com/" + pathRest, MovedPermanently)
}
}
// backend entry points
def myAuthenticator: Authenticator[User] = ???
def retrieveOrdersFromDB: Seq[Order] = ???
def myDbActor: ActorRef = ???
def processOrderRequest(id: Int, complete: Order => Unit): Unit = ???
}
}

View file

@ -5,17 +5,20 @@ package akka.http.javadsl.model;
/**
* Contains the set of predefined content-types.
* Contains the set of predefined content-types for convenience.
* <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()}
* you can obtain it from a {@link MediaType} by using: {@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();
public static final ContentType APPLICATION_JSON = MediaTypes.APPLICATION_JSON.toContentType();
public static final ContentType APPLICATION_OCTET_STREAM = MediaTypes.APPLICATION_OCTET_STREAM.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 TEXT_XML = MediaTypes.TEXT_XML.toContentType();
public static final ContentType APPLICATION_X_WWW_FORM_URLENCODED = MediaTypes.APPLICATION_X_WWW_FORM_URLENCODED.toContentType();
public static final ContentType MULTIPART_FORM_DATA = MediaTypes.MULTIPART_FORM_DATA.toContentType();
}

View file

@ -90,10 +90,15 @@ object ContentType {
object ContentTypes {
val `application/json` = ContentType(MediaTypes.`application/json`)
val `application/octet-stream` = ContentType(MediaTypes.`application/octet-stream`)
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`)
val `text/xml` = ContentType(MediaTypes.`text/xml`)
val `application/x-www-form-urlencoded` = ContentType(MediaTypes.`application/x-www-form-urlencoded`)
val `multipart/form-data` = ContentType(MediaTypes.`multipart/form-data`)
// used for explicitly suppressing the rendering of Content-Type headers on requests and responses
val NoContentType = ContentType(MediaTypes.NoMediaType)