From 8696022889af03d5af7594e79c3927c840025830 Mon Sep 17 00:00:00 2001 From: Kotharu Date: Sun, 21 Oct 2018 18:19:00 +0530 Subject: [PATCH 1/3] BAEL-1724 Akka HTTP Initial Commit --- akka-http/pom.xml | 48 +++++++++ .../com/baeldung/akkahttp/AkkaHttpServer.java | 39 ++++++++ .../main/java/com/baeldung/akkahttp/User.java | 30 ++++++ .../java/com/baeldung/akkahttp/UserActor.java | 60 ++++++++++++ .../akkahttp/UserRegistryMessages.java | 70 +++++++++++++ .../com/baeldung/akkahttp/UserRoutes.java | 98 +++++++++++++++++++ .../java/com/baeldung/akkahttp/Users.java | 25 +++++ pom.xml | 1 + 8 files changed, 371 insertions(+) create mode 100644 akka-http/pom.xml create mode 100644 akka-http/src/main/java/com/baeldung/akkahttp/AkkaHttpServer.java create mode 100644 akka-http/src/main/java/com/baeldung/akkahttp/User.java create mode 100644 akka-http/src/main/java/com/baeldung/akkahttp/UserActor.java create mode 100644 akka-http/src/main/java/com/baeldung/akkahttp/UserRegistryMessages.java create mode 100644 akka-http/src/main/java/com/baeldung/akkahttp/UserRoutes.java create mode 100644 akka-http/src/main/java/com/baeldung/akkahttp/Users.java diff --git a/akka-http/pom.xml b/akka-http/pom.xml new file mode 100644 index 0000000000..05e50d2229 --- /dev/null +++ b/akka-http/pom.xml @@ -0,0 +1,48 @@ + + + + 4.0.0 + akka-http + akka-http + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + + + + com.typesafe.akka + akka-http_2.12 + ${akka.http.version} + + + com.typesafe.akka + akka-stream_2.12 + ${akka.stream.version} + + + com.typesafe.akka + akka-http-jackson_2.12 + ${akka.http.version} + + + com.typesafe.akka + akka-http-testkit_2.12 + ${akka.http.version} + test + + + + + UTF-8 + UTF-8 + 1.8 + 10.0.11 + 2.5.11 + + diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/AkkaHttpServer.java b/akka-http/src/main/java/com/baeldung/akkahttp/AkkaHttpServer.java new file mode 100644 index 0000000000..03992b6a36 --- /dev/null +++ b/akka-http/src/main/java/com/baeldung/akkahttp/AkkaHttpServer.java @@ -0,0 +1,39 @@ +package com.baeldung.akkahttp; + +import java.util.concurrent.CompletionStage; + +import akka.NotUsed; +import akka.actor.ActorRef; +import akka.actor.ActorSystem; +import akka.http.javadsl.ConnectHttp; +import akka.http.javadsl.Http; +import akka.http.javadsl.ServerBinding; +import akka.http.javadsl.model.HttpRequest; +import akka.http.javadsl.model.HttpResponse; +import akka.http.javadsl.server.AllDirectives; +import akka.stream.ActorMaterializer; +import akka.stream.javadsl.Flow; + +public class AkkaHttpServer extends AllDirectives { + + private final UserRoutes userRoutes; + + public AkkaHttpServer(ActorSystem system, ActorRef userRegistryActor) { + userRoutes = new UserRoutes(system, userRegistryActor); + } + + public static void main(String[] args) throws Exception { + ActorSystem system = ActorSystem.create("helloAkkaHttpServer"); + final ActorMaterializer materializer = ActorMaterializer.create(system); + + ActorRef userActorRef = system.actorOf(UserActor.props(), "userActor"); + + AkkaHttpServer app = new AkkaHttpServer(system, userActorRef); + + final Flow routeFlow = app.userRoutes.routes().flow(system, materializer); + + final CompletionStage binding = Http.get(system).bindAndHandle(routeFlow, + ConnectHttp.toHost("localhost", 8080), materializer); + + System.out.println("Server is online at http://localhost:8080/"); + } \ No newline at end of file diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/User.java b/akka-http/src/main/java/com/baeldung/akkahttp/User.java new file mode 100644 index 0000000000..9b5b95ebc1 --- /dev/null +++ b/akka-http/src/main/java/com/baeldung/akkahttp/User.java @@ -0,0 +1,30 @@ +package com.baeldung.akkahttp; + +/** + * User Entity + * + */ +public class User { + + private final String name; + private final String address; + + public User() { + this.name = ""; + this.address = ""; + } + + public User(String name, String address) { + this.name = name; + this.address = address; + } + + public String getName() { + return name; + } + + public String getAddress() { + return address; + } + +} \ No newline at end of file diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/UserActor.java b/akka-http/src/main/java/com/baeldung/akkahttp/UserActor.java new file mode 100644 index 0000000000..f6c7015070 --- /dev/null +++ b/akka-http/src/main/java/com/baeldung/akkahttp/UserActor.java @@ -0,0 +1,60 @@ +package com.baeldung.akkahttp; + +import java.util.ArrayList; +import java.util.List; + +import akka.actor.AbstractActor; +import akka.actor.Props; +import akka.event.Logging; +import akka.event.LoggingAdapter; + +/** + * User Actor + * + */ +public class UserActor extends AbstractActor { + + LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this); + + /** + * In memory List to hold the users added via POST operation. + */ + private final List users = new ArrayList<>(); + + /** + * To define UserActor Props. + * + * @return Props + */ + static Props props() { + return Props.create(UserActor.class); + } + + /** + * createReveive() implementation + */ + @Override + public Receive createReceive() { + return receiveBuilder().match(UserRegistryMessages.GetUsers.class, getUsers -> getSender().tell(new Users(users), getSelf())) + .match(UserRegistryMessages.CreateUser.class, createUser -> { + users.add(createUser.getUser()); + getSender().tell(new UserRegistryMessages.ActionPerformed(String.format("User %s created.", createUser.getUser() + .getName())), getSelf()); + }) + .match(UserRegistryMessages.GetUser.class, getUser -> { + getSender().tell(users.stream() + .filter(user -> user.getName() + .equals(getUser.getName())) + .findFirst(), getSelf()); + }) + .match(UserRegistryMessages.DeleteUser.class, deleteUser -> { + users.removeIf(user -> user.getName() + .equals(deleteUser.getName())); + getSender().tell(new UserRegistryMessages.ActionPerformed(String.format("User %s deleted.", deleteUser.getName())), getSelf()); + + }) + .matchAny(o -> log.info("received unknown message")) + .build(); + } + +} diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/UserRegistryMessages.java b/akka-http/src/main/java/com/baeldung/akkahttp/UserRegistryMessages.java new file mode 100644 index 0000000000..831819849a --- /dev/null +++ b/akka-http/src/main/java/com/baeldung/akkahttp/UserRegistryMessages.java @@ -0,0 +1,70 @@ +package com.baeldung.akkahttp; + +/** + * Defines all messages related to User Actor + * + */ +public interface UserRegistryMessages { + + class GetUsers implements Serializable { + + private static final long serialVersionUID = 1L; + } + + class ActionPerformed implements Serializable { + + private static final long serialVersionUID = 1L; + + private final String description; + + public ActionPerformed(String description) { + this.description = description; + } + + public String getDescription() { + return description; + } + } + + class CreateUser implements Serializable { + + private static final long serialVersionUID = 1L; + private final User user; + + public CreateUser(User user) { + this.user = user; + } + + public User getUser() { + return user; + } + } + + class GetUser implements Serializable { + + private static final long serialVersionUID = 1L; + private final String name; + + public GetUser(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } + + class DeleteUser implements Serializable { + + private static final long serialVersionUID = 1L; + private final String name; + + public DeleteUser(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } +} diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/UserRoutes.java b/akka-http/src/main/java/com/baeldung/akkahttp/UserRoutes.java new file mode 100644 index 0000000000..aa3c9b612f --- /dev/null +++ b/akka-http/src/main/java/com/baeldung/akkahttp/UserRoutes.java @@ -0,0 +1,98 @@ +package com.baeldung.akkahttp; + +import java.time.Duration; +import java.util.Optional; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.TimeUnit; + +import akka.actor.ActorRef; +import akka.actor.ActorSystem; +import akka.event.Logging; +import akka.event.LoggingAdapter; +import akka.http.javadsl.marshallers.jackson.Jackson; +import akka.http.javadsl.model.StatusCodes; +import akka.http.javadsl.server.AllDirectives; +import akka.http.javadsl.server.PathMatchers; +import akka.http.javadsl.server.Route; +import akka.pattern.PatternsCS; +import akka.util.Timeout; + +public class UserRoutes extends AllDirectives { + + private final ActorRef userActorRef; + private final LoggingAdapter log; + Timeout timeout = new Timeout(Duration.create(5, TimeUnit.SECONDS)); + + public UserRoutes(ActorSystem system, ActorRef userActorRef) { + this.userActorRef = userActorRef; + log = Logging.getLogger(system, this); + } + + public Route routes() { + return pathPrefix("users", () -> route(getOrPostUsers(), path(PathMatchers.segment(), name -> route(getUser(name), deleteUser(name))))); + } + + /** + * Defines and returns a route to get the user by name + * + * @param name + * @return Route + */ + private Route getUser(String name) { + + Route getRoute = get(() -> { + CompletionStage> user = PatternsCS.ask(userActorRef, new UserRegistryMessages.GetUser(name), timeout) + .thenApply(obj -> (Optional) obj); + + return onSuccess(() -> user, performed -> { + if (performed.isPresent()) + return complete(StatusCodes.OK, performed.get(), Jackson.marshaller()); + else + return complete(StatusCodes.NOT_FOUND); + }); + }); + return getRoute; + } + + /** + * Defines and returns a Route to delete the user by name. + * + * @param name + * @return Route + */ + private Route deleteUser(String name) { + + Route deleteRoute = delete(() -> { + CompletionStage userDeleted = PatternsCS.ask(userActorRef, new UserRegistryMessages.DeleteUser(name), timeout) + .thenApply(obj -> (ActionPerformed) obj); + + return onSuccess(() -> userDeleted, performed -> { + log.info("Deleted user [{}]: {}", name, performed.getDescription()); + return complete(StatusCodes.OK, performed, Jackson.marshaller()); + }); + }); + + return deleteRoute; + } + + /** + * Defines two routes, one to get all users and the other is to post users. + * + * @return + */ + private Route getOrPostUsers() { + return pathEnd(() -> route(get(() -> { + CompletionStage futureUsers = PatternsCS.ask(userActorRef, new UserRegistryMessages.GetUsers(), timeout) + .thenApply(obj -> (Users) obj); + return onSuccess(() -> futureUsers, users -> complete(StatusCodes.OK, users, Jackson.marshaller())); + }), post(() -> entity(Jackson.unmarshaller(User.class), user -> { + CompletionStage userCreated = PatternsCS.ask(userActorRef, new CreateUser(user), timeout) + .thenApply(obj -> (ActionPerformed) obj); + return onSuccess(() -> userCreated, performed -> { + log.info("Created user [{}]: {}", user.getName(), performed.getDescription()); + return complete(StatusCodes.CREATED, performed, Jackson.marshaller()); + }); + })))); + } + +} diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/Users.java b/akka-http/src/main/java/com/baeldung/akkahttp/Users.java new file mode 100644 index 0000000000..e5e6c62c06 --- /dev/null +++ b/akka-http/src/main/java/com/baeldung/akkahttp/Users.java @@ -0,0 +1,25 @@ +package com.baeldung.akkahttp; + +import java.util.ArrayList; +import java.util.List; + +/** + * This class is used to send the response enclosed by Users. + * + */ +public class Users { + + private final List users; + + public Users() { + this.users = new ArrayList<>(); + } + + public Users(List users) { + this.users = users; + } + + public List getUsers() { + return users; + } +} diff --git a/pom.xml b/pom.xml index 302530fd24..d0d0fa7049 100644 --- a/pom.xml +++ b/pom.xml @@ -1254,6 +1254,7 @@ aws aws-lambda akka-streams + akka-http algorithms annotations apache-cxf From 632e46c2a0bcde213e278f738879639752d01f9a Mon Sep 17 00:00:00 2001 From: Kotharu Date: Sun, 21 Oct 2018 18:40:44 +0530 Subject: [PATCH 2/3] Initial Commit --- .../com/baeldung/akkahttp/AkkaHttpServer.java | 20 ++++++++++--------- .../akkahttp/UserRegistryMessages.java | 2 ++ .../com/baeldung/akkahttp/UserRoutes.java | 5 ++++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/AkkaHttpServer.java b/akka-http/src/main/java/com/baeldung/akkahttp/AkkaHttpServer.java index 03992b6a36..d4c859891a 100644 --- a/akka-http/src/main/java/com/baeldung/akkahttp/AkkaHttpServer.java +++ b/akka-http/src/main/java/com/baeldung/akkahttp/AkkaHttpServer.java @@ -23,17 +23,19 @@ public class AkkaHttpServer extends AllDirectives { } public static void main(String[] args) throws Exception { - ActorSystem system = ActorSystem.create("helloAkkaHttpServer"); - final ActorMaterializer materializer = ActorMaterializer.create(system); + ActorSystem system = ActorSystem.create("helloAkkaHttpServer"); + final ActorMaterializer materializer = ActorMaterializer.create(system); - ActorRef userActorRef = system.actorOf(UserActor.props(), "userActor"); + ActorRef userActorRef = system.actorOf(UserActor.props(), "userActor"); - AkkaHttpServer app = new AkkaHttpServer(system, userActorRef); + AkkaHttpServer app = new AkkaHttpServer(system, userActorRef); - final Flow routeFlow = app.userRoutes.routes().flow(system, materializer); + final Flow routeFlow = app.userRoutes.routes() + .flow(system, materializer); - final CompletionStage binding = Http.get(system).bindAndHandle(routeFlow, - ConnectHttp.toHost("localhost", 8080), materializer); + final CompletionStage binding = Http.get(system) + .bindAndHandle(routeFlow, ConnectHttp.toHost("localhost", 8080), materializer); - System.out.println("Server is online at http://localhost:8080/"); - } \ No newline at end of file + System.out.println("Server is online at http://localhost:8080/"); + } +} \ No newline at end of file diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/UserRegistryMessages.java b/akka-http/src/main/java/com/baeldung/akkahttp/UserRegistryMessages.java index 831819849a..1f88e5f646 100644 --- a/akka-http/src/main/java/com/baeldung/akkahttp/UserRegistryMessages.java +++ b/akka-http/src/main/java/com/baeldung/akkahttp/UserRegistryMessages.java @@ -1,5 +1,7 @@ package com.baeldung.akkahttp; +import java.io.Serializable; + /** * Defines all messages related to User Actor * diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/UserRoutes.java b/akka-http/src/main/java/com/baeldung/akkahttp/UserRoutes.java index aa3c9b612f..aeddf9224d 100644 --- a/akka-http/src/main/java/com/baeldung/akkahttp/UserRoutes.java +++ b/akka-http/src/main/java/com/baeldung/akkahttp/UserRoutes.java @@ -1,10 +1,12 @@ package com.baeldung.akkahttp; -import java.time.Duration; import java.util.Optional; import java.util.concurrent.CompletionStage; import java.util.concurrent.TimeUnit; +import com.baeldung.akkahttp.UserRegistryMessages.ActionPerformed; +import com.baeldung.akkahttp.UserRegistryMessages.CreateUser; + import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.event.Logging; @@ -16,6 +18,7 @@ import akka.http.javadsl.server.PathMatchers; import akka.http.javadsl.server.Route; import akka.pattern.PatternsCS; import akka.util.Timeout; +import scala.concurrent.duration.Duration; public class UserRoutes extends AllDirectives { From 91cc2514b2a1cdf12c9d6747d3dfa16c192d332f Mon Sep 17 00:00:00 2001 From: Kotharu Date: Sun, 21 Oct 2018 19:32:08 +0530 Subject: [PATCH 3/3] Added Route Test --- .../com/baeldung/akkahttp/UserRoutesTest.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 akka-http/src/test/java/com/baeldung/akkahttp/UserRoutesTest.java diff --git a/akka-http/src/test/java/com/baeldung/akkahttp/UserRoutesTest.java b/akka-http/src/test/java/com/baeldung/akkahttp/UserRoutesTest.java new file mode 100644 index 0000000000..db749c4f0b --- /dev/null +++ b/akka-http/src/test/java/com/baeldung/akkahttp/UserRoutesTest.java @@ -0,0 +1,48 @@ +package com.baeldung.akkahttp; + +import org.junit.Test; + +import akka.actor.ActorRef; +import akka.actor.ActorSystem; +import akka.http.javadsl.model.ContentType; +import akka.http.javadsl.model.ContentType.NonBinary; +import akka.http.javadsl.model.HttpHeader; +import akka.http.javadsl.model.HttpRequest; +import akka.http.javadsl.model.MediaType; +import akka.http.javadsl.model.MediaTypes; +import akka.http.javadsl.model.StatusCodes; +import akka.http.javadsl.model.headers.RawHeader; +import akka.http.javadsl.testkit.JUnitRouteTest; +import akka.http.javadsl.testkit.TestRoute; + +public class UserRoutesTest extends JUnitRouteTest { + + ActorSystem system = ActorSystem.create("helloAkkaHttpServer"); + ActorRef userActorRef = system.actorOf(UserActor.props(), "userActor"); + + TestRoute appRoute = testRoute(new UserRoutes(system, userActorRef).routes()); + + @Test + public void givenUser_WhenGetOrPostUsers_ThenUserRetrieved() { + + appRoute.run(HttpRequest.GET("/users")) + .assertStatusCode(200) + .assertContentType("application/json") + .assertEntity("{\"users\":[]}"); + + appRoute.run(HttpRequest.GET("/usersw")) + .assertStatusCode(404); + + appRoute.run(HttpRequest.POST("/users")) + .assertStatusCode(400); + + final RawHeader contentTypeHeader = RawHeader.create("Content-Type", "application/json"); + final RawHeader acceptHeader = RawHeader.create("", "application/json"); + + appRoute.run(HttpRequest.POST("/users") + .addHeader(contentTypeHeader) + .addHeader(acceptHeader)) + .assertStatusCode(400); + + } +}