2015-07-10 19:13:56 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2015-07-17 15:30:14 +02:00
|
|
|
package docs.http.javadsl.server;
|
2015-07-10 19:13:56 +02:00
|
|
|
|
|
|
|
|
import akka.actor.ActorSystem;
|
2015-09-29 23:08:11 +02:00
|
|
|
import akka.dispatch.OnFailure;
|
|
|
|
|
import akka.http.impl.util.JavaMapping;
|
2015-07-10 19:10:29 +02:00
|
|
|
import akka.http.impl.util.Util;
|
2015-07-10 19:13:56 +02:00
|
|
|
import akka.http.javadsl.Http;
|
|
|
|
|
import akka.http.javadsl.IncomingConnection;
|
|
|
|
|
import akka.http.javadsl.ServerBinding;
|
|
|
|
|
import akka.http.javadsl.model.*;
|
2015-09-29 23:08:11 +02:00
|
|
|
import akka.http.javadsl.model.ContentTypes;
|
2015-09-30 14:06:23 +02:00
|
|
|
import akka.http.javadsl.model.HttpMethods;
|
2015-09-29 23:08:11 +02:00
|
|
|
import akka.http.javadsl.model.HttpRequest;
|
|
|
|
|
import akka.http.javadsl.model.HttpResponse;
|
|
|
|
|
import akka.http.javadsl.model.Uri;
|
2015-09-30 14:06:23 +02:00
|
|
|
import akka.http.scaladsl.model.*;
|
|
|
|
|
import akka.http.scaladsl.model.HttpEntity;
|
2015-09-29 23:08:11 +02:00
|
|
|
import akka.japi.JavaPartialFunction;
|
2015-07-10 19:13:56 +02:00
|
|
|
import akka.japi.function.Function;
|
|
|
|
|
import akka.japi.function.Procedure;
|
|
|
|
|
import akka.stream.ActorMaterializer;
|
|
|
|
|
import akka.stream.Materializer;
|
2015-09-29 23:08:11 +02:00
|
|
|
import akka.stream.javadsl.Flow;
|
2015-07-10 19:13:56 +02:00
|
|
|
import akka.stream.javadsl.Sink;
|
|
|
|
|
import akka.stream.javadsl.Source;
|
2015-09-29 23:08:11 +02:00
|
|
|
import akka.stream.stage.Context;
|
|
|
|
|
import akka.stream.stage.PushStage;
|
|
|
|
|
import akka.stream.stage.SyncDirective;
|
|
|
|
|
import akka.stream.stage.TerminationDirective;
|
|
|
|
|
import akka.util.ByteString;
|
|
|
|
|
import scala.Function1;
|
2015-07-10 19:13:56 +02:00
|
|
|
import scala.concurrent.Await;
|
|
|
|
|
import scala.concurrent.Future;
|
|
|
|
|
import scala.concurrent.duration.FiniteDuration;
|
2015-09-29 23:08:11 +02:00
|
|
|
import scala.runtime.BoxedUnit;
|
2015-07-10 19:13:56 +02:00
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
2015-09-29 23:08:11 +02:00
|
|
|
@SuppressWarnings("unused")
|
2015-07-13 16:46:07 +02:00
|
|
|
public class HttpServerExampleDocTest {
|
2015-09-29 23:08:11 +02:00
|
|
|
|
2015-09-25 12:51:55 +02:00
|
|
|
public static void bindingExample() throws Exception {
|
2015-07-10 19:13:56 +02:00
|
|
|
//#binding-example
|
|
|
|
|
ActorSystem system = ActorSystem.create();
|
|
|
|
|
Materializer materializer = ActorMaterializer.create(system);
|
|
|
|
|
|
|
|
|
|
Source<IncomingConnection, Future<ServerBinding>> serverSource =
|
|
|
|
|
Http.get(system).bind("localhost", 8080, materializer);
|
|
|
|
|
|
|
|
|
|
Future<ServerBinding> serverBindingFuture =
|
|
|
|
|
serverSource.to(Sink.foreach(
|
|
|
|
|
new Procedure<IncomingConnection>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(IncomingConnection connection) throws Exception {
|
|
|
|
|
System.out.println("Accepted new connection from " + connection.remoteAddress());
|
|
|
|
|
// ... and then actually handle the connection
|
|
|
|
|
}
|
|
|
|
|
})).run(materializer);
|
|
|
|
|
//#binding-example
|
2015-09-25 12:51:55 +02:00
|
|
|
Await.result(serverBindingFuture, new FiniteDuration(3, TimeUnit.SECONDS));
|
2015-07-10 19:13:56 +02:00
|
|
|
}
|
2015-09-29 23:08:11 +02:00
|
|
|
|
|
|
|
|
public static void bindingFailureExample() throws Exception {
|
|
|
|
|
//#binding-failure-handling
|
|
|
|
|
ActorSystem system = ActorSystem.create();
|
|
|
|
|
Materializer materializer = ActorMaterializer.create(system);
|
|
|
|
|
|
|
|
|
|
Source<IncomingConnection, Future<ServerBinding>> serverSource =
|
|
|
|
|
Http.get(system).bind("localhost", 80, materializer);
|
|
|
|
|
|
|
|
|
|
Future<ServerBinding> serverBindingFuture =
|
|
|
|
|
serverSource.to(Sink.foreach(
|
|
|
|
|
new Procedure<IncomingConnection>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(IncomingConnection connection) throws Exception {
|
|
|
|
|
System.out.println("Accepted new connection from " + connection.remoteAddress());
|
|
|
|
|
// ... and then actually handle the connection
|
|
|
|
|
}
|
|
|
|
|
})).run(materializer);
|
|
|
|
|
|
|
|
|
|
serverBindingFuture.onFailure(new OnFailure() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onFailure(Throwable failure) throws Throwable {
|
|
|
|
|
// possibly report the failure somewhere...
|
|
|
|
|
}
|
|
|
|
|
}, system.dispatcher());
|
|
|
|
|
//#binding-failure-handling
|
|
|
|
|
Await.result(serverBindingFuture, new FiniteDuration(3, TimeUnit.SECONDS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void connectionSourceFailureExample() throws Exception {
|
|
|
|
|
//#incoming-connections-source-failure-handling
|
|
|
|
|
ActorSystem system = ActorSystem.create();
|
|
|
|
|
Materializer materializer = ActorMaterializer.create(system);
|
|
|
|
|
|
|
|
|
|
Source<IncomingConnection, Future<ServerBinding>> serverSource =
|
|
|
|
|
Http.get(system).bind("localhost", 8080, materializer);
|
|
|
|
|
|
|
|
|
|
Flow<IncomingConnection, IncomingConnection, BoxedUnit> failureDetection =
|
|
|
|
|
Flow.of(IncomingConnection.class).transform(() ->
|
|
|
|
|
new PushStage<IncomingConnection, IncomingConnection>() {
|
|
|
|
|
@Override
|
|
|
|
|
public SyncDirective onPush(IncomingConnection elem, Context<IncomingConnection> ctx) {
|
|
|
|
|
return ctx.push(elem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public TerminationDirective onUpstreamFailure(Throwable cause, Context<IncomingConnection> ctx) {
|
|
|
|
|
// signal the failure to external monitoring service!
|
|
|
|
|
return super.onUpstreamFailure(cause, ctx);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Future<ServerBinding> serverBindingFuture =
|
|
|
|
|
serverSource
|
|
|
|
|
.via(failureDetection) // feed signals through our custom stage
|
|
|
|
|
.to(Sink.foreach(
|
|
|
|
|
new Procedure<IncomingConnection>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(IncomingConnection connection) throws Exception {
|
|
|
|
|
System.out.println("Accepted new connection from " + connection.remoteAddress());
|
|
|
|
|
// ... and then actually handle the connection
|
|
|
|
|
}
|
|
|
|
|
})).run(materializer);
|
|
|
|
|
//#incoming-connections-source-failure-handling
|
|
|
|
|
Await.result(serverBindingFuture, new FiniteDuration(3, TimeUnit.SECONDS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void connectionStreamFailureExample() throws Exception {
|
|
|
|
|
//#connection-stream-failure-handling
|
|
|
|
|
ActorSystem system = ActorSystem.create();
|
|
|
|
|
Materializer materializer = ActorMaterializer.create(system);
|
|
|
|
|
|
|
|
|
|
Source<IncomingConnection, Future<ServerBinding>> serverSource =
|
|
|
|
|
Http.get(system).bind("localhost", 8080, materializer);
|
|
|
|
|
|
|
|
|
|
Flow<HttpRequest, HttpRequest, BoxedUnit> failureDetection =
|
|
|
|
|
Flow.of(HttpRequest.class).transform(() ->
|
|
|
|
|
new PushStage<HttpRequest, HttpRequest>() {
|
|
|
|
|
@Override
|
|
|
|
|
public SyncDirective onPush(HttpRequest elem, Context<HttpRequest> ctx) {
|
|
|
|
|
return ctx.push(elem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public TerminationDirective onUpstreamFailure(Throwable cause, Context<HttpRequest> ctx) {
|
|
|
|
|
// signal the failure to external monitoring service!
|
|
|
|
|
return super.onUpstreamFailure(cause, ctx);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Flow<HttpRequest, HttpResponse, BoxedUnit> httpEcho =
|
|
|
|
|
Flow.of(HttpRequest.class)
|
|
|
|
|
.via(failureDetection)
|
|
|
|
|
.map(request -> {
|
2015-09-30 14:06:23 +02:00
|
|
|
Source<ByteString, ?> bytes = request.entity().getDataBytes();
|
|
|
|
|
HttpEntity.Chunked entity = HttpEntities.create(ContentTypes.TEXT_PLAIN, (Source<ByteString, Object>) bytes);
|
|
|
|
|
|
|
|
|
|
return HttpResponse.create()
|
|
|
|
|
.withEntity(entity);
|
2015-09-29 23:08:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Future<ServerBinding> serverBindingFuture =
|
|
|
|
|
serverSource.to(Sink.foreach(con -> {
|
|
|
|
|
System.out.println("Accepted new connection from " + con.remoteAddress());
|
|
|
|
|
con.handleWith(httpEcho, materializer);
|
|
|
|
|
}
|
|
|
|
|
)).run(materializer);
|
|
|
|
|
//#connection-stream-failure-handling
|
|
|
|
|
Await.result(serverBindingFuture, new FiniteDuration(3, TimeUnit.SECONDS));
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-10 19:10:29 +02:00
|
|
|
public static void fullServerExample() throws Exception {
|
2015-07-10 19:13:56 +02:00
|
|
|
//#full-server-example
|
2015-07-10 19:10:29 +02:00
|
|
|
ActorSystem system = ActorSystem.create();
|
|
|
|
|
//#full-server-example
|
|
|
|
|
try {
|
|
|
|
|
//#full-server-example
|
|
|
|
|
final Materializer materializer = ActorMaterializer.create(system);
|
2015-07-10 19:13:56 +02:00
|
|
|
|
2015-07-10 19:10:29 +02:00
|
|
|
Source<IncomingConnection, Future<ServerBinding>> serverSource =
|
|
|
|
|
Http.get(system).bind("localhost", 8080, materializer);
|
2015-07-10 19:13:56 +02:00
|
|
|
|
2015-07-10 19:10:29 +02:00
|
|
|
//#request-handler
|
|
|
|
|
final Function<HttpRequest, HttpResponse> requestHandler =
|
|
|
|
|
new Function<HttpRequest, HttpResponse>() {
|
|
|
|
|
private final HttpResponse NOT_FOUND =
|
|
|
|
|
HttpResponse.create()
|
|
|
|
|
.withStatus(404)
|
|
|
|
|
.withEntity("Unknown resource!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public HttpResponse apply(HttpRequest request) throws Exception {
|
|
|
|
|
Uri uri = request.getUri();
|
|
|
|
|
if (request.method() == HttpMethods.GET) {
|
|
|
|
|
if (uri.path().equals("/"))
|
|
|
|
|
return
|
|
|
|
|
HttpResponse.create()
|
2015-09-29 20:58:47 +02:00
|
|
|
.withEntity(ContentTypes.TEXT_HTML,
|
2015-07-10 19:10:29 +02:00
|
|
|
"<html><body>Hello world!</body></html>");
|
|
|
|
|
else if (uri.path().equals("/hello")) {
|
|
|
|
|
String name = Util.getOrElse(uri.parameter("name"), "Mister X");
|
2015-07-10 19:13:56 +02:00
|
|
|
|
2015-07-10 19:10:29 +02:00
|
|
|
return
|
|
|
|
|
HttpResponse.create()
|
|
|
|
|
.withEntity("Hello " + name + "!");
|
|
|
|
|
}
|
|
|
|
|
else if (uri.path().equals("/ping"))
|
|
|
|
|
return HttpResponse.create().withEntity("PONG!");
|
|
|
|
|
else
|
|
|
|
|
return NOT_FOUND;
|
|
|
|
|
}
|
|
|
|
|
else return NOT_FOUND;
|
2015-07-10 19:13:56 +02:00
|
|
|
}
|
2015-07-10 19:10:29 +02:00
|
|
|
};
|
|
|
|
|
//#request-handler
|
2015-07-10 19:13:56 +02:00
|
|
|
|
2015-07-10 19:10:29 +02:00
|
|
|
Future<ServerBinding> serverBindingFuture =
|
2015-07-10 19:13:56 +02:00
|
|
|
serverSource.to(Sink.foreach(
|
2015-07-10 19:10:29 +02:00
|
|
|
new Procedure<IncomingConnection>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(IncomingConnection connection) throws Exception {
|
|
|
|
|
System.out.println("Accepted new connection from " + connection.remoteAddress());
|
2015-07-10 19:13:56 +02:00
|
|
|
|
2015-07-10 19:10:29 +02:00
|
|
|
connection.handleWithSyncHandler(requestHandler, materializer);
|
|
|
|
|
// this is equivalent to
|
|
|
|
|
//connection.handleWith(Flow.of(HttpRequest.class).map(requestHandler), materializer);
|
|
|
|
|
}
|
|
|
|
|
})).run(materializer);
|
|
|
|
|
//#full-server-example
|
|
|
|
|
|
|
|
|
|
Await.result(serverBindingFuture, new FiniteDuration(1, TimeUnit.SECONDS)); // will throw if binding fails
|
|
|
|
|
System.out.println("Press ENTER to stop.");
|
|
|
|
|
new BufferedReader(new InputStreamReader(System.in)).readLine();
|
|
|
|
|
} finally {
|
|
|
|
|
system.shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
fullServerExample();
|
2015-07-10 19:13:56 +02:00
|
|
|
}
|
|
|
|
|
}
|