Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into BAEL-2563
Browse files Browse the repository at this point in the history
  • Loading branch information
MherBaghinyan committed Mar 11, 2019
2 parents d03c218 + 3c6e900 commit 5ac596b
Show file tree
Hide file tree
Showing 1,894 changed files with 46,576 additions and 8,349 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ jmeter/src/main/resources/*-JMeter.csv
**/nb-configuration.xml
core-scala/.cache-main
core-scala/.cache-tests


persistence-modules/hibernate5/transaction.log
apache-avro/src/main/java/com/baeldung/avro/model/
jta/transaction-logs/
software-security/sql-injection-samples/derby.log
spring-soap/src/main/java/com/baeldung/springsoap/gen/
2 changes: 1 addition & 1 deletion JGit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<groupId>com.baeldung</groupId>
<artifactId>JGit</artifactId>
<version>1.0-SNAPSHOT</version>
<name>JGit</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<name>JGit</name>

<parent>
<groupId>com.baeldung</groupId>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Java and Spring Tutorials
================

This project is **a collection of small and focused tutorials** - each covering a single and well defined area of development in the Java ecosystem.
A strong focus of these is, of course, the Spring Framework - Spring, Spring Boot and Spring Securiyt.
A strong focus of these is, of course, the Spring Framework - Spring, Spring Boot and Spring Security.
In additional to Spring, the following technologies are in focus: `core Java`, `Jackson`, `HttpClient`, `Guava`.


Expand Down
2 changes: 1 addition & 1 deletion Twitter4J/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>Twitter4J</artifactId>
<packaging>jar</packaging>
<name>Twitter4J</name>
<packaging>jar</packaging>

<parent>
<groupId>com.baeldung</groupId>
Expand Down
3 changes: 3 additions & 0 deletions akka-http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Relevant articles:

- [Introduction to Akka HTTP](https://www.baeldung.com/akka-http)
47 changes: 47 additions & 0 deletions akka-http/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<modelVersion>4.0.0</modelVersion>
<artifactId>akka-http</artifactId>
<name>akka-http</name>

<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http_2.12</artifactId>
<version>${akka.http.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream_2.12</artifactId>
<version>${akka.stream.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http-jackson_2.12</artifactId>
<version>${akka.http.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http-testkit_2.12</artifactId>
<version>${akka.http.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<akka.http.version>10.0.11</akka.http.version>
<akka.stream.version>2.5.11</akka.stream.version>
</properties>
</project>
26 changes: 26 additions & 0 deletions akka-http/src/main/java/com/baeldung/akkahttp/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.akkahttp;

public class User {

private final Long id;

private final String name;

public User() {
this.name = "";
this.id = null;
}

public User(Long id, String name) {
this.name = name;
this.id = id;
}

public String getName() {
return name;
}

public Long getId() {
return id;
}
}
41 changes: 41 additions & 0 deletions akka-http/src/main/java/com/baeldung/akkahttp/UserActor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baeldung.akkahttp;

import akka.actor.AbstractActor;
import akka.actor.Props;
import akka.japi.pf.FI;
import com.baeldung.akkahttp.UserMessages.ActionPerformed;
import com.baeldung.akkahttp.UserMessages.CreateUserMessage;
import com.baeldung.akkahttp.UserMessages.GetUserMessage;


class UserActor extends AbstractActor {

private UserService userService = new UserService();

static Props props() {
return Props.create(UserActor.class);
}

@Override
public Receive createReceive() {
return receiveBuilder()
.match(CreateUserMessage.class, handleCreateUser())
.match(GetUserMessage.class, handleGetUser())
.build();
}

private FI.UnitApply<CreateUserMessage> handleCreateUser() {
return createUserMessageMessage -> {
userService.createUser(createUserMessageMessage.getUser());
sender().tell(new ActionPerformed(String.format("User %s created.", createUserMessageMessage.getUser()
.getName())), getSelf());
};
}

private FI.UnitApply<GetUserMessage> handleGetUser() {
return getUserMessageMessage -> {
sender().tell(userService.getUser(getUserMessageMessage.getUserId()), getSelf());
};
}

}
49 changes: 49 additions & 0 deletions akka-http/src/main/java/com/baeldung/akkahttp/UserMessages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.baeldung.akkahttp;

import java.io.Serializable;

public interface UserMessages {

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 CreateUserMessage implements Serializable {

private static final long serialVersionUID = 1L;
private final User user;

public CreateUserMessage(User user) {
this.user = user;
}

public User getUser() {
return user;
}
}

class GetUserMessage implements Serializable {
private static final long serialVersionUID = 1L;
private final Long userId;

public GetUserMessage(Long userId) {
this.userId = userId;
}

public Long getUserId() {
return userId;
}
}

}
70 changes: 70 additions & 0 deletions akka-http/src/main/java/com/baeldung/akkahttp/UserServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.baeldung.akkahttp;

import java.util.Optional;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.http.javadsl.marshallers.jackson.Jackson;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.server.HttpApp;
import akka.http.javadsl.server.Route;
import akka.pattern.PatternsCS;
import akka.util.Timeout;
import com.baeldung.akkahttp.UserMessages.ActionPerformed;
import com.baeldung.akkahttp.UserMessages.CreateUserMessage;
import com.baeldung.akkahttp.UserMessages.GetUserMessage;
import scala.concurrent.duration.Duration;
import static akka.http.javadsl.server.PathMatchers.*;

class UserServer extends HttpApp {

private final ActorRef userActor;

Timeout timeout = new Timeout(Duration.create(5, TimeUnit.SECONDS));

UserServer(ActorRef userActor) {
this.userActor = userActor;
}

@Override
public Route routes() {
return path("users", this::postUser)
.orElse(path(segment("users").slash(longSegment()), id ->
route(getUser(id))));
}

private Route getUser(Long id) {
return get(() -> {
CompletionStage<Optional<User>> user = PatternsCS.ask(userActor, new GetUserMessage(id), timeout)
.thenApply(obj -> (Optional<User>) obj);

return onSuccess(() -> user, performed -> {
if (performed.isPresent())
return complete(StatusCodes.OK, performed.get(), Jackson.marshaller());
else
return complete(StatusCodes.NOT_FOUND);
});
});
}

private Route postUser() {
return route(post(() -> entity(Jackson.unmarshaller(User.class), user -> {
CompletionStage<ActionPerformed> userCreated = PatternsCS.ask(userActor, new CreateUserMessage(user), timeout)
.thenApply(obj -> (ActionPerformed) obj);

return onSuccess(() -> userCreated, performed -> {
return complete(StatusCodes.CREATED, performed, Jackson.marshaller());
});
})));
}

public static void main(String[] args) throws Exception {
ActorSystem system = ActorSystem.create("userServer");
ActorRef userActor = system.actorOf(UserActor.props(), "userActor");
UserServer server = new UserServer(userActor);
server.startServer("localhost", 8080, system);
}

}
35 changes: 35 additions & 0 deletions akka-http/src/main/java/com/baeldung/akkahttp/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.baeldung.akkahttp;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class UserService {

private final static List<User> users = new ArrayList<>();

static {
users.add(new User(1l, "Alice"));
users.add(new User(2l, "Bob"));
users.add(new User(3l, "Chris"));
users.add(new User(4l, "Dick"));
users.add(new User(5l, "Eve"));
users.add(new User(6l, "Finn"));
}

public Optional<User> getUser(Long id) {
return users.stream()
.filter(user -> user.getId()
.equals(id))
.findFirst();
}

public void createUser(User user) {
users.add(user);
}

public List<User> getUsers(){
return users;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.baeldung.akkahttp;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.http.javadsl.model.ContentTypes;
import akka.http.javadsl.model.HttpEntities;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.testkit.JUnitRouteTest;
import akka.http.javadsl.testkit.TestRoute;
import org.junit.Test;

public class UserServerUnitTest extends JUnitRouteTest {

ActorSystem system = ActorSystem.create("helloAkkaHttpServer");

ActorRef userActorRef = system.actorOf(UserActor.props(), "userActor");

TestRoute appRoute = testRoute(new UserServer(userActorRef).routes());

@Test
public void whenRequest_thenActorResponds() {

appRoute.run(HttpRequest.GET("/users/1"))
.assertEntity(alice())
.assertStatusCode(200);

appRoute.run(HttpRequest.GET("/users/42"))
.assertStatusCode(404);

appRoute.run(HttpRequest.DELETE("/users/1"))
.assertStatusCode(200);

appRoute.run(HttpRequest.DELETE("/users/42"))
.assertStatusCode(200);

appRoute.run(HttpRequest.POST("/users")
.withEntity(HttpEntities.create(ContentTypes.APPLICATION_JSON, zaphod())))
.assertStatusCode(201);

}

private String alice() {
return "{\"id\":1,\"name\":\"Alice\"}";
}

private String zaphod() {
return "{\"id\":42,\"name\":\"Zaphod\"}";
}

}
1 change: 0 additions & 1 deletion algorithms-genetic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
</build>

<properties>
<lombok.version>1.16.12</lombok.version>
<commons-math3.version>3.6.1</commons-math3.version>
<io.jenetics.version>3.7.0</io.jenetics.version>
<org.assertj.core.version>3.9.0</org.assertj.core.version>
Expand Down
7 changes: 6 additions & 1 deletion algorithms-miscellaneous-1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
- [Check If a String Contains All The Letters of The Alphabet](https://www.baeldung.com/java-string-contains-all-letters)
- [Find the Middle Element of a Linked List](http://www.baeldung.com/java-linked-list-middle-element)
- [Calculate Factorial in Java](https://www.baeldung.com/java-calculate-factorial)
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)
- [Generate Combinations in Java](https://www.baeldung.com/java-combinations-algorithm)
Loading

0 comments on commit 5ac596b

Please sign in to comment.