diff --git a/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Point.java b/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Point.java new file mode 100644 index 0000000000..68b1e7c594 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Point.java @@ -0,0 +1,29 @@ +package com.baeldung.algorithms.rectanglesoverlap; + +public class Point { + + private int x; + private int y; + + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Rectangle.java b/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Rectangle.java new file mode 100644 index 0000000000..38f5edec61 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/rectanglesoverlap/Rectangle.java @@ -0,0 +1,40 @@ +package com.baeldung.algorithms.rectanglesoverlap; + +public class Rectangle { + + private Point bottomLeft; + private Point topRight; + + public Rectangle(Point bottomLeft, Point topRight) { + this.bottomLeft = bottomLeft; + this.topRight = topRight; + } + + public Point getBottomLeft() { + return bottomLeft; + } + + public void setBottomLeft(Point bottomLeft) { + this.bottomLeft = bottomLeft; + } + + public Point getTopRight() { + return topRight; + } + + public void setTopRight(Point topRight) { + this.topRight = topRight; + } + + public boolean isOverlapping(Rectangle other) { + // one rectangle is to the top of the other + if (this.topRight.getY() < other.bottomLeft.getY() || this.bottomLeft.getY() > other.topRight.getY()) { + return false; + } + // one rectangle is to the left of the other + if (this.topRight.getX() < other.bottomLeft.getX() || this.bottomLeft.getX() > other.topRight.getX()) { + return false; + } + return true; + } +} diff --git a/algorithms/src/test/java/com/baeldung/algorithms/rectanglesoverlap/RectangleUnitTest.java b/algorithms/src/test/java/com/baeldung/algorithms/rectanglesoverlap/RectangleUnitTest.java new file mode 100644 index 0000000000..6707b34477 --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/rectanglesoverlap/RectangleUnitTest.java @@ -0,0 +1,42 @@ +package com.baeldung.algorithms.rectanglesoverlap; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import org.junit.Test; + +import com.baeldung.algorithms.rectanglesoverlap.Point; +import com.baeldung.algorithms.rectanglesoverlap.Rectangle; + +public class RectangleUnitTest { + + @Test + public void givenTwoOverlappingRectangles_whenisOverlappingCalled_shouldReturnTrue() { + Rectangle rectangle1 = new Rectangle(new Point(2, 1), new Point(4, 3)); + Rectangle rectangle2 = new Rectangle(new Point(1, 1), new Point(6, 4)); + assertTrue(rectangle1.isOverlapping(rectangle2)); + + rectangle1 = new Rectangle(new Point(-5, -2), new Point(2, 3)); + rectangle2 = new Rectangle(new Point(-2, -1), new Point(5, 2)); + assertTrue(rectangle1.isOverlapping(rectangle2)); + + rectangle1 = new Rectangle(new Point(-5, 1), new Point(2, 4)); + rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, 5)); + assertTrue(rectangle1.isOverlapping(rectangle2)); + } + + @Test + public void givenTwoNonOverlappingRectangles_whenisOverlappingCalled_shouldReturnFalse() { + Rectangle rectangle1 = new Rectangle(new Point(-5, 1), new Point(-3, 4)); + Rectangle rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, 5)); + assertFalse(rectangle1.isOverlapping(rectangle2)); + + rectangle1 = new Rectangle(new Point(-5, 1), new Point(3, 4)); + rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, -1)); + assertFalse(rectangle1.isOverlapping(rectangle2)); + + rectangle1 = new Rectangle(new Point(-2, 1), new Point(0, 3)); + rectangle2 = new Rectangle(new Point(3, 1), new Point(5, 4)); + assertFalse(rectangle1.isOverlapping(rectangle2)); + } + +} diff --git a/apache-cxf/README.md b/apache-cxf/README.md index 1e66ce5da8..d03999dce3 100644 --- a/apache-cxf/README.md +++ b/apache-cxf/README.md @@ -3,3 +3,4 @@ - [Apache CXF Support for RESTful Web Services](http://www.baeldung.com/apache-cxf-rest-api) - [A Guide to Apache CXF with Spring](http://www.baeldung.com/apache-cxf-with-spring) - [Introduction to Apache CXF](http://www.baeldung.com/introduction-to-apache-cxf) +- [Server-Sent Events (SSE) In JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse) diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml b/apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml index 46572a2b75..2e82dc3829 100644 --- a/apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml @@ -20,7 +20,7 @@ - ${artifactId} + ${project.artifactId} net.wasdev.wlp.maven.plugins diff --git a/aws-lambda/pom.xml b/aws-lambda/pom.xml index 3a2be70da6..c47c3cd86f 100644 --- a/aws-lambda/pom.xml +++ b/aws-lambda/pom.xml @@ -95,7 +95,6 @@ 2.8.2 1.11.241 3.0.0 - 2.10 \ No newline at end of file diff --git a/aws/.gitignore b/aws/.gitignore index b83d22266a..bf11a4cc38 100644 --- a/aws/.gitignore +++ b/aws/.gitignore @@ -1 +1,2 @@ /target/ +.idea/ \ No newline at end of file diff --git a/aws/pom.xml b/aws/pom.xml index 26bc0c8037..ab63f6afa1 100644 --- a/aws/pom.xml +++ b/aws/pom.xml @@ -100,26 +100,6 @@ - - - org.apache.maven.plugins - maven-dependency-plugin - ${maven-dependency-plugin.version} - - - copy-dependencies - test-compile - - copy-dependencies - - - test - so,dll,dylib - ${project.basedir}/native-libs - - - - @@ -144,7 +124,6 @@ 1.10.L001 0.9.4.0006L 3.0.0 - 2.10 \ No newline at end of file diff --git a/aws/src/main/java/com/baeldung/rds/AWSRDSService.java b/aws/src/main/java/com/baeldung/rds/AWSRDSService.java new file mode 100644 index 0000000000..d4da92f30f --- /dev/null +++ b/aws/src/main/java/com/baeldung/rds/AWSRDSService.java @@ -0,0 +1,184 @@ +package com.baeldung.rds; + +import com.amazonaws.auth.AWSCredentialsProvider; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.regions.Regions; +import com.amazonaws.services.rds.AmazonRDS; +import com.amazonaws.services.rds.AmazonRDSClientBuilder; +import com.amazonaws.services.rds.model.*; + +import java.io.IOException; +import java.io.InputStream; +import java.sql.*; +import java.util.List; +import java.util.Properties; +import java.util.UUID; +import java.util.logging.Logger; + +public class AWSRDSService { + + + final static Logger logger = Logger.getLogger(AWSRDSService.class.getName()); + private AWSCredentialsProvider credentials; + private AmazonRDS amazonRDS; + private String db_username; + private String db_password; + private String db_database; + private String db_hostname; + + /* + * User access key and secret key must be set before execute any operation. + * Follow the link on how to get the user access and secret key + * https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/ + * **/ + public AWSRDSService() throws IOException { + //Init RDS client with credentials and region. + credentials = new + AWSStaticCredentialsProvider(new + BasicAWSCredentials("", + "")); + amazonRDS = AmazonRDSClientBuilder.standard().withCredentials(credentials) + .withRegion(Regions.AP_SOUTHEAST_2).build(); + Properties prop = new Properties(); + InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties"); + prop.load(input); + db_username = prop.getProperty("db_username"); + db_password = prop.getProperty("db_password"); + db_database = prop.getProperty("db_database"); + } + + public AWSRDSService(AmazonRDS amazonRDS){ + this.amazonRDS = amazonRDS; + } + + /** + * create the RDS instance. + * After instance creation, update the db_hostname with endpoint in db.properties. + * */ + + public String launchInstance() { + + String identifier = ""; + CreateDBInstanceRequest request = new CreateDBInstanceRequest(); + // RDS instance name + request.setDBInstanceIdentifier("Sydney"); + request.setEngine("postgres"); + request.setMultiAZ(false); + request.setMasterUsername(db_username); + request.setMasterUserPassword(db_password); + request.setDBName(db_database); + request.setStorageType("gp2"); + request.setAllocatedStorage(10); + + DBInstance instance = amazonRDS.createDBInstance(request); + + // Information about the new RDS instance + identifier = instance.getDBInstanceIdentifier(); + String status = instance.getDBInstanceStatus(); + Endpoint endpoint = instance.getEndpoint(); + String endpoint_url = "Endpoint URL not available yet."; + if (endpoint != null) { + endpoint_url = endpoint.toString(); + } + logger.info(identifier + "\t" + status); + logger.info(endpoint_url); + + return identifier; + + } + + // Describe DB instances + public void listInstances() { + DescribeDBInstancesResult result = amazonRDS.describeDBInstances(); + List instances = result.getDBInstances(); + for (DBInstance instance : instances) { + // Information about each RDS instance + String identifier = instance.getDBInstanceIdentifier(); + String engine = instance.getEngine(); + String status = instance.getDBInstanceStatus(); + Endpoint endpoint = instance.getEndpoint(); + String endpoint_url = "Endpoint URL not available yet."; + if (endpoint != null) { + endpoint_url = endpoint.toString(); + } + logger.info(identifier + "\t" + engine + "\t" + status); + logger.info("\t" + endpoint_url); + } + + } + + //Delete RDS instance + public void terminateInstance(String identifier) { + + DeleteDBInstanceRequest request = new DeleteDBInstanceRequest(); + request.setDBInstanceIdentifier(identifier); + request.setSkipFinalSnapshot(true); + + // Delete the RDS instance + DBInstance instance = amazonRDS.deleteDBInstance(request); + + // Information about the RDS instance being deleted + String status = instance.getDBInstanceStatus(); + Endpoint endpoint = instance.getEndpoint(); + String endpoint_url = "Endpoint URL not available yet."; + if (endpoint != null) { + endpoint_url = endpoint.toString(); + } + + logger.info(identifier + "\t" + status); + logger.info(endpoint_url); + + } + + + public void runJdbcTests() throws SQLException, IOException { + + // Getting database properties from db.properties + Properties prop = new Properties(); + InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties"); + prop.load(input); + db_hostname = prop.getProperty("db_hostname"); + String jdbc_url = "jdbc:postgresql://" + db_hostname + ":5432/" + db_database; + + // Create a connection using the JDBC driver + try (Connection conn = DriverManager.getConnection(jdbc_url, db_username, db_password)) { + + // Create the test table if not exists + Statement statement = conn.createStatement(); + String sql = "CREATE TABLE IF NOT EXISTS jdbc_test (id SERIAL PRIMARY KEY, content VARCHAR(80))"; + statement.executeUpdate(sql); + + // Do some INSERT + PreparedStatement preparedStatement = conn.prepareStatement("INSERT INTO jdbc_test (content) VALUES (?)"); + String content = "" + UUID.randomUUID(); + preparedStatement.setString(1, content); + preparedStatement.executeUpdate(); + logger.info("INSERT: " + content); + + // Do some SELECT + sql = "SELECT * FROM jdbc_test"; + ResultSet resultSet = statement.executeQuery(sql); + while (resultSet.next()) { + String count = resultSet.getString("content"); + logger.info("Total Records: " + count); + } + } + + + } + + public static void main(String[] args) throws IOException, SQLException { + AWSRDSService awsrdsService = new AWSRDSService(); + + String instanceName = awsrdsService.launchInstance(); + + //Add some wait for instance creation. + + awsrdsService.listInstances(); + + awsrdsService.runJdbcTests(); + + awsrdsService.terminateInstance(instanceName); + } +} diff --git a/aws/src/main/resources/db.properties b/aws/src/main/resources/db.properties new file mode 100644 index 0000000000..e934611a2b --- /dev/null +++ b/aws/src/main/resources/db.properties @@ -0,0 +1,4 @@ +db_hostname= +db_username=username +db_password=password +db_database=mydb \ No newline at end of file diff --git a/checker-plugin/pom.xml b/checker-plugin/pom.xml index 7538340f69..45f0939e77 100644 --- a/checker-plugin/pom.xml +++ b/checker-plugin/pom.xml @@ -41,27 +41,6 @@ - - - org.apache.maven.plugins - maven-dependency-plugin - - - - - properties - - - - - maven-compiler-plugin ${maven-compiler-plugin.version} diff --git a/core-java-8/README.md b/core-java-8/README.md index e23a2a6d1e..64d423aafe 100644 --- a/core-java-8/README.md +++ b/core-java-8/README.md @@ -29,3 +29,6 @@ - [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic) - [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference) - [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time) +- [Set the Time Zone of a Date in Java](https://www.baeldung.com/java-set-date-time-zone) +- [An Overview of Regular Expressions Performance in Java](https://www.baeldung.com/java-regex-performance) +- [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects) diff --git a/core-java-8/pom.xml b/core-java-8/pom.xml index fa0d79e405..18bdaa15f4 100644 --- a/core-java-8/pom.xml +++ b/core-java-8/pom.xml @@ -116,23 +116,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - org.apache.maven.plugins maven-compiler-plugin diff --git a/core-java-9/README.md b/core-java-9/README.md index 6fa2c24f9e..bf07cfcc86 100644 --- a/core-java-9/README.md +++ b/core-java-9/README.md @@ -24,3 +24,5 @@ - [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional) - [Java 9 java.lang.Module API](http://www.baeldung.com/java-9-module-api) - [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range) +- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap) +- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api) diff --git a/core-java-9/src/test/java/com/baeldung/java9/process/ProcessUnderstandingTest.java b/core-java-9/src/test/java/com/baeldung/java9/process/ProcessUnderstandingTest.java index 2f61846c1c..2c5525b9ed 100644 --- a/core-java-9/src/test/java/com/baeldung/java9/process/ProcessUnderstandingTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/process/ProcessUnderstandingTest.java @@ -48,7 +48,7 @@ class ProcessUnderstandingTest { } //@Test - windows specific - public void givenSubProcess_thenStartSuccessIsAlive() throws IOException { + public void givenSubProcess_whenStarted_thenStartSuccessIsAlive() throws IOException { ProcessBuilder builder = new ProcessBuilder("notepad.exe"); assertTrue(builder.start().isAlive()); } @@ -75,7 +75,7 @@ class ProcessUnderstandingTest { } //@Test - windows specific - public void givenSubProcess_checkAlive() throws IOException, InterruptedException { + public void givenSubProcess_whenDestroyed_thenCheckIfAlive() throws IOException, InterruptedException { ProcessBuilder builder = new ProcessBuilder("notepad.exe"); Process process = builder.start(); Thread.sleep(10000); diff --git a/core-java-collections/README.md b/core-java-collections/README.md index ab13ba7c01..f187141712 100644 --- a/core-java-collections/README.md +++ b/core-java-collections/README.md @@ -43,3 +43,9 @@ - [An Introduction to Java.util.Hashtable Class](http://www.baeldung.com/java-hash-table) - [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another) - [Finding Max/Min of a List or Collection](http://www.baeldung.com/java-collection-min-max) +- [Java Null-Safe Streams from Collections](https://www.baeldung.com/java-null-safe-streams-from-collections) +- [Remove All Occurrences of a Specific Value from a List](https://www.baeldung.com/java-remove-value-from-list) +- [Thread Safe LIFO Data Structure Implementations](https://www.baeldung.com/java-lifo-thread-safe) +- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list) +- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap) +- [](https://www.baeldung.com/java-hashset-arraylist-contains-performance) diff --git a/core-java-collections/src/main/java/com/baeldung/java/map/MapUtil.java b/core-java-collections/src/main/java/com/baeldung/java/map/MapUtil.java new file mode 100644 index 0000000000..688c7592f3 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/java/map/MapUtil.java @@ -0,0 +1,44 @@ +/** + * + */ +package com.baeldung.java.map; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.Map.Entry; +import java.util.stream.Stream; + +/** + * @author swpraman + * + */ +public class MapUtil { + + public static Stream keys(Map map, V value) { + return map.entrySet() + .stream() + .filter(entry -> value.equals(entry.getValue())) + .map(Map.Entry::getKey); + } + + public static Set getKeys(Map map, V value) { + Set keys = new HashSet<>(); + for (Entry entry : map.entrySet()) { + if (entry.getValue().equals(value)) { + keys.add(entry.getKey()); + } + } + return keys; + } + + public static K getKey(Map map, V value) { + for (Entry entry : map.entrySet()) { + if (entry.getValue().equals(value)) { + return entry.getKey(); + } + } + return null; + } + +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/ArrayListBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/ArrayListBenchmark.java new file mode 100644 index 0000000000..dddd85007d --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/ArrayListBenchmark.java @@ -0,0 +1,77 @@ +package com.baeldung.performance; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class ArrayListBenchmark { + + @State(Scope.Thread) + public static class MyState { + + List employeeList = new ArrayList<>(); + //LinkedList employeeList = new LinkedList<>(); + + long iterations = 100000; + + Employee employee = new Employee(100L, "Harry"); + + int employeeIndex = -1; + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeList.add(new Employee(i, "John")); + } + + employeeList.add(employee); + employeeIndex = employeeList.indexOf(employee); + } + } + + @Benchmark + public void testAddAt(ArrayListBenchmark.MyState state) { + state.employeeList.add((int) (state.iterations), new Employee(state.iterations, "John")); + } + + @Benchmark + public boolean testContains(ArrayListBenchmark.MyState state) { + return state.employeeList.contains(state.employee); + } + + @Benchmark + public int testIndexOf(ArrayListBenchmark.MyState state) { + return state.employeeList.indexOf(state.employee); + } + + @Benchmark + public Employee testGet(ArrayListBenchmark.MyState state) { + return state.employeeList.get(state.employeeIndex); + } + + @Benchmark + public boolean testRemove(ArrayListBenchmark.MyState state) { + return state.employeeList.remove(state.employee); + } + +// @Benchmark +// public void testAdd(ArrayListBenchmark.MyState state) { +// state.employeeList.add(new Employee(state.iterations + 1, "John")); +// } + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(ArrayListBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/CopyOnWriteBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/CopyOnWriteBenchmark.java new file mode 100644 index 0000000000..30c50291dd --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/CopyOnWriteBenchmark.java @@ -0,0 +1,78 @@ +package com.baeldung.performance; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class CopyOnWriteBenchmark { + + @State(Scope.Thread) + public static class MyState { + + CopyOnWriteArrayList employeeList = new CopyOnWriteArrayList<>(); + + long iterations = 100000; + + Employee employee = new Employee(100L, "Harry"); + + int employeeIndex = -1; + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeList.add(new Employee(i, "John")); + } + + employeeList.add(employee); + + employeeIndex = employeeList.indexOf(employee); + } + } + + @Benchmark + public void testAdd(CopyOnWriteBenchmark.MyState state) { + state.employeeList.add(new Employee(state.iterations + 1, "John")); + } + + @Benchmark + public void testAddAt(CopyOnWriteBenchmark.MyState state) { + state.employeeList.add((int) (state.iterations), new Employee(state.iterations, "John")); + } + + @Benchmark + public boolean testContains(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.contains(state.employee); + } + + @Benchmark + public int testIndexOf(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.indexOf(state.employee); + } + + @Benchmark + public Employee testGet(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.get(state.employeeIndex); + } + + @Benchmark + public boolean testRemove(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.remove(state.employee); + } + + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(CopyOnWriteBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/Employee.java b/core-java-collections/src/main/java/com/baeldung/performance/Employee.java index daa68ae2f1..d811cfbb7d 100644 --- a/core-java-collections/src/main/java/com/baeldung/performance/Employee.java +++ b/core-java-collections/src/main/java/com/baeldung/performance/Employee.java @@ -10,6 +10,22 @@ public class Employee { this.id = id; } + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/core-java-collections/src/main/java/com/baeldung/performance/HashMapBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/HashMapBenchmark.java new file mode 100644 index 0000000000..085daaceb7 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/HashMapBenchmark.java @@ -0,0 +1,73 @@ +package com.baeldung.performance; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class HashMapBenchmark { + + @State(Scope.Thread) + public static class MyState { + + Map employeeMap = new HashMap<>(); + //LinkedHashMap employeeMap = new LinkedHashMap<>(); + //IdentityHashMap employeeMap = new IdentityHashMap<>(); + //WeakHashMap employeeMap = new WeakHashMap<>(); + //ConcurrentHashMap employeeMap = new ConcurrentHashMap<>(); + //ConcurrentSkipListMap employeeMap = new ConcurrentSkipListMap <>(); + + // TreeMap + + long iterations = 100000; + + Employee employee = new Employee(100L, "Harry"); + + int employeeIndex = -1; + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeMap.put(i, new Employee(i, "John")); + } + + //employeeMap.put(iterations, employee); + } + } + + @Benchmark + public Employee testGet(HashMapBenchmark.MyState state) { + return state.employeeMap.get(state.iterations); + } + + @Benchmark + public Employee testRemove(HashMapBenchmark.MyState state) { + return state.employeeMap.remove(state.iterations); + } + + @Benchmark + public Employee testPut(HashMapBenchmark.MyState state) { + return state.employeeMap.put(state.employee.getId(), state.employee); + } + + @Benchmark + public Boolean testContainsKey(HashMapBenchmark.MyState state) { + return state.employeeMap.containsKey(state.employee.getId()); + } + + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(HashMapBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/SetBenchMark.java b/core-java-collections/src/main/java/com/baeldung/performance/SetBenchMark.java new file mode 100644 index 0000000000..f70bab4fcf --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/SetBenchMark.java @@ -0,0 +1,66 @@ +package com.baeldung.performance; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class SetBenchMark { + + @State(Scope.Thread) + public static class MyState { + + //Set employeeSet = new HashSet<>(); + LinkedHashSet employeeSet = new LinkedHashSet<>(); + //ConcurrentSkipListSet employeeSet = new ConcurrentSkipListSet <>(); + + // TreeSet  + + long iterations = 1000; + Employee employee = new Employee(100L, "Harry"); + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeSet.add(new Employee(i, "John")); + } + + //employeeSet.add(employee); + } + } + + @Benchmark + public boolean testAdd(SetBenchMark.MyState state) { + return state.employeeSet.add(state.employee); + } + + @Benchmark + public Boolean testContains(SetBenchMark.MyState state) { + return state.employeeSet.contains(state.employee); + } + + @Benchmark + public boolean testRemove(SetBenchMark.MyState state) { + return state.employeeSet.remove(state.employee); + } + + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(SetBenchMark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-collections/src/test/java/com/baeldung/collection/ClearVsRemoveAllUnitTest.java b/core-java-collections/src/test/java/com/baeldung/collection/ClearVsRemoveAllUnitTest.java new file mode 100644 index 0000000000..8b0a7ef0db --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/collection/ClearVsRemoveAllUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.collection; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Unit tests demonstrating differences between ArrayList#clear() and ArrayList#removeAll() + */ +class ClearVsRemoveAllUnitTest { + + /* + * Tests + */ + @Test + void givenArrayListWithElements_whenClear_thenListBecomesEmpty() { + Collection collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); + + collection.clear(); + + assertTrue(collection.isEmpty()); + } + + @Test + void givenTwoArrayListsWithCommonElements_whenRemoveAll_thenFirstListMissElementsFromSecondList() { + Collection firstCollection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); + Collection secondCollection = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7)); + + firstCollection.removeAll(secondCollection); + + assertEquals(Arrays.asList(1, 2), firstCollection); + assertEquals(Arrays.asList(3, 4, 5, 6, 7), secondCollection); + } + +} diff --git a/core-java-collections/src/test/java/com/baeldung/java/map/MapUtilUnitTest.java b/core-java-collections/src/test/java/com/baeldung/java/map/MapUtilUnitTest.java new file mode 100644 index 0000000000..e31385e972 --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/java/map/MapUtilUnitTest.java @@ -0,0 +1,104 @@ +/** + * + */ +package com.baeldung.java.map; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.collections4.BidiMap; +import org.apache.commons.collections4.bidimap.DualHashBidiMap; +import org.junit.Test; + +import com.google.common.collect.HashBiMap; + +/** + * @author swpraman + * + */ +public class MapUtilUnitTest { + + + @Test + public void whenUsingImperativeWayForSingleKey_shouldReturnSingleKey() { + Map capitalCountryMap = new HashMap<>(); + capitalCountryMap.put("Tokyo", "Japan"); + capitalCountryMap.put("New Delhi", "India"); + assertEquals("New Delhi", MapUtil.getKey(capitalCountryMap, "India")); + } + + @Test + public void whenUsingImperativeWayForAllKeys_shouldReturnAllKeys() { + Map capitalCountryMap = new HashMap<>(); + capitalCountryMap.put("Tokyo", "Japan"); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + capitalCountryMap.put("Pretoria", "South Africa"); + capitalCountryMap.put("Bloemfontein", "South Africa"); + + assertEquals(new HashSet(Arrays.asList( + new String[] {"Cape Town", "Pretoria", "Bloemfontein"})), + MapUtil.getKeys(capitalCountryMap, "South Africa")); + } + + @Test + public void whenUsingFunctionalWayForSingleKey_shouldReturnSingleKey() { + Map capitalCountryMap = new HashMap<>(); + capitalCountryMap.put("Tokyo", "Japan"); + capitalCountryMap.put("Berlin", "Germany"); + assertEquals("Berlin", MapUtil.keys(capitalCountryMap, "Germany").findFirst().get()); + } + + @Test + public void whenUsingFunctionalWayForAllKeys_shouldReturnAllKeys() { + Map capitalCountryMap = new HashMap<>(); + capitalCountryMap.put("Tokyo", "Japan"); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + capitalCountryMap.put("Pretoria", "South Africa"); + capitalCountryMap.put("Bloemfontein", "South Africa"); + assertEquals(new HashSet(Arrays.asList( + new String[] {"Cape Town", "Pretoria", "Bloemfontein"})), + MapUtil.keys(capitalCountryMap, "South Africa").collect(Collectors.toSet())); + } + + @Test + public void whenUsingBidiMap_shouldReturnKey() { + BidiMap capitalCountryMap = new DualHashBidiMap(); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + assertEquals("Berlin", capitalCountryMap.getKey("Germany")); + } + + @Test + public void whenUsingBidiMapAddDuplicateValue_shouldRemoveOldEntry() { + BidiMap capitalCountryMap = new DualHashBidiMap(); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + capitalCountryMap.put("Pretoria", "South Africa"); + assertEquals("Pretoria", capitalCountryMap.getKey("South Africa")); + } + + @Test + public void whenUsingBiMap_shouldReturnKey() { + HashBiMap capitalCountryMap = HashBiMap.create(); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + assertEquals("Berlin", capitalCountryMap.inverse().get("Germany")); + } + + @Test(expected=IllegalArgumentException.class) + public void whenUsingBiMapAddDuplicateValue_shouldThrowException() { + HashBiMap capitalCountryMap = HashBiMap.create(); + capitalCountryMap.put("Berlin", "Germany"); + capitalCountryMap.put("Cape Town", "South Africa"); + capitalCountryMap.put("Pretoria", "South Africa"); + assertEquals("Berlin", capitalCountryMap.inverse().get("Germany")); + } + +} diff --git a/core-java-concurrency-collections/pom.xml b/core-java-concurrency-collections/pom.xml index 5e0a80d33c..9473de8c51 100644 --- a/core-java-concurrency-collections/pom.xml +++ b/core-java-concurrency-collections/pom.xml @@ -57,26 +57,6 @@ true - - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - - diff --git a/core-java-concurrency/pom.xml b/core-java-concurrency/pom.xml index eb81983a2a..bd22253c2c 100644 --- a/core-java-concurrency/pom.xml +++ b/core-java-concurrency/pom.xml @@ -57,26 +57,6 @@ true - - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - - diff --git a/core-java-io/README.md b/core-java-io/README.md index 5e5ddf42b4..17c93d9739 100644 --- a/core-java-io/README.md +++ b/core-java-io/README.md @@ -30,3 +30,4 @@ - [Download a File From an URL in Java](http://www.baeldung.com/java-download-file) - [Create a Symbolic Link with Java](http://www.baeldung.com/java-symlink) - [Quick Use of FilenameFilter](http://www.baeldung.com/java-filename-filter) +- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap) diff --git a/core-java-persistence/README.md b/core-java-persistence/README.md index 08afcadb72..ca0ce81eef 100644 --- a/core-java-persistence/README.md +++ b/core-java-persistence/README.md @@ -5,4 +5,5 @@ ### Relevant Articles: - [Introduction to JDBC](http://www.baeldung.com/java-jdbc) - [Batch Processing in JDBC](http://www.baeldung.com/jdbc-batch-processing) -- [Introduction to the JDBC RowSet Interface in Java](http://www.baeldung.com/java-jdbc-rowset) \ No newline at end of file +- [Introduction to the JDBC RowSet Interface in Java](http://www.baeldung.com/java-jdbc-rowset) +- [A Simple Guide to Connection Pooling in Java](https://www.baeldung.com/java-connection-pooling) diff --git a/core-java-sun/pom.xml b/core-java-sun/pom.xml index 7292335232..57d5e9da5b 100644 --- a/core-java-sun/pom.xml +++ b/core-java-sun/pom.xml @@ -166,22 +166,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - org.apache.maven.plugins maven-jar-plugin diff --git a/core-java/README.md b/core-java/README.md index e74f6cf815..ade7f46504 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -145,3 +145,9 @@ - [Common Java Exceptions](http://www.baeldung.com/java-common-exceptions) - [Java Constructors vs Static Factory Methods](https://www.baeldung.com/java-constructors-vs-static-factory-methods) - [Differences Between Final, Finally and Finalize in Java](https://www.baeldung.com/java-final-finally-finalize) +- [Static and Dynamic Binding in Java](https://www.baeldung.com/java-static-dynamic-binding) +- [Java List Initialization in One Line](https://www.baeldung.com/java-init-list-one-line) +- [Difference Between Throw and Throws in Java](https://www.baeldung.com/java-throw-throws) +- [ClassCastException: Arrays$ArrayList cannot be cast to ArrayList](https://www.baeldung.com/java-classcastexception-arrays-arraylist) +- [Throw Exception in Optional in Java 8](https://www.baeldung.com/java-optional-throw-exception) +- [Add a Character to a String at a Given Position](https://www.baeldung.com/java-add-character-to-string) diff --git a/core-java/src/main/java/com/baeldung/decimalformat/DoubletoString.java b/core-java/src/main/java/com/baeldung/decimalformat/DoubletoString.java new file mode 100644 index 0000000000..87d10a3548 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/decimalformat/DoubletoString.java @@ -0,0 +1,38 @@ +package com.baeldung.decimalformat; + + import java.math.RoundingMode; + import java.text.DecimalFormat; + import java.text.NumberFormat; + import java.util.Locale; + + public class DoubletoString { + + public static void main(String[] args) { + + double doubleValue = 345.56; + + System.out.println(String.valueOf((int) doubleValue)); + + System.out.println(String.format("%.0f", doubleValue)); + + doubleValue = Math.floor(doubleValue); + DecimalFormat df = new DecimalFormat("#"); + df.setRoundingMode(RoundingMode.FLOOR); + System.out.println(df.format(doubleValue)); + + Locale enlocale = new Locale("en", "US"); + String pattern = "###,##"; + df = (DecimalFormat) NumberFormat.getNumberInstance(enlocale); + df.applyPattern(pattern); + String format = df.format(doubleValue); + System.out.println(format); + + Locale dalocale = new Locale("da", "DK"); + df = (DecimalFormat) NumberFormat.getNumberInstance(dalocale); + df.applyPattern(pattern); + System.out.println(df.format(doubleValue)); + + + } + + } diff --git a/core-kotlin/README.md b/core-kotlin/README.md index 734b2c08b3..f63451bc02 100644 --- a/core-kotlin/README.md +++ b/core-kotlin/README.md @@ -8,7 +8,6 @@ - [Generics in Kotlin](http://www.baeldung.com/kotlin-generics) - [Introduction to Kotlin Coroutines](http://www.baeldung.com/kotlin-coroutines) - [Destructuring Declarations in Kotlin](http://www.baeldung.com/kotlin-destructuring-declarations) -- [Kotlin with Mockito](http://www.baeldung.com/kotlin-mockito) - [Lazy Initialization in Kotlin](http://www.baeldung.com/kotlin-lazy-initialization) - [Overview of Kotlin Collections API](http://www.baeldung.com/kotlin-collections-api) - [Converting a List to Map in Kotlin](http://www.baeldung.com/kotlin-list-to-map) @@ -19,8 +18,6 @@ - [Extension Methods in Kotlin](http://www.baeldung.com/kotlin-extension-methods) - [Infix Functions in Kotlin](http://www.baeldung.com/kotlin-infix-functions) - [Try-with-resources in Kotlin](http://www.baeldung.com/kotlin-try-with-resources) -- [HTTP Requests with Kotlin and khttp](http://www.baeldung.com/kotlin-khttp) -- [Kotlin Dependency Injection with Kodein](http://www.baeldung.com/kotlin-kodein-dependency-injection) - [Regular Expressions in Kotlin](http://www.baeldung.com/kotlin-regular-expressions) - [Objects in Kotlin](http://www.baeldung.com/kotlin-objects) - [Reading from a File in Kotlin](http://www.baeldung.com/kotlin-read-file) @@ -28,13 +25,12 @@ - [Filtering Kotlin Collections](http://www.baeldung.com/kotlin-filter-collection) - [Writing to a File in Kotlin](http://www.baeldung.com/kotlin-write-file) - [Lambda Expressions in Kotlin](http://www.baeldung.com/kotlin-lambda-expressions) -- [Writing Specifications with Kotlin and Spek](http://www.baeldung.com/kotlin-spek) -- [Processing JSON with Kotlin and Klaxson](http://www.baeldung.com/kotlin-json-klaxson) - [Kotlin String Templates](http://www.baeldung.com/kotlin-string-template) -- [Java EE 8 Security API](http://www.baeldung.com/java-ee-8-security) -- [Kotlin with Ktor](http://www.baeldung.com/kotlin-ktor) - [Working with Enums in Kotlin](http://www.baeldung.com/kotlin-enum) - [Create a Java and Kotlin Project with Maven](http://www.baeldung.com/kotlin-maven-java-project) - [Reflection with Kotlin](http://www.baeldung.com/kotlin-reflection) - [Get a Random Number in Kotlin](http://www.baeldung.com/kotlin-random-number) - [Idiomatic Logging in Kotlin](http://www.baeldung.com/kotlin-logging) +- [Kotlin Constructors](https://www.baeldung.com/kotlin-constructors) +- [Creational Design Patterns in Kotlin: Builder](https://www.baeldung.com/kotlin-builder-pattern) +- [Kotlin Nested and Inner Classes](https://www.baeldung.com/kotlin-inner-classes) \ No newline at end of file diff --git a/core-kotlin/build.gradle b/core-kotlin/build.gradle index 6c1e06aa25..2b6527fca7 100755 --- a/core-kotlin/build.gradle +++ b/core-kotlin/build.gradle @@ -6,7 +6,6 @@ version '1.0-SNAPSHOT' buildscript { ext.kotlin_version = '1.2.41' - ext.ktor_version = '0.9.2' repositories { mavenCentral() @@ -44,14 +43,6 @@ sourceSets { } dependencies { - compile "io.ktor:ktor-server-netty:$ktor_version" compile "ch.qos.logback:logback-classic:1.2.1" - compile "io.ktor:ktor-gson:$ktor_version" testCompile group: 'junit', name: 'junit', version: '4.12' - implementation 'com.beust:klaxon:3.0.1' - -} -task runServer(type: JavaExec) { - main = 'APIServer' - classpath = sourceSets.main.runtimeClasspath } \ No newline at end of file diff --git a/core-kotlin/kotlin-ktor/webapp/WEB-INF/web.xml b/core-kotlin/kotlin-ktor/webapp/WEB-INF/web.xml deleted file mode 100755 index 513a80cb27..0000000000 --- a/core-kotlin/kotlin-ktor/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - io.ktor.ktor.config - application.conf - - - - KtorServlet - KtorServlet - io.ktor.server.servlet.ServletApplicationEngine - - - true - - - - 304857600 - 304857600 - 0 - - - - - KtorServlet - / - - - \ No newline at end of file diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index 6fdc7c7c1a..0894a57320 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -12,33 +12,7 @@ ../parent-kotlin - - - exposed - exposed - https://dl.bintray.com/kotlin/exposed - - - - - org.jetbrains.spek - spek-api - 1.1.5 - test - - - org.jetbrains.spek - spek-subject-extension - 1.1.5 - test - - - org.jetbrains.spek - spek-junit-platform-engine - 1.1.5 - test - org.apache.commons commons-math3 @@ -50,56 +24,52 @@ ${junit.platform.version} test - - khttp - khttp - ${khttp.version} - - - com.nhaarman - mockito-kotlin - ${mockito-kotlin.version} - test - - - com.github.salomonbrys.kodein - kodein - ${kodein.version} - org.assertj assertj-core ${assertj.version} test - - com.beust - klaxon - ${klaxon.version} - - - org.jetbrains.exposed - exposed - ${exposed.version} - com.h2database h2 ${h2database.version} + + com.github.kittinunf.fuel + fuel + ${fuel.version} + + + com.github.kittinunf.fuel + fuel-gson + ${fuel.version} + + + com.github.kittinunf.fuel + fuel-rxjava + ${fuel.version} + + + com.github.kittinunf.fuel + fuel-coroutines + ${fuel.version} + + + nl.komponents.kovenant + kovenant + 3.3.0 + pom + - 1.5.0 - 4.1.0 - 3.0.4 - 0.1.0 3.6.1 1.1.1 5.2.0 3.10.0 1.4.197 - 0.10.4 + 1.15.0 - \ No newline at end of file + diff --git a/core-kotlin/src/main/kotlin/com/baeldung/fuel/Interceptors.kt b/core-kotlin/src/main/kotlin/com/baeldung/fuel/Interceptors.kt new file mode 100644 index 0000000000..377ef979dc --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/fuel/Interceptors.kt @@ -0,0 +1,11 @@ +package com.baeldung.fuel + +import com.github.kittinunf.fuel.core.Request + +fun tokenInterceptor() = { + next: (Request) -> Request -> + { req: Request -> + req.header(mapOf("Authorization" to "Bearer AbCdEf123456")) + next(req) + } +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/fuel/Post.kt b/core-kotlin/src/main/kotlin/com/baeldung/fuel/Post.kt new file mode 100644 index 0000000000..035dfe7aa0 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/fuel/Post.kt @@ -0,0 +1,15 @@ +package com.baeldung.fuel + +import com.github.kittinunf.fuel.core.ResponseDeserializable +import com.google.gson.Gson + +data class Post(var userId:Int, + var id:Int, + var title:String, + var body:String){ + + + class Deserializer : ResponseDeserializable> { + override fun deserialize(content: String): Array = Gson().fromJson(content, Array::class.java) + } +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/fuel/PostRoutingAPI.kt b/core-kotlin/src/main/kotlin/com/baeldung/fuel/PostRoutingAPI.kt new file mode 100644 index 0000000000..8238c41e56 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/fuel/PostRoutingAPI.kt @@ -0,0 +1,42 @@ +package com.baeldung.fuel + +import com.github.kittinunf.fuel.core.Method +import com.github.kittinunf.fuel.util.FuelRouting + +sealed class PostRoutingAPI : FuelRouting { + + override val basePath = "https://jsonplaceholder.typicode.com" + + class posts(val id: String, override val body: String?): PostRoutingAPI() + + class comments(val postId: String, override val body: String?): PostRoutingAPI() + + override val method: Method + get() { + return when(this) { + is PostRoutingAPI.posts -> Method.GET + is PostRoutingAPI.comments -> Method.GET + } + } + + override val path: String + get() { + return when(this) { + is PostRoutingAPI.posts -> "/posts" + is PostRoutingAPI.comments -> "/comments" + } + } + + override val params: List>? + get() { + return when(this) { + is PostRoutingAPI.posts -> listOf("id" to this.id) + is PostRoutingAPI.comments -> listOf("postId" to this.postId) + } + } + + override val headers: Map? + get() { + return null + } +} diff --git a/core-kotlin/src/main/kotlin/com/baeldung/ktor/APIServer.kt b/core-kotlin/src/main/kotlin/com/baeldung/ktor/APIServer.kt deleted file mode 100755 index a12182ccc8..0000000000 --- a/core-kotlin/src/main/kotlin/com/baeldung/ktor/APIServer.kt +++ /dev/null @@ -1,73 +0,0 @@ -@file:JvmName("APIServer") - - -import io.ktor.application.call -import io.ktor.application.install -import io.ktor.features.CallLogging -import io.ktor.features.ContentNegotiation -import io.ktor.features.DefaultHeaders -import io.ktor.gson.gson -import io.ktor.request.path -import io.ktor.request.receive -import io.ktor.response.respond -import io.ktor.routing.* -import io.ktor.server.engine.embeddedServer -import io.ktor.server.netty.Netty -import org.slf4j.event.Level - -data class Author(val name: String, val website: String) -data class ToDo(var id: Int, val name: String, val description: String, val completed: Boolean) - -fun main(args: Array) { - - val toDoList = ArrayList(); - val jsonResponse = """{ - "id": 1, - "task": "Pay waterbill", - "description": "Pay water bill today", - }""" - - - embeddedServer(Netty, 8080) { - install(DefaultHeaders) { - header("X-Developer", "Baeldung") - } - install(CallLogging) { - level = Level.DEBUG - filter { call -> call.request.path().startsWith("/todo") } - filter { call -> call.request.path().startsWith("/author") } - } - install(ContentNegotiation) { - gson { - setPrettyPrinting() - } - } - routing() { - route("/todo") { - post { - var toDo = call.receive(); - toDo.id = toDoList.size; - toDoList.add(toDo); - call.respond("Added") - - } - delete("/{id}") { - call.respond(toDoList.removeAt(call.parameters["id"]!!.toInt())); - } - get("/{id}") { - - call.respond(toDoList[call.parameters["id"]!!.toInt()]); - } - get { - call.respond(toDoList); - } - } - get("/author"){ - call.respond(Author("Baeldung","baeldung.com")); - - } - - - } - }.start(wait = true) -} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/builder/BuilderPatternUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/builder/BuilderPatternUnitTest.kt index 17f5a43479..a6687b6e0a 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/builder/BuilderPatternUnitTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/builder/BuilderPatternUnitTest.kt @@ -93,6 +93,7 @@ internal class BuilderPatternUnitTest { Assertions.assertNull(foodOrder.fish) } + @Test fun whenBuildingFoodOrderApplySettingValues_thenFieldsNotNull() { diff --git a/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt new file mode 100644 index 0000000000..f0f9267618 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt @@ -0,0 +1,286 @@ +package com.baeldung.fuel + +import awaitObjectResult +import awaitStringResponse +import com.github.kittinunf.fuel.Fuel +import com.github.kittinunf.fuel.core.FuelManager +import com.github.kittinunf.fuel.core.Request +import com.github.kittinunf.fuel.core.interceptors.cUrlLoggingRequestInterceptor +import com.github.kittinunf.fuel.gson.responseObject +import com.github.kittinunf.fuel.httpGet +import com.github.kittinunf.fuel.rx.rx_object +import com.google.gson.Gson +import kotlinx.coroutines.experimental.runBlocking +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import java.io.File +import java.util.concurrent.CountDownLatch + +internal class FuelHttpUnitTest { + + @Test + fun whenMakingAsyncHttpGetRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val latch = CountDownLatch(1) + + "http://httpbin.org/get".httpGet().response{ + request, response, result -> + + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + + latch.countDown() + } + + latch.await() + + } + + @Test + fun whenMakingSyncHttpGetRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val (request, response, result) = "http://httpbin.org/get".httpGet().response() + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + + } + + @Test + fun whenMakingSyncHttpGetURLEncodedRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val (request, response, result) = + "https://jsonplaceholder.typicode.com/posts" + .httpGet(listOf("id" to "1")).response() + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + + } + + @Test + fun whenMakingAsyncHttpPostRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val latch = CountDownLatch(1) + + Fuel.post("http://httpbin.org/post").response{ + request, response, result -> + + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + + latch.countDown() + } + + latch.await() + + } + + @Test + fun whenMakingSyncHttpPostRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val (request, response, result) = Fuel.post("http://httpbin.org/post").response() + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + } + + @Test + fun whenMakingSyncHttpPostRequestwithBody_thenResponseNotNullAndErrorNullAndStatusCode200() { + + val (request, response, result) = Fuel.post("https://jsonplaceholder.typicode.com/posts") + .body("{ \"title\" : \"foo\",\"body\" : \"bar\",\"id\" : \"1\"}") + .response() + + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(201,response.statusCode) + } + + @Test + fun givenFuelInstance_whenMakingSyncHttpGetRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + FuelManager.instance.basePath = "http://httpbin.org" + FuelManager.instance.baseHeaders = mapOf("OS" to "macOS High Sierra") + + FuelManager.instance.addRequestInterceptor(cUrlLoggingRequestInterceptor()) + FuelManager.instance.addRequestInterceptor(tokenInterceptor()) + + + val (request, response, result) = "/get" + .httpGet().response() + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + } + + @Test + fun givenInterceptors_whenMakingSyncHttpGetRequest_thenResponseNotNullAndErrorNullAndStatusCode200() { + + FuelManager.instance.basePath = "http://httpbin.org" + FuelManager.instance.addRequestInterceptor(cUrlLoggingRequestInterceptor()) + FuelManager.instance.addRequestInterceptor(tokenInterceptor()) + + val (request, response, result) = "/get" + .httpGet().response() + val (data, error) = result + + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + } + + @Test + fun whenDownloadFile_thenCreateFileResponseNotNullAndErrorNullAndStatusCode200() { + + Fuel.download("http://httpbin.org/bytes/32768").destination { response, url -> + File.createTempFile("temp", ".tmp") + }.response{ + request, response, result -> + + val (data, error) = result + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + } + } + + @Test + fun whenDownloadFilewithProgressHandler_thenCreateFileResponseNotNullAndErrorNullAndStatusCode200() { + + val (request, response, result) = Fuel.download("http://httpbin.org/bytes/327680") + .destination { response, url -> File.createTempFile("temp", ".tmp") + }.progress { readBytes, totalBytes -> + val progress = readBytes.toFloat() / totalBytes.toFloat() + }.response () + + val (data, error) = result + Assertions.assertNull(error) + Assertions.assertNotNull(data) + Assertions.assertEquals(200,response.statusCode) + + + } + + @Test + fun whenMakeGetRequest_thenDeserializePostwithGson() { + + val latch = CountDownLatch(1) + + "https://jsonplaceholder.typicode.com/posts/1".httpGet().responseObject { _,_, result -> + val post = result.component1() + Assertions.assertEquals(1, post?.userId) + latch.countDown() + } + + latch.await() + + } + + @Test + fun whenMakePOSTRequest_thenSerializePostwithGson() { + + val post = Post(1,1, "Lorem", "Lorem Ipse dolor sit amet") + + val (request, response, result) = Fuel.post("https://jsonplaceholder.typicode.com/posts") + .header("Content-Type" to "application/json") + .body(Gson().toJson(post).toString()) + .response() + + Assertions.assertEquals(201,response.statusCode) + + } + + @Test + fun whenMakeGETRequestWithRxJava_thenDeserializePostwithGson() { + + val latch = CountDownLatch(1) + + + "https://jsonplaceholder.typicode.com/posts?id=1" + .httpGet().rx_object(Post.Deserializer()).subscribe{ + res, throwable -> + + val post = res.component1() + Assertions.assertEquals(1, post?.get(0)?.userId) + latch.countDown() + } + + latch.await() + + } + + @Test + fun whenMakeGETRequestUsingCoroutines_thenResponseStatusCode200() { + + runBlocking { + val (request, response, result) = Fuel.get("http://httpbin.org/get").awaitStringResponse() + + result.fold({ data -> + Assertions.assertEquals(200, response.statusCode) + + }, { error -> }) + } + + } + + @Test + fun whenMakeGETRequestUsingCoroutines_thenDeserializeResponse() { + + + runBlocking { + Fuel.get("https://jsonplaceholder.typicode.com/posts?id=1").awaitObjectResult(Post.Deserializer()) + .fold({ data -> + Assertions.assertEquals(1, data.get(0).userId) + }, { error -> }) + } + + } + + @Test + fun whenMakeGETPostRequestUsingRoutingAPI_thenDeserializeResponse() { + + val latch = CountDownLatch(1) + + Fuel.request(PostRoutingAPI.posts("1",null)) + .responseObject(Post.Deserializer()) { + request, response, result -> + Assertions.assertEquals(1, result.component1()?.get(0)?.userId) + latch.countDown() + } + + latch.await() + } + + @Test + fun whenMakeGETCommentRequestUsingRoutingAPI_thenResponseStausCode200() { + + val latch = CountDownLatch(1) + + Fuel.request(PostRoutingAPI.comments("1",null)) + .responseString { request, response, result -> + Assertions.assertEquals(200, response.statusCode) + latch.countDown() + } + + latch.await() + } + + +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTest.kt new file mode 100644 index 0000000000..469118f0f6 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTest.kt @@ -0,0 +1,191 @@ +package com.baeldung.kotlin + +import nl.komponents.kovenant.* +import nl.komponents.kovenant.Kovenant.deferred +import nl.komponents.kovenant.combine.and +import nl.komponents.kovenant.combine.combine +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import java.io.IOException +import java.util.* +import java.util.concurrent.TimeUnit + +class KovenantTest { + @Before + fun setupTestMode() { + Kovenant.testMode { error -> + println("An unexpected error occurred") + Assert.fail(error.message) + } + } + + @Test + fun testSuccessfulDeferred() { + val def = deferred() + Assert.assertFalse(def.promise.isDone()) + + def.resolve(1L) + Assert.assertTrue(def.promise.isDone()) + Assert.assertTrue(def.promise.isSuccess()) + Assert.assertFalse(def.promise.isFailure()) + } + + @Test + fun testFailedDeferred() { + val def = deferred() + Assert.assertFalse(def.promise.isDone()) + + def.reject(RuntimeException()) + Assert.assertTrue(def.promise.isDone()) + Assert.assertFalse(def.promise.isSuccess()) + Assert.assertTrue(def.promise.isFailure()) + } + + @Test + fun testResolveDeferredTwice() { + val def = deferred() + def.resolve(1L) + try { + def.resolve(1L) + } catch (e: AssertionError) { + // Expected. + // This is slightly unusual. The AssertionError comes from Assert.fail() from setupTestMode() + } + } + + @Test + fun testSuccessfulTask() { + val promise = task { 1L } + Assert.assertTrue(promise.isDone()) + Assert.assertTrue(promise.isSuccess()) + Assert.assertFalse(promise.isFailure()) + } + + @Test + fun testFailedTask() { + val promise = task { throw RuntimeException() } + Assert.assertTrue(promise.isDone()) + Assert.assertFalse(promise.isSuccess()) + Assert.assertTrue(promise.isFailure()) + } + + @Test + fun testCallbacks() { + val promise = task { 1L } + + promise.success { + println("This was a success") + Assert.assertEquals(1L, it) + } + + promise.fail { + println(it) + Assert.fail("This shouldn't happen") + } + + promise.always { + println("This always happens") + } + } + + @Test + fun testGetValues() { + val promise = task { 1L } + Assert.assertEquals(1L, promise.get()) + } + + @Test + fun testAllSucceed() { + val numbers = all( + task { 1L }, + task { 2L }, + task { 3L } + ) + + Assert.assertEquals(listOf(1L, 2L, 3L), numbers.get()) + } + + @Test + fun testOneFails() { + val runtimeException = RuntimeException() + + val numbers = all( + task { 1L }, + task { 2L }, + task { throw runtimeException } + ) + + Assert.assertEquals(runtimeException, numbers.getError()) + } + + @Test + fun testAnySucceeds() { + val promise = any( + task { + TimeUnit.SECONDS.sleep(3) + 1L + }, + task { + TimeUnit.SECONDS.sleep(2) + 2L + }, + task { + TimeUnit.SECONDS.sleep(1) + 3L + } + ) + + Assert.assertTrue(promise.isDone()) + Assert.assertTrue(promise.isSuccess()) + Assert.assertFalse(promise.isFailure()) + } + + @Test + fun testAllFails() { + val runtimeException = RuntimeException() + val ioException = IOException() + val illegalStateException = IllegalStateException() + val promise = any( + task { + TimeUnit.SECONDS.sleep(3) + throw runtimeException + }, + task { + TimeUnit.SECONDS.sleep(2) + throw ioException + }, + task { + TimeUnit.SECONDS.sleep(1) + throw illegalStateException + } + ) + + Assert.assertTrue(promise.isDone()) + Assert.assertFalse(promise.isSuccess()) + Assert.assertTrue(promise.isFailure()) + } + + @Test + fun testSimpleCombine() { + val promise = task { 1L } and task { "Hello" } + val result = promise.get() + + Assert.assertEquals(1L, result.first) + Assert.assertEquals("Hello", result.second) + } + + @Test + fun testLongerCombine() { + val promise = combine( + task { 1L }, + task { "Hello" }, + task { Currency.getInstance("USD") } + ) + val result = promise.get() + + Assert.assertEquals(1L, result.first) + Assert.assertEquals("Hello", result.second) + Assert.assertEquals(Currency.getInstance("USD"), result.third) + } +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTimeoutTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTimeoutTest.kt new file mode 100644 index 0000000000..e37d2cc2fa --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/KovenantTimeoutTest.kt @@ -0,0 +1,38 @@ +package com.baeldung.kotlin + +import nl.komponents.kovenant.Promise +import nl.komponents.kovenant.any +import nl.komponents.kovenant.task +import org.junit.Assert +import org.junit.Ignore +import org.junit.Test + +@Ignore +// Note that this can not run in the same test run if KovenantTest has already been executed +class KovenantTimeoutTest { + @Test + fun testTimeout() { + val promise = timedTask(1000) { "Hello" } + val result = promise.get() + Assert.assertEquals("Hello", result) + } + + @Test + fun testTimeoutExpired() { + val promise = timedTask(1000) { + Thread.sleep(3000) + "Hello" + } + val result = promise.get() + Assert.assertNull(result) + } + + fun timedTask(millis: Long, body: () -> T) : Promise> { + val timeoutTask = task { + Thread.sleep(millis) + null + } + val activeTask = task(body = body) + return any(activeTask, timeoutTask) + } +} diff --git a/disruptor/pom.xml b/disruptor/pom.xml index d3cef3bd9b..c26dcc0cd4 100644 --- a/disruptor/pom.xml +++ b/disruptor/pom.xml @@ -36,22 +36,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - org.apache.maven.plugins maven-jar-plugin diff --git a/drools/pom.xml b/drools/pom.xml index 631bc8c792..009ac8acec 100644 --- a/drools/pom.xml +++ b/drools/pom.xml @@ -48,12 +48,18 @@ poi-ooxml ${apache-poi-version} + + org.optaplanner + optaplanner-core + ${opta-planner-version} + 4.4.6 7.4.1.Final 3.13 + 7.10.0.Final diff --git a/drools/src/main/java/com/baeldung/drools/optaplanner/CourseSchedule.java b/drools/src/main/java/com/baeldung/drools/optaplanner/CourseSchedule.java new file mode 100644 index 0000000000..7b2ba117a1 --- /dev/null +++ b/drools/src/main/java/com/baeldung/drools/optaplanner/CourseSchedule.java @@ -0,0 +1,63 @@ +package com.baeldung.drools.optaplanner; + +import java.util.ArrayList; +import java.util.List; + +import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty; +import org.optaplanner.core.api.domain.solution.PlanningScore; +import org.optaplanner.core.api.domain.solution.PlanningSolution; +import org.optaplanner.core.api.domain.solution.drools.ProblemFactCollectionProperty; +import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider; +import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@PlanningSolution +public class CourseSchedule { + + Logger logger = LoggerFactory.getLogger("CourseSchedule"); + + private List roomList; + private List periodList; + private List lectureList; + private HardSoftScore score; + + public CourseSchedule(){ + roomList = new ArrayList<>(); + periodList = new ArrayList<>(); + lectureList = new ArrayList<>(); + } + + @ValueRangeProvider(id = "availableRooms") + @ProblemFactCollectionProperty + public List getRoomList() { + return roomList; + } + + @ValueRangeProvider(id = "availablePeriods") + @ProblemFactCollectionProperty + public List getPeriodList() { + return periodList; + } + + @PlanningEntityCollectionProperty + public List getLectureList() { + return lectureList; + } + + @PlanningScore + public HardSoftScore getScore() { + return score; + } + + public void setScore(HardSoftScore score) { + this.score = score; + } + + public void printCourseSchedule() { + lectureList.stream() + .map(c -> "Lecture in Room " + c.getRoomNumber().toString() + " during Period " + c.getPeriod().toString()) + .forEach(k -> logger.info(k)); + } + +} diff --git a/drools/src/main/java/com/baeldung/drools/optaplanner/Lecture.java b/drools/src/main/java/com/baeldung/drools/optaplanner/Lecture.java new file mode 100644 index 0000000000..193cdb08b1 --- /dev/null +++ b/drools/src/main/java/com/baeldung/drools/optaplanner/Lecture.java @@ -0,0 +1,30 @@ +package com.baeldung.drools.optaplanner; + +import org.optaplanner.core.api.domain.entity.PlanningEntity; +import org.optaplanner.core.api.domain.variable.PlanningVariable; + +@PlanningEntity +public class Lecture { + + private Integer roomNumber; + private Integer period; + + @PlanningVariable(valueRangeProviderRefs = {"availablePeriods"}) + public Integer getPeriod() { + return period; + } + + @PlanningVariable(valueRangeProviderRefs = {"availableRooms"}) + public Integer getRoomNumber() { + return roomNumber; + } + + public void setPeriod(Integer period) { + this.period = period; + } + + public void setRoomNumber(Integer roomNumber) { + this.roomNumber = roomNumber; + } + +} diff --git a/drools/src/main/java/com/baeldung/drools/optaplanner/ScoreCalculator.java b/drools/src/main/java/com/baeldung/drools/optaplanner/ScoreCalculator.java new file mode 100644 index 0000000000..86501cdccd --- /dev/null +++ b/drools/src/main/java/com/baeldung/drools/optaplanner/ScoreCalculator.java @@ -0,0 +1,32 @@ +package com.baeldung.drools.optaplanner; + +import java.util.HashSet; + +import org.optaplanner.core.api.score.Score; +import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore; +import org.optaplanner.core.impl.score.director.easy.EasyScoreCalculator; + +public class ScoreCalculator implements EasyScoreCalculator { + + @Override + public Score calculateScore(CourseSchedule courseSchedule) { + int hardScore = 0; + int softScore = 0; + + HashSet occupiedRooms = new HashSet<>(); + for (Lecture lecture : courseSchedule.getLectureList()) { + if(lecture.getPeriod() != null && lecture.getRoomNumber() != null) { + String roomInUse = lecture.getPeriod().toString() + ":" + lecture.getRoomNumber().toString(); + if (occupiedRooms.contains(roomInUse)) { + hardScore += -1; + } else { + occupiedRooms.add(roomInUse); + } + } else { + hardScore += -1; + } + } + + return HardSoftScore.valueOf(hardScore, softScore); + } +} diff --git a/drools/src/main/resources/courseSchedule.drl b/drools/src/main/resources/courseSchedule.drl new file mode 100644 index 0000000000..35dd57df84 --- /dev/null +++ b/drools/src/main/resources/courseSchedule.drl @@ -0,0 +1,14 @@ +package com.baeldung.drools.optaplanner + +import com.baeldung.drools.optaplanner.Lecture; +import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScoreHolder; + +global HardSoftScoreHolder scoreHolder; + +rule "noNullRoomPeriod" + when + Lecture( roomNumber == null ); + Lecture( period == null ); + then + scoreHolder.addHardConstraintMatch(kcontext, -1); +end \ No newline at end of file diff --git a/drools/src/main/resources/courseScheduleSolverConfigDrools.xml b/drools/src/main/resources/courseScheduleSolverConfigDrools.xml new file mode 100644 index 0000000000..7cf95fdcd3 --- /dev/null +++ b/drools/src/main/resources/courseScheduleSolverConfigDrools.xml @@ -0,0 +1,12 @@ + + + + + + courseSchedule.drl + + + + 10 + + \ No newline at end of file diff --git a/drools/src/main/resources/courseScheduleSolverConfiguration.xml b/drools/src/main/resources/courseScheduleSolverConfiguration.xml new file mode 100644 index 0000000000..dfedb8f2fd --- /dev/null +++ b/drools/src/main/resources/courseScheduleSolverConfiguration.xml @@ -0,0 +1,12 @@ + + + + + + com.baeldung.drools.optaplanner.ScoreCalculator + + + + 10 + + \ No newline at end of file diff --git a/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java b/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java new file mode 100644 index 0000000000..1880e30b86 --- /dev/null +++ b/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java @@ -0,0 +1,49 @@ +package com.baeldung.drools.optaplanner; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.optaplanner.core.api.solver.Solver; +import org.optaplanner.core.api.solver.SolverFactory; + +public class OptaPlannerUnitTest { + + static CourseSchedule unsolvedCourseSchedule; + + @BeforeAll + public static void setUp() { + + unsolvedCourseSchedule = new CourseSchedule(); + + for(int i = 0; i < 10; i++){ + unsolvedCourseSchedule.getLectureList().add(new Lecture()); + } + + unsolvedCourseSchedule.getPeriodList().addAll(Arrays.asList(new Integer[] { 1, 2, 3 })); + unsolvedCourseSchedule.getRoomList().addAll(Arrays.asList(new Integer[] { 1, 2 })); + } + + @Test + public void test_whenCustomJavaSolver() { + + SolverFactory solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfiguration.xml"); + Solver solver = solverFactory.buildSolver(); + CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule); + + Assert.assertNotNull(solvedCourseSchedule.getScore()); + Assert.assertEquals(-4, solvedCourseSchedule.getScore().getHardScore()); + } + + @Test + public void test_whenDroolsSolver() { + + SolverFactory solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfigDrools.xml"); + Solver solver = solverFactory.buildSolver(); + CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule); + + Assert.assertNotNull(solvedCourseSchedule.getScore()); + Assert.assertEquals(0, solvedCourseSchedule.getScore().getHardScore()); + } +} diff --git a/ejb/ejb-client/pom.xml b/ejb/ejb-client/pom.xml index 44112a4396..6231030cec 100755 --- a/ejb/ejb-client/pom.xml +++ b/ejb/ejb-client/pom.xml @@ -16,7 +16,6 @@ org.wildfly wildfly-ejb-client-bom pom - import com.baeldung.ejb diff --git a/ejb/pom.xml b/ejb/pom.xml index a188d43272..4cb700d087 100755 --- a/ejb/pom.xml +++ b/ejb/pom.xml @@ -17,7 +17,6 @@ ejb-remote - ejb-client ejb-session-beans diff --git a/enterprise-patterns/README.md b/enterprise-patterns/README.md deleted file mode 100644 index ff12555376..0000000000 --- a/enterprise-patterns/README.md +++ /dev/null @@ -1 +0,0 @@ -## Relevant articles: diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/AuditFilter.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/AuditFilter.java deleted file mode 100644 index d24c0a94b3..0000000000 --- a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/AuditFilter.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.baeldung.enterprise.patterns.front.controller.filters; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import java.io.IOException; - -public class AuditFilter extends BaseFilter { - @Override - public void doFilter( - ServletRequest request, - ServletResponse response, - FilterChain chain - ) throws IOException, ServletException { - HttpServletRequest httpServletRequest = (HttpServletRequest) request; - HttpSession session = httpServletRequest.getSession(false); - if (session != null && session.getAttribute("username") != null) { - request.setAttribute("username", session.getAttribute("username")); - } - chain.doFilter(request, response); - } -} diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/VisitorCounterFilter.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/VisitorCounterFilter.java deleted file mode 100644 index 0ae7cd73fd..0000000000 --- a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/VisitorCounterFilter.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.enterprise.patterns.front.controller.filters; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.annotation.WebFilter; -import java.io.IOException; - -@WebFilter(servletNames = "front-controller") -public class VisitorCounterFilter extends BaseFilter { - private int counter; - - @Override - public void doFilter( - ServletRequest request, - ServletResponse response, - FilterChain chain - ) throws IOException, ServletException { - request.setAttribute("counter", ++counter); - chain.doFilter(request, response); - } -} diff --git a/enterprise-patterns/pom.xml b/enterprise-patterns/pom.xml deleted file mode 100644 index ffd0b66aad..0000000000 --- a/enterprise-patterns/pom.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - 4.0.0 - com.baeldung.enterprise.patterns - enterprise-patterns-parent - pom - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - spring-dispatcher-servlet - - - diff --git a/gson/README.md b/gson/README.md index bedfbd206c..4122b21431 100644 --- a/gson/README.md +++ b/gson/README.md @@ -7,3 +7,4 @@ - [Gson Deserialization Cookbook](http://www.baeldung.com/gson-deserialization-guide) - [Jackson vs Gson](http://www.baeldung.com/jackson-vs-gson) - [Exclude Fields from Serialization in Gson](http://www.baeldung.com/gson-exclude-fields-serialization) +- [Save Data to a JSON File with Gson](https://www.baeldung.com/gson-save-file) diff --git a/guava/README.md b/guava/README.md index bb4e225649..fe1a347d72 100644 --- a/guava/README.md +++ b/guava/README.md @@ -33,3 +33,4 @@ - [Using Guava CountingOutputStream](http://www.baeldung.com/guava-counting-outputstream) - [Hamcrest Text Matchers](http://www.baeldung.com/hamcrest-text-matchers) - [Quick Guide to the Guava RateLimiter](http://www.baeldung.com/guava-rate-limiter) +- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap) diff --git a/hazelcast/README.md b/hazelcast/README.md index b90f66a8d0..7adb13f2af 100644 --- a/hazelcast/README.md +++ b/hazelcast/README.md @@ -1,2 +1,3 @@ ### Relevant Articles: - [Guide to Hazelcast with Java](http://www.baeldung.com/java-hazelcast) +- [Introduction to Hazelcast Jet](https://www.baeldung.com/hazelcast-jet) diff --git a/hibernate5/README.md b/hibernate5/README.md index 1bce52bd5e..8f2f8eb469 100644 --- a/hibernate5/README.md +++ b/hibernate5/README.md @@ -13,3 +13,4 @@ - [Pessimistic Locking in JPA](http://www.baeldung.com/jpa-pessimistic-locking) - [Bootstrapping JPA Programmatically in Java](http://www.baeldung.com/java-bootstrap-jpa) - [Optimistic Locking in JPA](http://www.baeldung.com/jpa-optimistic-locking) +- [Hibernate Entity Lifecycle](https://www.baeldung.com/hibernate-entity-lifecycle) diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/BatchEmployee.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/BatchEmployee.java new file mode 100644 index 0000000000..00643ab3dd --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/BatchEmployee.java @@ -0,0 +1,56 @@ +package com.baeldung.hibernate.proxy; + +import org.hibernate.annotations.BatchSize; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +@BatchSize(size = 5) +public class BatchEmployee implements Serializable { + + @Id + @GeneratedValue (strategy = GenerationType.SEQUENCE) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + private Boss boss; + + @Column(name = "name") + private String name; + + @Column(name = "surname") + private String surname; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Boss getBoss() { + return boss; + } + + public void setBoss(Boss boss) { + this.boss = boss; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Boss.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Boss.java new file mode 100644 index 0000000000..b6e01814d0 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Boss.java @@ -0,0 +1,49 @@ +package com.baeldung.hibernate.proxy; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +public class Boss implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private Long id; + + @Column(name = "name") + private String name; + + @Column(name = "surname") + private String surname; + + public Boss() { } + + public Boss(String name, String surname) { + this.name = name; + this.surname = surname; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java new file mode 100644 index 0000000000..6bc64c35ef --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/Employee.java @@ -0,0 +1,53 @@ +package com.baeldung.hibernate.proxy; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +public class Employee implements Serializable { + + @Id + @GeneratedValue (strategy = GenerationType.SEQUENCE) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + private Boss boss; + + @Column(name = "name") + private String name; + + @Column(name = "surname") + private String surname; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Boss getBoss() { + return boss; + } + + public void setBoss(Boss boss) { + this.boss = boss; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/proxy/HibernateUtil.java b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/HibernateUtil.java new file mode 100644 index 0000000000..e6ad0432bd --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/proxy/HibernateUtil.java @@ -0,0 +1,57 @@ +package com.baeldung.hibernate.proxy; + +import org.apache.commons.lang3.StringUtils; +import org.hibernate.SessionFactory; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.service.ServiceRegistry; + +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; +import java.util.Properties; + +public class HibernateUtil { + + private static SessionFactory sessionFactory; + private static String PROPERTY_FILE_NAME; + + public static SessionFactory getSessionFactory(String propertyFileName) throws IOException { + PROPERTY_FILE_NAME = propertyFileName; + if (sessionFactory == null) { + ServiceRegistry serviceRegistry = configureServiceRegistry(); + sessionFactory = getSessionFactoryBuilder(serviceRegistry).build(); + } + return sessionFactory; + } + + private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) { + MetadataSources metadataSources = new MetadataSources(serviceRegistry); + metadataSources.addPackage("com.baeldung.hibernate.proxy"); + metadataSources.addAnnotatedClass(Boss.class); + metadataSources.addAnnotatedClass(Employee.class); + + Metadata metadata = metadataSources.buildMetadata(); + return metadata.getSessionFactoryBuilder(); + + } + + private static ServiceRegistry configureServiceRegistry() throws IOException { + Properties properties = getProperties(); + return new StandardServiceRegistryBuilder().applySettings(properties) + .build(); + } + + private static Properties getProperties() throws IOException { + Properties properties = new Properties(); + URL propertiesURL = Thread.currentThread() + .getContextClassLoader() + .getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties")); + try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) { + properties.load(inputStream); + } + return properties; + } +} diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java new file mode 100644 index 0000000000..fa41797dd2 --- /dev/null +++ b/hibernate5/src/test/java/com/baeldung/hibernate/proxy/HibernateProxyUnitTest.java @@ -0,0 +1,75 @@ +package com.baeldung.hibernate.proxy; + +import org.hibernate.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.List; + +import static org.junit.Assert.fail; + +public class HibernateProxyUnitTest { + + private Session session; + + @Before + public void init(){ + try { + session = HibernateUtil.getSessionFactory("hibernate.properties") + .openSession(); + } catch (HibernateException | IOException e) { + fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]"); + } + + Boss boss = new Boss("Eduard", "Freud"); + session.save(boss); + } + + @After + public void close(){ + if(session != null) { + session.close(); + } + } + + @Test(expected = NullPointerException.class) + public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull() { + Employee employee = session.get(Employee.class, new Long(14)); + assertNull(employee); + employee.getId(); + } + + @Test + public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() { + Employee employee = session.load(Employee.class, new Long(14)); + assertNotNull(employee); + } + + @Test + public void givenABatchEmployeeList_whenSaveOne_thenSaveTheWholeBatch() { + Transaction transaction = session.beginTransaction(); + + for (long i = 1; i <= 5; i++) { + Employee employee = new Employee(); + employee.setName("Employee " + i); + session.save(employee); + } + + //After this line is possible to see all the insertions in the logs + session.flush(); + session.clear(); + transaction.commit(); + + transaction = session.beginTransaction(); + + List employeeList = session.createQuery("from Employee") + .setCacheMode(CacheMode.IGNORE).getResultList(); + + assertEquals(employeeList.size(), 5); + transaction.commit(); + } +} diff --git a/intelliJ/README.md b/intelliJ/README.md new file mode 100644 index 0000000000..d45bd0cee5 --- /dev/null +++ b/intelliJ/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Writing IntelliJ IDEA Plugins](https://www.baeldung.com/intellij-new-custom-plugin) diff --git a/java-dates/pom.xml b/java-dates/pom.xml index 877dd615a8..13e2a077e1 100644 --- a/java-dates/pom.xml +++ b/java-dates/pom.xml @@ -61,23 +61,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - org.apache.maven.plugins maven-compiler-plugin diff --git a/java-numbers/pom.xml b/java-numbers/pom.xml index bf4d3e8792..bb63c8cfe1 100644 --- a/java-numbers/pom.xml +++ b/java-numbers/pom.xml @@ -83,23 +83,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - org.apache.maven.plugins maven-javadoc-plugin diff --git a/java-numbers/src/main/java/com/baeldung/maths/BigDecimalDemo.java b/java-numbers/src/main/java/com/baeldung/maths/BigDecimalDemo.java new file mode 100644 index 0000000000..7de0197769 --- /dev/null +++ b/java-numbers/src/main/java/com/baeldung/maths/BigDecimalDemo.java @@ -0,0 +1,29 @@ +package com.baeldung.maths; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class BigDecimalDemo { + + /** Calculate total amount to be paid for an item rounded to cents.. + * @param quantity + * @param unitPrice + * @param discountRate + * @param taxRate + * @return + */ + public static BigDecimal calculateTotalAmount(BigDecimal quantity, + BigDecimal unitPrice, BigDecimal discountRate, BigDecimal taxRate) { + BigDecimal amount = quantity.multiply(unitPrice); + BigDecimal discount = amount.multiply(discountRate); + BigDecimal discountedAmount = amount.subtract(discount); + BigDecimal tax = discountedAmount.multiply(taxRate); + BigDecimal total = discountedAmount.add(tax); + + // round to 2 decimal places using HALF_EVEN + BigDecimal roundedTotal = total.setScale(2, RoundingMode.HALF_EVEN); + + return roundedTotal; + } + +} diff --git a/java-numbers/src/test/java/com/baeldung/maths/BigDecimalDemoUnitTest.java b/java-numbers/src/test/java/com/baeldung/maths/BigDecimalDemoUnitTest.java new file mode 100644 index 0000000000..2bf9872bec --- /dev/null +++ b/java-numbers/src/test/java/com/baeldung/maths/BigDecimalDemoUnitTest.java @@ -0,0 +1,120 @@ +package com.baeldung.maths; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.MathContext; +import java.math.RoundingMode; +import java.util.Random; + +import org.junit.jupiter.api.Test; + +public class BigDecimalDemoUnitTest { + + @Test + public void whenBigDecimalCreated_thenValueMatches() { + BigDecimal bdFromString = new BigDecimal("0.1"); + BigDecimal bdFromCharArray = new BigDecimal( + new char[] { '3', '.', '1', '6', '1', '5' }); + BigDecimal bdlFromInt = new BigDecimal(42); + BigDecimal bdFromLong = new BigDecimal(123412345678901L); + BigInteger bigInteger = BigInteger.probablePrime(100, new Random()); + BigDecimal bdFromBigInteger = new BigDecimal(bigInteger); + + assertEquals("0.1", bdFromString.toString()); + assertEquals("3.1615", bdFromCharArray.toString()); + assertEquals("42", bdlFromInt.toString()); + assertEquals("123412345678901", bdFromLong.toString()); + assertEquals(bigInteger.toString(), bdFromBigInteger.toString()); + } + + @Test + public void whenBigDecimalCreatedFromDouble_thenValueMayNotMatch() { + BigDecimal bdFromDouble = new BigDecimal(0.1d); + assertNotEquals("0.1", bdFromDouble.toString()); + } + + @Test + public void whenBigDecimalCreatedUsingValueOf_thenValueMatches() { + BigDecimal bdFromLong1 = BigDecimal.valueOf(123412345678901L); + BigDecimal bdFromLong2 = BigDecimal.valueOf(123412345678901L, 2); + BigDecimal bdFromDouble = BigDecimal.valueOf(0.1d); + + assertEquals("123412345678901", bdFromLong1.toString()); + assertEquals("1234123456789.01", bdFromLong2.toString()); + assertEquals("0.1", bdFromDouble.toString()); + } + + @Test + public void whenEqualsCalled_thenSizeAndScaleMatched() { + BigDecimal bd1 = new BigDecimal("1.0"); + BigDecimal bd2 = new BigDecimal("1.00"); + + assertFalse(bd1.equals(bd2)); + } + + @Test + public void whenComparingBigDecimals_thenExpectedResult() { + BigDecimal bd1 = new BigDecimal("1.0"); + BigDecimal bd2 = new BigDecimal("1.00"); + BigDecimal bd3 = new BigDecimal("2.0"); + + assertTrue(bd1.compareTo(bd3) < 0); + assertTrue(bd3.compareTo(bd1) > 0); + assertTrue(bd1.compareTo(bd2) == 0); + assertTrue(bd1.compareTo(bd3) <= 0); + assertTrue(bd1.compareTo(bd2) >= 0); + assertTrue(bd1.compareTo(bd3) != 0); + } + + @Test + public void whenPerformingArithmetic_thenExpectedResult() { + BigDecimal bd1 = new BigDecimal("4.0"); + BigDecimal bd2 = new BigDecimal("2.0"); + + BigDecimal sum = bd1.add(bd2); + BigDecimal difference = bd1.subtract(bd2); + BigDecimal quotient = bd1.divide(bd2); + BigDecimal product = bd1.multiply(bd2); + + assertTrue(sum.compareTo(new BigDecimal("6.0")) == 0); + assertTrue(difference.compareTo(new BigDecimal("2.0")) == 0); + assertTrue(quotient.compareTo(new BigDecimal("2.0")) == 0); + assertTrue(product.compareTo(new BigDecimal("8.0")) == 0); + } + + @Test + public void whenGettingAttributes_thenExpectedResult() { + BigDecimal bd = new BigDecimal("-12345.6789"); + + assertEquals(9, bd.precision()); + assertEquals(4, bd.scale()); + assertEquals(-1, bd.signum()); + } + + @Test + public void whenRoundingDecimal_thenExpectedResult() { + BigDecimal bd = new BigDecimal("2.5"); + // Round to 1 digit using HALF_EVEN + BigDecimal rounded = bd + .round(new MathContext(1, RoundingMode.HALF_EVEN)); + + assertEquals("2", rounded.toString()); + } + + @Test + public void givenPurchaseTxn_whenCalculatingTotalAmount_thenExpectedResult() { + BigDecimal quantity = new BigDecimal("4.5"); + BigDecimal unitPrice = new BigDecimal("2.69"); + BigDecimal discountRate = new BigDecimal("0.10"); + BigDecimal taxRate = new BigDecimal("0.0725"); + + BigDecimal amountToBePaid = BigDecimalDemo + .calculateTotalAmount(quantity, unitPrice, discountRate, taxRate); + assertEquals("11.68", amountToBePaid.toString()); + } +} diff --git a/java-numbers/src/test/java/com/baeldung/maths/BigIntegerDemoUnitTest.java b/java-numbers/src/test/java/com/baeldung/maths/BigIntegerDemoUnitTest.java new file mode 100644 index 0000000000..3537ccb3a3 --- /dev/null +++ b/java-numbers/src/test/java/com/baeldung/maths/BigIntegerDemoUnitTest.java @@ -0,0 +1,128 @@ +package com.baeldung.maths; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.math.BigInteger; +import java.util.Random; + +import org.junit.jupiter.api.Test; + +public class BigIntegerDemoUnitTest { + + @Test + public void whenBigIntegerCreatedFromConstructor_thenExpectedResult() { + BigInteger biFromString = new BigInteger("1234567890987654321"); + BigInteger biFromByteArray = new BigInteger( + new byte[] { 64, 64, 64, 64, 64, 64 }); + BigInteger biFromSignMagnitude = new BigInteger(-1, + new byte[] { 64, 64, 64, 64, 64, 64 }); + + assertEquals("1234567890987654321", biFromString.toString()); + assertEquals("70644700037184", biFromByteArray.toString()); + assertEquals("-70644700037184", biFromSignMagnitude.toString()); + } + + @Test + public void whenLongConvertedToBigInteger_thenValueMatches() { + BigInteger bi = BigInteger.valueOf(2305843009213693951L); + + assertEquals("2305843009213693951", bi.toString()); + } + + @Test + public void givenBigIntegers_whentCompared_thenExpectedResult() { + BigInteger i = new BigInteger("123456789012345678901234567890"); + BigInteger j = new BigInteger("123456789012345678901234567891"); + BigInteger k = new BigInteger("123456789012345678901234567892"); + + assertTrue(i.compareTo(i) == 0); + assertTrue(j.compareTo(i) > 0); + assertTrue(j.compareTo(k) < 0); + } + + @Test + public void givenBigIntegers_whenPerformingArithmetic_thenExpectedResult() { + BigInteger i = new BigInteger("4"); + BigInteger j = new BigInteger("2"); + + BigInteger sum = i.add(j); + BigInteger difference = i.subtract(j); + BigInteger quotient = i.divide(j); + BigInteger product = i.multiply(j); + + assertEquals(new BigInteger("6"), sum); + assertEquals(new BigInteger("2"), difference); + assertEquals(new BigInteger("2"), quotient); + assertEquals(new BigInteger("8"), product); + } + + @Test + public void givenBigIntegers_whenPerformingBitOperations_thenExpectedResult() { + BigInteger i = new BigInteger("17"); + BigInteger j = new BigInteger("7"); + + BigInteger and = i.and(j); + BigInteger or = i.or(j); + BigInteger not = j.not(); + BigInteger xor = i.xor(j); + BigInteger andNot = i.andNot(j); + BigInteger shiftLeft = i.shiftLeft(1); + BigInteger shiftRight = i.shiftRight(1); + + assertEquals(new BigInteger("1"), and); + assertEquals(new BigInteger("23"), or); + assertEquals(new BigInteger("-8"), not); + assertEquals(new BigInteger("22"), xor); + assertEquals(new BigInteger("16"), andNot); + assertEquals(new BigInteger("34"), shiftLeft); + assertEquals(new BigInteger("8"), shiftRight); + } + + @Test + public void givenBigIntegers_whenPerformingBitManipulations_thenExpectedResult() { + BigInteger i = new BigInteger("1018"); + + int bitCount = i.bitCount(); + int bitLength = i.bitLength(); + int getLowestSetBit = i.getLowestSetBit(); + boolean testBit3 = i.testBit(3); + BigInteger setBit12 = i.setBit(12); + BigInteger flipBit0 = i.flipBit(0); + BigInteger clearBit3 = i.clearBit(3); + + assertEquals(8, bitCount); + assertEquals(10, bitLength); + assertEquals(1, getLowestSetBit); + assertEquals(true, testBit3); + assertEquals(new BigInteger("5114"), setBit12); + assertEquals(new BigInteger("1019"), flipBit0); + assertEquals(new BigInteger("1010"), clearBit3); + } + + @Test + public void givenBigIntegers_whenModularCalculation_thenExpectedResult() { + BigInteger i = new BigInteger("31"); + BigInteger j = new BigInteger("24"); + BigInteger k = new BigInteger("16"); + + BigInteger gcd = j.gcd(k); + BigInteger multiplyAndmod = j.multiply(k) + .mod(i); + BigInteger modInverse = j.modInverse(i); + BigInteger modPow = j.modPow(k, i); + + assertEquals(new BigInteger("8"), gcd); + assertEquals(new BigInteger("12"), multiplyAndmod); + assertEquals(new BigInteger("22"), modInverse); + assertEquals(new BigInteger("7"), modPow); + } + + @Test + public void givenBigIntegers_whenPrimeOperations_thenExpectedResult() { + BigInteger i = BigInteger.probablePrime(100, new Random()); + + boolean isProbablePrime = i.isProbablePrime(1000); + assertEquals(true, isProbablePrime); + } +} diff --git a/java-streams/README.md b/java-streams/README.md index 4bfcabb7cf..548d4b6a33 100644 --- a/java-streams/README.md +++ b/java-streams/README.md @@ -12,3 +12,4 @@ - [Iterable to Stream in Java](http://www.baeldung.com/java-iterable-to-stream) - [How to Iterate Over a Stream With Indices](http://www.baeldung.com/java-stream-indices) - [Primitive Type Streams in Java 8](http://www.baeldung.com/java-8-primitive-streams) +- [Stream Ordering in Java](https://www.baeldung.com/java-stream-ordering) diff --git a/java-streams/pom.xml b/java-streams/pom.xml index 4f8651a756..023a5f695b 100644 --- a/java-streams/pom.xml +++ b/java-streams/pom.xml @@ -86,23 +86,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - org.apache.maven.plugins maven-compiler-plugin diff --git a/java-streams/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java b/java-streams/src/test/java/com/baeldung/streamordering/BenchmarkManualTest.java similarity index 98% rename from java-streams/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java rename to java-streams/src/test/java/com/baeldung/streamordering/BenchmarkManualTest.java index ba1cb1f726..656a6d95f9 100644 --- a/java-streams/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java +++ b/java-streams/src/test/java/com/baeldung/streamordering/BenchmarkManualTest.java @@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; -public class BenchmarkUnitTest +public class BenchmarkManualTest { public void diff --git a/java-strings/README.md b/java-strings/README.md index d5b9b45b20..233d986d98 100644 --- a/java-strings/README.md +++ b/java-strings/README.md @@ -24,4 +24,6 @@ - [Check If a String Is Numeric in Java](http://www.baeldung.com/java-check-string-number) - [Why Use char[] Array Over a String for Storing Passwords in Java?](http://www.baeldung.com/java-storing-passwords) - [Convert a String to Title Case](http://www.baeldung.com/java-string-title-case) -- [Compact Strings in Java 9](http://www.baeldung.com/java-9-compact-string) \ No newline at end of file +- [Compact Strings in Java 9](http://www.baeldung.com/java-9-compact-string) +- [Java Check a String for Lowercase/Uppercase Letter, Special Character and Digit](https://www.baeldung.com/java-lowercase-uppercase-special-character-digit-regex) +- [Convert java.util.Date to String](https://www.baeldung.com/java-util-date-to-string) diff --git a/java-strings/pom.xml b/java-strings/pom.xml index 0c83b4d9e7..2afe18f07a 100644 --- a/java-strings/pom.xml +++ b/java-strings/pom.xml @@ -71,23 +71,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - org.apache.maven.plugins maven-compiler-plugin diff --git a/java-strings/src/test/java/com/baeldung/string/RemovingEmojiFromStringUnitTest.java b/java-strings/src/test/java/com/baeldung/string/RemovingEmojiFromStringUnitTest.java index 163f28d0d8..8688f9dcf5 100644 --- a/java-strings/src/test/java/com/baeldung/string/RemovingEmojiFromStringUnitTest.java +++ b/java-strings/src/test/java/com/baeldung/string/RemovingEmojiFromStringUnitTest.java @@ -1,8 +1,6 @@ package com.baeldung.string; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -12,35 +10,28 @@ import org.junit.Test; import com.vdurmont.emoji.EmojiParser; public class RemovingEmojiFromStringUnitTest { - String text = "la confĂ©rence, commencera Ă  10 heures đŸ˜… ✿"; + String text = "la confĂ©rence, commencera Ă  10 heures đŸ˜…"; String regex = "[^\\p{L}\\p{N}\\p{P}\\p{Z}]"; @Test public void whenRemoveEmojiUsingLibrary_thenSuccess() { String result = EmojiParser.removeAllEmojis(text); System.out.println(result); - assertThat(result, not(containsString("đŸ˜…"))); - assertThat(result, containsString("Ă ")); - assertThat(result, containsString("la")); - assertThat(result, containsString("10")); + assertEquals(result, "la confĂ©rence, commencera Ă  10 heures "); } @Test public void whenReplaceEmojiUsingLibrary_thenSuccess() { String result = EmojiParser.parseToAliases(text); System.out.println(result); - assertThat(result, not(containsString("đŸ˜…"))); - assertThat(result, containsString("sweat_smile")); + assertEquals(result, "la confĂ©rence, commencera Ă  10 heures :sweat_smile:"); } @Test public void whenRemoveEmojiUsingRegex_thenSuccess() { String result = text.replaceAll(regex, ""); System.out.println(result); - assertThat(result, not(containsString("đŸ˜…"))); - assertThat(result, containsString("Ă ")); - assertThat(result, containsString("la")); - assertThat(result, containsString("10")); + assertEquals(result, "la confĂ©rence, commencera Ă  10 heures "); } @Test @@ -50,29 +41,20 @@ public class RemovingEmojiFromStringUnitTest { String result = matcher.replaceAll(""); System.out.println(result); - assertThat(result, not(containsString("đŸ˜…"))); - assertThat(result, containsString("Ă ")); - assertThat(result, containsString("la")); - assertThat(result, containsString("10")); + assertEquals(result, "la confĂ©rence, commencera Ă  10 heures "); } @Test public void whenRemoveEmojiUsingCodepoints_thenSuccess() { String result = text.replaceAll("[\\x{0001f300}-\\x{0001f64f}]|[\\x{0001f680}-\\x{0001f6ff}]", ""); System.out.println(result); - assertThat(result, not(containsString("đŸ˜…"))); - assertThat(result, containsString("Ă ")); - assertThat(result, containsString("la")); - assertThat(result, containsString("10")); + assertEquals(result, "la confĂ©rence, commencera Ă  10 heures "); } @Test public void whenRemoveEmojiUsingUnicode_thenSuccess() { String result = text.replaceAll("[\ud83c\udf00-\ud83d\ude4f]|[\ud83d\ude80-\ud83d\udeff]", ""); System.out.println(result); - assertThat(result, not(containsString("đŸ˜…"))); - assertThat(result, containsString("Ă ")); - assertThat(result, containsString("la")); - assertThat(result, containsString("10")); + assertEquals(result, "la confĂ©rence, commencera Ă  10 heures "); } } diff --git a/java-strings/src/test/java/com/baeldung/string/SubstringUnitTest.java b/java-strings/src/test/java/com/baeldung/string/SubstringUnitTest.java new file mode 100644 index 0000000000..3a4e231828 --- /dev/null +++ b/java-strings/src/test/java/com/baeldung/string/SubstringUnitTest.java @@ -0,0 +1,62 @@ +package com.baeldung.string; + +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Assert; +import org.junit.Test; + +public class SubstringUnitTest { + + String text = "Julia Evans was born on 25-09-1984. She is currently living in the USA (United States of America)."; + + @Test + public void givenAString_whenUsedStringUtils_ShouldReturnProperSubstring() { + Assert.assertEquals("United States of America", StringUtils.substringBetween(text, "(", ")")); + Assert.assertEquals("the USA (United States of America).", StringUtils.substringAfter(text, "living in ")); + Assert.assertEquals("Julia Evans", StringUtils.substringBefore(text, " was born")); + } + + @Test + public void givenAString_whenUsedScanner_ShouldReturnProperSubstring() { + try (Scanner scanner = new Scanner(text)) { + scanner.useDelimiter("\\."); + Assert.assertEquals("Julia Evans was born on 25-09-1984", scanner.next()); + } + } + + @Test + public void givenAString_whenUsedSplit_ShouldReturnProperSubstring() { + String[] sentences = text.split("\\."); + Assert.assertEquals("Julia Evans was born on 25-09-1984", sentences[0]); + } + + @Test + public void givenAString_whenUsedRegex_ShouldReturnProperSubstring() { + Pattern pattern = Pattern.compile("\\d{2}\\-\\d{2}-\\d{4}"); + Matcher matcher = pattern.matcher(text); + + if (matcher.find()) { + Assert.assertEquals("25-09-1984", matcher.group()); + } + } + + @Test + public void givenAString_whenUsedSubSequence_ShouldReturnProperSubstring() { + Assert.assertEquals("USA (United States of America)", text.subSequence(67, text.length() - 1)); + } + + @Test + public void givenAString_whenUsedSubstring_ShouldReturnProperSubstring() { + Assert.assertEquals("USA (United States of America).", text.substring(67)); + Assert.assertEquals("USA (United States of America)", text.substring(67, text.length() - 1)); + } + + @Test + public void givenAString_whenUsedSubstringWithIndexOf_ShouldReturnProperSubstring() { + Assert.assertEquals("United States of America", text.substring(text.indexOf('(') + 1, text.indexOf(')'))); + } + +} diff --git a/javaxval/bin/.gitignore b/javaxval/bin/.gitignore deleted file mode 100644 index 55a6bcf596..0000000000 --- a/javaxval/bin/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -.classpath -.project -.settings/ -target/ - - diff --git a/javaxval/bin/pom.xml b/javaxval/bin/pom.xml deleted file mode 100644 index a16072bb1a..0000000000 --- a/javaxval/bin/pom.xml +++ /dev/null @@ -1,44 +0,0 @@ - - 4.0.0 - com.baeldung - javaxval - 0.1-SNAPSHOT - - - - - - javax.validation - validation-api - 1.1.0.Final - - - - org.hibernate - hibernate-validator - 5.2.1.Final - - - - org.hibernate - hibernate-validator-annotation-processor - 5.2.1.Final - - - - javax.el - javax.el-api - 2.2.4 - - - - org.glassfish.web - javax.el - 2.2.4 - - - - - - \ No newline at end of file diff --git a/jee-7/pom.xml b/jee-7/pom.xml index fbf102185d..08c8b5a068 100644 --- a/jee-7/pom.xml +++ b/jee-7/pom.xml @@ -384,10 +384,6 @@ - - ${maven.min.version} - - bintray-mvc-spec-maven diff --git a/jersey/README.md b/jersey/README.md index a4c8c52d68..3121c560cf 100644 --- a/jersey/README.md +++ b/jersey/README.md @@ -1 +1,2 @@ -- [Jersey Filters and Interceptors] (http://www.baeldung.com/jersey-filters-interceptors) +- [Jersey Filters and Interceptors](http://www.baeldung.com/jersey-filters-interceptors) +- [Jersey MVC Support](https://www.baeldung.com/jersey-mvc) diff --git a/jhipster/jhipster-monolithic/pom.xml b/jhipster/jhipster-monolithic/pom.xml index 24956745e4..9708b83a70 100644 --- a/jhipster/jhipster-monolithic/pom.xml +++ b/jhipster/jhipster-monolithic/pom.xml @@ -412,6 +412,7 @@ org.apache.maven.plugins maven-eclipse-plugin + ${maven-eclipse-plugin.version} true true @@ -879,10 +880,6 @@ - - ${maven.version} - - -Djava.security.egd=file:/dev/./urandom -Xmx256m 3.6.2 @@ -909,6 +906,7 @@ 1.1.0.Final 1.4.1 3.0.1 + 2.10 yyyyMMddHHmmss 3.0.0 3.1.3 diff --git a/jnosql/jnosql-artemis/pom.xml b/jnosql/jnosql-artemis/pom.xml index 6186b3510c..9c2bfd1ba0 100644 --- a/jnosql/jnosql-artemis/pom.xml +++ b/jnosql/jnosql-artemis/pom.xml @@ -19,7 +19,7 @@ - ${artifactId} + ${project.artifactId} net.wasdev.wlp.maven.plugins diff --git a/jsf/pom.xml b/jsf/pom.xml index 19ea4b569f..2ac8a9f7c2 100644 --- a/jsf/pom.xml +++ b/jsf/pom.xml @@ -36,11 +36,6 @@ ${jstl.version} - - org.springframework - spring-web - ${org.springframework.version} - org.springframework spring-webmvc diff --git a/json/README.md b/json/README.md index 8c2abc3e47..e0679bc60b 100644 --- a/json/README.md +++ b/json/README.md @@ -9,3 +9,4 @@ - [Introduction to JsonPath](http://www.baeldung.com/guide-to-jayway-jsonpath) - [Introduction to JSON-Java (org.json)](http://www.baeldung.com/java-org-json) - [Overview of JSON Pointer](https://www.baeldung.com/json-pointer) +- [Introduction to the JSON Binding API (JSR 367) in Java](http://www.baeldung.com/java-json-binding-api) \ No newline at end of file diff --git a/json/pom.xml b/json/pom.xml index fa3fcafa65..b688baec06 100644 --- a/json/pom.xml +++ b/json/pom.xml @@ -33,7 +33,11 @@ json 20171018 - + + javax.json.bind + javax.json.bind-api + ${jsonb-api.version} + junit junit @@ -46,12 +50,26 @@ javax.json 1.1.2 - + + org.eclipse + yasson + ${yasson.version} + + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + test + 1.4.1 1.2.21 + 1.0 + 4.1 + 1.0.1 diff --git a/jsonb/src/main/java/com/baeldung/adapter/PersonAdapter.java b/json/src/main/java/com/baeldung/adapter/PersonAdapter.java similarity index 100% rename from jsonb/src/main/java/com/baeldung/adapter/PersonAdapter.java rename to json/src/main/java/com/baeldung/adapter/PersonAdapter.java diff --git a/jsonb/src/main/java/com/baeldung/jsonb/Person.java b/json/src/main/java/com/baeldung/jsonb/Person.java similarity index 100% rename from jsonb/src/main/java/com/baeldung/jsonb/Person.java rename to json/src/main/java/com/baeldung/jsonb/Person.java diff --git a/jsonb/src/test/java/com/baeldung/jsonb/JsonbTest.java b/json/src/test/java/com/baeldung/jsonb/JsonbUnitTest.java similarity index 99% rename from jsonb/src/test/java/com/baeldung/jsonb/JsonbTest.java rename to json/src/test/java/com/baeldung/jsonb/JsonbUnitTest.java index 67beda77e1..f258700c6b 100644 --- a/jsonb/src/test/java/com/baeldung/jsonb/JsonbTest.java +++ b/json/src/test/java/com/baeldung/jsonb/JsonbUnitTest.java @@ -18,8 +18,9 @@ import org.apache.commons.collections4.ListUtils; import org.junit.Test; import com.baeldung.adapter.PersonAdapter; +import com.baeldung.jsonb.Person; -public class JsonbTest { +public class JsonbUnitTest { @Test public void givenPersonList_whenSerializeWithJsonb_thenGetPersonJsonArray() { diff --git a/jsonb/.gitignore b/jsonb/.gitignore deleted file mode 100644 index dec013dfa4..0000000000 --- a/jsonb/.gitignore +++ /dev/null @@ -1,12 +0,0 @@ -#folders# -.idea -/target -/neoDb* -/data -/src/main/webapp/WEB-INF/classes -*/META-INF/* - -# Packaged files # -*.jar -*.war -*.ear \ No newline at end of file diff --git a/jsonb/README.md b/jsonb/README.md deleted file mode 100644 index 9293a44808..0000000000 --- a/jsonb/README.md +++ /dev/null @@ -1,4 +0,0 @@ -## JSON B - -## Relevant articles: -- [Introduction to the JSON Binding API (JSR 367) in Java](http://www.baeldung.com/java-json-binding-api) diff --git a/jsonb/pom.xml b/jsonb/pom.xml deleted file mode 100644 index 160b9d5df7..0000000000 --- a/jsonb/pom.xml +++ /dev/null @@ -1,101 +0,0 @@ - - - 4.0.0 - com.baeldung - json-b - 0.0.1-SNAPSHOT - jar - json-b - json-b sample project - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - - javax.json.bind - javax.json.bind-api - ${jsonb-api.version} - - - - org.apache.commons - commons-collections4 - ${commons-collections4.version} - test - - - - org.junit.jupiter - junit-jupiter-api - ${junit.jupiter.version} - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - test - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - - - - - - yasson - - true - - - - - org.eclipse - yasson - ${yasson.version} - - - org.glassfish - javax.json - ${javax.json.version} - - - - - johnzon - - - - org.apache.geronimo.specs - geronimo-json_1.1_spec - ${geronimo-json_1.1_spec.version} - - - org.apache.johnzon - johnzon-jsonb - ${johnzon.version} - - - - - - - 1.8 - 1.8 - 1.0.0 - 1.0 - 1.1.3 - 1.0 - 1.0.1 - 1.1.2 - 4.1 - - - \ No newline at end of file diff --git a/jta/pom.xml b/jta/pom.xml new file mode 100644 index 0000000000..89bdccf25e --- /dev/null +++ b/jta/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + com.baeldung + jta-demo + 1.0-SNAPSHOT + jar + + JEE JTA demo + + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-jta-bitronix + + + org.springframework.boot + spring-boot-starter-jdbc + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.hsqldb + hsqldb + 2.4.1 + + + + + + autoconfiguration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + **/*IntegrationTest.java + **/*IntTest.java + + + **/AutoconfigurationTest.java + + + + + + + json + + + + + + + + diff --git a/jta/src/main/java/com/baeldung/jtademo/JtaDemoApplication.java b/jta/src/main/java/com/baeldung/jtademo/JtaDemoApplication.java new file mode 100644 index 0000000000..4d8779efe5 --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/JtaDemoApplication.java @@ -0,0 +1,48 @@ +package com.baeldung.jtademo; + +import org.hsqldb.jdbc.pool.JDBCXADataSource; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.jta.bitronix.BitronixXADataSourceWrapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; + +@EnableAutoConfiguration +@EnableTransactionManagement +@Configuration +@ComponentScan +public class JtaDemoApplication { + + @Bean("dataSourceAccount") + public DataSource dataSource() throws Exception { + return createHsqlXADatasource("jdbc:hsqldb:mem:accountDb"); + } + + @Bean("dataSourceAudit") + public DataSource dataSourceAudit() throws Exception { + return createHsqlXADatasource("jdbc:hsqldb:mem:auditDb"); + } + + private DataSource createHsqlXADatasource(String connectionUrl) throws Exception { + JDBCXADataSource dataSource = new JDBCXADataSource(); + dataSource.setUrl(connectionUrl); + dataSource.setUser("sa"); + BitronixXADataSourceWrapper wrapper = new BitronixXADataSourceWrapper(); + return wrapper.wrapDataSource(dataSource); + } + + @Bean("jdbcTemplateAccount") + public JdbcTemplate jdbcTemplate(@Qualifier("dataSourceAccount") DataSource dataSource) { + return new JdbcTemplate(dataSource); + } + + @Bean("jdbcTemplateAudit") + public JdbcTemplate jdbcTemplateAudit(@Qualifier("dataSourceAudit") DataSource dataSource) { + return new JdbcTemplate(dataSource); + } +} diff --git a/jta/src/main/java/com/baeldung/jtademo/dto/TransferLog.java b/jta/src/main/java/com/baeldung/jtademo/dto/TransferLog.java new file mode 100644 index 0000000000..cc1474ce81 --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/dto/TransferLog.java @@ -0,0 +1,27 @@ +package com.baeldung.jtademo.dto; + +import java.math.BigDecimal; + +public class TransferLog { + private String fromAccountId; + private String toAccountId; + private BigDecimal amount; + + public TransferLog(String fromAccountId, String toAccountId, BigDecimal amount) { + this.fromAccountId = fromAccountId; + this.toAccountId = toAccountId; + this.amount = amount; + } + + public String getFromAccountId() { + return fromAccountId; + } + + public String getToAccountId() { + return toAccountId; + } + + public BigDecimal getAmount() { + return amount; + } +} diff --git a/jta/src/main/java/com/baeldung/jtademo/services/AuditService.java b/jta/src/main/java/com/baeldung/jtademo/services/AuditService.java new file mode 100644 index 0000000000..f6810e15c8 --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/services/AuditService.java @@ -0,0 +1,33 @@ +package com.baeldung.jtademo.services; + +import com.baeldung.jtademo.dto.TransferLog; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.ResultSetExtractor; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; + +@Service +public class AuditService { + + final JdbcTemplate jdbcTemplate; + + @Autowired + public AuditService(@Qualifier("jdbcTemplateAudit") JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public void log(String fromAccount, String toAccount, BigDecimal amount) { + jdbcTemplate.update("insert into AUDIT_LOG(FROM_ACCOUNT, TO_ACCOUNT, AMOUNT) values ?,?,?", fromAccount, toAccount, amount); + } + + public TransferLog lastTransferLog() { + return jdbcTemplate.query("select FROM_ACCOUNT,TO_ACCOUNT,AMOUNT from AUDIT_LOG order by ID desc", (ResultSetExtractor) (rs) -> { + if (!rs.next()) + return null; + return new TransferLog(rs.getString(1), rs.getString(2), BigDecimal.valueOf(rs.getDouble(3))); + }); + } +} diff --git a/jta/src/main/java/com/baeldung/jtademo/services/BankAccountService.java b/jta/src/main/java/com/baeldung/jtademo/services/BankAccountService.java new file mode 100644 index 0000000000..0c881edbaa --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/services/BankAccountService.java @@ -0,0 +1,32 @@ +package com.baeldung.jtademo.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.ResultSetExtractor; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; + +@Service +public class BankAccountService { + + final JdbcTemplate jdbcTemplate; + + @Autowired + public BankAccountService(@Qualifier("jdbcTemplateAccount") JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public void transfer(String fromAccountId, String toAccountId, BigDecimal amount) { + jdbcTemplate.update("update ACCOUNT set BALANCE=BALANCE-? where ID=?", amount, fromAccountId); + jdbcTemplate.update("update ACCOUNT set BALANCE=BALANCE+? where ID=?", amount, toAccountId); + } + + public BigDecimal balanceOf(String accountId) { + return jdbcTemplate.query("select BALANCE from ACCOUNT where ID=?", new Object[] { accountId }, (ResultSetExtractor) (rs) -> { + rs.next(); + return new BigDecimal(rs.getDouble(1)); + }); + } +} diff --git a/jta/src/main/java/com/baeldung/jtademo/services/TellerService.java b/jta/src/main/java/com/baeldung/jtademo/services/TellerService.java new file mode 100644 index 0000000000..d3bd80a2ee --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/services/TellerService.java @@ -0,0 +1,45 @@ +package com.baeldung.jtademo.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; +import javax.transaction.UserTransaction; +import java.math.BigDecimal; + +@Service +public class TellerService { + private final BankAccountService bankAccountService; + private final AuditService auditService; + private final UserTransaction userTransaction; + + @Autowired + public TellerService(BankAccountService bankAccountService, AuditService auditService, UserTransaction userTransaction) { + this.bankAccountService = bankAccountService; + this.auditService = auditService; + this.userTransaction = userTransaction; + } + + @Transactional + public void executeTransfer(String fromAccontId, String toAccountId, BigDecimal amount) { + bankAccountService.transfer(fromAccontId, toAccountId, amount); + auditService.log(fromAccontId, toAccountId, amount); + BigDecimal balance = bankAccountService.balanceOf(fromAccontId); + if (balance.compareTo(BigDecimal.ZERO) <= 0) { + throw new RuntimeException("Insufficient fund."); + } + } + + public void executeTransferProgrammaticTx(String fromAccontId, String toAccountId, BigDecimal amount) throws Exception { + userTransaction.begin(); + bankAccountService.transfer(fromAccontId, toAccountId, amount); + auditService.log(fromAccontId, toAccountId, amount); + BigDecimal balance = bankAccountService.balanceOf(fromAccontId); + if (balance.compareTo(BigDecimal.ZERO) <= 0) { + userTransaction.rollback(); + throw new RuntimeException("Insufficient fund."); + } else { + userTransaction.commit(); + } + } +} diff --git a/jta/src/main/java/com/baeldung/jtademo/services/TestHelper.java b/jta/src/main/java/com/baeldung/jtademo/services/TestHelper.java new file mode 100644 index 0000000000..c1e8e355ec --- /dev/null +++ b/jta/src/main/java/com/baeldung/jtademo/services/TestHelper.java @@ -0,0 +1,43 @@ +package com.baeldung.jtademo.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.Resource; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.init.ScriptUtils; +import org.springframework.stereotype.Component; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +@Component +public class TestHelper { + final JdbcTemplate jdbcTemplateAccount; + + final JdbcTemplate jdbcTemplateAudit; + + @Autowired + public TestHelper(@Qualifier("jdbcTemplateAccount") JdbcTemplate jdbcTemplateAccount, @Qualifier("jdbcTemplateAudit") JdbcTemplate jdbcTemplateAudit) { + this.jdbcTemplateAccount = jdbcTemplateAccount; + this.jdbcTemplateAudit = jdbcTemplateAudit; + } + + public void runAccountDbInit() throws SQLException { + runScript("account.sql", jdbcTemplateAccount.getDataSource()); + } + + public void runAuditDbInit() throws SQLException { + runScript("audit.sql", jdbcTemplateAudit.getDataSource()); + } + + private void runScript(String scriptName, DataSource dataSouorce) throws SQLException { + DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); + Resource script = resourceLoader.getResource(scriptName); + try (Connection con = dataSouorce.getConnection()) { + ScriptUtils.executeSqlScript(con, script); + } + } + +} diff --git a/jta/src/main/resources/account.sql b/jta/src/main/resources/account.sql new file mode 100644 index 0000000000..af14f89b01 --- /dev/null +++ b/jta/src/main/resources/account.sql @@ -0,0 +1,9 @@ +DROP SCHEMA PUBLIC CASCADE; + +create table ACCOUNT ( +ID char(8) PRIMARY KEY, +BALANCE NUMERIC(28,10) +); + +insert into ACCOUNT(ID, BALANCE) values ('a0000001', 1000); +insert into ACCOUNT(ID, BALANCE) values ('a0000002', 2000); \ No newline at end of file diff --git a/spring-boot-logging-log4j2/src/main/resources/application.properties b/jta/src/main/resources/application.properties similarity index 100% rename from spring-boot-logging-log4j2/src/main/resources/application.properties rename to jta/src/main/resources/application.properties diff --git a/jta/src/main/resources/audit.sql b/jta/src/main/resources/audit.sql new file mode 100644 index 0000000000..aa5845f402 --- /dev/null +++ b/jta/src/main/resources/audit.sql @@ -0,0 +1,8 @@ +DROP SCHEMA PUBLIC CASCADE; + +create table AUDIT_LOG ( +ID INTEGER IDENTITY PRIMARY KEY, +FROM_ACCOUNT varchar(8), +TO_ACCOUNT varchar(8), +AMOUNT numeric(28,10) +); \ No newline at end of file diff --git a/jta/src/test/java/com/baeldung/jtademo/JtaDemoUnitTest.java b/jta/src/test/java/com/baeldung/jtademo/JtaDemoUnitTest.java new file mode 100644 index 0000000000..3f6004262b --- /dev/null +++ b/jta/src/test/java/com/baeldung/jtademo/JtaDemoUnitTest.java @@ -0,0 +1,91 @@ +package com.baeldung.jtademo; + +import com.baeldung.jtademo.dto.TransferLog; +import com.baeldung.jtademo.services.AuditService; +import com.baeldung.jtademo.services.BankAccountService; +import com.baeldung.jtademo.services.TellerService; +import com.baeldung.jtademo.services.TestHelper; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.math.BigDecimal; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = JtaDemoApplication.class) +public class JtaDemoUnitTest { + @Autowired + TestHelper testHelper; + + @Autowired + TellerService tellerService; + + @Autowired + BankAccountService accountService; + + @Autowired + AuditService auditService; + + @Before + public void beforeTest() throws Exception { + testHelper.runAuditDbInit(); + testHelper.runAccountDbInit(); + } + + @Test + public void givenAnnotationTx_whenNoException_thenAllCommitted() throws Exception { + tellerService.executeTransfer("a0000001", "a0000002", BigDecimal.valueOf(500)); + + assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500)); + assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500)); + + TransferLog lastTransferLog = auditService.lastTransferLog(); + assertThat(lastTransferLog).isNotNull(); + assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001"); + assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002"); + assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500)); + } + + @Test + public void givenAnnotationTx_whenException_thenAllRolledBack() throws Exception { + assertThatThrownBy(() -> { + tellerService.executeTransfer("a0000002", "a0000001", BigDecimal.valueOf(100000)); + }).hasMessage("Insufficient fund."); + + assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000)); + assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000)); + assertThat(auditService.lastTransferLog()).isNull(); + } + + @Test + public void givenProgrammaticTx_whenCommit_thenAllCommitted() throws Exception { + tellerService.executeTransferProgrammaticTx("a0000001", "a0000002", BigDecimal.valueOf(500)); + + BigDecimal result = accountService.balanceOf("a0000001"); + assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500)); + assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500)); + + TransferLog lastTransferLog = auditService.lastTransferLog(); + assertThat(lastTransferLog).isNotNull(); + assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001"); + assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002"); + assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500)); + } + + @Test + public void givenProgrammaticTx_whenRollback_thenAllRolledBack() throws Exception { + assertThatThrownBy(() -> { + tellerService.executeTransferProgrammaticTx("a0000002", "a0000001", BigDecimal.valueOf(100000)); + }).hasMessage("Insufficient fund."); + + assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000)); + assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000)); + assertThat(auditService.lastTransferLog()).isNull(); + } +} diff --git a/jws/pom.xml b/jws/pom.xml index 01d9820b42..1970ab9921 100644 --- a/jws/pom.xml +++ b/jws/pom.xml @@ -13,20 +13,23 @@ 1.0.0-SNAPSHOT + + + OpenNMS Repository + http://repo.opennms.org/maven2/ + + + - javax.jnlp + javax.samples.jnlp jnlp-servlet ${jnlp-servlet.version} - system - ${project.basedir}/java-core-samples-lib/jnlp-servlet.jar - javax.jnlp - jardiff - ${jardiff.version} - system - ${project.basedir}/java-core-samples-lib/jardiff.jar + javax.samples.jnlp + jnlp-jardiff + ${jnlp-jardiff.version} @@ -81,8 +84,8 @@ 3.0.2 3.0.0 - 7.0 - 7.0 + 1.6.0 + 1.6.0 diff --git a/core-kotlin/kotlin-ktor/.gitignore b/kotlin-libraries/.gitignore similarity index 100% rename from core-kotlin/kotlin-ktor/.gitignore rename to kotlin-libraries/.gitignore diff --git a/kotlin-libraries/README.md b/kotlin-libraries/README.md new file mode 100644 index 0000000000..ce30c71792 --- /dev/null +++ b/kotlin-libraries/README.md @@ -0,0 +1,10 @@ +## Relevant articles: + +- [Kotlin with Mockito](http://www.baeldung.com/kotlin-mockito) +- [HTTP Requests with Kotlin and khttp](http://www.baeldung.com/kotlin-khttp) +- [Kotlin Dependency Injection with Kodein](http://www.baeldung.com/kotlin-kodein-dependency-injection) +- [Writing Specifications with Kotlin and Spek](http://www.baeldung.com/kotlin-spek) +- [Processing JSON with Kotlin and Klaxson](http://www.baeldung.com/kotlin-json-klaxson) +- [Kotlin with Ktor](http://www.baeldung.com/kotlin-ktor) +- [Idiomatic Logging in Kotlin](http://www.baeldung.com/kotlin-logging) +- [Guide to the Kotlin Exposed Framework](https://www.baeldung.com/kotlin-exposed-persistence) \ No newline at end of file diff --git a/core-kotlin/kotlin-ktor/build.gradle b/kotlin-libraries/build.gradle old mode 100755 new mode 100644 similarity index 83% rename from core-kotlin/kotlin-ktor/build.gradle rename to kotlin-libraries/build.gradle index 5c8f523cf1..b244df34b0 --- a/core-kotlin/kotlin-ktor/build.gradle +++ b/kotlin-libraries/build.gradle @@ -1,47 +1,57 @@ - - -group 'com.baeldung.ktor' -version '1.0-SNAPSHOT' - - -buildscript { - ext.kotlin_version = '1.2.41' - ext.ktor_version = '0.9.2' - - repositories { - mavenCentral() - } - dependencies { - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - } -} - -apply plugin: 'java' -apply plugin: 'kotlin' -apply plugin: 'application' - -mainClassName = 'APIServer.kt' - -sourceCompatibility = 1.8 -compileKotlin { kotlinOptions.jvmTarget = "1.8" } -compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } - -kotlin { experimental { coroutines "enable" } } - -repositories { - mavenCentral() - jcenter() - maven { url "https://dl.bintray.com/kotlin/ktor" } -} - -dependencies { - compile "io.ktor:ktor-server-netty:$ktor_version" - compile "ch.qos.logback:logback-classic:1.2.1" - compile "io.ktor:ktor-gson:$ktor_version" - testCompile group: 'junit', name: 'junit', version: '4.12' - -} -task runServer(type: JavaExec) { - main = 'APIServer' - classpath = sourceSets.main.runtimeClasspath + + +group 'com.baeldung.ktor' +version '1.0-SNAPSHOT' + + +buildscript { + ext.kotlin_version = '1.2.41' + ext.ktor_version = '0.9.2' + + repositories { + mavenCentral() + } + dependencies { + + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'java' +apply plugin: 'kotlin' +apply plugin: 'application' + +mainClassName = 'APIServer.kt' + +sourceCompatibility = 1.8 +compileKotlin { kotlinOptions.jvmTarget = "1.8" } +compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } + +kotlin { experimental { coroutines "enable" } } + +repositories { + mavenCentral() + jcenter() + maven { url "https://dl.bintray.com/kotlin/ktor" } +} +sourceSets { + main{ + kotlin{ + srcDirs 'com/baeldung/ktor' + } + } + +} + +dependencies { + compile "io.ktor:ktor-server-netty:$ktor_version" + compile "ch.qos.logback:logback-classic:1.2.1" + compile "io.ktor:ktor-gson:$ktor_version" + testCompile group: 'junit', name: 'junit', version: '4.12' + implementation 'com.beust:klaxon:3.0.1' + +} +task runServer(type: JavaExec) { + main = 'APIServer' + classpath = sourceSets.main.runtimeClasspath } \ No newline at end of file diff --git a/core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.jar b/kotlin-libraries/gradle/wrapper/gradle-wrapper.jar old mode 100755 new mode 100644 similarity index 100% rename from core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.jar rename to kotlin-libraries/gradle/wrapper/gradle-wrapper.jar diff --git a/core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.properties b/kotlin-libraries/gradle/wrapper/gradle-wrapper.properties old mode 100755 new mode 100644 similarity index 97% rename from core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.properties rename to kotlin-libraries/gradle/wrapper/gradle-wrapper.properties index 0b83b5a3e3..933b6473ce --- a/core-kotlin/kotlin-ktor/gradle/wrapper/gradle-wrapper.properties +++ b/kotlin-libraries/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip diff --git a/core-kotlin/kotlin-ktor/gradlew b/kotlin-libraries/gradlew old mode 100755 new mode 100644 similarity index 100% rename from core-kotlin/kotlin-ktor/gradlew rename to kotlin-libraries/gradlew diff --git a/core-kotlin/kotlin-ktor/gradlew.bat b/kotlin-libraries/gradlew.bat old mode 100755 new mode 100644 similarity index 96% rename from core-kotlin/kotlin-ktor/gradlew.bat rename to kotlin-libraries/gradlew.bat index e95643d6a2..f9553162f1 --- a/core-kotlin/kotlin-ktor/gradlew.bat +++ b/kotlin-libraries/gradlew.bat @@ -1,84 +1,84 @@ -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/kotlin-libraries/pom.xml b/kotlin-libraries/pom.xml new file mode 100644 index 0000000000..c5b7fed951 --- /dev/null +++ b/kotlin-libraries/pom.xml @@ -0,0 +1,105 @@ + + + 4.0.0 + kotlin-libraries + jar + + + com.baeldung + parent-kotlin + 1.0.0-SNAPSHOT + ../parent-kotlin + + + + + exposed + exposed + https://dl.bintray.com/kotlin/exposed + + + + + + org.jetbrains.spek + spek-api + 1.1.5 + test + + + org.jetbrains.spek + spek-subject-extension + 1.1.5 + test + + + org.jetbrains.spek + spek-junit-platform-engine + 1.1.5 + test + + + org.apache.commons + commons-math3 + ${commons-math3.version} + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + khttp + khttp + ${khttp.version} + + + com.nhaarman + mockito-kotlin + ${mockito-kotlin.version} + test + + + com.github.salomonbrys.kodein + kodein + ${kodein.version} + + + org.assertj + assertj-core + ${assertj.version} + test + + + com.beust + klaxon + ${klaxon.version} + + + org.jetbrains.exposed + exposed + ${exposed.version} + + + com.h2database + h2 + ${h2database.version} + + + + + 1.5.0 + 4.1.0 + 3.0.4 + 0.1.0 + 3.6.1 + 1.1.1 + 5.2.0 + 3.10.0 + 1.4.197 + 0.10.4 + + + \ No newline at end of file diff --git a/core-kotlin/kotlin-ktor/resources/logback.xml b/kotlin-libraries/resources/logback.xml old mode 100755 new mode 100644 similarity index 96% rename from core-kotlin/kotlin-ktor/resources/logback.xml rename to kotlin-libraries/resources/logback.xml index 274cdcdb02..9452207268 --- a/core-kotlin/kotlin-ktor/resources/logback.xml +++ b/kotlin-libraries/resources/logback.xml @@ -1,11 +1,11 @@ - - - - %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - + + + + %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + \ No newline at end of file diff --git a/core-kotlin/kotlin-ktor/settings.gradle b/kotlin-libraries/settings.gradle old mode 100755 new mode 100644 similarity index 94% rename from core-kotlin/kotlin-ktor/settings.gradle rename to kotlin-libraries/settings.gradle index 13bbce9583..c91c993971 --- a/core-kotlin/kotlin-ktor/settings.gradle +++ b/kotlin-libraries/settings.gradle @@ -1,2 +1,2 @@ -rootProject.name = 'KtorWithKotlin' - +rootProject.name = 'KtorWithKotlin' + diff --git a/core-kotlin/src/main/kotlin/com/baeldung/klaxon/CustomProduct.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/CustomProduct.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/klaxon/CustomProduct.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/CustomProduct.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/klaxon/Product.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/Product.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/klaxon/Product.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/Product.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/klaxon/ProductData.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/ProductData.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/klaxon/ProductData.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/klaxon/ProductData.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Controller.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Controller.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Controller.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Controller.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Dao.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Dao.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Dao.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Dao.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/JdbcDao.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/JdbcDao.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/JdbcDao.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/JdbcDao.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/MongoDao.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/MongoDao.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/MongoDao.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/MongoDao.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Service.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Service.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/kodein/Service.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/kodein/Service.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/mockito/BookService.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/mockito/BookService.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/mockito/BookService.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/mockito/BookService.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/kotlin/mockito/LendBookManager.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/mockito/LendBookManager.kt similarity index 100% rename from core-kotlin/src/main/kotlin/com/baeldung/kotlin/mockito/LendBookManager.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/mockito/LendBookManager.kt diff --git a/core-kotlin/kotlin-ktor/src/main/kotlin/APIServer.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/ktor/APIServer.kt similarity index 100% rename from core-kotlin/kotlin-ktor/src/main/kotlin/APIServer.kt rename to kotlin-libraries/src/main/kotlin/com/baeldung/ktor/APIServer.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/klaxon/KlaxonUnitTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/klaxon/KlaxonUnitTest.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/klaxon/KlaxonUnitTest.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/klaxon/KlaxonUnitTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt diff --git a/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt new file mode 100644 index 0000000000..1b61c05887 --- /dev/null +++ b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt @@ -0,0 +1,17 @@ +package com.baeldung.kotlin.junit5 + +class Calculator { + fun add(a: Int, b: Int) = a + b + + fun divide(a: Int, b: Int) = if (b == 0) { + throw DivideByZeroException(a) + } else { + a / b + } + + fun square(a: Int) = a * a + + fun squareRoot(a: Int) = Math.sqrt(a.toDouble()) + + fun log(base: Int, value: Int) = Math.log(value.toDouble()) / Math.log(base.toDouble()) +} diff --git a/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt new file mode 100644 index 0000000000..60bc4e2944 --- /dev/null +++ b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt @@ -0,0 +1,3 @@ +package com.baeldung.kotlin.junit5 + +class DivideByZeroException(val numerator: Int) : Exception() diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpLiveTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpLiveTest.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpLiveTest.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpLiveTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/kodein/KodeinUnitTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/kodein/KodeinUnitTest.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/kodein/KodeinUnitTest.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/kodein/KodeinUnitTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTest.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTest.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTestMockitoKotlin.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTestMockitoKotlin.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTestMockitoKotlin.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/mockito/LendBookManagerTestMockitoKotlin.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt similarity index 100% rename from core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt rename to kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt diff --git a/libraries-data/README.md b/libraries-data/README.md index 32d34f63be..652ae0e04c 100644 --- a/libraries-data/README.md +++ b/libraries-data/README.md @@ -10,4 +10,4 @@ - [A Guide to Apache Ignite](http://www.baeldung.com/apache-ignite) - [Apache Ignite with Spring Data](http://www.baeldung.com/apache-ignite-spring-data) - [Guide to JMapper](https://www.baeldung.com/jmapper) -- [A Guide to Apache Crunch](https://www.baeldung.com/crunch) +- [A Guide to Apache Crunch](https://www.baeldung.com/apache-crunch) diff --git a/libraries-server/README.md b/libraries-server/README.md index c25e9a10ab..28f963ade8 100644 --- a/libraries-server/README.md +++ b/libraries-server/README.md @@ -6,4 +6,4 @@ - [Programatically Create, Configure, and Run a Tomcat Server](http://www.baeldung.com/tomcat-programmatic-setup) - [Creating and Configuring Jetty 9 Server in Java](http://www.baeldung.com/jetty-java-programmatic) - [Testing Netty with EmbeddedChannel](http://www.baeldung.com/testing-netty-embedded-channel) - +- [MQTT Client in Java](https://www.baeldung.com/java-mqtt-client) diff --git a/libraries/pom.xml b/libraries/pom.xml index c8135d8d2a..1a88acfb7b 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -716,11 +716,6 @@ resilience4j-timelimiter ${resilience4j.version} - - org.apache.commons - commons-math3 - ${common-math3-version} - org.knowm.xchart xchart @@ -782,19 +777,6 @@ - - org.apache.maven.plugins - maven-dependency-plugin - - - org.apache.felix - maven-bundle-plugin - ${maven-bundle-plugin.version} - maven-plugin - - - true - maven-failsafe-plugin ${maven-failsafe-plugin.version} @@ -964,7 +946,6 @@ 2.1.2 2.5.11 0.12.1 - 3.6.1 3.5.2 3.6 2.7.1 diff --git a/logging-modules/log4j/README.md b/logging-modules/log4j/README.md index 8aae1b5826..9f4184ccba 100644 --- a/logging-modules/log4j/README.md +++ b/logging-modules/log4j/README.md @@ -4,3 +4,4 @@ - [Generate equals() and hashCode() with Eclipse](http://www.baeldung.com/java-eclipse-equals-and-hashcode) - [Introduction to SLF4J](http://www.baeldung.com/slf4j-with-log4j2-logback) - [A Guide to Rolling File Appenders](http://www.baeldung.com/java-logging-rolling-file-appenders) +- [Logging Exceptions Using SLF4J](https://www.baeldung.com/slf4j-log-exceptions) diff --git a/logging-modules/log4j2/README.md b/logging-modules/log4j2/README.md index e209c6f035..2cf6f9768f 100644 --- a/logging-modules/log4j2/README.md +++ b/logging-modules/log4j2/README.md @@ -3,3 +3,4 @@ - [Intro to Log4j2 – Appenders, Layouts and Filters](http://www.baeldung.com/log4j2-appenders-layouts-filters) - [Log4j 2 and Lambda Expressions](http://www.baeldung.com/log4j-2-lazy-logging) - [Programmatic Configuration with Log4j 2](http://www.baeldung.com/log4j2-programmatic-config) +- [Creating a Custom Log4j2 Appender](https://www.baeldung.com/log4j2-custom-appender) diff --git a/lombok/src/main/java/com/baeldung/lombok/builder/Child.java b/lombok/src/main/java/com/baeldung/lombok/builder/Child.java new file mode 100644 index 0000000000..70f6d9c46e --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/builder/Child.java @@ -0,0 +1,19 @@ +package com.baeldung.lombok.builder; + +import lombok.Builder; +import lombok.Getter; + +@Getter +public class Child extends Parent { + + private final String childName; + private final int childAge; + + @Builder(builderMethodName = "childBuilder") + public Child(String parentName, int parentAge, String childName, int childAge) { + super(parentName, parentAge); + this.childName = childName; + this.childAge = childAge; + } + +} diff --git a/lombok/src/main/java/com/baeldung/lombok/builder/Parent.java b/lombok/src/main/java/com/baeldung/lombok/builder/Parent.java new file mode 100644 index 0000000000..0cf76d4b00 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/builder/Parent.java @@ -0,0 +1,11 @@ +package com.baeldung.lombok.builder; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class Parent { + private final String parentName; + private final int parentAge; +} \ No newline at end of file diff --git a/lombok/src/main/java/com/baeldung/lombok/getter/GetterBoolean.java b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBoolean.java new file mode 100644 index 0000000000..2191396e5d --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBoolean.java @@ -0,0 +1,15 @@ +package com.baeldung.lombok.getter; + + +import lombok.Getter; + +/** + * Related Article Sections: + * 4. Using @Getter on a Boolean Field + * + */ +public class GetterBoolean { + + @Getter + private Boolean running = true; +} diff --git a/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitive.java b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitive.java new file mode 100644 index 0000000000..5601f85b8b --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitive.java @@ -0,0 +1,16 @@ +package com.baeldung.lombok.getter; + + +import lombok.Getter; + +/** + * Related Article Sections: + * 3. Using @Getter on a boolean Field + * + */ +public class GetterBooleanPrimitive { + + @Getter + private boolean running; + +} diff --git a/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitiveSameAccessor.java b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitiveSameAccessor.java new file mode 100644 index 0000000000..af29a33c20 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitiveSameAccessor.java @@ -0,0 +1,18 @@ +package com.baeldung.lombok.getter; + + +import lombok.Getter; + +/** + * Related Article Sections: + * 3.2. Two boolean Fields With the Same Accessor Name + * + */ +public class GetterBooleanPrimitiveSameAccessor { + + @Getter + boolean running = true; + + @Getter + boolean isRunning = false; +} diff --git a/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanSameAccessor.java b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanSameAccessor.java new file mode 100644 index 0000000000..d972273b71 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanSameAccessor.java @@ -0,0 +1,13 @@ +package com.baeldung.lombok.getter; + +import lombok.Getter; + +/** + * Related Article Sections: + * 3.1. A boolean Field Having the Same Name With Its Accessor + * + */ +public class GetterBooleanSameAccessor { + @Getter + private boolean isRunning = true; +} diff --git a/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanType.java b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanType.java new file mode 100644 index 0000000000..0d3b9a928a --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/getter/GetterBooleanType.java @@ -0,0 +1,15 @@ +package com.baeldung.lombok.getter; + + +import lombok.Getter; + +/** + * Related Article Sections: + * 4. Using @Getter on a Boolean Field + * + */ +public class GetterBooleanType { + + @Getter + private Boolean running = true; +} diff --git a/lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java b/lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java index acad7d6c04..56a380569d 100644 --- a/lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java +++ b/lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java @@ -1,42 +1,59 @@ package com.baeldung.lombok.builder; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.*; - -public class BuilderUnitTest -{ +public class BuilderUnitTest { @Test public void givenBuilder_WidgetIsBuilt() { - Widget testWidget = Widget.builder().name("foo").id(1).build(); - assertThat(testWidget.getName()) - .isEqualTo("foo"); - assertThat(testWidget.getId()) - .isEqualTo(1); + Widget testWidget = Widget.builder() + .name("foo") + .id(1) + .build(); + assertThat(testWidget.getName()).isEqualTo("foo"); + assertThat(testWidget.getId()).isEqualTo(1); } @Test public void givenToBuilder_whenToBuilder_BuilderIsCreated() { - Widget testWidget = Widget.builder().name("foo").id(1).build(); + Widget testWidget = Widget.builder() + .name("foo") + .id(1) + .build(); Widget.WidgetBuilder widgetBuilder = testWidget.toBuilder(); - Widget newWidget = widgetBuilder.id(2).build(); - assertThat(newWidget.getName()) - .isEqualTo("foo"); - assertThat(newWidget.getId()) - .isEqualTo(2); + Widget newWidget = widgetBuilder.id(2) + .build(); + assertThat(newWidget.getName()).isEqualTo("foo"); + assertThat(newWidget.getId()).isEqualTo(2); } - - @Test public void givenBuilderMethod_ClientIsBuilt() { - ImmutableClient testImmutableClient = ClientBuilder.builder().name("foo").id(1).build(); - assertThat(testImmutableClient.getName()) - .isEqualTo("foo"); - assertThat(testImmutableClient.getId()) - .isEqualTo(1); + ImmutableClient testImmutableClient = ClientBuilder.builder() + .name("foo") + .id(1) + .build(); + assertThat(testImmutableClient.getName()).isEqualTo("foo"); + assertThat(testImmutableClient.getId()).isEqualTo(1); } + + @Test + public void givenBuilderAtMethodLevel_ChildInheritingParentIsBuilt() { + Child child = Child.childBuilder() + .parentName("Andrea") + .parentAge(38) + .childName("Emma") + .childAge(6) + .build(); + + assertThat(child.getChildName()).isEqualTo("Emma"); + assertThat(child.getChildAge()).isEqualTo(6); + assertThat(child.getParentName()).isEqualTo("Andrea"); + assertThat(child.getParentAge()).isEqualTo(38); + } + } diff --git a/lombok/src/test/java/com/baeldung/lombok/getter/GetterBooleanUnitTest.java b/lombok/src/test/java/com/baeldung/lombok/getter/GetterBooleanUnitTest.java new file mode 100644 index 0000000000..632594d575 --- /dev/null +++ b/lombok/src/test/java/com/baeldung/lombok/getter/GetterBooleanUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.lombok.getter; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class GetterBooleanUnitTest { + + @Test + public void whenBasicBooleanField_thenMethodNamePrefixedWithIsFollowedByFieldName() { + GetterBooleanPrimitive lombokExamples = new GetterBooleanPrimitive(); + assertFalse(lombokExamples.isRunning()); + } + + @Test + public void whenBooleanFieldPrefixedWithIs_thenMethodNameIsSameAsFieldName() { + GetterBooleanSameAccessor lombokExamples = new GetterBooleanSameAccessor(); + assertTrue(lombokExamples.isRunning()); + } + + @Test + public void whenTwoBooleanFieldsCauseNamingConflict_thenLombokMapsToFirstDeclaredField() { + GetterBooleanPrimitiveSameAccessor lombokExamples = new GetterBooleanPrimitiveSameAccessor(); + assertTrue(lombokExamples.isRunning() == lombokExamples.running); + assertFalse(lombokExamples.isRunning() == lombokExamples.isRunning); + } + + @Test + public void whenFieldOfBooleanType_thenLombokPrefixesMethodWithGetInsteadOfIs() { + GetterBooleanType lombokExamples = new GetterBooleanType(); + assertTrue(lombokExamples.getRunning()); + } +} diff --git a/lucene/README.md b/lucene/README.md index b1d33472f4..ea7f715480 100644 --- a/lucene/README.md +++ b/lucene/README.md @@ -2,3 +2,4 @@ - [Introduction to Apache Lucene](http://www.baeldung.com/lucene) - [A Simple File Search with Lucene](http://www.baeldung.com/lucene-file-search) +- [Guide to Lucene Analyzers](https://www.baeldung.com/lucene-analyzers) diff --git a/maven/README.md b/maven/README.md index c5b875ce02..2d838bcb76 100644 --- a/maven/README.md +++ b/maven/README.md @@ -8,3 +8,5 @@ - [The Maven Verifier Plugin](http://www.baeldung.com/maven-verifier-plugin) - [The Maven Clean Plugin](http://www.baeldung.com/maven-clean-plugin) - [Build a Jar with Maven and Ignore the Test Results](http://www.baeldung.com/maven-ignore-test-results) +- [Maven Project with Multiple Source Directories](https://www.baeldung.com/maven-project-multiple-src-directories) +- [Integration Testing with Maven](https://www.baeldung.com/maven-integration-test) diff --git a/meecrowave/pom.xml b/meecrowave/pom.xml deleted file mode 100644 index cf13aa1c1b..0000000000 --- a/meecrowave/pom.xml +++ /dev/null @@ -1,57 +0,0 @@ - - 4.0.0 - com.baeldung - apache-meecrowave - 0.0.1 - apache-meecrowave - A sample REST API application with Meecrowave - - - 1.8 - 1.8 - - - - - org.apache.meecrowave - meecrowave-core - 1.2.1 - - - - org.apache.meecrowave - meecrowave-jpa - 1.2.1 - - - - com.squareup.okhttp3 - okhttp - 3.10.0 - - - org.apache.meecrowave - meecrowave-junit - 1.2.0 - test - - - - junit - junit - 4.10 - test - - - - - - - org.apache.meecrowave - meecrowave-maven-plugin - 1.2.1 - - - - \ No newline at end of file diff --git a/meecrowave/src/main/java/com/baeldung/meecrowave/Article.java b/meecrowave/src/main/java/com/baeldung/meecrowave/Article.java deleted file mode 100644 index 7925e8ff99..0000000000 --- a/meecrowave/src/main/java/com/baeldung/meecrowave/Article.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.meecrowave; - -public class Article { - private String name; - private String author; - - public Article() { - } - - public Article(String name, String author) { - this.author = author; - this.name = name; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAuthor() { - return author; - } - - public void setAuthor(String author) { - this.author = author; - } -} \ No newline at end of file diff --git a/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleEndpoints.java b/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleEndpoints.java deleted file mode 100644 index 6cb7012c64..0000000000 --- a/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleEndpoints.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.meecrowave; - -import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; - -@RequestScoped -@Path("article") -public class ArticleEndpoints { - - @Inject - ArticleService articleService; - - @GET - public Response getArticle() { - return Response.ok() - .entity(new Article("name", "author")) - .build(); - - } - - @POST - public Response createArticle(Article article) { - return Response.status(Status.CREATED) - .entity(articleService.createArticle(article)) - .build(); - } -} \ No newline at end of file diff --git a/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleService.java b/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleService.java deleted file mode 100644 index 7bd6b87345..0000000000 --- a/meecrowave/src/main/java/com/baeldung/meecrowave/ArticleService.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baeldung.meecrowave; - -import javax.enterprise.context.ApplicationScoped; - -@ApplicationScoped -public class ArticleService { - public Article createArticle(Article article) { - return article; - } -} diff --git a/meecrowave/src/main/java/com/baeldung/meecrowave/Server.java b/meecrowave/src/main/java/com/baeldung/meecrowave/Server.java deleted file mode 100644 index 2aa7d0556f..0000000000 --- a/meecrowave/src/main/java/com/baeldung/meecrowave/Server.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.meecrowave; - -import org.apache.meecrowave.Meecrowave; - -public class Server { - public static void main(String[] args) { - final Meecrowave.Builder builder = new Meecrowave.Builder(); - builder.setScanningPackageIncludes("com.baeldung.meecrowave"); - builder.setJaxrsMapping("/api/*"); - builder.setJsonpPrettify(true); - - try (Meecrowave meecrowave = new Meecrowave(builder)) { - meecrowave.bake().await(); - } - } -} diff --git a/meecrowave/src/test/java/com/baeldung/meecrowave/ArticleEndpointsTest.java b/meecrowave/src/test/java/com/baeldung/meecrowave/ArticleEndpointsTest.java deleted file mode 100644 index 0dc9773490..0000000000 --- a/meecrowave/src/test/java/com/baeldung/meecrowave/ArticleEndpointsTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.baeldung.meecrowave; - - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; - -import org.apache.meecrowave.Meecrowave; -import org.apache.meecrowave.junit.MonoMeecrowave; -import org.apache.meecrowave.testing.ConfigurationInject; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; - -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -@RunWith(MonoMeecrowave.Runner.class) -public class ArticleEndpointsTest { - - @ConfigurationInject - private Meecrowave.Builder config; - private static OkHttpClient client; - - @BeforeClass - public static void setup() { - client = new OkHttpClient(); - } - - @Test - public void whenRetunedArticle_thenCorrect() throws IOException { - final String base = "http://localhost:"+config.getHttpPort(); - - Request request = new Request.Builder() - .url(base+"/article") - .build(); - Response response = client.newCall(request).execute(); - assertEquals(200, response.code()); - } -} diff --git a/metrics/pom.xml b/metrics/pom.xml index 86f197240b..9cf1ec588f 100644 --- a/metrics/pom.xml +++ b/metrics/pom.xml @@ -84,6 +84,13 @@ spectator-api ${spectator-api.version} + + + org.assertj + assertj-core + test + + diff --git a/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java b/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java index e7f3d7ec72..689e23ad6d 100644 --- a/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java @@ -1,8 +1,12 @@ package com.baeldung.metrics.micrometer; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.within; +import static org.assertj.core.api.Assertions.withinPercentage; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.collection.IsMapContaining.hasEntry; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import io.micrometer.atlas.AtlasMeterRegistry; @@ -31,8 +35,10 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import org.assertj.core.data.Percentage; import org.junit.Before; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import com.netflix.spectator.atlas.AtlasConfig; @@ -156,7 +162,8 @@ public class MicrometerAtlasIntegrationTest { timer.record(30, TimeUnit.MILLISECONDS); assertTrue(2 == timer.count()); - assertTrue(50 > timer.totalTime(TimeUnit.MILLISECONDS) && 45 <= timer.totalTime(TimeUnit.MILLISECONDS)); + + assertThat(timer.totalTime(TimeUnit.MILLISECONDS)).isBetween(40.0, 55.0); } @Test @@ -173,7 +180,7 @@ public class MicrometerAtlasIntegrationTest { } long timeElapsed = longTaskTimer.stop(currentTaskId); - assertTrue(timeElapsed / (int) 1e6 == 2); + assertEquals(2L, timeElapsed/((int) 1e6),1L); } @Test diff --git a/metrics/src/test/java/com/baeldung/metrics/servo/AtlasObserverLiveTest.java b/metrics/src/test/java/com/baeldung/metrics/servo/AtlasObserverLiveTest.java index 134c3c91cf..b8a5b93e49 100644 --- a/metrics/src/test/java/com/baeldung/metrics/servo/AtlasObserverLiveTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/servo/AtlasObserverLiveTest.java @@ -27,6 +27,9 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.assertThat; +/** + * Atlas server needs to be up and running for this live test to work. + */ public class AtlasObserverLiveTest { private final String atlasUri = "http://localhost:7101/api/v1"; diff --git a/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeManualTest.java b/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeManualTest.java index d810de155a..92bbd615b9 100644 --- a/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeManualTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeManualTest.java @@ -109,20 +109,20 @@ public class MetricTypeManualTest { .build(), MILLISECONDS); Stopwatch stopwatch = timer.start(); - MILLISECONDS.sleep(1); - timer.record(2, MILLISECONDS); + SECONDS.sleep(1); + timer.record(2, SECONDS); stopwatch.stop(); - assertEquals("timer should count 1 millisecond", 1, timer + assertEquals("timer should count 1 second", 1000, timer .getValue() - .intValue()); - assertEquals("timer should count 3 millisecond in total", 3, timer.getTotalTime() - .intValue()); + .intValue(),1000); + assertEquals("timer should count 3 second in total", 3000, timer.getTotalTime() + .intValue(),1000); assertEquals("timer should record 2 updates", 2, timer .getCount() .intValue()); - assertEquals("timer should have max 2", 2, timer.getMax(), 0.01); + assertEquals("timer should have max 2", 2000, timer.getMax(), 0.01); } @Test @@ -161,7 +161,6 @@ public class MetricTypeManualTest { } @Test - //== public void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Exception { System.setProperty("netflix.servo", "1000"); StatsTimer timer = new StatsTimer(MonitorConfig @@ -178,21 +177,21 @@ public class MetricTypeManualTest { .build(), MILLISECONDS); Stopwatch stopwatch = timer.start(); - MILLISECONDS.sleep(1); - timer.record(3, MILLISECONDS); + SECONDS.sleep(1); + timer.record(3, SECONDS); stopwatch.stop(); stopwatch = timer.start(); - timer.record(6, MILLISECONDS); - MILLISECONDS.sleep(2); + timer.record(6, SECONDS); + SECONDS.sleep(2); stopwatch.stop(); - assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalTime()); - assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalMeasurement()); + assertEquals("timer should count 12 seconds in total", 12000, timer.getTotalTime(),500); + assertEquals("timer should count 12 seconds in total", 12000, timer.getTotalMeasurement(),500); assertEquals("timer should record 4 updates", 4, timer.getCount()); - assertEquals("stats timer value time-cost/update should be 2", 3, timer + assertEquals("stats timer value time-cost/update should be 2", 3000, timer .getValue() - .intValue()); + .intValue(),500); final Map metricMap = timer .getMonitors() @@ -235,4 +234,4 @@ public class MetricTypeManualTest { assertEquals("information collected", informational.getValue()); } -} +} \ No newline at end of file diff --git a/msf4j/pom.xml b/msf4j/pom.xml index cca8281764..9a6483772b 100644 --- a/msf4j/pom.xml +++ b/msf4j/pom.xml @@ -6,12 +6,22 @@ msf4j 0.0.1-SNAPSHOT - - org.wso2.msf4j - msf4j-service - 2.6.0 - + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + + + + org.wso2.msf4j + msf4j-service + ${msf4j.version} + pom + + + org.wso2.msf4j @@ -27,7 +37,7 @@ com.baeldung.msf4j.msf4jintro.Application - 2.6.1 + 2.6.3 \ No newline at end of file diff --git a/parent-boot-1/pom.xml b/parent-boot-1/pom.xml index c52327a42e..7742841d07 100644 --- a/parent-boot-1/pom.xml +++ b/parent-boot-1/pom.xml @@ -37,16 +37,17 @@ - - - org.springframework.boot - spring-boot-maven-plugin - 1.5.15.RELEASE - - + + + + org.springframework.boot + spring-boot-maven-plugin + 1.5.15.RELEASE + + + - 3.1.0 diff --git a/parent-boot-2/pom.xml b/parent-boot-2/pom.xml index 86a77a90db..de6cb5d068 100644 --- a/parent-boot-2/pom.xml +++ b/parent-boot-2/pom.xml @@ -36,13 +36,15 @@ - - - org.springframework.boot - spring-boot-maven-plugin - 2.0.4.RELEASE - - + + + + org.springframework.boot + spring-boot-maven-plugin + 2.0.4.RELEASE + + + diff --git a/patterns/design-patterns/README.md b/patterns/design-patterns/README.md index 5fb8f735ab..75f7cec73a 100644 --- a/patterns/design-patterns/README.md +++ b/patterns/design-patterns/README.md @@ -11,3 +11,4 @@ - [Visitor Design Pattern in Java](http://www.baeldung.com/java-visitor-pattern) - [The DAO Pattern in Java](http://www.baeldung.com/java-dao-pattern) - [Interpreter Design Pattern in Java](http://www.baeldung.com/java-interpreter-pattern) +- [State Design Pattern in Java](https://www.baeldung.com/java-state-design-pattern) diff --git a/persistence-modules/spring-data-dynamodb/pom.xml b/persistence-modules/spring-data-dynamodb/pom.xml index f3d794d001..4cb805131a 100644 --- a/persistence-modules/spring-data-dynamodb/pom.xml +++ b/persistence-modules/spring-data-dynamodb/pom.xml @@ -154,25 +154,6 @@ org.apache.maven.plugins maven-war-plugin - - org.apache.maven.plugins - maven-dependency-plugin - ${maven-dependency-plugin.version} - - - copy-dependencies - test-compile - - copy-dependencies - - - test - so,dll,dylib - ${project.basedir}/native-libs - - - - @@ -196,7 +177,6 @@ 1.11.106 1.11.86 https://s3-us-west-2.amazonaws.com/dynamodb-local/release - 2.10 diff --git a/persistence-modules/spring-data-eclipselink/pom.xml b/persistence-modules/spring-data-eclipselink/pom.xml index 8a55425fc2..618fa83ee5 100644 --- a/persistence-modules/spring-data-eclipselink/pom.xml +++ b/persistence-modules/spring-data-eclipselink/pom.xml @@ -2,9 +2,7 @@ 4.0.0 - com.baeldung spring-data-eclipselink - 1.0.0-SNAPSHOT spring-data-eclipselink @@ -20,7 +18,6 @@ spring-boot-dependencies ${spring.version} pom - import org.springframework.boot diff --git a/persistence-modules/spring-data-neo4j/pom.xml b/persistence-modules/spring-data-neo4j/pom.xml index bd7d783646..2be4df71be 100644 --- a/persistence-modules/spring-data-neo4j/pom.xml +++ b/persistence-modules/spring-data-neo4j/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - com.baeldung spring-data-neo4j 1.0 @@ -74,11 +73,6 @@ ${neo4j-ogm.version} test - - org.neo4j - neo4j-ogm-embedded-driver - ${neo4j-ogm.version} - org.neo4j.test neo4j-harness diff --git a/persistence-modules/spring-jpa/README.md b/persistence-modules/spring-jpa/README.md index 1c89f2453d..299a5a1e51 100644 --- a/persistence-modules/spring-jpa/README.md +++ b/persistence-modules/spring-jpa/README.md @@ -18,6 +18,7 @@ - [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types) - [Obtaining Auto-generated Keys in Spring JDBC](http://www.baeldung.com/spring-jdbc-autogenerated-keys) - [Transactions with Spring 4 and JPA](http://www.baeldung.com/transaction-configuration-with-jpa-and-spring) +- [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries) ### Eclipse Config After importing the project into Eclipse, you may see the following error: diff --git a/pom.xml b/pom.xml index be6029a946..7a7e2d7f64 100644 --- a/pom.xml +++ b/pom.xml @@ -352,6 +352,7 @@ java-streams core-java-persistence core-kotlin + kotlin-libraries core-groovy core-java-concurrency core-java-concurrency-collections @@ -360,6 +361,7 @@ dozer ethereum ejb + ejb/ejb-client feign flips testing-modules/gatling @@ -444,6 +446,7 @@ spark-java spring-4 spring-5 + spring-5-data-reactive spring-5-reactive spring-5-reactive-security spring-5-reactive-client @@ -462,6 +465,7 @@ spring-boot-keycloak spring-boot-bootstrap spring-boot-admin + spring-boot-camel spring-boot-ops spring-boot-persistence spring-boot-security @@ -552,6 +556,7 @@ spring-thymeleaf spring-userservice spring-zuul + spring-remoting spring-reactor spring-vertx spring-jinq @@ -575,6 +580,7 @@ testing-modules/mockserver testing-modules/test-containers undertow + vaadin vertx-and-rxjava saas deeplearning4j @@ -597,6 +603,7 @@ spring-reactive-kotlin jnosql spring-boot-angular-ecommerce + jta @@ -707,6 +714,7 @@ spark-java spring-4 spring-5 + spring-5-data-reactive spring-5-reactive spring-5-reactive-security spring-5-reactive-client @@ -731,6 +739,7 @@ spring-boot-keycloak spring-boot-bootstrap spring-boot-admin + spring-boot-camel spring-boot-ops spring-boot-persistence spring-boot-security @@ -954,6 +963,7 @@ json-path json jsoup + jta testing-modules/junit-5 testing-modules/junit5-migration jws @@ -994,6 +1004,7 @@ persistence-modules/solr spark-java spring-4 + spring-5-data-reactive spring-5-reactive spring-5-reactive-security spring-5-reactive-client @@ -1010,6 +1021,7 @@ spring-boot-keycloak spring-boot-bootstrap spring-boot-admin + spring-boot-camel spring-boot-persistence spring-boot-security spring-boot-mvc @@ -1094,8 +1106,10 @@ spring-thymeleaf spring-userservice spring-zuul + spring-remoting spring-reactor spring-vertx + spring-vault spring-jinq spring-rest-embedded-tomcat testing-modules/testing @@ -1117,6 +1131,7 @@ testing-modules/mockserver testing-modules/test-containers undertow + vaadin vertx-and-rxjava saas deeplearning4j @@ -1226,6 +1241,7 @@ spring-boot-ops spring-5 core-kotlin + kotlin-libraries core-java google-web-toolkit spring-security-mvc-custom @@ -1297,4 +1313,4 @@ 3.8 - \ No newline at end of file + diff --git a/ratpack/build.gradle b/ratpack/build.gradle index aeddd5f9f9..25af3ddb51 100644 --- a/ratpack/build.gradle +++ b/ratpack/build.gradle @@ -1,35 +1,35 @@ -buildscript { - repositories { - jcenter() - } - dependencies { - classpath "io.ratpack:ratpack-gradle:1.4.5" - classpath "com.h2database:h2:1.4.193" - } -} - -if (!JavaVersion.current().java8Compatible) { - throw new IllegalStateException("Must be built with Java 8 or higher") -} - -apply plugin: "io.ratpack.ratpack-java" -apply plugin: 'java' - -repositories { - jcenter() -} - -dependencies { - compile ratpack.dependency('hikari') - compile 'com.h2database:h2:1.4.193' - testCompile 'junit:junit:4.11' - runtime "org.slf4j:slf4j-simple:1.7.21" -} - -test { - testLogging { - events 'started', 'passed' - } -} - -mainClassName = "com.baeldung.Application" +buildscript { + repositories { + jcenter() + } + dependencies { + classpath "io.ratpack:ratpack-gradle:1.5.4" + classpath "com.h2database:h2:1.4.193" + } +} + +if (!JavaVersion.current().java8Compatible) { + throw new IllegalStateException("Must be built with Java 8 or higher") +} + +apply plugin: "io.ratpack.ratpack-java" +apply plugin: 'java' + +repositories { + jcenter() +} + +dependencies { + compile ratpack.dependency('hikari') + compile 'com.h2database:h2:1.4.193' + testCompile 'junit:junit:4.11' + runtime "org.slf4j:slf4j-simple:1.7.21" +} + +test { + testLogging { + events 'started', 'passed' + } +} + +mainClassName = "com.baeldung.Application" diff --git a/ratpack/pom.xml b/ratpack/pom.xml index bbde3d1cd7..7de11e6955 100644 --- a/ratpack/pom.xml +++ b/ratpack/pom.xml @@ -35,6 +35,17 @@ io.ratpack ratpack-hystrix ${ratpack.version} + + + com.netflix.hystrix + hystrix-core + + + + + com.netflix.hystrix + hystrix-core + ${hystrix.version} io.ratpack @@ -76,10 +87,11 @@ 1.8 1.8 - 1.4.6 + 1.5.4 4.5.3 4.4.6 1.4.193 + 1.5.12 diff --git a/ratpack/src/main/java/com/baeldung/Application.java b/ratpack/src/main/java/com/baeldung/Application.java index 7f46b241ea..4a391d04a1 100644 --- a/ratpack/src/main/java/com/baeldung/Application.java +++ b/ratpack/src/main/java/com/baeldung/Application.java @@ -1,49 +1,81 @@ -package com.baeldung; - -import java.util.ArrayList; -import java.util.List; - -import com.baeldung.filter.RequestValidatorFilter; -import com.baeldung.model.Employee; - -import ratpack.guice.Guice; -import ratpack.hikari.HikariModule; -import ratpack.http.MutableHeaders; -import ratpack.jackson.Jackson; -import ratpack.http.MutableHeaders; -import ratpack.server.RatpackServer; - -public class Application { - - public static void main(String[] args) throws Exception { - - List employees = new ArrayList(); - employees.add(new Employee(1L, "Mr", "John Doe")); - employees.add(new Employee(2L, "Mr", "White Snow")); - - - RatpackServer.start( - server -> server.registry(Guice.registry(bindings -> bindings.module(HikariModule.class, config -> { - config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); - config.addDataSourceProperty("URL", - "jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'"); - }))).handlers(chain -> chain - .all( - // ctx -> { - // MutableHeaders headers = - // ctx.getResponse().getHeaders(); - // headers.set("Access-Control-Allow-Origin","*"); - // headers.set("Accept-Language", "en-us"); - // headers.set("Accept-Charset", "UTF-8"); - // ctx.next(); - // } - new RequestValidatorFilter()) - .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!")) - .get("data/employees", ctx -> ctx.render(Jackson.json(employees))) - .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!")) - .post(":amount", ctx -> ctx - .render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!")))); - } - -} - +package com.baeldung; + +import com.baeldung.filter.RequestValidatorFilter; +import com.baeldung.handler.EmployeeHandler; +import com.baeldung.handler.RedirectHandler; +import com.baeldung.model.Employee; +import com.baeldung.repository.EmployeeRepository; +import com.baeldung.repository.EmployeeRepositoryImpl; +import com.zaxxer.hikari.HikariConfig; +import io.netty.buffer.PooledByteBufAllocator; +import ratpack.func.Action; +import ratpack.func.Function; +import ratpack.guice.BindingsSpec; +import ratpack.guice.Guice; +import ratpack.handling.Chain; +import ratpack.hikari.HikariModule; +import ratpack.http.client.HttpClient; +import ratpack.jackson.Jackson; +import ratpack.registry.Registry; +import ratpack.server.RatpackServer; +import ratpack.server.RatpackServerSpec; +import ratpack.server.ServerConfig; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.List; + +public class Application { + + public static void main(String[] args) throws Exception { + + final Action hikariConfigAction = hikariConfig -> { + hikariConfig.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); + hikariConfig.addDataSourceProperty("URL", "jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'"); + }; + + final Action bindingsSpecAction = bindings -> bindings.module(HikariModule.class, hikariConfigAction); + final HttpClient httpClient = HttpClient.of(httpClientSpec -> { + httpClientSpec.poolSize(10) + .connectTimeout(Duration.of(60, ChronoUnit.SECONDS)) + .maxContentLength(ServerConfig.DEFAULT_MAX_CONTENT_LENGTH) + .responseMaxChunkSize(16384) + .readTimeout(Duration.of(60, ChronoUnit.SECONDS)) + .byteBufAllocator(PooledByteBufAllocator.DEFAULT); + }); + final Function registryFunction = Guice.registry(bindingsSpecAction); + + final Action chainAction = chain -> chain.all(new RequestValidatorFilter()) + .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!")) + .get("data/employees", ctx -> ctx.render(Jackson.json(createEmpList()))) + .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens() + .get("name") + "!!!")) + .post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens() + .get("amount") + " added successfully !!!")); + + final Action routerChainAction = routerChain -> { + routerChain.path("redirect", new RedirectHandler()) + .prefix("employee", empChain -> { + empChain.get(":id", new EmployeeHandler()); + }); + }; + final Action ratpackServerSpecAction = serverSpec -> serverSpec.registry(registryFunction) + .registryOf(registrySpec -> { + registrySpec.add(EmployeeRepository.class, new EmployeeRepositoryImpl()); + registrySpec.add(HttpClient.class, httpClient); + }) + .handlers(chain -> chain.insert(routerChainAction) + .insert(chainAction)); + + RatpackServer.start(ratpackServerSpecAction); + } + + private static List createEmpList() { + List employees = new ArrayList<>(); + employees.add(new Employee(1L, "Mr", "John Doe")); + employees.add(new Employee(2L, "Mr", "White Snow")); + return employees; + } + +} diff --git a/ratpack/src/main/java/com/baeldung/handler/EmployeeHandler.java b/ratpack/src/main/java/com/baeldung/handler/EmployeeHandler.java new file mode 100644 index 0000000000..9a6b79259c --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/handler/EmployeeHandler.java @@ -0,0 +1,20 @@ +package com.baeldung.handler; + +import com.baeldung.repository.EmployeeRepository; +import com.baeldung.model.Employee; +import ratpack.exec.Promise; +import ratpack.handling.Context; +import ratpack.handling.Handler; + +public class EmployeeHandler implements Handler { + @Override + public void handle(Context ctx) throws Exception { + EmployeeRepository repository = ctx.get(EmployeeRepository.class); + Long id = Long.valueOf(ctx.getPathTokens() + .get("id")); + Promise employeePromise = repository.findEmployeeById(id); + employeePromise.map(employee -> employee.getName()) + .then(name -> ctx.getResponse() + .send(name)); + } +} diff --git a/ratpack/src/main/java/com/baeldung/handler/FooHandler.java b/ratpack/src/main/java/com/baeldung/handler/FooHandler.java new file mode 100644 index 0000000000..d2c4dbdce5 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/handler/FooHandler.java @@ -0,0 +1,12 @@ +package com.baeldung.handler; + +import ratpack.handling.Context; +import ratpack.handling.Handler; + +public class FooHandler implements Handler { + @Override + public void handle(Context ctx) throws Exception { + ctx.getResponse() + .send("Hello Foo!"); + } +} diff --git a/ratpack/src/main/java/com/baeldung/handler/RedirectHandler.java b/ratpack/src/main/java/com/baeldung/handler/RedirectHandler.java new file mode 100644 index 0000000000..c95543a961 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/handler/RedirectHandler.java @@ -0,0 +1,23 @@ +package com.baeldung.handler; + +import ratpack.exec.Promise; +import ratpack.handling.Context; +import ratpack.handling.Handler; +import ratpack.http.client.HttpClient; +import ratpack.http.client.ReceivedResponse; + +import java.net.URI; + +public class RedirectHandler implements Handler { + @Override + public void handle(Context ctx) throws Exception { + HttpClient client = ctx.get(HttpClient.class); + URI uri = URI.create("http://localhost:5050/employee/1"); + Promise responsePromise = client.get(uri); + responsePromise.map(response -> response.getBody() + .getText() + .toUpperCase()) + .then(responseText -> ctx.getResponse() + .send(responseText)); + } +} \ No newline at end of file diff --git a/ratpack/src/main/java/com/baeldung/repository/EmployeeRepository.java b/ratpack/src/main/java/com/baeldung/repository/EmployeeRepository.java new file mode 100644 index 0000000000..de04c23da5 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/repository/EmployeeRepository.java @@ -0,0 +1,10 @@ +package com.baeldung.repository; + +import com.baeldung.model.Employee; +import ratpack.exec.Promise; + +public interface EmployeeRepository { + + Promise findEmployeeById(Long id) throws Exception; + +} diff --git a/ratpack/src/main/java/com/baeldung/repository/EmployeeRepositoryImpl.java b/ratpack/src/main/java/com/baeldung/repository/EmployeeRepositoryImpl.java new file mode 100644 index 0000000000..6d15012ccb --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/repository/EmployeeRepositoryImpl.java @@ -0,0 +1,25 @@ +package com.baeldung.repository; + +import com.baeldung.model.Employee; +import ratpack.exec.Promise; + +import java.util.HashMap; +import java.util.Map; + +public class EmployeeRepositoryImpl implements EmployeeRepository { + + private static final Map EMPLOYEE_MAP = new HashMap<>(); + + public EmployeeRepositoryImpl() { + EMPLOYEE_MAP.put(1L, new Employee(1L, "Ms", "Jane Doe")); + EMPLOYEE_MAP.put(2L, new Employee(2L, "Mr", "NY")); + } + + @Override + public Promise findEmployeeById(Long id) throws Exception { + return Promise.async(downstream -> { + Thread.sleep(500); + downstream.success(EMPLOYEE_MAP.get(id)); + }); + } +} diff --git a/ratpack/src/test/java/com/baeldung/AppHttpUnitTest.java b/ratpack/src/test/java/com/baeldung/AppHttpUnitTest.java new file mode 100644 index 0000000000..a97516dd74 --- /dev/null +++ b/ratpack/src/test/java/com/baeldung/AppHttpUnitTest.java @@ -0,0 +1,58 @@ +package com.baeldung; + +import com.baeldung.model.Employee; +import org.junit.Test; +import ratpack.exec.Promise; +import ratpack.func.Action; +import ratpack.handling.Chain; +import ratpack.handling.Handler; +import ratpack.registry.Registry; +import ratpack.test.embed.EmbeddedApp; +import ratpack.test.exec.ExecHarness; + +import static org.junit.Assert.assertEquals; + +public class AppHttpUnitTest { + + @Test + public void givenAnyUri_GetEmployeeFromSameRegistry() throws Exception { + Handler allHandler = ctx -> { + Long id = Long.valueOf(ctx.getPathTokens() + .get("id")); + Employee employee = new Employee(id, "Mr", "NY"); + ctx.next(Registry.single(Employee.class, employee)); + }; + Handler empNameHandler = ctx -> { + Employee employee = ctx.get(Employee.class); + ctx.getResponse() + .send("Name of employee with ID " + employee.getId() + " is " + employee.getName()); + }; + Handler empTitleHandler = ctx -> { + Employee employee = ctx.get(Employee.class); + ctx.getResponse() + .send("Title of employee with ID " + employee.getId() + " is " + employee.getTitle()); + }; + + Action chainAction = chain -> chain.prefix("employee/:id", empChain -> { + empChain.all(allHandler) + .get("name", empNameHandler) + .get("title", empTitleHandler); + }); + EmbeddedApp.fromHandlers(chainAction) + .test(testHttpClient -> { + assertEquals("Name of employee with ID 1 is NY", testHttpClient.get("employee/1/name") + .getBody() + .getText()); + assertEquals("Title of employee with ID 1 is Mr", testHttpClient.get("employee/1/title") + .getBody() + .getText()); + }); + } + + @Test + public void givenSyncDataSource_GetDataFromPromise() throws Exception { + String value = ExecHarness.yieldSingle(execution -> Promise.sync(() -> "Foo")) + .getValueOrThrow(); + assertEquals("Foo", value); + } +} diff --git a/rxjava/README.md b/rxjava/README.md index 76135797a6..f8a4ca1dbe 100644 --- a/rxjava/README.md +++ b/rxjava/README.md @@ -12,3 +12,4 @@ - [RxJava StringObservable](http://www.baeldung.com/rxjava-string) - [Filtering Observables in RxJava](http://www.baeldung.com/rxjava-filtering) - [RxJava One Observable, Multiple Subscribers](http://www.baeldung.com/rxjava-multiple-subscribers-observable) +- [Difference Between Flatmap and Switchmap in RxJava](https://www.baeldung.com/rxjava-flatmap-switchmap) diff --git a/spring-4/README.md b/spring-4/README.md index 9855a9254d..4600a603ef 100644 --- a/spring-4/README.md +++ b/spring-4/README.md @@ -1,3 +1,4 @@ ### Relevant Articles: - [Guide to Flips For Spring](http://www.baeldung.com/guide-to-flips-for-spring/) - [A Guide to Flips for Spring](http://www.baeldung.com/flips-spring) +- [Configuring a Hikari Connection Pool with Spring Boot](https://www.baeldung.com/spring-boot-hikari) diff --git a/spring-data-5-reactive/README.md b/spring-5-data-reactive/README.md similarity index 100% rename from spring-data-5-reactive/README.md rename to spring-5-data-reactive/README.md diff --git a/spring-data-5-reactive/pom.xml b/spring-5-data-reactive/pom.xml similarity index 96% rename from spring-data-5-reactive/pom.xml rename to spring-5-data-reactive/pom.xml index 36ace53da2..e4dd454635 100644 --- a/spring-data-5-reactive/pom.xml +++ b/spring-5-data-reactive/pom.xml @@ -2,9 +2,7 @@ 4.0.0 - com.baeldung spring-5-data-reactive - 0.0.1-SNAPSHOT jar @@ -44,11 +42,6 @@ reactor-test test - - org.jetbrains.kotlin - kotlin-stdlib-jdk8 - ${kotlin.version} - org.jetbrains.kotlin kotlin-test @@ -101,6 +94,7 @@ kotlin-maven-plugin + ${kotlin-maven-plugin.version} compile @@ -204,6 +198,7 @@ UTF-8 1.8 1.2.40 + 1.2.40 1.0.0 5.0.2 diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/Spring5ReactiveApplication.java diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/model/Account.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/model/Account.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/model/Account.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/model/Account.java diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountCrudRepository.java diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountMongoRepository.java diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/repository/AccountRxJavaRepository.java diff --git a/spring-data-5-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java b/spring-5-data-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java similarity index 100% rename from spring-data-5-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java rename to spring-5-data-reactive/src/main/java/com/baeldung/reactive/template/AccountTemplateOperations.java diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/Application.kt b/spring-5-data-reactive/src/main/kotlin/com/baeldung/Application.kt similarity index 100% rename from spring-data-5-reactive/src/main/kotlin/com/baeldung/Application.kt rename to spring-5-data-reactive/src/main/kotlin/com/baeldung/Application.kt diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/Event.kt b/spring-5-data-reactive/src/main/kotlin/com/baeldung/Event.kt similarity index 100% rename from spring-data-5-reactive/src/main/kotlin/com/baeldung/Event.kt rename to spring-5-data-reactive/src/main/kotlin/com/baeldung/Event.kt diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/EventRepository.kt b/spring-5-data-reactive/src/main/kotlin/com/baeldung/EventRepository.kt similarity index 100% rename from spring-data-5-reactive/src/main/kotlin/com/baeldung/EventRepository.kt rename to spring-5-data-reactive/src/main/kotlin/com/baeldung/EventRepository.kt diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt b/spring-5-data-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt similarity index 100% rename from spring-data-5-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt rename to spring-5-data-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt b/spring-5-data-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt similarity index 100% rename from spring-data-5-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt rename to spring-5-data-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/resources/logback.xml b/spring-5-data-reactive/src/main/resources/logback.xml similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/resources/logback.xml rename to spring-5-data-reactive/src/main/resources/logback.xml diff --git a/spring-data-5-reactive/src/main/resources/static/index.html b/spring-5-data-reactive/src/main/resources/static/index.html similarity index 100% rename from spring-data-5-reactive/src/main/resources/static/index.html rename to spring-5-data-reactive/src/main/resources/static/index.html diff --git a/spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java b/spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java similarity index 100% rename from spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java rename to spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java diff --git a/spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java b/spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java similarity index 100% rename from spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java rename to spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java diff --git a/spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java b/spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java similarity index 100% rename from spring-data-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java rename to spring-5-data-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java diff --git a/spring-data-5-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java b/spring-5-data-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java similarity index 100% rename from spring-data-5-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java rename to spring-5-data-reactive/src/test/java/com/baeldung/reactive/template/AccountTemplateOperationsIntegrationTest.java diff --git a/spring-5-mvc/README.md b/spring-5-mvc/README.md index 71b7a19b43..7e83077f54 100644 --- a/spring-5-mvc/README.md +++ b/spring-5-mvc/README.md @@ -1,3 +1,4 @@ ### Relevant Articles: - [Spring Boot and Kotlin](http://www.baeldung.com/spring-boot-kotlin) -- [Spring MVC Streaming and SSE Request Processing](http://www.baeldung.com/spring-mvc-sse-streams) +- [Spring MVC Streaming and SSE Request Processing](https://www.baeldung.com/spring-mvc-sse-streams) +- [Overview and Need for DelegatingFilterProxy in Spring](https://www.baeldung.com/spring-delegating-filter-proxy) diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml index f2f7dd1729..6e39743ed0 100644 --- a/spring-5-reactive-client/pom.xml +++ b/spring-5-reactive-client/pom.xml @@ -3,9 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung spring-5-reactive-client - 0.0.1-SNAPSHOT jar spring-5-reactive-client spring 5 sample project about new features @@ -92,11 +90,6 @@ org.projectlombok lombok - - org.apache.commons - commons-lang3 - - diff --git a/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeFunctionalConfig.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeFunctionalConfig.java new file mode 100644 index 0000000000..8d7860ccdc --- /dev/null +++ b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeFunctionalConfig.java @@ -0,0 +1,75 @@ +package com.baeldung.reactive.functional; + +import static org.springframework.web.reactive.function.BodyExtractors.toMono; +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; +import static org.springframework.web.reactive.function.server.RequestPredicates.POST; +import static org.springframework.web.reactive.function.server.RouterFunctions.route; +import static org.springframework.web.reactive.function.server.ServerResponse.ok; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.web.server.ServerHttpSecurity; +import org.springframework.security.web.server.SecurityWebFilterChain; +import org.springframework.web.reactive.function.server.RouterFunction; +import org.springframework.web.reactive.function.server.ServerResponse; + +import com.baeldung.webflux.Employee; +import com.baeldung.webflux.EmployeeRepository; + +@Configuration +public class EmployeeFunctionalConfig { + + @Bean + EmployeeRepository employeeRepository() { + return new EmployeeRepository(); + } + + @Bean + RouterFunction getAllEmployeesRoute() { + return route(GET("/employees"), + req -> ok().body( + employeeRepository().findAllEmployees(), Employee.class)); + } + + @Bean + RouterFunction getEmployeeByIdRoute() { + return route(GET("/employees/{id}"), + req -> ok().body( + employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class)); + } + + @Bean + RouterFunction updateEmployee() { + return route(POST("/employees/update"), + req -> req.body(toMono(Employee.class)) + .doOnNext(employeeRepository()::updateEmployee) + .then(ok().build())); + } + + @Bean + RouterFunction composedRoutes() { + return + route(GET("/employees"), + req -> ok().body( + employeeRepository().findAllEmployees(), Employee.class)) + + .and(route(GET("/employees/{id}"), + req -> ok().body( + employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class))) + + .and(route(POST("/employees/update"), + req -> req.body(toMono(Employee.class)) + .doOnNext(employeeRepository()::updateEmployee) + .then(ok().build()))); + } + + @Bean + public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { + http.csrf() + .disable() + .authorizeExchange() + .anyExchange() + .permitAll(); + return http.build(); + } +} diff --git a/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalApplication.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalApplication.java new file mode 100644 index 0000000000..1f2bd871fc --- /dev/null +++ b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.reactive.functional; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class EmployeeSpringFunctionalApplication { + + public static void main(String[] args) { + SpringApplication.run(EmployeeSpringFunctionalApplication.class, args); + } + +} diff --git a/spring-5-reactive-security/src/test/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalIntegrationTest.java b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalIntegrationTest.java new file mode 100644 index 0000000000..1197820066 --- /dev/null +++ b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalIntegrationTest.java @@ -0,0 +1,28 @@ +package com.baeldung.reactive.functional; + +import org.junit.Test; +import org.springframework.test.web.reactive.server.WebTestClient; + +import com.baeldung.webflux.Employee; + +public class EmployeeSpringFunctionalIntegrationTest { + + private static EmployeeFunctionalConfig config = new EmployeeFunctionalConfig(); + + @Test + public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() { + WebTestClient client = WebTestClient + .bindToRouterFunction(config.getEmployeeByIdRoute()) + .build(); + + Employee expected = new Employee("1", "Employee 1"); + + client.get() + .uri("/employees/1") + .exchange() + .expectStatus() + .isOk() + .expectBody(Employee.class) + .isEqualTo(expected); + } +} diff --git a/spring-5-security/README.md b/spring-5-security/README.md index 564dcd3c96..55fa7ab042 100644 --- a/spring-5-security/README.md +++ b/spring-5-security/README.md @@ -5,3 +5,4 @@ - [A Custom Spring SecurityConfigurer](http://www.baeldung.com/spring-security-custom-configurer) - [New Password Storage In Spring Security 5](http://www.baeldung.com/spring-security-5-password-storage) - [Default Password Encoder in Spring Security 5](https://www.baeldung.com/spring-security-5-default-password-encoder) +- [Extracting Principal and Authorities using Spring Security OAuth](https://www.baeldung.com/spring-security-oauth-principal-authorities-extractor) diff --git a/spring-5/README.md b/spring-5/README.md index baf03fb3b3..d3c1decbc7 100644 --- a/spring-5/README.md +++ b/spring-5/README.md @@ -12,3 +12,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Introduction to Spring REST Docs](http://www.baeldung.com/spring-rest-docs) - [Spring ResponseStatusException](http://www.baeldung.com/spring-response-status-exception) - [Spring Assert Statements](http://www.baeldung.com/spring-assert) +- [Configuring a Hikari Connection Pool with Spring Boot](https://www.baeldung.com/spring-boot-hikari) + diff --git a/spring-boot-angular-ecommerce/README.md b/spring-boot-angular-ecommerce/README.md index 933059bc45..c6564643e2 100644 --- a/spring-boot-angular-ecommerce/README.md +++ b/spring-boot-angular-ecommerce/README.md @@ -1,2 +1,2 @@ ### Relevant Articles: -- [A Simple E-Commerce Implementation with Spring]() +- [A Simple E-Commerce Implementation with Spring](https://www.baeldung.com/spring-angular-ecommerce) diff --git a/camel-api/README.md b/spring-boot-camel/README.md similarity index 100% rename from camel-api/README.md rename to spring-boot-camel/README.md diff --git a/camel-api/pom.xml b/spring-boot-camel/pom.xml similarity index 100% rename from camel-api/pom.xml rename to spring-boot-camel/pom.xml diff --git a/camel-api/src/main/java/com/baeldung/camel/Application.java b/spring-boot-camel/src/main/java/com/baeldung/camel/Application.java similarity index 100% rename from camel-api/src/main/java/com/baeldung/camel/Application.java rename to spring-boot-camel/src/main/java/com/baeldung/camel/Application.java diff --git a/camel-api/src/main/java/com/baeldung/camel/ExampleServices.java b/spring-boot-camel/src/main/java/com/baeldung/camel/ExampleServices.java similarity index 100% rename from camel-api/src/main/java/com/baeldung/camel/ExampleServices.java rename to spring-boot-camel/src/main/java/com/baeldung/camel/ExampleServices.java diff --git a/camel-api/src/main/java/com/baeldung/camel/MyBean.java b/spring-boot-camel/src/main/java/com/baeldung/camel/MyBean.java similarity index 100% rename from camel-api/src/main/java/com/baeldung/camel/MyBean.java rename to spring-boot-camel/src/main/java/com/baeldung/camel/MyBean.java diff --git a/camel-api/src/main/resources/application.properties b/spring-boot-camel/src/main/resources/application.properties similarity index 100% rename from camel-api/src/main/resources/application.properties rename to spring-boot-camel/src/main/resources/application.properties diff --git a/camel-api/src/main/resources/application.yml b/spring-boot-camel/src/main/resources/application.yml similarity index 100% rename from camel-api/src/main/resources/application.yml rename to spring-boot-camel/src/main/resources/application.yml diff --git a/camel-api/src/main/resources/logback.xml b/spring-boot-camel/src/main/resources/logback.xml similarity index 100% rename from camel-api/src/main/resources/logback.xml rename to spring-boot-camel/src/main/resources/logback.xml diff --git a/spring-boot-logging-log4j2/README.md b/spring-boot-logging-log4j2/README.md index 7676bd1919..305957ed8d 100644 --- a/spring-boot-logging-log4j2/README.md +++ b/spring-boot-logging-log4j2/README.md @@ -1,2 +1,3 @@ ### Relevant Articles: - [Logging in Spring Boot](http://www.baeldung.com/spring-boot-logging) +- [How to Disable Console Logging in Spring Boot](https://www.baeldung.com/spring-boot-disable-console-logging) diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/pom.xml b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/pom.xml new file mode 100644 index 0000000000..c4f9b12676 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/pom.xml @@ -0,0 +1,53 @@ + + 4.0.0 + com.baeldung + 0.0.1-SNAPSHOT + disabling-console-jul + + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.slf4j + jul-to-slf4j + + + org.slf4j + slf4j-simple + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + UTF-8 + UTF-8 + 1.8 + + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/DisablingConsoleJulApp.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/DisablingConsoleJulApp.java new file mode 100644 index 0000000000..c9187e9ace --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/DisablingConsoleJulApp.java @@ -0,0 +1,11 @@ +package com.baeldung.springbootlogging.disablingconsole.jul.properties; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DisablingConsoleJulApp { + public static void main(String[] args) { + SpringApplication.run(DisablingConsoleJulApp.class, args); + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/controllers/DisabledConsoleRestController.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/controllers/DisabledConsoleRestController.java new file mode 100644 index 0000000000..e33089ed36 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/java/com/baeldung/springbootlogging/disablingconsole/jul/properties/controllers/DisabledConsoleRestController.java @@ -0,0 +1,19 @@ +package com.baeldung.springbootlogging.disablingconsole.jul.properties.controllers; + +import java.time.LocalTime; +import java.util.logging.Logger; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class DisabledConsoleRestController { + + private static final Logger LOG = Logger.getLogger(DisabledConsoleRestController.class.getName()); + + @GetMapping("/disabled-console-jul-properties") + public String logTime() { + LOG.info("Current time: " + LocalTime.now()); + return "finished!"; + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/application.properties b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/application.properties new file mode 100644 index 0000000000..d81eed95d3 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/application.properties @@ -0,0 +1 @@ +logging.file=true \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/logging.properties b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/logging.properties new file mode 100644 index 0000000000..0136354868 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-jul/src/main/resources/logging.properties @@ -0,0 +1,4 @@ +handlers= java.util.logging.FileHandler + +java.util.logging.FileHandler.pattern=baeldung.log +java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/pom.xml b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/pom.xml new file mode 100644 index 0000000000..97d10c574a --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + com.baeldung + 0.0.1-SNAPSHOT + disabling-console-log4j2 + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + UTF-8 + UTF-8 + 1.8 + + + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/DisablingConsoleLog4j2App.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/DisablingConsoleLog4j2App.java new file mode 100644 index 0000000000..ae59f3b3e1 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/DisablingConsoleLog4j2App.java @@ -0,0 +1,11 @@ +package com.baeldung.springbootlogging.disablingconsole.log4j2.xml; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DisablingConsoleLog4j2App { + public static void main(String[] args) { + SpringApplication.run(DisablingConsoleLog4j2App.class, args); + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/controllers/DisabledConsoleRestController.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/controllers/DisabledConsoleRestController.java new file mode 100644 index 0000000000..f6485b63d4 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/java/com/baeldung/springbootlogging/disablingconsole/log4j2/xml/controllers/DisabledConsoleRestController.java @@ -0,0 +1,20 @@ +package com.baeldung.springbootlogging.disablingconsole.log4j2.xml.controllers; + +import java.time.LocalTime; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class DisabledConsoleRestController { + + private static final Logger LOG = LoggerFactory.getLogger(DisabledConsoleRestController.class); + + @GetMapping("/disabled-console-log4j2-xml") + public String logTime() { + LOG.info("Current time: {}", LocalTime.now()); + return "finished!"; + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/resources/log4j2.xml b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..20975f40d9 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-log4j2/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/pom.xml b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/pom.xml new file mode 100644 index 0000000000..6f2170390b --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/pom.xml @@ -0,0 +1,9 @@ + + 4.0.0 + + com.baeldung + disabling-console-logging + 0.0.1-SNAPSHOT + + disabling-console-logback + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/DisablingConsoleLogbackApp.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/DisablingConsoleLogbackApp.java new file mode 100644 index 0000000000..9b38d42477 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/DisablingConsoleLogbackApp.java @@ -0,0 +1,11 @@ +package com.baeldung.springbootlogging.disablingconsole.logback.xml; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DisablingConsoleLogbackApp { + public static void main(String[] args) { + SpringApplication.run(DisablingConsoleLogbackApp.class, args); + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/controllers/DisabledConsoleRestController.java b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/controllers/DisabledConsoleRestController.java new file mode 100644 index 0000000000..e1d6b1d45e --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/java/com/baeldung/springbootlogging/disablingconsole/logback/xml/controllers/DisabledConsoleRestController.java @@ -0,0 +1,20 @@ +package com.baeldung.springbootlogging.disablingconsole.logback.xml.controllers; + +import java.time.LocalTime; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class DisabledConsoleRestController { + + private static final Logger LOG = LoggerFactory.getLogger(DisabledConsoleRestController.class); + + @GetMapping("/disabled-console-logback-xml") + public String logTime() { + LOG.info("Current time: {}", LocalTime.now()); + return "finished!"; + } +} diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/application.properties b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/application.properties new file mode 100644 index 0000000000..70c2f97194 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/application.properties @@ -0,0 +1,7 @@ +logging.file=disabled-console.log + +### we could potentially 'disable' console logging by setting a blank log pattern: +#logging.pattern.console= + +### or disable the logs completely by setting the root logging level as 'OFF': +#logging.level.root=OFF diff --git a/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/logback-spring.xml b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/logback-spring.xml new file mode 100644 index 0000000000..5c751e994f --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/disabling-console-logback/src/main/resources/logback-spring.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/disabling-console-logging/pom.xml b/spring-boot-logging-log4j2/disabling-console-logging/pom.xml new file mode 100644 index 0000000000..bc248e49b2 --- /dev/null +++ b/spring-boot-logging-log4j2/disabling-console-logging/pom.xml @@ -0,0 +1,21 @@ + + 4.0.0 + 0.0.1-SNAPSHOT + com.baeldung + disabling-console-logging + pom + Projects for Disabling Spring Boot Console Logging tutorials + + + com.baeldung + spring-boot-logging-log4j2 + 0.0.1-SNAPSHOT + + + + disabling-console-logback + disabling-console-log4j2 + disabling-console-jul + + \ No newline at end of file diff --git a/spring-boot-logging-log4j2/pom.xml b/spring-boot-logging-log4j2/pom.xml index 61f949b820..ee4d07c3e8 100644 --- a/spring-boot-logging-log4j2/pom.xml +++ b/spring-boot-logging-log4j2/pom.xml @@ -6,8 +6,8 @@ com.baeldung spring-boot-logging-log4j2 0.0.1-SNAPSHOT - jar - Demo project for Spring Boot Logging with Log4J2 + pom + Projects for Spring Boot Logging tutorials parent-boot-2 @@ -20,16 +20,6 @@ org.springframework.boot spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-logging - - - - - org.springframework.boot - spring-boot-starter-log4j2 @@ -38,6 +28,11 @@ test + + + spring-boot-logging-log4j2-app + disabling-console-logging + diff --git a/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/pom.xml b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/pom.xml new file mode 100644 index 0000000000..1d4c8867fb --- /dev/null +++ b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + com.baeldung + spring-boot-logging-log4j2-app + 0.0.1-SNAPSHOT + jar + Demo project for Spring Boot Logging with Log4J2 + + + spring-boot-logging-log4j2 + com.baeldung + 0.0.1-SNAPSHOT + .. + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + diff --git a/spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/LoggingController.java b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/java/com/baeldung/springbootlogging/LoggingController.java similarity index 100% rename from spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/LoggingController.java rename to spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/java/com/baeldung/springbootlogging/LoggingController.java diff --git a/spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java similarity index 100% rename from spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java rename to spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/style.css b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/resources/application.properties similarity index 100% rename from spring-security-mvc-socket/src/main/webapp/resources/styles/style.css rename to spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/resources/application.properties diff --git a/spring-boot-logging-log4j2/src/main/resources/log4j2-spring.xml b/spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/resources/log4j2-spring.xml similarity index 100% rename from spring-boot-logging-log4j2/src/main/resources/log4j2-spring.xml rename to spring-boot-logging-log4j2/spring-boot-logging-log4j2-app/src/main/resources/log4j2-spring.xml diff --git a/spring-boot-mvc/README.md b/spring-boot-mvc/README.md index d1987e105f..b46dbe3bae 100644 --- a/spring-boot-mvc/README.md +++ b/spring-boot-mvc/README.md @@ -1,3 +1,10 @@ ### Relevant Articles: - [Guide to the Favicon in Spring Boot](http://www.baeldung.com/spring-boot-favicon) +- [Custom Validation MessageSource in Spring Boot](https://www.baeldung.com/spring-custom-validation-message-source) +- [Spring Boot Annotations](http://www.baeldung.com/spring-boot-annotations) +- [Spring Scheduling Annotations](http://www.baeldung.com/spring-scheduling-annotations) +- [Spring Web Annotations](http://www.baeldung.com/spring-mvc-annotations) +- [Spring Core Annotations](http://www.baeldung.com/spring-core-annotations) +- [Display RSS Feed with Spring MVC](http://www.baeldung.com/spring-mvc-rss-feed) + diff --git a/spring-boot-mvc/pom.xml b/spring-boot-mvc/pom.xml index d0fce26bb5..e456155f36 100644 --- a/spring-boot-mvc/pom.xml +++ b/spring-boot-mvc/pom.xml @@ -32,6 +32,13 @@ org.springframework.boot spring-boot-starter-validation + + + + com.rometools + rome + ${rome.version} + @@ -47,6 +54,8 @@ UTF-8 UTF-8 1.8 + + 1.10.0 diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Bike.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Bike.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Biker.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Biker.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Car.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Car.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/CarMechanic.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/CarMechanic.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/CarUtility.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/CarUtility.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CustomException.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomException.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/CustomException.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomException.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CustomResponseController.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomResponseController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/CustomResponseController.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomResponseController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CustomResponseWithBuilderController.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomResponseWithBuilderController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/CustomResponseWithBuilderController.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/CustomResponseWithBuilderController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Driver.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Driver.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Engine.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Engine.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/Vehicle.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/Vehicle.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRepository.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRepository.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRestController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleRestController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java b/spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleService.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java rename to spring-boot-mvc/src/main/java/com/baeldung/annotations/VehicleService.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedApplication.java b/spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedApplication.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedApplication.java rename to spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedApplication.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedController.java b/spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedController.java rename to spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedView.java b/spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedView.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedView.java rename to spring-boot-mvc/src/main/java/com/baeldung/rss/RssFeedView.java diff --git a/spring-mvc-java/src/test/java/com/baeldung/rss/RssFeedUnitTest.java b/spring-boot-mvc/src/test/java/com/baeldung/rss/RssFeedUnitTest.java similarity index 100% rename from spring-mvc-java/src/test/java/com/baeldung/rss/RssFeedUnitTest.java rename to spring-boot-mvc/src/test/java/com/baeldung/rss/RssFeedUnitTest.java diff --git a/spring-boot-ops/pom.xml b/spring-boot-ops/pom.xml index 223fe72bd5..57779c3f8e 100644 --- a/spring-boot-ops/pom.xml +++ b/spring-boot-ops/pom.xml @@ -3,9 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung spring-boot-ops - 0.0.1-SNAPSHOT war spring-boot-ops Demo project for Spring Boot @@ -19,10 +17,6 @@ - - org.springframework.boot spring-boot-starter-web @@ -95,13 +89,8 @@ - ${artifactId} + ${project.artifactId} - - org.springframework.boot - spring-boot-maven-plugin - - org.springframework.boot spring-boot-maven-plugin diff --git a/spring-boot-persistence/README.MD b/spring-boot-persistence/README.MD index 72fdca74fa..6cf172426a 100644 --- a/spring-boot-persistence/README.MD +++ b/spring-boot-persistence/README.MD @@ -3,3 +3,4 @@ - [Spring Boot with Multiple SQL Import Files](http://www.baeldung.com/spring-boot-sql-import-files) - [Configuring Separate Spring DataSource for Tests](http://www.baeldung.com/spring-testing-separate-data-source) - [Quick Guide on data.sql and schema.sql Files in Spring Boot](http://www.baeldung.com/spring-boot-data-sql-and-schema-sql) +- [Configuring a Tomcat Connection Pool in Spring Boot](https://www.baeldung.com/spring-boot-tomcat-connection-pool) diff --git a/spring-boot-security/pom.xml b/spring-boot-security/pom.xml index 18d292c8a1..6bc013d1ed 100644 --- a/spring-boot-security/pom.xml +++ b/spring-boot-security/pom.xml @@ -2,7 +2,6 @@ 4.0.0 - com.baeldung spring-boot-security 0.0.1-SNAPSHOT jar @@ -28,10 +27,6 @@ - - - - org.springframework.boot spring-boot-starter-security @@ -72,11 +67,6 @@ spring-security-test test - - org.springframework.security - spring-security-test - test - diff --git a/spring-boot/README.MD b/spring-boot/README.MD index f11ea1b7ed..54382992d4 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -31,3 +31,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Spring Shutdown Callbacks](http://www.baeldung.com/spring-shutdown-callbacks) - [Spring Boot Integration Testing with Embedded MongoDB](http://www.baeldung.com/spring-boot-embedded-mongodb) - [Container Configuration in Spring Boot 2](http://www.baeldung.com/embeddedservletcontainercustomizer-configurableembeddedservletcontainer-spring-boot) +- [Introduction to Chaos Monkey](https://www.baeldung.com/spring-boot-chaos-monkey) +- [Spring Component Scanning](https://www.baeldung.com/spring-component-scanning) diff --git a/spring-boot/src/main/java/com/baeldung/properties/ConfigProperties.java b/spring-boot/src/main/java/com/baeldung/properties/ConfigProperties.java new file mode 100644 index 0000000000..863510738b --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/ConfigProperties.java @@ -0,0 +1,117 @@ +package com.baeldung.properties; + +import java.util.List; +import java.util.Map; + +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.Pattern; + +import org.hibernate.validator.constraints.Length; +import org.hibernate.validator.constraints.NotBlank; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.validation.annotation.Validated; + +@Configuration +@PropertySource("classpath:configprops.properties") +@ConfigurationProperties(prefix = "mail") +@Validated +public class ConfigProperties { + + @Validated + public static class Credentials { + + @Length(max = 4, min = 1) + private String authMethod; + private String username; + private String password; + + public String getAuthMethod() { + return authMethod; + } + + public void setAuthMethod(String authMethod) { + this.authMethod = authMethod; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + } + + @NotBlank + private String host; + + @Min(1025) + @Max(65536) + private int port; + + @Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$") + private String from; + + private Credentials credentials; + private List defaultRecipients; + private Map additionalHeaders; + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public Credentials getCredentials() { + return credentials; + } + + public void setCredentials(Credentials credentials) { + this.credentials = credentials; + } + + public List getDefaultRecipients() { + return defaultRecipients; + } + + public void setDefaultRecipients(List defaultRecipients) { + this.defaultRecipients = defaultRecipients; + } + + public Map getAdditionalHeaders() { + return additionalHeaders; + } + + public void setAdditionalHeaders(Map additionalHeaders) { + this.additionalHeaders = additionalHeaders; + } +} diff --git a/spring-boot/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java b/spring-boot/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java new file mode 100644 index 0000000000..ee9671c755 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.properties; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class }) +public class ConfigPropertiesDemoApplication { + public static void main(String[] args) { + new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer()) + .run(); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/properties/CustomJsonProperties.java b/spring-boot/src/main/java/com/baeldung/properties/CustomJsonProperties.java new file mode 100644 index 0000000000..084138ec6f --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/CustomJsonProperties.java @@ -0,0 +1,71 @@ +package com.baeldung.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "custom") +public class CustomJsonProperties { + + private String host; + + private int port; + + private boolean resend; + + private Person sender; + + public static class Person { + + private String name; + private String address; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public boolean isResend() { + return resend; + } + + public void setResend(boolean resend) { + this.resend = resend; + } + + public Person getSender() { + return sender; + } + + public void setSender(Person sender) { + this.sender = sender; + } +} diff --git a/spring-boot/src/main/java/com/baeldung/properties/JsonProperties.java b/spring-boot/src/main/java/com/baeldung/properties/JsonProperties.java new file mode 100644 index 0000000000..31b3be14b4 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/JsonProperties.java @@ -0,0 +1,64 @@ +package com.baeldung.properties; + +import java.util.LinkedHashMap; +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +@Component +@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class) +@ConfigurationProperties +public class JsonProperties { + + private String host; + + private int port; + + private boolean resend; + + private List topics; + + private LinkedHashMap sender; + + public LinkedHashMap getSender() { + return sender; + } + + public void setSender(LinkedHashMap sender) { + this.sender = sender; + } + + public List getTopics() { + return topics; + } + + public void setTopics(List topics) { + this.topics = topics; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public boolean isResend() { + return resend; + } + + public void setResend(boolean resend) { + this.resend = resend; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } +} diff --git a/spring-boot/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java b/spring-boot/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java new file mode 100644 index 0000000000..0aee149123 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java @@ -0,0 +1,68 @@ +package com.baeldung.properties; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.Resource; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JsonPropertyContextInitializer implements ApplicationContextInitializer { + + private final static String CUSTOM_PREFIX = "custom."; + + @Override + @SuppressWarnings("unchecked") + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + try { + Resource resource = configurableApplicationContext.getResource("classpath:configprops.json"); + Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); + Set set = readValue.entrySet(); + List propertySources = convertEntrySet(set, Optional.empty()); + for (PropertySource propertySource : propertySources) { + configurableApplicationContext.getEnvironment() + .getPropertySources() + .addFirst(propertySource); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static List convertEntrySet(Set entrySet, Optional parentKey) { + return entrySet.stream() + .map((Map.Entry e) -> convertToPropertySourceList(e, parentKey)) + .flatMap(Collection::stream) + .collect(Collectors.toList()); + } + + private static List convertToPropertySourceList(Map.Entry e, Optional parentKey) { + String key = parentKey.map(s -> s + ".") + .orElse("") + (String) e.getKey(); + Object value = e.getValue(); + return covertToPropertySourceList(key, value); + } + + @SuppressWarnings("unchecked") + private static List covertToPropertySourceList(String key, Object value) { + if (value instanceof LinkedHashMap) { + LinkedHashMap map = (LinkedHashMap) value; + Set entrySet = map.entrySet(); + return convertEntrySet(entrySet, Optional.ofNullable(key)); + } + String finalKey = CUSTOM_PREFIX + key; + return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value))); + } + +} \ No newline at end of file diff --git a/spring-boot/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java b/spring-boot/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java new file mode 100644 index 0000000000..c14d3faea5 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java @@ -0,0 +1,21 @@ +package com.baeldung.properties; + +import java.io.IOException; +import java.util.Map; + +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.support.EncodedResource; +import org.springframework.core.io.support.PropertySourceFactory; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JsonPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { + Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); + return new MapPropertySource("json-property", readValue); + } + +} \ No newline at end of file diff --git a/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java b/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java index cb0304fc41..395d68060b 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java +++ b/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java @@ -1,13 +1,15 @@ package org.baeldung.properties; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.ComponentScan; -@EnableAutoConfiguration -@ComponentScan(basePackageClasses = ConfigProperties.class) +@SpringBootApplication +@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class }) public class ConfigPropertiesDemoApplication { public static void main(String[] args) { - SpringApplication.run(ConfigPropertiesDemoApplication.class); + new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer()) + .run(); } + } diff --git a/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java b/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java new file mode 100644 index 0000000000..3fae8a8e98 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java @@ -0,0 +1,71 @@ +package org.baeldung.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "custom") +public class CustomJsonProperties { + + private String host; + + private int port; + + private boolean resend; + + private Person sender; + + public static class Person { + + private String name; + private String address; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public boolean isResend() { + return resend; + } + + public void setResend(boolean resend) { + this.resend = resend; + } + + public Person getSender() { + return sender; + } + + public void setSender(Person sender) { + this.sender = sender; + } +} diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java b/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java new file mode 100644 index 0000000000..5c31cd1344 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java @@ -0,0 +1,64 @@ +package org.baeldung.properties; + +import java.util.LinkedHashMap; +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +@Component +@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class) +@ConfigurationProperties +public class JsonProperties { + + private String host; + + private int port; + + private boolean resend; + + private List topics; + + private LinkedHashMap sender; + + public LinkedHashMap getSender() { + return sender; + } + + public void setSender(LinkedHashMap sender) { + this.sender = sender; + } + + public List getTopics() { + return topics; + } + + public void setTopics(List topics) { + this.topics = topics; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public boolean isResend() { + return resend; + } + + public void setResend(boolean resend) { + this.resend = resend; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } +} diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java new file mode 100644 index 0000000000..fd9b3f35a5 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java @@ -0,0 +1,68 @@ +package org.baeldung.properties; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.Resource; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JsonPropertyContextInitializer implements ApplicationContextInitializer { + + private final static String CUSTOM_PREFIX = "custom."; + + @Override + @SuppressWarnings("unchecked") + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + try { + Resource resource = configurableApplicationContext.getResource("classpath:configprops.json"); + Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); + Set set = readValue.entrySet(); + List propertySources = convertEntrySet(set, Optional.empty()); + for (PropertySource propertySource : propertySources) { + configurableApplicationContext.getEnvironment() + .getPropertySources() + .addFirst(propertySource); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static List convertEntrySet(Set entrySet, Optional parentKey) { + return entrySet.stream() + .map((Map.Entry e) -> convertToPropertySourceList(e, parentKey)) + .flatMap(Collection::stream) + .collect(Collectors.toList()); + } + + private static List convertToPropertySourceList(Map.Entry e, Optional parentKey) { + String key = parentKey.map(s -> s + ".") + .orElse("") + (String) e.getKey(); + Object value = e.getValue(); + return covertToPropertySourceList(key, value); + } + + @SuppressWarnings("unchecked") + private static List covertToPropertySourceList(String key, Object value) { + if (value instanceof LinkedHashMap) { + LinkedHashMap map = (LinkedHashMap) value; + Set entrySet = map.entrySet(); + return convertEntrySet(entrySet, Optional.ofNullable(key)); + } + String finalKey = CUSTOM_PREFIX + key; + return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value))); + } + +} \ No newline at end of file diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java new file mode 100644 index 0000000000..9578179519 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java @@ -0,0 +1,21 @@ +package org.baeldung.properties; + +import java.io.IOException; +import java.util.Map; + +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.support.EncodedResource; +import org.springframework.core.io.support.PropertySourceFactory; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JsonPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { + Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); + return new MapPropertySource("json-property", readValue); + } + +} \ No newline at end of file diff --git a/spring-boot/src/main/resources/configprops.json b/spring-boot/src/main/resources/configprops.json new file mode 100644 index 0000000000..1602663775 --- /dev/null +++ b/spring-boot/src/main/resources/configprops.json @@ -0,0 +1,10 @@ +{ + "host" : "mailer@mail.com", + "port" : 9090, + "resend" : true, + "topics" : ["spring", "boot"], + "sender" : { + "name": "sender", + "address": "street" + } +} diff --git a/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java new file mode 100644 index 0000000000..3a4b6551b1 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java @@ -0,0 +1,45 @@ +package com.baeldung.properties; + +import org.baeldung.properties.ConfigProperties; +import org.baeldung.properties.ConfigPropertiesDemoApplication; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ConfigPropertiesDemoApplication.class) +@TestPropertySource("classpath:configprops-test.properties") +public class ConfigPropertiesIntegrationTest { + + @Autowired + private ConfigProperties properties; + + @Test + public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception { + Assert.assertTrue("From address is read as null!", properties.getFrom() != null); + } + + @Test + public void whenListPropertyQueriedthenReturnsProperty() throws Exception { + Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients().size() == 2); + Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients().size() == 2); + } + + @Test + public void whenMapPropertyQueriedthenReturnsProperty() throws Exception { + Assert.assertTrue("Couldn't bind map property!", properties.getAdditionalHeaders() != null); + Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders().size() == 3); + } + + @Test + public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception { + Assert.assertTrue("Couldn't bind map property!", properties.getCredentials() != null); + Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getAuthMethod().equals("SHA1")); + Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getUsername().equals("john")); + Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getPassword().equals("password")); + } +} diff --git a/spring-boot/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java new file mode 100644 index 0000000000..48c551d1dd --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java @@ -0,0 +1,63 @@ +package com.baeldung.properties; + +import java.util.Arrays; + +import org.baeldung.properties.ConfigPropertiesDemoApplication; +import org.baeldung.properties.CustomJsonProperties; +import org.baeldung.properties.JsonProperties; +import org.baeldung.properties.JsonPropertyContextInitializer; +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class) +public class JsonPropertiesIntegrationTest { + + @Autowired + private JsonProperties jsonProperties; + + @Autowired + private CustomJsonProperties customJsonProperties; + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() { + Assert.assertEquals("mailer@mail.com", jsonProperties.getHost()); + Assert.assertEquals(9090, jsonProperties.getPort()); + Assert.assertTrue(jsonProperties.isResend()); + } + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() { + Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot"))); + } + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() { + Assert.assertEquals("sender", jsonProperties.getSender() + .get("name")); + Assert.assertEquals("street", jsonProperties.getSender() + .get("address")); + } + + @Test + public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() { + Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost()); + Assert.assertEquals(9090, customJsonProperties.getPort()); + Assert.assertTrue(customJsonProperties.isResend()); + } + + @Test + public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() { + Assert.assertNotNull(customJsonProperties.getSender()); + Assert.assertEquals("sender", customJsonProperties.getSender() + .getName()); + Assert.assertEquals("street", customJsonProperties.getSender() + .getAddress()); + } + +} diff --git a/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java new file mode 100644 index 0000000000..e3d4c62953 --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java @@ -0,0 +1,59 @@ +package org.baeldung.properties; + +import java.util.Arrays; + +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class) +public class JsonPropertiesIntegrationTest { + + @Autowired + private JsonProperties jsonProperties; + + @Autowired + private CustomJsonProperties customJsonProperties; + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() { + Assert.assertEquals("mailer@mail.com", jsonProperties.getHost()); + Assert.assertEquals(9090, jsonProperties.getPort()); + Assert.assertTrue(jsonProperties.isResend()); + } + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() { + Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot"))); + } + + @Test + public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() { + Assert.assertEquals("sender", jsonProperties.getSender() + .get("name")); + Assert.assertEquals("street", jsonProperties.getSender() + .get("address")); + } + + @Test + public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() { + Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost()); + Assert.assertEquals(9090, customJsonProperties.getPort()); + Assert.assertTrue(customJsonProperties.isResend()); + } + + @Test + public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() { + Assert.assertNotNull(customJsonProperties.getSender()); + Assert.assertEquals("sender", customJsonProperties.getSender() + .getName()); + Assert.assertEquals("street", customJsonProperties.getSender() + .getAddress()); + } + +} diff --git a/spring-cloud/README.md b/spring-cloud/README.md index 31d9ffb684..805052e4db 100644 --- a/spring-cloud/README.md +++ b/spring-cloud/README.md @@ -27,3 +27,4 @@ - [An Intro to Spring Cloud Security](http://www.baeldung.com/spring-cloud-security) - [An Intro to Spring Cloud Task](http://www.baeldung.com/spring-cloud-task) - [Running Spring Boot Applications With Minikube](http://www.baeldung.com/spring-boot-minikube) +- [Introduction to Netflix Archaius with Spring Cloud](https://www.baeldung.com/netflix-archaius-spring-cloud-integration) diff --git a/spring-cloud/pom.xml b/spring-cloud/pom.xml index b378d0070c..456d4e63e7 100644 --- a/spring-cloud/pom.xml +++ b/spring-cloud/pom.xml @@ -25,6 +25,7 @@ spring-cloud-zookeeper spring-cloud-gateway spring-cloud-stream + spring-cloud-stream-starters/twitterhdfs spring-cloud-connectors-heroku spring-cloud-aws spring-cloud-consul diff --git a/spring-cloud/spring-cloud-stream-starters/twitter/twitter b/spring-cloud/spring-cloud-stream-starters/twitter/twitter new file mode 160000 index 0000000000..f9673ef0c1 --- /dev/null +++ b/spring-cloud/spring-cloud-stream-starters/twitter/twitter @@ -0,0 +1 @@ +Subproject commit f9673ef0c11c51b327555aaca61ee196935f998b diff --git a/spring-cloud/spring-cloud-stream-starters/boot/.gitignore b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/.gitignore similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/.gitignore rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/.gitignore diff --git a/spring-cloud/spring-cloud-stream-starters/boot/pom.xml b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/pom.xml similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/pom.xml rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/pom.xml diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/aggregate/AggregateApp.java b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/aggregate/AggregateApp.java similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/aggregate/AggregateApp.java rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/aggregate/AggregateApp.java diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/processor/ProcessorApp.java b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/processor/ProcessorApp.java similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/processor/ProcessorApp.java rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/processor/ProcessorApp.java diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/sink/SinkApp.java b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/sink/SinkApp.java similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/sink/SinkApp.java rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/sink/SinkApp.java diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/source/SourceApp.java b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/source/SourceApp.java similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/java/com/baeldung/twitterhdfs/source/SourceApp.java rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/java/com/baeldung/twitterhdfs/source/SourceApp.java diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/resources/application.properties b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/resources/application.properties similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/resources/application.properties rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/resources/application.properties diff --git a/jsonb/src/main/resources/logback.xml b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/resources/logback.xml similarity index 100% rename from jsonb/src/main/resources/logback.xml rename to spring-cloud/spring-cloud-stream-starters/twitterhdfs/src/main/resources/logback.xml diff --git a/spring-core/README.md b/spring-core/README.md index adc21ffdaf..c0577b4ebc 100644 --- a/spring-core/README.md +++ b/spring-core/README.md @@ -18,3 +18,5 @@ - [Spring – Injecting Collections](http://www.baeldung.com/spring-injecting-collections) - [Access a File from the Classpath in a Spring Application](http://www.baeldung.com/spring-classpath-file-access) - [Controlling Bean Creation Order with @DependsOn Annotation](http://www.baeldung.com/spring-depends-on) +- [Spring Autowiring of Generic Types](https://www.baeldung.com/spring-autowire-generics) +- [Spring Application Context Events](https://www.baeldung.com/spring-context-events) diff --git a/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java b/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java index 1e6fad63aa..e0fe01acdd 100644 --- a/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java +++ b/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java @@ -1,13 +1,14 @@ package com.baeldung.dependency.exception.app; import com.baeldung.dependency.exception.repository.InventoryRepository; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service public class PurchaseDeptService { private InventoryRepository repository; - public PurchaseDeptService(InventoryRepository repository) { + public PurchaseDeptService(@Qualifier("dresses") InventoryRepository repository) { this.repository = repository; } } \ No newline at end of file diff --git a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java index 4a6c836143..5a1371ce04 100644 --- a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java +++ b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java @@ -1,9 +1,10 @@ package com.baeldung.dependency.exception.repository; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Repository; -@Primary +@Qualifier("dresses") @Repository public class DressRepository implements InventoryRepository { } diff --git a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java index 60495914cd..227d8934b6 100644 --- a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java +++ b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java @@ -1,7 +1,9 @@ package com.baeldung.dependency.exception.repository; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Repository; +@Qualifier("shoes") @Repository public class ShoeRepository implements InventoryRepository { } diff --git a/spring-data-jpa/README.md b/spring-data-jpa/README.md index f54764e05c..20e9d41839 100644 --- a/spring-data-jpa/README.md +++ b/spring-data-jpa/README.md @@ -10,6 +10,7 @@ - [Spring Data Annotations](http://www.baeldung.com/spring-data-annotations) - [Spring Data Java 8 Support](http://www.baeldung.com/spring-data-java-8) - [A Simple Tagging Implementation with JPA](http://www.baeldung.com/jpa-tagging) +- [Spring Data Composable Repositories](https://www.baeldung.com/spring-data-composable-repositories) ### Eclipse Config After importing the project into Eclipse, you may see the following error: diff --git a/spring-data-jpa/pom.xml b/spring-data-jpa/pom.xml index c4893df759..8691ce1f09 100644 --- a/spring-data-jpa/pom.xml +++ b/spring-data-jpa/pom.xml @@ -41,6 +41,26 @@ guava 21.0 + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + + + org.junit.platform + junit-platform-launcher + ${junit-platform.version} + test + \ No newline at end of file diff --git a/spring-data-jpa/src/main/java/com/baeldung/Application.java b/spring-data-jpa/src/main/java/com/baeldung/Application.java index 4e14f94311..72d29d9fa5 100644 --- a/spring-data-jpa/src/main/java/com/baeldung/Application.java +++ b/spring-data-jpa/src/main/java/com/baeldung/Application.java @@ -1,11 +1,11 @@ package com.baeldung; -import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl; + @SpringBootApplication @EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class) public class Application { diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate.java new file mode 100644 index 0000000000..bf6ff0a0b9 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate.java @@ -0,0 +1,46 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Transient; + +import org.springframework.context.ApplicationEventPublisher; + +@Entity +class Aggregate { + @Transient + private ApplicationEventPublisher eventPublisher; + @Id + private long id; + + private Aggregate() { + } + + Aggregate(long id, ApplicationEventPublisher eventPublisher) { + this.id = id; + this.eventPublisher = eventPublisher; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "DomainEntity [id=" + id + "]"; + } + + void domainOperation() { + // some business logic + if (eventPublisher != null) { + eventPublisher.publishEvent(new DomainEvent()); + } + } + + long getId() { + return id; + } + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2.java new file mode 100644 index 0000000000..3d2816299a --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2.java @@ -0,0 +1,44 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import java.util.ArrayList; +import java.util.Collection; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Transient; + +import org.springframework.data.domain.AfterDomainEventPublication; +import org.springframework.data.domain.DomainEvents; + +@Entity +public class Aggregate2 { + @Transient + private final Collection domainEvents; + @Id + @GeneratedValue + private long id; + + public Aggregate2() { + domainEvents = new ArrayList<>(); + } + + @AfterDomainEventPublication + public void clearEvents() { + domainEvents.clear(); + } + + public void domainOperation() { + // some domain operation + domainEvents.add(new DomainEvent()); + } + + @DomainEvents + public Collection events() { + return domainEvents; + } + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2Repository.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2Repository.java new file mode 100644 index 0000000000..2a95abe347 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate2Repository.java @@ -0,0 +1,10 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import org.springframework.data.repository.CrudRepository; + +public interface Aggregate2Repository extends CrudRepository { + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3.java new file mode 100644 index 0000000000..e0c3131b06 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3.java @@ -0,0 +1,23 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import org.springframework.data.domain.AbstractAggregateRoot; + +@Entity +public class Aggregate3 extends AbstractAggregateRoot { + @Id + @GeneratedValue + private long id; + + public void domainOperation() { + // some domain operation + registerEvent(new DomainEvent()); + } + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3Repository.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3Repository.java new file mode 100644 index 0000000000..e442bdb210 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/Aggregate3Repository.java @@ -0,0 +1,14 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import org.springframework.data.repository.CrudRepository; + +/** + * @author goobar + * + */ +public interface Aggregate3Repository extends CrudRepository { + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/AggregateRepository.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/AggregateRepository.java new file mode 100644 index 0000000000..5a15156d03 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/AggregateRepository.java @@ -0,0 +1,10 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import org.springframework.data.repository.CrudRepository; + +public interface AggregateRepository extends CrudRepository { + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DddConfig.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DddConfig.java new file mode 100644 index 0000000000..1315b11875 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DddConfig.java @@ -0,0 +1,15 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.PropertySource; + +@SpringBootConfiguration +@EnableAutoConfiguration +@PropertySource("classpath:/ddd.properties") +public class DddConfig { + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainEvent.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainEvent.java new file mode 100644 index 0000000000..1e6479d4fc --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainEvent.java @@ -0,0 +1,8 @@ +/** + * + */ +package com.baeldung.ddd.event; + +class DomainEvent { + +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainService.java b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainService.java new file mode 100644 index 0000000000..082c9bd88e --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/ddd/event/DomainService.java @@ -0,0 +1,31 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import javax.transaction.Transactional; + +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Service; + +@Service +public class DomainService { + private final ApplicationEventPublisher eventPublisher; + private final AggregateRepository repository; + + public DomainService(AggregateRepository repository, ApplicationEventPublisher eventPublisher) { + this.repository = repository; + this.eventPublisher = eventPublisher; + } + + @Transactional + public void serviceDomainOperation(long entityId) { + repository.findById(entityId) + .ifPresent(entity -> { + entity.domainOperation(); + repository.save(entity); + eventPublisher.publishEvent(new DomainEvent()); + }); + } + +} diff --git a/spring-data-jpa/src/main/resources/ddd.properties b/spring-data-jpa/src/main/resources/ddd.properties new file mode 100644 index 0000000000..e5126b694b --- /dev/null +++ b/spring-data-jpa/src/main/resources/ddd.properties @@ -0,0 +1 @@ +spring.datasource.initialization-mode=never \ No newline at end of file diff --git a/spring-data-jpa/src/main/resources/persistence-multiple-db.properties b/spring-data-jpa/src/main/resources/persistence-multiple-db.properties index 53c6cecf8b..75534e8a54 100644 --- a/spring-data-jpa/src/main/resources/persistence-multiple-db.properties +++ b/spring-data-jpa/src/main/resources/persistence-multiple-db.properties @@ -3,7 +3,7 @@ jdbc.driverClassName=org.h2.Driver user.jdbc.url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS product.jdbc.url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS PRODUCTS jdbc.user=sa -jdbc.pass= +jdbc.pass=sa # hibernate.X hibernate.dialect=org.hibernate.dialect.H2Dialect diff --git a/spring-data-jpa/src/main/resources/persistence.properties b/spring-data-jpa/src/main/resources/persistence.properties index 5e83653401..3543e1b52b 100644 --- a/spring-data-jpa/src/main/resources/persistence.properties +++ b/spring-data-jpa/src/main/resources/persistence.properties @@ -2,7 +2,7 @@ jdbc.driverClassName=org.h2.Driver jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS jdbc.user=sa -jdbc.pass= +jdbc.pass=sa # hibernate.X hibernate.dialect=org.hibernate.dialect.H2Dialect diff --git a/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/JpaRepositoriesIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/JpaRepositoriesIntegrationTest.java index eaadb9e44a..01405c0b8a 100644 --- a/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/JpaRepositoriesIntegrationTest.java +++ b/spring-data-jpa/src/test/java/com/baeldung/dao/repositories/JpaRepositoriesIntegrationTest.java @@ -9,28 +9,22 @@ import static junit.framework.TestCase.assertTrue; import java.util.List; import java.util.Optional; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.config.PersistenceProductConfiguration; -import com.baeldung.config.PersistenceUserConfiguration; -import com.baeldung.dao.repositories.ItemTypeRepository; -import com.baeldung.dao.repositories.LocationRepository; -import com.baeldung.dao.repositories.ReadOnlyLocationRepository; -import com.baeldung.dao.repositories.StoreRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import com.baeldung.config.PersistenceConfiguration; +import com.baeldung.config.PersistenceProductConfiguration; +import com.baeldung.config.PersistenceUserConfiguration; import com.baeldung.domain.Item; import com.baeldung.domain.ItemType; import com.baeldung.domain.Location; import com.baeldung.domain.Store; @RunWith(SpringRunner.class) -@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class}) +@DataJpaTest(excludeAutoConfiguration = { PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class }) public class JpaRepositoriesIntegrationTest { @Autowired private LocationRepository locationRepository; diff --git a/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate2EventsIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate2EventsIntegrationTest.java new file mode 100644 index 0000000000..3f650d4d63 --- /dev/null +++ b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate2EventsIntegrationTest.java @@ -0,0 +1,68 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +@SpringJUnitConfig +@SpringBootTest +class Aggregate2EventsIntegrationTest { + @MockBean + private TestEventHandler eventHandler; + @Autowired + private Aggregate2Repository repository; + + // @formatter:off + @DisplayName("given aggregate with @AfterDomainEventPublication," + + " when do domain operation and save twice," + + " then an event is published only for the first time") + // @formatter:on + @Test + void afterDomainEvents() { + // given + Aggregate2 aggregate = new Aggregate2(); + + // when + aggregate.domainOperation(); + repository.save(aggregate); + repository.save(aggregate); + + // then + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); + } + + @BeforeEach + void beforeEach() { + repository.deleteAll(); + } + + // @formatter:off + @DisplayName("given aggregate with @DomainEvents," + + " when do domain operation and save," + + " then an event is published") + // @formatter:on + @Test + void domainEvents() { + // given + Aggregate2 aggregate = new Aggregate2(); + + // when + aggregate.domainOperation(); + repository.save(aggregate); + + // then + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); + } + +} diff --git a/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate3EventsIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate3EventsIntegrationTest.java new file mode 100644 index 0000000000..893dcac3f8 --- /dev/null +++ b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/Aggregate3EventsIntegrationTest.java @@ -0,0 +1,63 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +@SpringJUnitConfig +@SpringBootTest +class Aggregate3EventsIntegrationTest { + + @MockBean + private TestEventHandler eventHandler; + @Autowired + private Aggregate3Repository repository; + + // @formatter:off + @DisplayName("given aggregate extending AbstractAggregateRoot," + + " when do domain operation and save twice," + + " then an event is published only for the first time") + // @formatter:on + @Test + void afterDomainEvents() { + // given + Aggregate3 aggregate = new Aggregate3(); + + // when + aggregate.domainOperation(); + repository.save(aggregate); + repository.save(aggregate); + + // then + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); + } + + // @formatter:off + @DisplayName("given aggregate extending AbstractAggregateRoot," + + " when do domain operation and save," + + " then an event is published") + // @formatter:on + @Test + void domainEvents() { + // given + Aggregate3 aggregate = new Aggregate3(); + + // when + aggregate.domainOperation(); + repository.save(aggregate); + + // then + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); + } + +} diff --git a/spring-data-jpa/src/test/java/com/baeldung/ddd/event/AggregateEventsIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/AggregateEventsIntegrationTest.java new file mode 100644 index 0000000000..f0e1147245 --- /dev/null +++ b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/AggregateEventsIntegrationTest.java @@ -0,0 +1,82 @@ +package com.baeldung.ddd.event; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +@SpringJUnitConfig +@SpringBootTest +class AggregateEventsIntegrationTest { + + @Autowired + private DomainService domainService; + + @MockBean + private TestEventHandler eventHandler; + @Autowired + private ApplicationEventPublisher eventPublisher; + @Autowired + private AggregateRepository repository; + + // @formatter:off + @DisplayName("given existing aggregate," + + " when do domain operation directly on aggregate," + + " then domain event is NOT published") + // @formatter:on + @Test + void aggregateEventsTest() { + Aggregate existingDomainEntity = new Aggregate(0, eventPublisher); + repository.save(existingDomainEntity); + + // when + repository.findById(existingDomainEntity.getId()) + .get() + .domainOperation(); + + // then + verifyZeroInteractions(eventHandler); + } + + @BeforeEach + void beforeEach() { + repository.deleteAll(); + } + + // @formatter:off + @DisplayName("given existing aggregate," + + " when do domain operation on service," + + " then domain event is published") + // @formatter:on + @Test + void serviceEventsTest() { + Aggregate existingDomainEntity = new Aggregate(1, eventPublisher); + repository.save(existingDomainEntity); + + // when + domainService.serviceDomainOperation(existingDomainEntity.getId()); + + // then + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); + } + + @TestConfiguration + public static class TestConfig { + @Bean + public DomainService domainService(AggregateRepository repository, ApplicationEventPublisher eventPublisher) { + return new DomainService(repository, eventPublisher); + } + } + +} diff --git a/spring-data-jpa/src/test/java/com/baeldung/ddd/event/TestEventHandler.java b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/TestEventHandler.java new file mode 100644 index 0000000000..721402c17a --- /dev/null +++ b/spring-data-jpa/src/test/java/com/baeldung/ddd/event/TestEventHandler.java @@ -0,0 +1,12 @@ +/** + * + */ +package com.baeldung.ddd.event; + +import org.springframework.transaction.event.TransactionalEventListener; + +interface TestEventHandler { + @TransactionalEventListener + void handleEvent(DomainEvent event); + +} diff --git a/spring-data-jpa/src/test/java/com/baeldung/services/JpaMultipleDBIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/services/JpaMultipleDBIntegrationTest.java index 29b96ae597..71a3fb0b44 100644 --- a/spring-data-jpa/src/test/java/com/baeldung/services/JpaMultipleDBIntegrationTest.java +++ b/spring-data-jpa/src/test/java/com/baeldung/services/JpaMultipleDBIntegrationTest.java @@ -1,31 +1,33 @@ package com.baeldung.services; -import com.baeldung.config.PersistenceProductConfiguration; -import com.baeldung.config.PersistenceUserConfiguration; -import com.baeldung.dao.repositories.user.PossessionRepository; -import com.baeldung.dao.repositories.product.ProductRepository; -import com.baeldung.dao.repositories.user.UserRepository; -import com.baeldung.domain.user.Possession; -import com.baeldung.domain.product.Product; -import com.baeldung.domain.user.User; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Collections; +import java.util.Optional; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; -import java.util.Collections; -import java.util.Optional; - -import static org.junit.Assert.*; +import com.baeldung.config.PersistenceProductConfiguration; +import com.baeldung.config.PersistenceUserConfiguration; +import com.baeldung.dao.repositories.product.ProductRepository; +import com.baeldung.dao.repositories.user.PossessionRepository; +import com.baeldung.dao.repositories.user.UserRepository; +import com.baeldung.domain.product.Product; +import com.baeldung.domain.user.Possession; +import com.baeldung.domain.user.User; @RunWith(SpringRunner.class) -@ContextConfiguration(classes = {PersistenceUserConfiguration.class, PersistenceProductConfiguration.class}) +@ContextConfiguration(classes = { PersistenceUserConfiguration.class, PersistenceProductConfiguration.class }) @EnableTransactionManagement @DirtiesContext public class JpaMultipleDBIntegrationTest { diff --git a/spring-data-jpa/src/test/java/com/baeldung/services/SpringDataJPABarAuditIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/services/SpringDataJPABarAuditIntegrationTest.java index 3c36f43192..f3b857c73d 100644 --- a/spring-data-jpa/src/test/java/com/baeldung/services/SpringDataJPABarAuditIntegrationTest.java +++ b/spring-data-jpa/src/test/java/com/baeldung/services/SpringDataJPABarAuditIntegrationTest.java @@ -1,8 +1,16 @@ package com.baeldung.services; -import com.baeldung.config.PersistenceConfiguration; -import com.baeldung.domain.Bar; -import org.junit.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,15 +18,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import com.baeldung.config.PersistenceConfiguration; +import com.baeldung.domain.Bar; @RunWith(SpringRunner.class) @ContextConfiguration(classes = { PersistenceConfiguration.class }, loader = AnnotationConfigContextLoader.class) diff --git a/spring-freemarker/pom.xml b/spring-freemarker/pom.xml index 96607f7f10..f0626195fd 100644 --- a/spring-freemarker/pom.xml +++ b/spring-freemarker/pom.xml @@ -1,7 +1,7 @@ 4.0.0 - com.freemarker.example + com.baeldung.freemarker spring-freemarker war 1.0-SNAPSHOT @@ -48,10 +48,10 @@ - 4.3.4.RELEASE - 2.3.23 + 5.0.8.RELEASE + 2.3.28 3.1.0 false - \ No newline at end of file + diff --git a/spring-integration/README.md b/spring-integration/README.md index d782862901..e116f934c8 100644 --- a/spring-integration/README.md +++ b/spring-integration/README.md @@ -1,6 +1,7 @@ ### Relevant Articles: - [Introduction to Spring Integration](http://www.baeldung.com/spring-integration) - [Security In Spring Integration](http://www.baeldung.com/spring-integration-security) +- [Spring Integration Java DSL](https://www.baeldung.com/spring-integration-java-dsl) ### Running the Sample Executing the `mvn exec:java` maven command (either from the command line or from an IDE) will start up the application. Follow the command prompt for further instructions. diff --git a/spring-jersey/README.md b/spring-jersey/README.md index 8b2eecc0e1..37928620d3 100644 --- a/spring-jersey/README.md +++ b/spring-jersey/README.md @@ -3,3 +3,4 @@ ## REST API with Jersey & Spring Example Project - [REST API with Jersey and Spring](http://www.baeldung.com/jersey-rest-api-with-spring) - [JAX-RS Client with Jersey](http://www.baeldung.com/jersey-jax-rs-client) +- [Reactive JAX-RS Client API](https://www.baeldung.com/jax-rs-reactive-client) diff --git a/spring-jooq/pom.xml b/spring-jooq/pom.xml index e1c13ef774..744ae34cc8 100644 --- a/spring-jooq/pom.xml +++ b/spring-jooq/pom.xml @@ -2,8 +2,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-jooq - 0.0.1-SNAPSHOT - + parent-boot-1 com.baeldung @@ -54,11 +53,6 @@ spring-test test - - org.springframework.boot - spring-boot-starter-test - - diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index c4a0e3579c..44b1d65bc8 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -28,10 +28,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [The Spring @Controller and @RestController Annotations](http://www.baeldung.com/spring-controller-vs-restcontroller) - [Spring MVC @PathVariable with a dot (.) gets truncated](http://www.baeldung.com/spring-mvc-pathvariable-dot) - [A Quick Example of Spring Websockets’ @SendToUser Annotation](http://www.baeldung.com/spring-websockets-sendtouser) -- [Spring Boot Annotations](http://www.baeldung.com/spring-boot-annotations) -- [Spring Scheduling Annotations](http://www.baeldung.com/spring-scheduling-annotations) -- [Spring Web Annotations](http://www.baeldung.com/spring-mvc-annotations) -- [Spring Core Annotations](http://www.baeldung.com/spring-core-annotations) - [Using Spring ResponseEntity to Manipulate the HTTP Response](http://www.baeldung.com/spring-response-entity) - [Using Spring @ResponseStatus to Set HTTP Status Code](http://www.baeldung.com/spring-response-status) -- [Display RSS Feed with Spring MVC](http://www.baeldung.com/spring-mvc-rss-feed) diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 83f2556fe0..9d3e0ca1b2 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -5,70 +5,54 @@ spring-mvc-java 0.1-SNAPSHOT spring-mvc-java + war - parent-boot-2 + parent-spring-5 com.baeldung 0.0.1-SNAPSHOT - ../parent-boot-2 + ../parent-spring-5 - - - org.springframework.boot - spring-boot-starter-thymeleaf + + org.springframework + spring-webmvc + ${spring.version} - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-devtools - - - org.springframework.boot - spring-boot-test - test - - - org.springframework.boot - spring-boot-starter-aop - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-websocket - - - - - com.fasterxml.jackson.core - jackson-databind - - - javax.servlet javax.servlet-api + 4.0.1 + + + javax.servlet.jsp + javax.servlet.jsp-api + 2.3.3 javax.servlet jstl + ${jstl.version} + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson-databind.version} org.aspectj aspectjrt + 1.9.1 org.aspectj aspectjweaver + 1.9.1 @@ -80,6 +64,7 @@ net.sourceforge.htmlunit htmlunit + 2.32 commons-logging @@ -104,10 +89,12 @@ com.jayway.jsonpath json-path test + 2.4.0 org.springframework spring-test + ${spring.version} test @@ -120,34 +107,29 @@ - org.hibernate + org.hibernate.validator hibernate-validator ${hibernate-validator.version} - - javax.el - javax.el-api - ${javax.el.version} - - - org.glassfish.web - javax.el - ${javax.el.version} - com.google.code.gson gson + 2.8.5 - - - - com.rometools - rome - ${rome.version} - + + org.springframework + spring-websocket + ${spring.version} + + + + org.springframework + spring-messaging + ${spring.version} + @@ -169,6 +151,7 @@ org.apache.maven.plugins maven-war-plugin + ${maven-war-plugin.version} false @@ -262,7 +245,7 @@ 3.0.9.RELEASE - 5.2.5.Final + 6.0.10.Final 5.1.40 @@ -281,24 +264,19 @@ 2.23 - 2.6 + 3.2.2 2.7 1.6.1 3.1.0 - 1.8.9 + 1.9.1 3.16-beta1 - - 1.10.0 - - 2.2.4 + 3.0.1-b06 - - com.baeldung.app.Application diff --git a/spring-mvc-java/src/main/java/com/baeldung/app/Application.java b/spring-mvc-java/src/main/java/com/baeldung/app/Application.java deleted file mode 100644 index 68d078de78..0000000000 --- a/spring-mvc-java/src/main/java/com/baeldung/app/Application.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.app; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; -import org.springframework.context.annotation.ComponentScan; - -@EnableAutoConfiguration -@ComponentScan(value = {"com.baeldung.web.controller"}, resourcePattern = "**/FileUploadController.class") -@SpringBootApplication -public class Application extends SpringBootServletInitializer { - - public static void main(final String[] args) { - SpringApplication.run(Application.class, args); - } -} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/config/AppInitializer.java b/spring-mvc-java/src/main/java/com/baeldung/config/AppInitializer.java index 9cf6e384f1..eec12f466f 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/config/AppInitializer.java +++ b/spring-mvc-java/src/main/java/com/baeldung/config/AppInitializer.java @@ -14,13 +14,20 @@ public class AppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.setConfigLocation("org.baeldung.config"); + + context.scan("com.baeldung"); container.addListener(new ContextLoaderListener(context)); - ServletRegistration.Dynamic dispatcher = container.addServlet("java-servlet", new DispatcherServlet(context)); + ServletRegistration.Dynamic dispatcher = container.addServlet("mvc", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); - dispatcher.addMapping("/java-servlet/*"); + dispatcher.addMapping("/"); + + // final MultipartConfigElement multipartConfigElement = new + // MultipartConfigElement(TMP_FOLDER, MAX_UPLOAD_SIZE, + // MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2); + // + // appServlet.setMultipartConfig(multipartConfigElement); } } diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationConfig.java deleted file mode 100644 index 261d5793dc..0000000000 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationConfig.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.spring.web.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.servlet.view.JstlView; - -@EnableWebMvc -@Configuration -@ComponentScan(basePackages = { "com.baeldung.web.controller" }) -public class ApplicationConfig extends WebMvcConfigurerAdapter { - - public ApplicationConfig() { - super(); - } - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - registry.addViewController("/").setViewName("index"); - } - - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/jsp/"); - bean.setSuffix(".jsp"); - return bean; - } -} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java deleted file mode 100644 index de47f9f69e..0000000000 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.baeldung.spring.web.config; - -import javax.servlet.ServletContext; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.MessageSource; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Description; -import org.springframework.context.support.ResourceBundleMessageSource; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.servlet.view.JstlView; -import org.thymeleaf.spring4.SpringTemplateEngine; -import org.thymeleaf.spring4.view.ThymeleafViewResolver; -import org.thymeleaf.templateresolver.ServletContextTemplateResolver; - -@EnableWebMvc -@Configuration -public class ClientWebConfig implements WebMvcConfigurer { - - public ClientWebConfig() { - super(); - } - - // API - - @Autowired - private ServletContext ctx; - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - - registry.addViewController("/sample.html"); - } - - @Bean - public ViewResolver thymeleafViewResolver() { - final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); - viewResolver.setTemplateEngine(templateEngine()); - viewResolver.setOrder(1); - return viewResolver; - } - - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(0); - return bean; - } - - @Bean - @Description("Thymeleaf template resolver serving HTML 5") - public ServletContextTemplateResolver templateResolver() { - final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(ctx); - templateResolver.setPrefix("/WEB-INF/templates/"); - templateResolver.setSuffix(".html"); - templateResolver.setTemplateMode("HTML5"); - return templateResolver; - } - - @Bean - @Description("Thymeleaf template engine with Spring integration") - public SpringTemplateEngine templateEngine() { - final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); - templateEngine.setTemplateResolver(templateResolver()); - return templateEngine; - } - - @Bean - @Description("Spring message resolver") - public MessageSource messageSource() { - final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); - messageSource.setBasename("messages"); - return messageSource; - } - - @Override - public void addResourceHandlers(final ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); - } - -} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java deleted file mode 100644 index 498105ded1..0000000000 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.baeldung.spring.web.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.servlet.view.JstlView; - -@EnableWebMvc -@Configuration -public class ContentManagementWebConfig extends WebMvcConfigurerAdapter { - - public ContentManagementWebConfig() { - super(); - } - - // API - - @Override - public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { - configurer.favorPathExtension(false).favorParameter(true).parameterName("mediaType").ignoreAcceptHeader(true).useJaf(false).defaultContentType(MediaType.APPLICATION_JSON).mediaType("xml", MediaType.APPLICATION_XML).mediaType("json", - MediaType.APPLICATION_JSON); - } - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } - - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(0); - return bean; - } - -} diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java deleted file mode 100644 index 80ce22edd6..0000000000 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.spring.web.config; - -import org.springframework.web.WebApplicationInitializer; -import org.springframework.web.context.ContextLoaderListener; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.context.support.GenericWebApplicationContext; -import org.springframework.web.servlet.DispatcherServlet; - -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRegistration; -import java.util.Set; - -public class MainWebAppInitializer implements WebApplicationInitializer { - - private static final String TMP_FOLDER = "/tmp"; - private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB - - /** - * Register and configure all Servlet container components necessary to power the web application. - */ - @Override - public void onStartup(final ServletContext sc) throws ServletException { - - // Create the 'root' Spring application context - final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); - root.scan("com.baeldung.spring.web.config"); - // root.getEnvironment().setDefaultProfiles("embedded"); - - sc.addListener(new ContextLoaderListener(root)); - - // Handles requests into the application - final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); - appServlet.setLoadOnStartup(1); - - // final MultipartConfigElement multipartConfigElement = new - // MultipartConfigElement(TMP_FOLDER, MAX_UPLOAD_SIZE, - // MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2); - // - // appServlet.setMultipartConfig(multipartConfigElement); - - final Set mappingConflicts = appServlet.addMapping("/"); - if (!mappingConflicts.isEmpty()) { - throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); - } - } - -} diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java index 11be08a79d..191d721dfb 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -3,40 +3,105 @@ package com.baeldung.spring.web.config; import java.util.ArrayList; import java.util.List; +import javax.servlet.ServletContext; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Description; +import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.core.io.ClassPathResource; import org.springframework.http.MediaType; import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; import org.springframework.web.servlet.view.ResourceBundleViewResolver; import org.springframework.web.servlet.view.XmlViewResolver; import org.springframework.web.util.UrlPathHelper; -import com.baeldung.excel.*; +import org.thymeleaf.spring4.SpringTemplateEngine; +import org.thymeleaf.spring4.view.ThymeleafViewResolver; +import org.thymeleaf.templateresolver.ServletContextTemplateResolver; + +import com.baeldung.excel.ExcelPOIHelper; -@Configuration @EnableWebMvc -@ComponentScan("com.baeldung.web") -public class WebConfig extends WebMvcConfigurerAdapter { +@Configuration +@ComponentScan(basePackages = { "com.baeldung.web.controller" }) +public class WebConfig implements WebMvcConfigurer { - public WebConfig() { - super(); + @Autowired + private ServletContext ctx; + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("index"); + } + + @Bean + public ViewResolver thymeleafViewResolver() { + final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); + viewResolver.setTemplateEngine(templateEngine()); + viewResolver.setOrder(1); + return viewResolver; } - // @Bean - // public StandardServletMultipartResolver multipartResolver() { - // return new StandardServletMultipartResolver(); - // } + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + bean.setOrder(0); + return bean; + } + @Bean + @Description("Thymeleaf template resolver serving HTML 5") + public ServletContextTemplateResolver templateResolver() { + final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(ctx); + templateResolver.setPrefix("/WEB-INF/templates/"); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode("HTML5"); + return templateResolver; + } + + @Bean + @Description("Thymeleaf template engine with Spring integration") + public SpringTemplateEngine templateEngine() { + final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); + templateEngine.setTemplateResolver(templateResolver()); + return templateEngine; + } + + @Bean + @Description("Spring message resolver") + public MessageSource messageSource() { + final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); + messageSource.setBasename("messages"); + return messageSource; + } + + @Override + public void addResourceHandlers(final ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + + @Override + public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { + configurer.favorPathExtension(false).favorParameter(true).parameterName("mediaType").ignoreAcceptHeader(true).useRegisteredExtensionsOnly(false).defaultContentType(MediaType.APPLICATION_JSON).mediaType("xml", MediaType.APPLICATION_XML).mediaType("json", + MediaType.APPLICATION_JSON); + } @Bean(name = "multipartResolver") public CommonsMultipartResolver multipartResolver() { @@ -45,23 +110,7 @@ public class WebConfig extends WebMvcConfigurerAdapter { return multipartResolver; } - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } - - @Bean - public ViewResolver internalResourceViewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(2); - return bean; - } - + @Bean public ViewResolver xmlViewResolver() { final XmlViewResolver bean = new XmlViewResolver(); @@ -112,5 +161,4 @@ public class WebConfig extends WebMvcConfigurerAdapter { public ExcelPOIHelper excelPOIHelper() { return new ExcelPOIHelper(); } - -} +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java index 93ec13da58..0793658e90 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java @@ -2,13 +2,13 @@ package com.baeldung.spring.web.config; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; -import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; @Configuration @EnableWebSocketMessageBroker -public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { +public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(final MessageBrokerRegistry config) { diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java index 7f14380e5e..dbd52e20ba 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java @@ -6,9 +6,9 @@ import org.springframework.http.server.ServerHttpResponse; import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.WebSocketHandler; -import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; import org.springframework.web.socket.server.support.DefaultHandshakeHandler; import javax.servlet.http.HttpSession; @@ -16,7 +16,7 @@ import java.util.Map; @Configuration @EnableWebSocketMessageBroker -public class WebSocketSendToUserConfig extends AbstractWebSocketMessageBrokerConfigurer { +public class WebSocketSendToUserConfig implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/SampleController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/SampleController.java new file mode 100644 index 0000000000..c13986e005 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/SampleController.java @@ -0,0 +1,13 @@ +package com.baeldung.web.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class SampleController { + @GetMapping("/sample") + public String showForm() { + return "sample"; + } + +} diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java b/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java index b628228b7e..384bd85ec6 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java @@ -20,12 +20,12 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import com.baeldung.spring.web.config.ApplicationConfig; +import com.baeldung.spring.web.config.WebConfig; import com.baeldung.spring.web.config.WebConfig; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration(classes = { ApplicationConfig.class, WebConfig.class }) +@ContextConfiguration(classes = { WebConfig.class, WebConfig.class }) public class GreetControllerIntegrationTest { @Autowired diff --git a/spring-remoting/pom.xml b/spring-remoting/pom.xml index 8db51e2a16..9f05bc91d2 100644 --- a/spring-remoting/pom.xml +++ b/spring-remoting/pom.xml @@ -32,7 +32,7 @@ remoting-hessian-burlap remoting-amqp remoting-jms - spring-remoting-rmi + remoting-rmi \ No newline at end of file diff --git a/spring-remoting/remoting-amqp/remoting-amqp-client/pom.xml b/spring-remoting/remoting-amqp/remoting-amqp-client/pom.xml index b715327c28..98e0bc5df5 100644 --- a/spring-remoting/remoting-amqp/remoting-amqp-client/pom.xml +++ b/spring-remoting/remoting-amqp/remoting-amqp-client/pom.xml @@ -26,7 +26,7 @@ com.baeldung - api + remoting-http-api ${project.version} diff --git a/spring-remoting/remoting-amqp/remoting-amqp-server/pom.xml b/spring-remoting/remoting-amqp/remoting-amqp-server/pom.xml index a456ba8d2c..7fe276f824 100644 --- a/spring-remoting/remoting-amqp/remoting-amqp-server/pom.xml +++ b/spring-remoting/remoting-amqp/remoting-amqp-server/pom.xml @@ -16,7 +16,7 @@ com.baeldung - api + remoting-http-api ${project.version} @@ -25,13 +25,13 @@ com.baeldung - spring-remoting-http-server - ${spring-remoting-http-server.version} + remoting-http-server + ${remoting-http-server.version} - 1.0-SNAPSHOT + 1.0-SNAPSHOT diff --git a/spring-remoting/remoting-hessian-burlap/pom.xml b/spring-remoting/remoting-hessian-burlap/pom.xml index 682e460880..6cb5a51812 100644 --- a/spring-remoting/remoting-hessian-burlap/pom.xml +++ b/spring-remoting/remoting-hessian-burlap/pom.xml @@ -2,19 +2,18 @@ - + 4.0.0 + remoting-hessian-burlap + pom + + spring-remoting com.baeldung 1.0-SNAPSHOT - pom + - server - client + remoting-hessian-burlap-server + remoting-hessian-burlap-client - 4.0.0 - - remoting-hessian-burlap - - \ No newline at end of file diff --git a/spring-remoting/remoting-hessian-burlap/client/pom.xml b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/pom.xml similarity index 95% rename from spring-remoting/remoting-hessian-burlap/client/pom.xml rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/pom.xml index 1ae9b10019..f337d5e51f 100644 --- a/spring-remoting/remoting-hessian-burlap/client/pom.xml +++ b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/pom.xml @@ -24,7 +24,8 @@ ${project.groupId} - api + remoting-http-api + ${project.version} com.caucho diff --git a/spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/BurlapClient.java b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/java/com/baeldung/client/BurlapClient.java similarity index 100% rename from spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/BurlapClient.java rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/java/com/baeldung/client/BurlapClient.java diff --git a/spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/HessianClient.java b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/java/com/baeldung/client/HessianClient.java similarity index 100% rename from spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/HessianClient.java rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/java/com/baeldung/client/HessianClient.java diff --git a/meecrowave/src/main/resources/logback.xml b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/resources/logback.xml similarity index 100% rename from meecrowave/src/main/resources/logback.xml rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/main/resources/logback.xml diff --git a/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java similarity index 100% rename from spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java diff --git a/spring-remoting/remoting-hessian-burlap/client/src/test/resources/application.properties b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/test/resources/application.properties similarity index 100% rename from spring-remoting/remoting-hessian-burlap/client/src/test/resources/application.properties rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-client/src/test/resources/application.properties diff --git a/spring-remoting/remoting-hessian-burlap/server/pom.xml b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/pom.xml similarity index 94% rename from spring-remoting/remoting-hessian-burlap/server/pom.xml rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/pom.xml index f1fed73ed8..5b6e03348f 100644 --- a/spring-remoting/remoting-hessian-burlap/server/pom.xml +++ b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/pom.xml @@ -14,7 +14,7 @@ com.baeldung - spring-remoting-http-server + remoting-http-server ${project.version} diff --git a/spring-remoting/remoting-hessian-burlap/server/src/main/java/com/baeldung/server/Server.java b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/src/main/java/com/baeldung/server/Server.java similarity index 100% rename from spring-remoting/remoting-hessian-burlap/server/src/main/java/com/baeldung/server/Server.java rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/src/main/java/com/baeldung/server/Server.java diff --git a/spring-cloud/spring-cloud-stream-starters/boot/src/main/resources/logback.xml b/spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/src/main/resources/logback.xml similarity index 100% rename from spring-cloud/spring-cloud-stream-starters/boot/src/main/resources/logback.xml rename to spring-remoting/remoting-hessian-burlap/remoting-hessian-burlap-server/src/main/resources/logback.xml diff --git a/spring-remoting/remoting-http/pom.xml b/spring-remoting/remoting-http/pom.xml index 95b5ad3e14..3262736ec8 100644 --- a/spring-remoting/remoting-http/pom.xml +++ b/spring-remoting/remoting-http/pom.xml @@ -3,17 +3,19 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - - com.baeldung - remoting-http - 1.0-SNAPSHOT - - spring-remoting-http + remoting-http Parent for all modules related to HTTP Spring Remoting. pom + + + com.baeldung + spring-remoting + 1.0-SNAPSHOT + + - server - client - api + remoting-http-server + remoting-http-client + remoting-http-api \ No newline at end of file diff --git a/spring-remoting/remoting-http/api/pom.xml b/spring-remoting/remoting-http/remoting-http-api/pom.xml similarity index 77% rename from spring-remoting/remoting-http/api/pom.xml rename to spring-remoting/remoting-http/remoting-http-api/pom.xml index 633217f7de..579255060d 100644 --- a/spring-remoting/remoting-http/api/pom.xml +++ b/spring-remoting/remoting-http/remoting-http-api/pom.xml @@ -2,13 +2,15 @@ - - spring-remoting-http + 4.0.0 + remoting-http-api + remoting-http-api + API definition shared between client and server. + + + remoting-http com.baeldung 1.0-SNAPSHOT - 4.0.0 - api - spring-remoting-http-api - API definition shared between client and server. + \ No newline at end of file diff --git a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Booking.java b/spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/Booking.java similarity index 100% rename from spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/Booking.java rename to spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/Booking.java diff --git a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/BookingException.java b/spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/BookingException.java similarity index 100% rename from spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/BookingException.java rename to spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/BookingException.java diff --git a/spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/CabBookingService.java b/spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/CabBookingService.java similarity index 100% rename from spring-remoting/remoting-http/api/src/main/java/com/baeldung/api/CabBookingService.java rename to spring-remoting/remoting-http/remoting-http-api/src/main/java/com/baeldung/api/CabBookingService.java diff --git a/spring-remoting/remoting-http/client/pom.xml b/spring-remoting/remoting-http/remoting-http-client/pom.xml similarity index 83% rename from spring-remoting/remoting-http/client/pom.xml rename to spring-remoting/remoting-http/remoting-http-client/pom.xml index 94a536fc18..9a6e98ffdd 100644 --- a/spring-remoting/remoting-http/client/pom.xml +++ b/spring-remoting/remoting-http/remoting-http-client/pom.xml @@ -3,14 +3,15 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - + remoting-http-client + Shows how to invoke a remote service using Spring Remoting HTTP. + com.baeldung - spring-remoting-http + remoting-http 1.0-SNAPSHOT - spring-remoting-http-client - Shows how to invoke a remote service using Spring Remoting HTTP. + org.springframework.boot @@ -24,7 +25,8 @@ ${project.groupId} - api + remoting-http-api + ${project.version} \ No newline at end of file diff --git a/spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Client.java b/spring-remoting/remoting-http/remoting-http-client/src/main/java/com/baeldug/client/Client.java similarity index 100% rename from spring-remoting/remoting-http/client/src/main/java/com/baeldug/client/Client.java rename to spring-remoting/remoting-http/remoting-http-client/src/main/java/com/baeldug/client/Client.java diff --git a/spring-data-5-reactive/src/main/resources/logback.xml b/spring-remoting/remoting-http/remoting-http-client/src/main/resources/logback.xml similarity index 100% rename from spring-data-5-reactive/src/main/resources/logback.xml rename to spring-remoting/remoting-http/remoting-http-client/src/main/resources/logback.xml diff --git a/spring-remoting/remoting-http/server/pom.xml b/spring-remoting/remoting-http/remoting-http-server/pom.xml similarity index 79% rename from spring-remoting/remoting-http/server/pom.xml rename to spring-remoting/remoting-http/remoting-http-server/pom.xml index 517f4451f7..53d27a5efa 100644 --- a/spring-remoting/remoting-http/server/pom.xml +++ b/spring-remoting/remoting-http/remoting-http-server/pom.xml @@ -3,13 +3,15 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + remoting-http-server + Shows how to expose a service using Spring Remoting HTTP. + com.baeldung - spring-remoting-http + remoting-http 1.0-SNAPSHOT - spring-remoting-http-server - Shows how to expose a service using Spring Remoting HTTP. + org.springframework.boot @@ -17,7 +19,8 @@ ${project.groupId} - api + remoting-http-api + ${project.version} \ No newline at end of file diff --git a/spring-remoting/remoting-http/server/readme.md b/spring-remoting/remoting-http/remoting-http-server/readme.md similarity index 100% rename from spring-remoting/remoting-http/server/readme.md rename to spring-remoting/remoting-http/remoting-http-server/readme.md diff --git a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java b/spring-remoting/remoting-http/remoting-http-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java similarity index 100% rename from spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java rename to spring-remoting/remoting-http/remoting-http-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java diff --git a/spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/Server.java b/spring-remoting/remoting-http/remoting-http-server/src/main/java/com/baeldung/server/Server.java similarity index 100% rename from spring-remoting/remoting-http/server/src/main/java/com/baeldung/server/Server.java rename to spring-remoting/remoting-http/remoting-http-server/src/main/java/com/baeldung/server/Server.java diff --git a/spring-remoting/remoting-hessian-burlap/client/src/main/resources/logback.xml b/spring-remoting/remoting-http/remoting-http-server/src/main/resources/logback.xml similarity index 100% rename from spring-remoting/remoting-hessian-burlap/client/src/main/resources/logback.xml rename to spring-remoting/remoting-http/remoting-http-server/src/main/resources/logback.xml diff --git a/spring-remoting/remoting-http/server/src/main/resources/logback.xml b/spring-remoting/remoting-http/server/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/spring-remoting/remoting-http/server/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/spring-remoting/remoting-jms/remoting-jms-client/pom.xml b/spring-remoting/remoting-jms/remoting-jms-client/pom.xml index 01b7bea657..ed27282a38 100644 --- a/spring-remoting/remoting-jms/remoting-jms-client/pom.xml +++ b/spring-remoting/remoting-jms/remoting-jms-client/pom.xml @@ -28,7 +28,8 @@ com.baeldung - api + remoting-http-api + ${project.version} diff --git a/spring-remoting/remoting-jms/remoting-jms-server/pom.xml b/spring-remoting/remoting-jms/remoting-jms-server/pom.xml index 8feac4df57..7657267f1d 100644 --- a/spring-remoting/remoting-jms/remoting-jms-server/pom.xml +++ b/spring-remoting/remoting-jms/remoting-jms-server/pom.xml @@ -28,11 +28,7 @@ com.baeldung - api - - - com.baeldung - api + remoting-http-api ${project.version} diff --git a/spring-remoting/spring-remoting-rmi/pom.xml b/spring-remoting/remoting-rmi/pom.xml similarity index 88% rename from spring-remoting/spring-remoting-rmi/pom.xml rename to spring-remoting/remoting-rmi/pom.xml index 723158775f..cd15a6ec60 100644 --- a/spring-remoting/spring-remoting-rmi/pom.xml +++ b/spring-remoting/remoting-rmi/pom.xml @@ -2,18 +2,18 @@ - + 4.0.0 + pom + remoting-rmi + + spring-remoting com.baeldung 1.0-SNAPSHOT - 4.0.0 - pom - + + remoting-rmi-server remoting-rmi-client - spring-remoting-rmi - - \ No newline at end of file diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-client/pom.xml b/spring-remoting/remoting-rmi/remoting-rmi-client/pom.xml similarity index 86% rename from spring-remoting/spring-remoting-rmi/remoting-rmi-client/pom.xml rename to spring-remoting/remoting-rmi/remoting-rmi-client/pom.xml index 003976dc7b..1e3cd4b977 100644 --- a/spring-remoting/spring-remoting-rmi/remoting-rmi-client/pom.xml +++ b/spring-remoting/remoting-rmi/remoting-rmi-client/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - spring-remoting-rmi + remoting-rmi com.baeldung 1.0-SNAPSHOT @@ -24,7 +24,8 @@ com.baeldung - api + remoting-http-api + ${project.version} \ No newline at end of file diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-client/src/main/java/com/baeldung/client/RmiClient.java b/spring-remoting/remoting-rmi/remoting-rmi-client/src/main/java/com/baeldung/client/RmiClient.java similarity index 100% rename from spring-remoting/spring-remoting-rmi/remoting-rmi-client/src/main/java/com/baeldung/client/RmiClient.java rename to spring-remoting/remoting-rmi/remoting-rmi-client/src/main/java/com/baeldung/client/RmiClient.java diff --git a/spring-remoting/remoting-hessian-burlap/server/src/main/resources/logback.xml b/spring-remoting/remoting-rmi/remoting-rmi-client/src/main/resources/logback.xml similarity index 100% rename from spring-remoting/remoting-hessian-burlap/server/src/main/resources/logback.xml rename to spring-remoting/remoting-rmi/remoting-rmi-client/src/main/resources/logback.xml diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/pom.xml b/spring-remoting/remoting-rmi/remoting-rmi-server/pom.xml similarity index 83% rename from spring-remoting/spring-remoting-rmi/remoting-rmi-server/pom.xml rename to spring-remoting/remoting-rmi/remoting-rmi-server/pom.xml index f1633e5dc2..68b01829e1 100644 --- a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/pom.xml +++ b/spring-remoting/remoting-rmi/remoting-rmi-server/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - spring-remoting-rmi + remoting-rmi com.baeldung 1.0-SNAPSHOT @@ -28,11 +28,7 @@ com.baeldung - api - - - com.baeldung - api + remoting-http-api ${project.version} diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java b/spring-remoting/remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java similarity index 100% rename from spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java rename to spring-remoting/remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/RmiServer.java b/spring-remoting/remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/RmiServer.java similarity index 100% rename from spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/RmiServer.java rename to spring-remoting/remoting-rmi/remoting-rmi-server/src/main/java/com/baeldung/server/RmiServer.java diff --git a/spring-remoting/remoting-http/client/src/main/resources/logback.xml b/spring-remoting/remoting-rmi/remoting-rmi-server/src/main/resources/logback.xml similarity index 100% rename from spring-remoting/remoting-http/client/src/main/resources/logback.xml rename to spring-remoting/remoting-rmi/remoting-rmi-server/src/main/resources/logback.xml diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-client/src/main/resources/logback.xml b/spring-remoting/spring-remoting-rmi/remoting-rmi-client/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/spring-remoting/spring-remoting-rmi/remoting-rmi-client/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/resources/logback.xml b/spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/spring-remoting/spring-remoting-rmi/remoting-rmi-server/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/app.js b/spring-security-anguar/client/anguarjs/app.js deleted file mode 100644 index 4b7268da3f..0000000000 --- a/spring-security-anguar/client/anguarjs/app.js +++ /dev/null @@ -1,40 +0,0 @@ -(function () { - 'use strict'; - - angular - .module('app', ['ngRoute']) - .config(config) - .run(run); - - config.$inject = ['$routeProvider', '$locationProvider']; - function config($routeProvider, $locationProvider) { - $routeProvider - .when('/', { - controller: 'HomeController', - templateUrl: 'home/home.view.html', - controllerAs: 'vm' - }) - .when('/login', { - controller: 'LoginController', - templateUrl: 'login/login.view.html', - controllerAs: 'vm' - }) - .otherwise({ redirectTo: '/login' }); - } - - run.$inject = ['$rootScope', '$location', '$http', '$window']; - function run($rootScope, $location, $http, $window) { - var userData = $window.sessionStorage.getItem('userData'); - if (userData) { - $http.defaults.headers.common['Authorization'] = 'Basic ' + JSON.parse(userData).authData; - } - - $rootScope.$on('$locationChangeStart', function (event, next, current) { - var restrictedPage = $.inArray($location.path(), ['/login']) === -1; - var loggedIn = $window.sessionStorage.getItem('userData');; - if (restrictedPage && !loggedIn) { - $location.path('/login'); - } - }); - } -})(); \ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/home/home.controller.js b/spring-security-anguar/client/anguarjs/home/home.controller.js deleted file mode 100644 index 6449029ec2..0000000000 --- a/spring-security-anguar/client/anguarjs/home/home.controller.js +++ /dev/null @@ -1,34 +0,0 @@ -(function () { - 'use strict'; - - angular - .module('app') - .controller('HomeController', HomeController); - - HomeController.$inject = ['$window', '$http', '$scope']; - function HomeController($window, $http, $scope) { - var vm = this; - - vm.user = null; - - initController(); - - function initController() { - - $http({ - url: 'http://localhost:8082/user', - method: "GET" - }).then(function (response) { - vm.user = response.data.name; - },function(error){ - console.log(error); - }); - }; - - $scope.logout = function(){ - $window.sessionStorage.setItem('userData', ''); - $http.defaults.headers.common['Authorization'] = 'Basic'; - } - } - -})(); \ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/home/home.view.html b/spring-security-anguar/client/anguarjs/home/home.view.html deleted file mode 100644 index bb5c2d3dbf..0000000000 --- a/spring-security-anguar/client/anguarjs/home/home.view.html +++ /dev/null @@ -1,3 +0,0 @@ -

Hi {{vm.user}}!

-

You're logged in!!

-

Logout

\ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/index.html b/spring-security-anguar/client/anguarjs/index.html deleted file mode 100644 index a5584d53be..0000000000 --- a/spring-security-anguar/client/anguarjs/index.html +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - -
- - - - - - - - - - \ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/login/login.controller.js b/spring-security-anguar/client/anguarjs/login/login.controller.js deleted file mode 100644 index 2da509ec83..0000000000 --- a/spring-security-anguar/client/anguarjs/login/login.controller.js +++ /dev/null @@ -1,38 +0,0 @@ -(function () { - 'use strict'; - - angular - .module('app') - .controller('LoginController', LoginController); - - LoginController.$inject = ['$location', '$window', '$http']; - function LoginController($location, $window, $http) { - var vm = this; - vm.login = login; - - (function initController() { - $window.localStorage.setItem('token', ''); - })(); - - function login() { - $http({ - url: 'http://localhost:8082/login', - method: "POST", - data: { 'userName': vm.username, 'password': vm.password } - }).then(function (response) { - if (response.data) { - var token = $window.btoa(vm.username + ':' + vm.password); - var userData = { - userName: vm.username, - authData: token - } - $window.sessionStorage.setItem('userData', JSON.stringify(userData)); - $http.defaults.headers.common['Authorization'] = 'Basic ' + token; - $location.path('/'); - } else { - alert("Authentication failed.") - } - }); - }; - } -})(); diff --git a/spring-security-anguar/client/anguarjs/login/login.view.html b/spring-security-anguar/client/anguarjs/login/login.view.html deleted file mode 100644 index 0d42f29b54..0000000000 --- a/spring-security-anguar/client/anguarjs/login/login.view.html +++ /dev/null @@ -1,18 +0,0 @@ -
-

Login

-
-
- - - Username is required -
-
- - - Password is required -
-
- -
-
-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app.css b/spring-security-anguar/client/angular2/app.css deleted file mode 100644 index cdd2d591d8..0000000000 --- a/spring-security-anguar/client/angular2/app.css +++ /dev/null @@ -1,7 +0,0 @@ -a { - cursor: pointer; -} - -.help-block { - font-size: 12px; -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/app.component.html b/spring-security-anguar/client/angular2/app/app.component.html deleted file mode 100644 index 7f77adea7b..0000000000 --- a/spring-security-anguar/client/angular2/app/app.component.html +++ /dev/null @@ -1,7 +0,0 @@ -
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular2/app/app.component.ts b/spring-security-anguar/client/angular2/app/app.component.ts deleted file mode 100644 index 0aae2b6992..0000000000 --- a/spring-security-anguar/client/angular2/app/app.component.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app', - templateUrl: './app/app.component.html' -}) - -export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/app.module.ts b/spring-security-anguar/client/angular2/app/app.module.ts deleted file mode 100644 index 4d484b49f8..0000000000 --- a/spring-security-anguar/client/angular2/app/app.module.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; -import { FormsModule } from '@angular/forms'; -import { HttpModule } from '@angular/http'; - -import { AppComponent } from './app.component'; -import { routing } from './app.routing'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -@NgModule({ - imports: [ - BrowserModule, - FormsModule, - HttpModule, - routing - ], - declarations: [ - AppComponent, - HomeComponent, - LoginComponent - ], - bootstrap: [AppComponent] -}) - -export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/app.routing.ts b/spring-security-anguar/client/angular2/app/app.routing.ts deleted file mode 100644 index c794fc5c50..0000000000 --- a/spring-security-anguar/client/angular2/app/app.routing.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Routes, RouterModule } from '@angular/router'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -const appRoutes: Routes = [ - { path: '', component: HomeComponent}, - { path: 'login', component: LoginComponent }, - { path: '**', redirectTo: '' } -]; - -export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/home/home.component.html b/spring-security-anguar/client/angular2/app/home/home.component.html deleted file mode 100644 index 5f3b24be3a..0000000000 --- a/spring-security-anguar/client/angular2/app/home/home.component.html +++ /dev/null @@ -1,4 +0,0 @@ -
-

Hi {{userName}}!

-

Logout

-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/home/home.component.ts b/spring-security-anguar/client/angular2/app/home/home.component.ts deleted file mode 100644 index 1b168bfb30..0000000000 --- a/spring-security-anguar/client/angular2/app/home/home.component.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Http, RequestOptions, Headers } from '@angular/http'; -import 'rxjs/add/operator/map' - -@Component({ - selector:'home', - templateUrl: './app/home/home.component.html' -}) - -export class HomeComponent implements OnInit { - - userName: string; - - constructor(private http: Http) { } - - ngOnInit() { - let url = 'http://localhost:8082/user'; - let headers = new Headers({ - 'Authorization': 'Basic ' + sessionStorage.getItem('token') - }); - let options = new RequestOptions({ headers: headers }); - this.http.post(url,{}, options). - map(res => res.json()). - subscribe( - principal => this.userName = principal.name, - error => { - if(error.status == 401) - alert('Unauthorized'); - } - ); - } - - logout() { - sessionStorage.setItem('token', ''); - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/login/login.component.html b/spring-security-anguar/client/angular2/app/login/login.component.html deleted file mode 100644 index d87b91a7bb..0000000000 --- a/spring-security-anguar/client/angular2/app/login/login.component.html +++ /dev/null @@ -1,18 +0,0 @@ -
-

Login

-
-
- - -
Username is required
-
-
- - -
Password is required
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular2/app/login/login.component.ts b/spring-security-anguar/client/angular2/app/login/login.component.ts deleted file mode 100644 index 81d6c1d7ae..0000000000 --- a/spring-security-anguar/client/angular2/app/login/login.component.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Router, ActivatedRoute } from '@angular/router'; -import { Http } from '@angular/http'; - -@Component({ - selector: 'login', - templateUrl: './app/login/login.component.html' -}) - -export class LoginComponent implements OnInit { - model: any = {}; - - constructor( - private route: ActivatedRoute, - private router: Router, - private http: Http) { } - - ngOnInit() { - sessionStorage.setItem('token', ''); - } - - login() { - let url = 'http://localhost:8082/login'; - let result = this.http.post(url, { - userName: this.model.username, - password: this.model.password - }).map(res => res.json()).subscribe(isValid => { - if (isValid) { - sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); - this.router.navigate(['']); - } else { - alert("Authentication failed."); - } - }); - } -} diff --git a/spring-security-anguar/client/angular2/app/main.ts b/spring-security-anguar/client/angular2/app/main.ts deleted file mode 100644 index 3a6c754786..0000000000 --- a/spring-security-anguar/client/angular2/app/main.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; - -import { AppModule } from './app.module'; - -platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/index.html b/spring-security-anguar/client/angular2/index.html deleted file mode 100644 index c9730a8568..0000000000 --- a/spring-security-anguar/client/angular2/index.html +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - Loading... - - diff --git a/spring-security-anguar/client/angular2/package.json b/spring-security-anguar/client/angular2/package.json deleted file mode 100644 index 9b36652fac..0000000000 --- a/spring-security-anguar/client/angular2/package.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "name": "angular2-quickstart", - "version": "1.0.0", - "description": "Sample of how easy and fast to start hacking with Angular2 and ng2-bootstrap", - "scripts": { - "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ", - "lite": "lite-server", - "tsc": "tsc", - "tsc:w": "tsc -w" - }, - "license": "ISC", - "dependencies": { - "@angular/common": "2.4.4", - "@angular/compiler": "2.4.4", - "@angular/core": "2.4.4", - "@angular/forms": "2.4.4", - "@angular/http": "2.4.4", - "@angular/platform-browser": "2.4.4", - "@angular/platform-browser-dynamic": "2.4.4", - "@angular/router": "3.4.4", - "@angular/upgrade": "2.4.4", - "angular2-in-memory-web-api": "0.0.21", - "bootstrap": "^3.3.7", - "core-js": "^2.4.1", - "ng2-bootstrap": "1.3.0", - "reflect-metadata": "^0.1.8", - "rxjs": "5.0.3", - "systemjs": "0.20.0", - "zone.js": "0.7.6" - }, - "devDependencies": { - "concurrently": "3.1.0", - "lite-server": "^2.2.0", - "typescript": "2.1.5" - }, - "repository": {} -} diff --git a/spring-security-anguar/client/angular2/systemjs.config.js b/spring-security-anguar/client/angular2/systemjs.config.js deleted file mode 100644 index ea4c543fdd..0000000000 --- a/spring-security-anguar/client/angular2/systemjs.config.js +++ /dev/null @@ -1,41 +0,0 @@ -/** - * System configuration for Angular 2 samples - * Adjust as necessary for your application needs. - */ -(function (global) { - System.config({ - paths: { - // paths serve as alias - 'npm:': 'node_modules/' - }, - // map tells the System loader where to look for things - map: { - // our app is within the app folder - app: 'app', - - // angular bundles - '@angular/core': 'npm:@angular/core/bundles/core.umd.js', - '@angular/common': 'npm:@angular/common/bundles/common.umd.js', - '@angular/http': 'npm:@angular/http/bundles/http.umd.js', - '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', - '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', - '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', - '@angular/router': 'npm:@angular/router/bundles/router.umd.js', - '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', - - // other libraries - 'rxjs': 'npm:rxjs', - 'tslib': 'npm:tslib/tslib.js' - }, - // packages tells the System loader how to load when no filename and/or no extension - packages: { - app: { - main: './main.js', - defaultExtension: 'js' - }, - rxjs: { - defaultExtension: 'js' - } - } - }); -})(this); diff --git a/spring-security-anguar/client/angular2/tsconfig.json b/spring-security-anguar/client/angular2/tsconfig.json deleted file mode 100644 index 55f9ad70c3..0000000000 --- a/spring-security-anguar/client/angular2/tsconfig.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "compilerOptions": { - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "lib": [ "es2015", "dom" ], - "module": "commonjs", - "moduleResolution": "node", - "noImplicitAny": true, - "sourceMap": true, - "suppressImplicitAnyIndexErrors": true, - "target": "es5" - }, - "exclude": [ - "node_modules/*" - ] -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/.angular-cli.json b/spring-security-anguar/client/angular4/.angular-cli.json deleted file mode 100644 index 967934b2f7..0000000000 --- a/spring-security-anguar/client/angular4/.angular-cli.json +++ /dev/null @@ -1,65 +0,0 @@ -{ - "$schema": "./node_modules/@angular/cli/lib/config/schema.json", - "project": { - "name": "angular-crud" - }, - "apps": [ - { - "root": "src", - "outDir": "dist", - "assets": [ - "assets", - "favicon.ico" - ], - "index": "index.html", - "main": "main.ts", - "polyfills": "polyfills.ts", - "test": "test.ts", - "tsconfig": "tsconfig.app.json", - "testTsconfig": "tsconfig.spec.json", - "prefix": "app", - "styles": [ - "../node_modules/bootstrap/dist/css/bootstrap.min.css", - "styles.css", - "../node_modules/font-awesome/css/font-awesome.min.css" - ], - "scripts": [ - "../node_modules/jquery/dist/jquery.js", - "../node_modules/bootstrap/dist/js/bootstrap.js" - ], - "environmentSource": "environments/environment.ts", - "environments": { - "dev": "environments/environment.ts", - "prod": "environments/environment.prod.ts" - } - } - ], - "e2e": { - "protractor": { - "config": "./protractor.conf.js" - } - }, - "lint": [ - { - "project": "src/tsconfig.app.json", - "exclude": "**/node_modules/**" - }, - { - "project": "src/tsconfig.spec.json", - "exclude": "**/node_modules/**" - }, - { - "project": "e2e/tsconfig.e2e.json", - "exclude": "**/node_modules/**" - } - ], - "test": { - "karma": { - "config": "./karma.conf.js" - } - }, - "defaults": { - "styleExt": "css", - "component": {} - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/package.json b/spring-security-anguar/client/angular4/package.json deleted file mode 100644 index 8c6ddcaf1f..0000000000 --- a/spring-security-anguar/client/angular4/package.json +++ /dev/null @@ -1,54 +0,0 @@ -{ - "name": "angular-crud", - "version": "0.0.0", - "license": "MIT", - "scripts": { - "ng": "ng", - "start": "ng serve", - "build": "ng build", - "test": "ng test", - "lint": "ng lint", - "e2e": "ng e2e" - }, - "private": true, - "dependencies": { - "@angular/animations": "^4.0.0", - "@angular/common": "^4.0.0", - "@angular/compiler": "^4.0.0", - "@angular/core": "^4.0.0", - "@angular/forms": "^4.0.0", - "@angular/http": "^4.0.0", - "@angular/platform-browser": "^4.0.0", - "@angular/platform-browser-dynamic": "^4.0.0", - "@angular/router": "^4.0.0", - "bootstrap": "^3.3.7", - "core-js": "^2.4.1", - "font-awesome": "^4.7.0", - "jquery": "^3.2.1", - "lodash": "^4.17.4", - "rxjs": "^5.4.1", - "zone.js": "^0.8.14" - }, - "devDependencies": { - "@angular/cli": "1.2.3", - "@angular/compiler-cli": "^4.0.0", - "@angular/language-service": "^4.0.0", - "@types/jasmine": "~2.5.53", - "@types/jasminewd2": "~2.0.2", - "@types/lodash": "^4.14.66", - "@types/node": "~6.0.60", - "codelyzer": "~3.0.1", - "jasmine-core": "~2.6.2", - "jasmine-spec-reporter": "~4.1.0", - "karma": "~1.7.0", - "karma-chrome-launcher": "~2.1.1", - "karma-cli": "~1.0.1", - "karma-coverage-istanbul-reporter": "^1.2.1", - "karma-jasmine": "~1.1.0", - "karma-jasmine-html-reporter": "^0.2.2", - "protractor": "~5.1.2", - "ts-node": "~3.0.4", - "tslint": "~5.3.2", - "typescript": "~2.3.3" - } -} diff --git a/spring-security-anguar/client/angular4/src/app/app.component.html b/spring-security-anguar/client/angular4/src/app/app.component.html deleted file mode 100644 index 7f77adea7b..0000000000 --- a/spring-security-anguar/client/angular4/src/app/app.component.html +++ /dev/null @@ -1,7 +0,0 @@ -
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular4/src/app/app.component.ts b/spring-security-anguar/client/angular4/src/app/app.component.ts deleted file mode 100644 index 36f986c63f..0000000000 --- a/spring-security-anguar/client/angular4/src/app/app.component.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app-root', - templateUrl: './app.component.html' -}) - -export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/app.module.ts b/spring-security-anguar/client/angular4/src/app/app.module.ts deleted file mode 100644 index 9e840e6f13..0000000000 --- a/spring-security-anguar/client/angular4/src/app/app.module.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; -import { FormsModule } from '@angular/forms'; -import { HttpModule } from '@angular/http'; - -// used to create fake backend - -import { AppComponent } from './app.component'; -import { routing } from './app.routing'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -@NgModule({ - imports: [ - BrowserModule, - FormsModule, - HttpModule, - routing - ], - declarations: [ - AppComponent, - HomeComponent, - LoginComponent - ], - bootstrap: [AppComponent] -}) - -export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/app.routing.ts b/spring-security-anguar/client/angular4/src/app/app.routing.ts deleted file mode 100644 index c794fc5c50..0000000000 --- a/spring-security-anguar/client/angular4/src/app/app.routing.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Routes, RouterModule } from '@angular/router'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -const appRoutes: Routes = [ - { path: '', component: HomeComponent}, - { path: 'login', component: LoginComponent }, - { path: '**', redirectTo: '' } -]; - -export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/home/home.component.html b/spring-security-anguar/client/angular4/src/app/home/home.component.html deleted file mode 100644 index 7ccd2c2a3a..0000000000 --- a/spring-security-anguar/client/angular4/src/app/home/home.component.html +++ /dev/null @@ -1,4 +0,0 @@ -
-

Hi {{userName}}!

-

Logout

-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/home/home.component.ts b/spring-security-anguar/client/angular4/src/app/home/home.component.ts deleted file mode 100644 index 0f66f42ce2..0000000000 --- a/spring-security-anguar/client/angular4/src/app/home/home.component.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Http, RequestOptions, Headers } from '@angular/http'; -import 'rxjs/add/operator/map' - -@Component({ - selector:'home', - templateUrl: './home.component.html' -}) - -export class HomeComponent implements OnInit { - - userName: string; - - constructor(private http: Http) { } - - ngOnInit() { - let url = 'http://localhost:8082/user'; - let headers = new Headers({ - 'Authorization': 'Basic ' + sessionStorage.getItem('token') - }); - let options = new RequestOptions({ headers: headers }); - this.http.post(url,{}, options). - map( - res => res.json(), - error => { - if(error.status == 401) - alert('Unauthorized'); - } - ).subscribe(principal => { - this.userName = principal.name; - }); - } - - logout() { - sessionStorage.setItem('token', ''); - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/login/login.component.html b/spring-security-anguar/client/angular4/src/app/login/login.component.html deleted file mode 100644 index d87b91a7bb..0000000000 --- a/spring-security-anguar/client/angular4/src/app/login/login.component.html +++ /dev/null @@ -1,18 +0,0 @@ -
-

Login

-
-
- - -
Username is required
-
-
- - -
Password is required
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular4/src/app/login/login.component.ts b/spring-security-anguar/client/angular4/src/app/login/login.component.ts deleted file mode 100644 index 2a2dc102af..0000000000 --- a/spring-security-anguar/client/angular4/src/app/login/login.component.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Router, ActivatedRoute } from '@angular/router'; -import { Http } from '@angular/http'; - -@Component({ - selector: 'login', - templateUrl: './login.component.html' -}) - -export class LoginComponent implements OnInit { - model: any = {}; - - constructor( - private route: ActivatedRoute, - private router: Router, - private http: Http) { } - - ngOnInit() { - sessionStorage.setItem('token', ''); - } - - - login() { - let url = 'http://localhost:8082/login'; - let result = this.http.post(url, { - userName: this.model.username, - password: this.model.password - }). - map(res => res.json()). - subscribe(isValid => { - if (isValid) { - sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); - this.router.navigate(['']); - } else { - alert("Authentication failed.") - } - }); - } -} diff --git a/spring-security-anguar/client/angular4/src/index.html b/spring-security-anguar/client/angular4/src/index.html deleted file mode 100644 index c716820396..0000000000 --- a/spring-security-anguar/client/angular4/src/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - AngularCRUD - - - - - - - - - \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/main.ts b/spring-security-anguar/client/angular4/src/main.ts deleted file mode 100644 index 49db98ae89..0000000000 --- a/spring-security-anguar/client/angular4/src/main.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; - -import { AppModule } from './app/app.module'; - -platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/polyfills.ts b/spring-security-anguar/client/angular4/src/polyfills.ts deleted file mode 100644 index a4984ced57..0000000000 --- a/spring-security-anguar/client/angular4/src/polyfills.ts +++ /dev/null @@ -1,68 +0,0 @@ -/** - * This file includes polyfills needed by Angular and is loaded before the app. - * You can add your own extra polyfills to this file. - * - * This file is divided into 2 sections: - * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. - * 2. Application imports. Files imported after ZoneJS that should be loaded before your main - * file. - * - * The current setup is for so-called "evergreen" browsers; the last versions of browsers that - * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), - * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. - * - * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html - */ - -/*************************************************************************************************** - * BROWSER POLYFILLS - */ - -/** IE9, IE10 and IE11 requires all of the following polyfills. **/ -// import 'core-js/es6/symbol'; -// import 'core-js/es6/object'; -// import 'core-js/es6/function'; -// import 'core-js/es6/parse-int'; -// import 'core-js/es6/parse-float'; -// import 'core-js/es6/number'; -// import 'core-js/es6/math'; -// import 'core-js/es6/string'; -// import 'core-js/es6/date'; -// import 'core-js/es6/array'; -// import 'core-js/es6/regexp'; -// import 'core-js/es6/map'; -// import 'core-js/es6/weak-map'; -// import 'core-js/es6/set'; -/** IE10 and IE11 requires the following for NgClass support on SVG elements */ -// import 'classlist.js'; // Run `npm install --save classlist.js`. -/** Evergreen browsers require these. **/ -import 'core-js/es6/reflect'; -import 'core-js/es7/reflect'; - - -/** - * Required to support Web Animations `@angular/animation`. - * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation - **/ -// import 'web-animations-js'; // Run `npm install --save web-animations-js`. - - -/*************************************************************************************************** - * Zone JS is required by Angular itself. - */ -import 'zone.js/dist/zone'; // Included with Angular CLI. - - -/*************************************************************************************************** - * APPLICATION IMPORTS - */ - -/** - * Date, currency, decimal and percent pipes. - * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 - */ -// import 'intl'; // Run `npm install --save intl`. -/** - * Need to import at least one locale-data with intl. - */ -// import 'intl/locale-data/jsonp/en'; \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/styles.css b/spring-security-anguar/client/angular4/src/styles.css deleted file mode 100644 index cdd2d591d8..0000000000 --- a/spring-security-anguar/client/angular4/src/styles.css +++ /dev/null @@ -1,7 +0,0 @@ -a { - cursor: pointer; -} - -.help-block { - font-size: 12px; -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/tsconfig.app.json b/spring-security-anguar/client/angular4/src/tsconfig.app.json deleted file mode 100644 index 213ce42a1b..0000000000 --- a/spring-security-anguar/client/angular4/src/tsconfig.app.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "extends": "../tsconfig.json", - "compilerOptions": { - "outDir": "../out-tsc/app", - "baseUrl": "./", - "module": "es2015", - "types": [] - }, - "exclude": [ - "test.ts", - "**/*.spec.ts" - ] -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/tsconfig.json b/spring-security-anguar/client/angular4/tsconfig.json deleted file mode 100644 index ef44e2862b..0000000000 --- a/spring-security-anguar/client/angular4/tsconfig.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "compileOnSave": false, - "compilerOptions": { - "baseUrl": "./", - "outDir": "./dist/out-tsc", - "sourceMap": true, - "declaration": false, - "moduleResolution": "node", - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "target": "es5", - "typeRoots": [ - "node_modules/@types" - ], - "lib": [ - "es2017", - "dom" - ] - } -} diff --git a/spring-security-anguar/client/angular4/tslint.json b/spring-security-anguar/client/angular4/tslint.json deleted file mode 100644 index 3ea984c776..0000000000 --- a/spring-security-anguar/client/angular4/tslint.json +++ /dev/null @@ -1,130 +0,0 @@ -{ - "rulesDirectory": [ - "node_modules/codelyzer" - ], - "rules": { - "arrow-return-shorthand": true, - "callable-types": true, - "class-name": true, - "comment-format": [ - true, - "check-space" - ], - "curly": true, - "deprecation": { - "severity": "warn" - }, - "eofline": true, - "forin": true, - "import-blacklist": [ - true, - "rxjs/Rx" - ], - "import-spacing": true, - "indent": [ - true, - "spaces" - ], - "interface-over-type-literal": true, - "label-position": true, - "max-line-length": [ - true, - 140 - ], - "member-access": false, - "member-ordering": [ - true, - { - "order": [ - "static-field", - "instance-field", - "static-method", - "instance-method" - ] - } - ], - "no-arg": true, - "no-bitwise": true, - "no-console": [ - true, - "debug", - "info", - "time", - "timeEnd", - "trace" - ], - "no-construct": true, - "no-debugger": true, - "no-duplicate-super": true, - "no-empty": false, - "no-empty-interface": true, - "no-eval": true, - "no-inferrable-types": [ - true, - "ignore-params" - ], - "no-misused-new": true, - "no-non-null-assertion": true, - "no-shadowed-variable": true, - "no-string-literal": false, - "no-string-throw": true, - "no-switch-case-fall-through": true, - "no-trailing-whitespace": true, - "no-unnecessary-initializer": true, - "no-unused-expression": true, - "no-use-before-declare": true, - "no-var-keyword": true, - "object-literal-sort-keys": false, - "one-line": [ - true, - "check-open-brace", - "check-catch", - "check-else", - "check-whitespace" - ], - "prefer-const": true, - "quotemark": [ - true, - "single" - ], - "radix": true, - "semicolon": [ - true, - "always" - ], - "triple-equals": [ - true, - "allow-null-check" - ], - "typedef-whitespace": [ - true, - { - "call-signature": "nospace", - "index-signature": "nospace", - "parameter": "nospace", - "property-declaration": "nospace", - "variable-declaration": "nospace" - } - ], - "unified-signatures": true, - "variable-name": false, - "whitespace": [ - true, - "check-branch", - "check-decl", - "check-operator", - "check-separator", - "check-type" - ], - "no-output-on-prefix": true, - "use-input-property-decorator": true, - "use-output-property-decorator": true, - "use-host-property-decorator": true, - "no-input-rename": true, - "no-output-rename": true, - "use-life-cycle-interface": true, - "use-pipe-transform-interface": true, - "component-class-suffix": true, - "directive-class-suffix": true - } -} diff --git a/spring-security-anguar/client/angular5/.angular-cli.json b/spring-security-anguar/client/angular5/.angular-cli.json deleted file mode 100644 index d390652214..0000000000 --- a/spring-security-anguar/client/angular5/.angular-cli.json +++ /dev/null @@ -1,61 +0,0 @@ -{ - "$schema": "./node_modules/@angular/cli/lib/config/schema.json", - "project": { - "name": "angular5" - }, - "apps": [ - { - "root": "src", - "outDir": "dist", - "assets": [ - "assets", - "favicon.ico" - ], - "index": "index.html", - "main": "main.ts", - "polyfills": "polyfills.ts", - "test": "test.ts", - "tsconfig": "tsconfig.app.json", - "testTsconfig": "tsconfig.spec.json", - "prefix": "app", - "styles": [ - "styles.css", - "../node_modules/ngx-toastr/toastr.css" - ], - "scripts": [], - "environmentSource": "environments/environment.ts", - "environments": { - "dev": "environments/environment.ts", - "prod": "environments/environment.prod.ts" - } - } - ], - "e2e": { - "protractor": { - "config": "./protractor.conf.js" - } - }, - "lint": [ - { - "project": "src/tsconfig.app.json", - "exclude": "**/node_modules/**" - }, - { - "project": "src/tsconfig.spec.json", - "exclude": "**/node_modules/**" - }, - { - "project": "e2e/tsconfig.e2e.json", - "exclude": "**/node_modules/**" - } - ], - "test": { - "karma": { - "config": "./karma.conf.js" - } - }, - "defaults": { - "styleExt": "css", - "component": {} - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/package.json b/spring-security-anguar/client/angular5/package.json deleted file mode 100644 index 45f455d191..0000000000 --- a/spring-security-anguar/client/angular5/package.json +++ /dev/null @@ -1,50 +0,0 @@ -{ - "name": "angular5", - "version": "0.0.0", - "license": "MIT", - "scripts": { - "ng": "ng", - "start": "ng serve", - "build": "ng build --prod", - "test": "ng test", - "lint": "ng lint", - "e2e": "ng e2e" - }, - "private": true, - "dependencies": { - "@angular/animations": "^5.0.0", - "@angular/common": "^5.0.0", - "@angular/compiler": "^5.0.0", - "@angular/core": "^5.0.0", - "@angular/forms": "^5.0.0", - "@angular/http": "^5.0.0", - "@angular/platform-browser": "^5.0.0", - "@angular/platform-browser-dynamic": "^5.0.0", - "@angular/router": "^5.0.0", - "core-js": "^2.4.1", - "ngx-toastr": "^8.1.1", - "rxjs": "^5.5.2", - "zone.js": "^0.8.14" - }, - "devDependencies": { - "@angular/cli": "^1.6.6", - "@angular/compiler-cli": "^5.0.0", - "@angular/language-service": "^5.0.0", - "@types/jasmine": "~2.5.53", - "@types/jasminewd2": "~2.0.2", - "@types/node": "~6.0.60", - "codelyzer": "^4.0.1", - "jasmine-core": "~2.6.2", - "jasmine-spec-reporter": "~4.1.0", - "karma": "~1.7.0", - "karma-chrome-launcher": "~2.1.1", - "karma-cli": "~1.0.1", - "karma-coverage-istanbul-reporter": "^1.2.1", - "karma-jasmine": "~1.1.0", - "karma-jasmine-html-reporter": "^0.2.2", - "protractor": "~5.1.2", - "ts-node": "~3.2.0", - "tslint": "~5.7.0", - "typescript": "~2.4.2" - } -} diff --git a/spring-security-anguar/client/angular5/src/app/app.component.html b/spring-security-anguar/client/angular5/src/app/app.component.html deleted file mode 100644 index 7f77adea7b..0000000000 --- a/spring-security-anguar/client/angular5/src/app/app.component.html +++ /dev/null @@ -1,7 +0,0 @@ -
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular5/src/app/app.component.ts b/spring-security-anguar/client/angular5/src/app/app.component.ts deleted file mode 100644 index 36f986c63f..0000000000 --- a/spring-security-anguar/client/angular5/src/app/app.component.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app-root', - templateUrl: './app.component.html' -}) - -export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/app.module.ts b/spring-security-anguar/client/angular5/src/app/app.module.ts deleted file mode 100644 index 9e840e6f13..0000000000 --- a/spring-security-anguar/client/angular5/src/app/app.module.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; -import { FormsModule } from '@angular/forms'; -import { HttpModule } from '@angular/http'; - -// used to create fake backend - -import { AppComponent } from './app.component'; -import { routing } from './app.routing'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -@NgModule({ - imports: [ - BrowserModule, - FormsModule, - HttpModule, - routing - ], - declarations: [ - AppComponent, - HomeComponent, - LoginComponent - ], - bootstrap: [AppComponent] -}) - -export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/app.routing.ts b/spring-security-anguar/client/angular5/src/app/app.routing.ts deleted file mode 100644 index c794fc5c50..0000000000 --- a/spring-security-anguar/client/angular5/src/app/app.routing.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Routes, RouterModule } from '@angular/router'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -const appRoutes: Routes = [ - { path: '', component: HomeComponent}, - { path: 'login', component: LoginComponent }, - { path: '**', redirectTo: '' } -]; - -export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/home/home.component.html b/spring-security-anguar/client/angular5/src/app/home/home.component.html deleted file mode 100644 index 7ccd2c2a3a..0000000000 --- a/spring-security-anguar/client/angular5/src/app/home/home.component.html +++ /dev/null @@ -1,4 +0,0 @@ -
-

Hi {{userName}}!

-

Logout

-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/home/home.component.ts b/spring-security-anguar/client/angular5/src/app/home/home.component.ts deleted file mode 100644 index 7fe1ac4eff..0000000000 --- a/spring-security-anguar/client/angular5/src/app/home/home.component.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Http, Headers, RequestOptions } from '@angular/http'; -import 'rxjs/add/operator/map' - -@Component({ - selector:'home', - templateUrl: './home.component.html' -}) - -export class HomeComponent implements OnInit { - - userName: string; - - constructor(private http: Http) { } - - ngOnInit() { - let url = 'http://localhost:8082/user'; - - let headers:Headers = new Headers({ - 'Authorization': 'Basic ' + sessionStorage.getItem('token') - }) - let options = new RequestOptions({headers: headers}); - this.http.post(url,{}, options).map( - res => res.json(), - error => { - if(error.status == 401) - alert('Unauthorized'); - } - ).subscribe(principal => { - this.userName = principal.name; - }); - } - - logout() { - sessionStorage.setItem('token', ''); - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/login/login.component.html b/spring-security-anguar/client/angular5/src/app/login/login.component.html deleted file mode 100644 index d87b91a7bb..0000000000 --- a/spring-security-anguar/client/angular5/src/app/login/login.component.html +++ /dev/null @@ -1,18 +0,0 @@ -
-

Login

-
-
- - -
Username is required
-
-
- - -
Password is required
-
-
- -
-
-
diff --git a/spring-security-anguar/client/angular5/src/app/login/login.component.ts b/spring-security-anguar/client/angular5/src/app/login/login.component.ts deleted file mode 100644 index 2db8f32871..0000000000 --- a/spring-security-anguar/client/angular5/src/app/login/login.component.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Router, ActivatedRoute } from '@angular/router'; -import { Http } from '@angular/http'; - -@Component({ - selector: 'login', - templateUrl: './login.component.html' -}) - -export class LoginComponent implements OnInit { - model: any = {}; - - constructor( - private route: ActivatedRoute, - private router: Router, - private http: Http) { } - - ngOnInit() { - sessionStorage.setItem('token', ''); - } - - - login() { - let url = 'http://localhost:8082/login'; - let result = this.http.post(url, { - userName: this.model.username, - password: this.model.password - }).map(res => res.json()).subscribe(isValid => { - if (isValid) { - sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); - this.router.navigate(['']); - } else { - alert("Authentication failed.") - } - }); - } -} diff --git a/spring-security-anguar/client/angular5/src/index.html b/spring-security-anguar/client/angular5/src/index.html deleted file mode 100644 index c716820396..0000000000 --- a/spring-security-anguar/client/angular5/src/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - AngularCRUD - - - - - - - - - \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/main.ts b/spring-security-anguar/client/angular5/src/main.ts deleted file mode 100644 index 49db98ae89..0000000000 --- a/spring-security-anguar/client/angular5/src/main.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; - -import { AppModule } from './app/app.module'; - -platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/polyfills.ts b/spring-security-anguar/client/angular5/src/polyfills.ts deleted file mode 100644 index a4984ced57..0000000000 --- a/spring-security-anguar/client/angular5/src/polyfills.ts +++ /dev/null @@ -1,68 +0,0 @@ -/** - * This file includes polyfills needed by Angular and is loaded before the app. - * You can add your own extra polyfills to this file. - * - * This file is divided into 2 sections: - * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. - * 2. Application imports. Files imported after ZoneJS that should be loaded before your main - * file. - * - * The current setup is for so-called "evergreen" browsers; the last versions of browsers that - * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), - * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. - * - * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html - */ - -/*************************************************************************************************** - * BROWSER POLYFILLS - */ - -/** IE9, IE10 and IE11 requires all of the following polyfills. **/ -// import 'core-js/es6/symbol'; -// import 'core-js/es6/object'; -// import 'core-js/es6/function'; -// import 'core-js/es6/parse-int'; -// import 'core-js/es6/parse-float'; -// import 'core-js/es6/number'; -// import 'core-js/es6/math'; -// import 'core-js/es6/string'; -// import 'core-js/es6/date'; -// import 'core-js/es6/array'; -// import 'core-js/es6/regexp'; -// import 'core-js/es6/map'; -// import 'core-js/es6/weak-map'; -// import 'core-js/es6/set'; -/** IE10 and IE11 requires the following for NgClass support on SVG elements */ -// import 'classlist.js'; // Run `npm install --save classlist.js`. -/** Evergreen browsers require these. **/ -import 'core-js/es6/reflect'; -import 'core-js/es7/reflect'; - - -/** - * Required to support Web Animations `@angular/animation`. - * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation - **/ -// import 'web-animations-js'; // Run `npm install --save web-animations-js`. - - -/*************************************************************************************************** - * Zone JS is required by Angular itself. - */ -import 'zone.js/dist/zone'; // Included with Angular CLI. - - -/*************************************************************************************************** - * APPLICATION IMPORTS - */ - -/** - * Date, currency, decimal and percent pipes. - * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 - */ -// import 'intl'; // Run `npm install --save intl`. -/** - * Need to import at least one locale-data with intl. - */ -// import 'intl/locale-data/jsonp/en'; \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/styles.css b/spring-security-anguar/client/angular5/src/styles.css deleted file mode 100644 index cdd2d591d8..0000000000 --- a/spring-security-anguar/client/angular5/src/styles.css +++ /dev/null @@ -1,7 +0,0 @@ -a { - cursor: pointer; -} - -.help-block { - font-size: 12px; -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/tsconfig.app.json b/spring-security-anguar/client/angular5/src/tsconfig.app.json deleted file mode 100644 index 213ce42a1b..0000000000 --- a/spring-security-anguar/client/angular5/src/tsconfig.app.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "extends": "../tsconfig.json", - "compilerOptions": { - "outDir": "../out-tsc/app", - "baseUrl": "./", - "module": "es2015", - "types": [] - }, - "exclude": [ - "test.ts", - "**/*.spec.ts" - ] -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/tsconfig.json b/spring-security-anguar/client/angular5/tsconfig.json deleted file mode 100644 index 0fdb5c817d..0000000000 --- a/spring-security-anguar/client/angular5/tsconfig.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "compileOnSave": false, - "compilerOptions": { - "outDir": "./dist/out-tsc", - "sourceMap": true, - "declaration": false, - "moduleResolution": "node", - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "target": "es5", - "typeRoots": [ - "node_modules/@types" - ], - "lib": [ - "es2017", - "dom" - ] - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/tslint.json b/spring-security-anguar/client/angular5/tslint.json deleted file mode 100644 index 1c1d53b0d9..0000000000 --- a/spring-security-anguar/client/angular5/tslint.json +++ /dev/null @@ -1,144 +0,0 @@ -{ - "rulesDirectory": [ - "node_modules/codelyzer" - ], - "rules": { - "arrow-return-shorthand": true, - "callable-types": true, - "class-name": true, - "comment-format": [ - true, - "check-space" - ], - "curly": true, - "deprecation": { - "severity": "warn" - }, - "eofline": true, - "forin": true, - "import-blacklist": [ - true, - "rxjs", - "rxjs/Rx" - ], - "import-spacing": true, - "indent": [ - true, - "spaces" - ], - "interface-over-type-literal": true, - "label-position": true, - "max-line-length": [ - true, - 140 - ], - "member-access": false, - "member-ordering": [ - true, - { - "order": [ - "static-field", - "instance-field", - "static-method", - "instance-method" - ] - } - ], - "no-arg": true, - "no-bitwise": true, - "no-console": [ - true, - "debug", - "info", - "time", - "timeEnd", - "trace" - ], - "no-construct": true, - "no-debugger": true, - "no-duplicate-super": true, - "no-empty": false, - "no-empty-interface": true, - "no-eval": true, - "no-inferrable-types": [ - true, - "ignore-params" - ], - "no-misused-new": true, - "no-non-null-assertion": true, - "no-shadowed-variable": true, - "no-string-literal": false, - "no-string-throw": true, - "no-switch-case-fall-through": true, - "no-trailing-whitespace": true, - "no-unnecessary-initializer": true, - "no-unused-expression": true, - "no-use-before-declare": true, - "no-var-keyword": true, - "object-literal-sort-keys": false, - "one-line": [ - true, - "check-open-brace", - "check-catch", - "check-else", - "check-whitespace" - ], - "prefer-const": true, - "quotemark": [ - true, - "single" - ], - "radix": true, - "semicolon": [ - true, - "always" - ], - "triple-equals": [ - true, - "allow-null-check" - ], - "typedef-whitespace": [ - true, - { - "call-signature": "nospace", - "index-signature": "nospace", - "parameter": "nospace", - "property-declaration": "nospace", - "variable-declaration": "nospace" - } - ], - "typeof-compare": true, - "unified-signatures": true, - "variable-name": false, - "whitespace": [ - true, - "check-branch", - "check-decl", - "check-operator", - "check-separator", - "check-type" - ], - "directive-selector": [ - true, - "attribute", - "app", - "camelCase" - ], - "component-selector": [ - true, - "element", - "app", - "kebab-case" - ], - "no-output-on-prefix": true, - "use-input-property-decorator": true, - "use-output-property-decorator": true, - "use-host-property-decorator": true, - "no-input-rename": true, - "no-output-rename": true, - "use-life-cycle-interface": true, - "use-pipe-transform-interface": true, - "component-class-suffix": true, - "directive-class-suffix": true - } -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/angular.json b/spring-security-anguar/client/angular6/angular.json deleted file mode 100644 index 0168c58817..0000000000 --- a/spring-security-anguar/client/angular6/angular.json +++ /dev/null @@ -1,127 +0,0 @@ -{ - "$schema": "./node_modules/@angular/cli/lib/config/schema.json", - "version": 1, - "newProjectRoot": "projects", - "projects": { - "angular6-login": { - "root": "", - "sourceRoot": "src", - "projectType": "application", - "prefix": "app", - "schematics": {}, - "architect": { - "build": { - "builder": "@angular-devkit/build-angular:browser", - "options": { - "outputPath": "dist/angular6-login", - "index": "src/index.html", - "main": "src/main.ts", - "polyfills": "src/polyfills.ts", - "tsConfig": "src/tsconfig.app.json", - "assets": [ - "src/favicon.ico", - "src/assets" - ], - "styles": [ - "src/styles.css" - ], - "scripts": [] - }, - "configurations": { - "production": { - "fileReplacements": [ - { - "replace": "src/environments/environment.ts", - "with": "src/environments/environment.prod.ts" - } - ], - "optimization": true, - "outputHashing": "all", - "sourceMap": false, - "extractCss": true, - "namedChunks": false, - "aot": true, - "extractLicenses": true, - "vendorChunk": false, - "buildOptimizer": true - } - } - }, - "serve": { - "builder": "@angular-devkit/build-angular:dev-server", - "options": { - "browserTarget": "angular6-login:build" - }, - "configurations": { - "production": { - "browserTarget": "angular6-login:build:production" - } - } - }, - "extract-i18n": { - "builder": "@angular-devkit/build-angular:extract-i18n", - "options": { - "browserTarget": "angular6-login:build" - } - }, - "test": { - "builder": "@angular-devkit/build-angular:karma", - "options": { - "main": "src/test.ts", - "polyfills": "src/polyfills.ts", - "tsConfig": "src/tsconfig.spec.json", - "karmaConfig": "src/karma.conf.js", - "styles": [ - "src/styles.css" - ], - "scripts": [], - "assets": [ - "src/favicon.ico", - "src/assets" - ] - } - }, - "lint": { - "builder": "@angular-devkit/build-angular:tslint", - "options": { - "tsConfig": [ - "src/tsconfig.app.json", - "src/tsconfig.spec.json" - ], - "exclude": [ - "**/node_modules/**" - ] - } - } - } - }, - "angular6-login-e2e": { - "root": "e2e/", - "projectType": "application", - "architect": { - "e2e": { - "builder": "@angular-devkit/build-angular:protractor", - "options": { - "protractorConfig": "e2e/protractor.conf.js", - "devServerTarget": "angular6-login:serve" - }, - "configurations": { - "production": { - "devServerTarget": "angular6-login:serve:production" - } - } - }, - "lint": { - "builder": "@angular-devkit/build-angular:tslint", - "options": { - "tsConfig": "e2e/tsconfig.e2e.json", - "exclude": [ - "**/node_modules/**" - ] - } - } - } - } - }, - "defaultProject": "angular6-login" -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/package.json b/spring-security-anguar/client/angular6/package.json deleted file mode 100644 index a0adcd03f3..0000000000 --- a/spring-security-anguar/client/angular6/package.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "name": "angular6-login", - "version": "0.0.0", - "scripts": { - "ng": "ng", - "start": "ng serve", - "build": "ng build", - "test": "ng test", - "lint": "ng lint", - "e2e": "ng e2e" - }, - "private": true, - "dependencies": { - "@angular/animations": "^6.0.3", - "@angular/common": "^6.0.3", - "@angular/compiler": "^6.0.3", - "@angular/core": "^6.0.3", - "@angular/forms": "^6.0.3", - "@angular/http": "^6.0.3", - "@angular/platform-browser": "^6.0.3", - "@angular/platform-browser-dynamic": "^6.0.3", - "@angular/router": "^6.0.3", - "core-js": "^2.5.4", - "rxjs": "^6.0.0", - "zone.js": "^0.8.26" - }, - "devDependencies": { - "@angular/compiler-cli": "^6.0.3", - "@angular-devkit/build-angular": "~0.6.8", - "typescript": "~2.7.2", - "@angular/cli": "~6.0.8", - "@angular/language-service": "^6.0.3", - "@types/jasmine": "~2.8.6", - "@types/jasminewd2": "~2.0.3", - "@types/node": "~8.9.4", - "codelyzer": "~4.2.1", - "jasmine-core": "~2.99.1", - "jasmine-spec-reporter": "~4.2.1", - "karma": "~1.7.1", - "karma-chrome-launcher": "~2.2.0", - "karma-coverage-istanbul-reporter": "~2.0.0", - "karma-jasmine": "~1.1.1", - "karma-jasmine-html-reporter": "^0.2.2", - "protractor": "~5.3.0", - "ts-node": "~5.0.1", - "tslint": "~5.9.1" - } -} diff --git a/spring-security-anguar/client/angular6/src/app/app.component.html b/spring-security-anguar/client/angular6/src/app/app.component.html deleted file mode 100644 index 6bf00f3018..0000000000 --- a/spring-security-anguar/client/angular6/src/app/app.component.html +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/spring-security-anguar/client/angular6/src/app/app.component.ts b/spring-security-anguar/client/angular6/src/app/app.component.ts deleted file mode 100644 index 36f986c63f..0000000000 --- a/spring-security-anguar/client/angular6/src/app/app.component.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app-root', - templateUrl: './app.component.html' -}) - -export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/app.module.ts b/spring-security-anguar/client/angular6/src/app/app.module.ts deleted file mode 100644 index 4f9a792e19..0000000000 --- a/spring-security-anguar/client/angular6/src/app/app.module.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; -import { FormsModule } from '@angular/forms'; -import { HttpClientModule } from '@angular/common/http'; - -import { AppComponent } from './app.component'; -import { routing } from './app.routing'; - -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -@NgModule({ - imports: [ - BrowserModule, - FormsModule, - HttpClientModule, - routing - ], - declarations: [ - AppComponent, - HomeComponent, - LoginComponent - ], - bootstrap: [AppComponent] -}) - -export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/app.routing.ts b/spring-security-anguar/client/angular6/src/app/app.routing.ts deleted file mode 100644 index e5f5e04aa8..0000000000 --- a/spring-security-anguar/client/angular6/src/app/app.routing.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Routes, RouterModule } from '@angular/router'; -import { HomeComponent } from './home/home.component'; -import { LoginComponent } from './login/login.component'; - -const appRoutes: Routes = [ - { path: '', component: HomeComponent }, - { path: 'login', component: LoginComponent }, - { path: '**', redirectTo: '' } -]; - -export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/home/home.component.html b/spring-security-anguar/client/angular6/src/app/home/home.component.html deleted file mode 100644 index 7ccd2c2a3a..0000000000 --- a/spring-security-anguar/client/angular6/src/app/home/home.component.html +++ /dev/null @@ -1,4 +0,0 @@ -
-

Hi {{userName}}!

-

Logout

-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/home/home.component.ts b/spring-security-anguar/client/angular6/src/app/home/home.component.ts deleted file mode 100644 index 6c9fcfd97f..0000000000 --- a/spring-security-anguar/client/angular6/src/app/home/home.component.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http'; -import { Observable, throwError } from 'rxjs'; -import { catchError, map, tap} from 'rxjs/operators'; -@Component({ - selector: 'home', - templateUrl: './home.component.html' -}) - -export class HomeComponent implements OnInit { - - userName: string; - - constructor(private http: HttpClient) { } - - ngOnInit() { - let url = 'http://localhost:8082/user'; - - let headers: HttpHeaders = new HttpHeaders({ - 'Authorization': 'Basic ' + sessionStorage.getItem('token') - }); - - let options = { headers: headers }; - this.http.post>(url, {}, options). - subscribe(principal => { - this.userName = principal['name']; - }, - error => { - if(error.status == 401) - alert('Unauthorized'); - } - ); - } - - logout() { - sessionStorage.setItem('token', ''); - } - private handleError(error: HttpErrorResponse) { - if (error.error instanceof ErrorEvent) { - console.error('An error occurred:', error.error.message); - } else { - console.error( - `Backend returned code ${error.status}, ` + - `body was: ${error.error}`); - } - return throwError( - 'Something bad happened; please try again later.'); - }; -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/login/login.component.html b/spring-security-anguar/client/angular6/src/app/login/login.component.html deleted file mode 100644 index 4291206469..0000000000 --- a/spring-security-anguar/client/angular6/src/app/login/login.component.html +++ /dev/null @@ -1,15 +0,0 @@ -
-
- - -
Username is required
-
-
- - -
Password is required
-
-
- -
-
\ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/login/login.component.ts b/spring-security-anguar/client/angular6/src/app/login/login.component.ts deleted file mode 100644 index 27af9ebba5..0000000000 --- a/spring-security-anguar/client/angular6/src/app/login/login.component.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { Router, ActivatedRoute } from '@angular/router'; -import { HttpClient } from '@angular/common/http'; -import { Observable } from 'rxjs'; - -@Component({ - selector: 'login', - templateUrl: './login.component.html' -}) - -export class LoginComponent implements OnInit { - - model: any = {}; - - constructor( - private route: ActivatedRoute, - private router: Router, - private http: HttpClient - ) { } - - ngOnInit() { - sessionStorage.setItem('token', ''); - } - - login() { - let url = 'http://localhost:8082/login'; - this.http.post>(url, { - userName: this.model.username, - password: this.model.password - }).subscribe(isValid => { - if (isValid) { - sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); - this.router.navigate(['']); - } else { - alert("Authentication failed.") - } - }); - } -} diff --git a/spring-security-anguar/client/angular6/src/index.html b/spring-security-anguar/client/angular6/src/index.html deleted file mode 100644 index c716820396..0000000000 --- a/spring-security-anguar/client/angular6/src/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - AngularCRUD - - - - - - - - - \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/main.ts b/spring-security-anguar/client/angular6/src/main.ts deleted file mode 100644 index 49db98ae89..0000000000 --- a/spring-security-anguar/client/angular6/src/main.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; - -import { AppModule } from './app/app.module'; - -platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/polyfills.ts b/spring-security-anguar/client/angular6/src/polyfills.ts deleted file mode 100644 index a4984ced57..0000000000 --- a/spring-security-anguar/client/angular6/src/polyfills.ts +++ /dev/null @@ -1,68 +0,0 @@ -/** - * This file includes polyfills needed by Angular and is loaded before the app. - * You can add your own extra polyfills to this file. - * - * This file is divided into 2 sections: - * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. - * 2. Application imports. Files imported after ZoneJS that should be loaded before your main - * file. - * - * The current setup is for so-called "evergreen" browsers; the last versions of browsers that - * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), - * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. - * - * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html - */ - -/*************************************************************************************************** - * BROWSER POLYFILLS - */ - -/** IE9, IE10 and IE11 requires all of the following polyfills. **/ -// import 'core-js/es6/symbol'; -// import 'core-js/es6/object'; -// import 'core-js/es6/function'; -// import 'core-js/es6/parse-int'; -// import 'core-js/es6/parse-float'; -// import 'core-js/es6/number'; -// import 'core-js/es6/math'; -// import 'core-js/es6/string'; -// import 'core-js/es6/date'; -// import 'core-js/es6/array'; -// import 'core-js/es6/regexp'; -// import 'core-js/es6/map'; -// import 'core-js/es6/weak-map'; -// import 'core-js/es6/set'; -/** IE10 and IE11 requires the following for NgClass support on SVG elements */ -// import 'classlist.js'; // Run `npm install --save classlist.js`. -/** Evergreen browsers require these. **/ -import 'core-js/es6/reflect'; -import 'core-js/es7/reflect'; - - -/** - * Required to support Web Animations `@angular/animation`. - * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation - **/ -// import 'web-animations-js'; // Run `npm install --save web-animations-js`. - - -/*************************************************************************************************** - * Zone JS is required by Angular itself. - */ -import 'zone.js/dist/zone'; // Included with Angular CLI. - - -/*************************************************************************************************** - * APPLICATION IMPORTS - */ - -/** - * Date, currency, decimal and percent pipes. - * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 - */ -// import 'intl'; // Run `npm install --save intl`. -/** - * Need to import at least one locale-data with intl. - */ -// import 'intl/locale-data/jsonp/en'; \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/styles.css b/spring-security-anguar/client/angular6/src/styles.css deleted file mode 100644 index cdd2d591d8..0000000000 --- a/spring-security-anguar/client/angular6/src/styles.css +++ /dev/null @@ -1,7 +0,0 @@ -a { - cursor: pointer; -} - -.help-block { - font-size: 12px; -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/tsconfig.app.json b/spring-security-anguar/client/angular6/src/tsconfig.app.json deleted file mode 100644 index 213ce42a1b..0000000000 --- a/spring-security-anguar/client/angular6/src/tsconfig.app.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "extends": "../tsconfig.json", - "compilerOptions": { - "outDir": "../out-tsc/app", - "baseUrl": "./", - "module": "es2015", - "types": [] - }, - "exclude": [ - "test.ts", - "**/*.spec.ts" - ] -} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/tsconfig.json b/spring-security-anguar/client/angular6/tsconfig.json deleted file mode 100644 index ef44e2862b..0000000000 --- a/spring-security-anguar/client/angular6/tsconfig.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "compileOnSave": false, - "compilerOptions": { - "baseUrl": "./", - "outDir": "./dist/out-tsc", - "sourceMap": true, - "declaration": false, - "moduleResolution": "node", - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "target": "es5", - "typeRoots": [ - "node_modules/@types" - ], - "lib": [ - "es2017", - "dom" - ] - } -} diff --git a/spring-security-anguar/client/angular6/tslint.json b/spring-security-anguar/client/angular6/tslint.json deleted file mode 100644 index 3ea984c776..0000000000 --- a/spring-security-anguar/client/angular6/tslint.json +++ /dev/null @@ -1,130 +0,0 @@ -{ - "rulesDirectory": [ - "node_modules/codelyzer" - ], - "rules": { - "arrow-return-shorthand": true, - "callable-types": true, - "class-name": true, - "comment-format": [ - true, - "check-space" - ], - "curly": true, - "deprecation": { - "severity": "warn" - }, - "eofline": true, - "forin": true, - "import-blacklist": [ - true, - "rxjs/Rx" - ], - "import-spacing": true, - "indent": [ - true, - "spaces" - ], - "interface-over-type-literal": true, - "label-position": true, - "max-line-length": [ - true, - 140 - ], - "member-access": false, - "member-ordering": [ - true, - { - "order": [ - "static-field", - "instance-field", - "static-method", - "instance-method" - ] - } - ], - "no-arg": true, - "no-bitwise": true, - "no-console": [ - true, - "debug", - "info", - "time", - "timeEnd", - "trace" - ], - "no-construct": true, - "no-debugger": true, - "no-duplicate-super": true, - "no-empty": false, - "no-empty-interface": true, - "no-eval": true, - "no-inferrable-types": [ - true, - "ignore-params" - ], - "no-misused-new": true, - "no-non-null-assertion": true, - "no-shadowed-variable": true, - "no-string-literal": false, - "no-string-throw": true, - "no-switch-case-fall-through": true, - "no-trailing-whitespace": true, - "no-unnecessary-initializer": true, - "no-unused-expression": true, - "no-use-before-declare": true, - "no-var-keyword": true, - "object-literal-sort-keys": false, - "one-line": [ - true, - "check-open-brace", - "check-catch", - "check-else", - "check-whitespace" - ], - "prefer-const": true, - "quotemark": [ - true, - "single" - ], - "radix": true, - "semicolon": [ - true, - "always" - ], - "triple-equals": [ - true, - "allow-null-check" - ], - "typedef-whitespace": [ - true, - { - "call-signature": "nospace", - "index-signature": "nospace", - "parameter": "nospace", - "property-declaration": "nospace", - "variable-declaration": "nospace" - } - ], - "unified-signatures": true, - "variable-name": false, - "whitespace": [ - true, - "check-branch", - "check-decl", - "check-operator", - "check-separator", - "check-type" - ], - "no-output-on-prefix": true, - "use-input-property-decorator": true, - "use-output-property-decorator": true, - "use-host-property-decorator": true, - "no-input-rename": true, - "no-output-rename": true, - "use-life-cycle-interface": true, - "use-pipe-transform-interface": true, - "component-class-suffix": true, - "directive-class-suffix": true - } -} diff --git a/spring-security-anguar/server/pom.xml b/spring-security-anguar/server/pom.xml deleted file mode 100644 index 39de129c87..0000000000 --- a/spring-security-anguar/server/pom.xml +++ /dev/null @@ -1,71 +0,0 @@ - - - 4.0.0 - com.baeldung - spring-security-angular - 0.0.1-SNAPSHOT - jar - spring-boot-security-rest - Spring Boot Security REST - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ - - - - - - org.springframework.boot - spring-boot-dependencies - 1.5.9.RELEASE - pom - import - - - - - - - - - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.security - spring-security-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - UTF-8 - UTF-8 - - - diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/SpringBootSecurityApplication.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/SpringBootSecurityApplication.java deleted file mode 100644 index 681c7590a8..0000000000 --- a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/SpringBootSecurityApplication.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.springbootsecurityrest.basicauth; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication(scanBasePackages = "com.baeldung.springbootsecurityrest") -@EnableAutoConfiguration -public class SpringBootSecurityApplication { - - public static void main(String[] args) { - SpringApplication.run(SpringBootSecurityApplication.class, args); - } -} diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/config/BasicAuthConfiguration.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/config/BasicAuthConfiguration.java deleted file mode 100644 index 3ed301439c..0000000000 --- a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/config/BasicAuthConfiguration.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.springbootsecurityrest.basicauth.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpMethod; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; - -@Configuration -@EnableWebSecurity -public class BasicAuthConfiguration extends WebSecurityConfigurerAdapter { - - @Override - protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth - .inMemoryAuthentication() - .withUser("user") - .password("password") - .roles("USER"); - } - - @Override - protected void configure(HttpSecurity http) throws Exception { - http.csrf().disable() - .authorizeRequests() - .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() - .antMatchers("/login").permitAll() - .anyRequest() - .authenticated() - .and() - .httpBasic(); - } -} diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/controller/UserController.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/controller/UserController.java deleted file mode 100644 index 825290ff2d..0000000000 --- a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/controller/UserController.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.springbootsecurityrest.controller; - -import java.security.Principal; -import java.util.Base64; - -import javax.servlet.http.HttpServletRequest; - -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import com.baeldung.springbootsecurityrest.vo.User; - -@RestController -@CrossOrigin -public class UserController { - - @RequestMapping("/login") - public boolean login(@RequestBody User user) { - if(user.getUserName().equals("user") && user.getPassword().equals("password")) { - return true; - } - return false; - } - - @RequestMapping("/user") - public Principal user(HttpServletRequest request) { - String authToken = request.getHeader("Authorization").substring("Basic".length()).trim(); - return () -> new String(Base64.getDecoder().decode(authToken)).split(":")[0]; - } -} diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/vo/User.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/vo/User.java deleted file mode 100644 index 0eda5ce9ec..0000000000 --- a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/vo/User.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.springbootsecurityrest.vo; - - -public class User { - - private String userName; - private String password; - - public String getUserName() { - return userName; - } - public void setUserName(String userName) { - this.userName = userName; - } - public String getPassword() { - return password; - } - public void setPassword(String password) { - this.password = password; - } -} diff --git a/spring-security-anguar/server/src/main/resources/application.properties b/spring-security-anguar/server/src/main/resources/application.properties deleted file mode 100644 index 565d97a7b0..0000000000 --- a/spring-security-anguar/server/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration -#security.user.password=password -#security.oauth2.client.client-id=client -#security.oauth2.client.client-secret=secret -server.port=8082 diff --git a/spring-security-anguar/server/src/main/resources/logback.xml b/spring-security-anguar/server/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/spring-security-anguar/server/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/spring-security-angular/README.md b/spring-security-angular/README.md new file mode 100644 index 0000000000..80312c4bab --- /dev/null +++ b/spring-security-angular/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Spring Security Login Page with Angular](https://www.baeldung.com/spring-security-login-angular) diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/IpApplication.java b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/IpApplication.java new file mode 100644 index 0000000000..fd270686a2 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/IpApplication.java @@ -0,0 +1,12 @@ +package org.baeldung.ip; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +@SpringBootApplication +public class IpApplication extends SpringBootServletInitializer { + public static void main(String[] args) { + SpringApplication.run(IpApplication.class, args); + } +} diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/CustomIpAuthenticationProvider.java b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/CustomIpAuthenticationProvider.java new file mode 100644 index 0000000000..078dd81259 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/CustomIpAuthenticationProvider.java @@ -0,0 +1,53 @@ +package org.baeldung.ip.config; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.web.authentication.WebAuthenticationDetails; +import org.springframework.stereotype.Component; + +@Component +public class CustomIpAuthenticationProvider implements AuthenticationProvider { + + Set whitelist = new HashSet(); + + public CustomIpAuthenticationProvider() { + super(); + whitelist.add("11.11.11.11"); + whitelist.add("127.0.0.1"); + } + + @Override + public Authentication authenticate(Authentication auth) throws AuthenticationException { + WebAuthenticationDetails details = (WebAuthenticationDetails) auth.getDetails(); + String userIp = details.getRemoteAddress(); + if(! whitelist.contains(userIp)){ + throw new BadCredentialsException("Invalid IP Address"); + } + final String name = auth.getName(); + final String password = auth.getCredentials().toString(); + + if (name.equals("john") && password.equals("123")) { + List authorities =new ArrayList(); + authorities.add(new SimpleGrantedAuthority("ROLE_USER")); + return new UsernamePasswordAuthenticationToken(name, password, authorities); + } + else{ + throw new BadCredentialsException("Invalid username or password"); + } + } + + @Override + public boolean supports(Class authentication) { + return authentication.equals(UsernamePasswordAuthenticationToken.class); + } +} diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityConfig.java new file mode 100644 index 0000000000..b4ed8277d6 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityConfig.java @@ -0,0 +1,36 @@ +package org.baeldung.ip.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +@EnableWebSecurity +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Autowired + private CustomIpAuthenticationProvider authenticationProvider; + + @Override + protected void configure(final AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().withUser("john").password("{noop}123").authorities("ROLE_USER"); + // auth.authenticationProvider(authenticationProvider); + } + + @Override + protected void configure(final HttpSecurity http) throws Exception { + // @formatter:off + http.authorizeRequests() + .antMatchers("/login").permitAll() +// .antMatchers("/foos/**").hasIpAddress("11.11.11.11") + .antMatchers("/foos/**").access("isAuthenticated() and hasIpAddress('11.11.11.11')") + .anyRequest().authenticated() + .and().formLogin().permitAll() + .and().csrf().disable(); + // @formatter:on + } + +} \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityXmlConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityXmlConfig.java new file mode 100644 index 0000000000..1d22ca4c67 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/config/SecurityXmlConfig.java @@ -0,0 +1,9 @@ +package org.baeldung.ip.config; + + +//@Configuration +//@EnableWebSecurity +//@ImportResource({ "classpath:spring-security-ip.xml" }) +public class SecurityXmlConfig { + +} \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/web/MainController.java b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/web/MainController.java new file mode 100644 index 0000000000..da5db5e825 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/web/MainController.java @@ -0,0 +1,21 @@ +package org.baeldung.ip.web; + +import javax.servlet.http.HttpServletRequest; + +import org.baeldung.custom.persistence.model.Foo; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class MainController { + + @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") + @ResponseBody + public Foo findById(@PathVariable final long id, HttpServletRequest request) { + return new Foo("Sample"); + } + +} diff --git a/spring-security-mvc-boot/src/main/resources/spring-security-ip.xml b/spring-security-mvc-boot/src/main/resources/spring-security-ip.xml new file mode 100644 index 0000000000..31796ad134 --- /dev/null +++ b/spring-security-mvc-boot/src/main/resources/spring-security-ip.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + diff --git a/spring-security-mvc-boot/src/test/java/org/baeldung/web/IpLiveTest.java b/spring-security-mvc-boot/src/test/java/org/baeldung/web/IpLiveTest.java new file mode 100644 index 0000000000..e12e2f87b0 --- /dev/null +++ b/spring-security-mvc-boot/src/test/java/org/baeldung/web/IpLiveTest.java @@ -0,0 +1,27 @@ +package org.baeldung.web; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import io.restassured.RestAssured; +import io.restassured.response.Response; + +import org.junit.Test; + + +public class IpLiveTest { + + @Test + public void givenUser_whenGetHomePage_thenOK() { + final Response response = RestAssured.given().auth().form("john", "123").get("http://localhost:8082/"); + assertEquals(200, response.getStatusCode()); + assertTrue(response.asString().contains("Welcome")); + } + + @Test + public void givenUserWithWrongIP_whenGetFooById_thenForbidden() { + final Response response = RestAssured.given().auth().form("john", "123").get("http://localhost:8082/foos/1"); + assertEquals(403, response.getStatusCode()); + assertTrue(response.asString().contains("Forbidden")); + } + +} \ No newline at end of file diff --git a/spring-security-mvc-digest-auth/pom.xml b/spring-security-mvc-digest-auth/pom.xml index 2d3f1a3a00..33b3b5ff31 100644 --- a/spring-security-mvc-digest-auth/pom.xml +++ b/spring-security-mvc-digest-auth/pom.xml @@ -1,8 +1,6 @@ 4.0.0 - - com.baeldung spring-security-mvc-digest-auth 0.1-SNAPSHOT spring-security-mvc-digest-auth @@ -91,12 +89,6 @@ ${spring.version} - - org.springframework - spring-web - ${spring.version} - - diff --git a/spring-security-mvc-socket/README.md b/spring-security-mvc-socket/README.md index 5ae9d4f4cd..828d9a3448 100644 --- a/spring-security-mvc-socket/README.md +++ b/spring-security-mvc-socket/README.md @@ -1,2 +1,3 @@ ### Relevant Articles: - [Intro to Security and WebSockets](http://www.baeldung.com/spring-security-websockets) +- [Spring WebSockets: Specific User Chat](http://www.baeldung.com/spring-websocket-specific-user-chat) \ No newline at end of file diff --git a/spring-security-mvc-socket/pom.xml b/spring-security-mvc-socket/pom.xml index 1f75becc98..b7559753b5 100644 --- a/spring-security-mvc-socket/pom.xml +++ b/spring-security-mvc-socket/pom.xml @@ -11,9 +11,9 @@ com.baeldung - parent-spring-5 + parent-spring-4 0.0.1-SNAPSHOT - ../parent-spring-5 + ../parent-spring-4 @@ -177,9 +177,11 @@ 5.2.10.Final + 4.2.3.RELEASE 1.11.3.RELEASE 1.4.196 1.2.3 + 2.8.7 \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/Constants.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/Constants.java new file mode 100644 index 0000000000..77bcf7f343 --- /dev/null +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/Constants.java @@ -0,0 +1,8 @@ +package com.baeldung.springsecuredsockets; + +public class Constants { + public static final String SECURED_CHAT_HISTORY = "/secured/history"; + public static final String SECURED_CHAT = "/secured/chat"; + public static final String SECURED_CHAT_ROOM = "/secured/room"; + public static final String SECURED_CHAT_SPECIFIC_USER = "/secured/user/queue/specific-user"; +} diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/AppConfig.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/AppConfig.java index 49b7ab2ff8..afb1970b25 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/AppConfig.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/AppConfig.java @@ -9,7 +9,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.resource.PathResourceResolver; import org.springframework.web.servlet.view.JstlView; import org.springframework.web.servlet.view.UrlBasedViewResolver; @@ -20,7 +20,7 @@ import java.sql.SQLException; @EnableJpaRepositories @ComponentScan("com.baeldung.springsecuredsockets") @Import({ SecurityConfig.class, DataStoreConfig.class, SocketBrokerConfig.class, SocketSecurityConfig.class }) -public class AppConfig implements WebMvcConfigurer { +public class AppConfig extends WebMvcConfigurerAdapter { public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SecurityConfig.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SecurityConfig.java index d7b57d1829..be00662e3e 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SecurityConfig.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SecurityConfig.java @@ -88,7 +88,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { .authorizeRequests() .antMatchers("/", "/index", "/authenticate") .permitAll() - .antMatchers("/secured/**/**", "/secured/socket", "/secured/success") + .antMatchers("/secured/**/**", "/secured/**/**/**", "/secured/socket", "/secured/success") .authenticated() .anyRequest().authenticated() .and() diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketBrokerConfig.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketBrokerConfig.java index 941cac8392..9b19de7b5a 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketBrokerConfig.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketBrokerConfig.java @@ -1,27 +1,33 @@ package com.baeldung.springsecuredsockets.config; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_HISTORY; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_ROOM; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_SPECIFIC_USER; + import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; -import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; - @Configuration @EnableWebSocketMessageBroker @ComponentScan("com.baeldung.springsecuredsockets.controllers") -public class SocketBrokerConfig implements WebSocketMessageBrokerConfigurer { +public class SocketBrokerConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { - config.enableSimpleBroker("/secured/history"); + config.enableSimpleBroker(SECURED_CHAT_HISTORY, SECURED_CHAT_SPECIFIC_USER); config.setApplicationDestinationPrefixes("/spring-security-mvc-socket"); + config.setUserDestinationPrefix("/secured/user"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { - registry.addEndpoint("/secured/chat").withSockJS(); + registry.addEndpoint(SECURED_CHAT_ROOM).withSockJS(); + registry.addEndpoint(SECURED_CHAT).withSockJS(); } } diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketSecurityConfig.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketSecurityConfig.java index a37dfb7672..c833e70d47 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketSecurityConfig.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/SocketSecurityConfig.java @@ -15,7 +15,7 @@ public class SocketSecurityConfig extends AbstractSecurityWebSocketMessageBroker @Override protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) { messages - .simpDestMatchers("/secured/**").authenticated() + .simpDestMatchers("/secured/**", "/secured/**/**").authenticated() .anyMessage().authenticated(); } } \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/controllers/SocketController.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/controllers/SocketController.java index 68c5e306d8..6a74009c16 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/controllers/SocketController.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/controllers/SocketController.java @@ -1,11 +1,19 @@ package com.baeldung.springsecuredsockets.controllers; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_HISTORY; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_ROOM; +import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_SPECIFIC_USER; + import com.baeldung.springsecuredsockets.transfer.socket.Message; import com.baeldung.springsecuredsockets.transfer.socket.OutputMessage; +import java.security.Principal; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.handler.annotation.Header; import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.Payload; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Controller; @@ -19,10 +27,19 @@ public class SocketController { private SimpMessagingTemplate simpMessagingTemplate; private static final Logger log = LoggerFactory.getLogger(SocketController.class); - @MessageMapping("/secured/chat") - @SendTo("/secured/history") - public OutputMessage send(Message msg) throws Exception { + @MessageMapping(SECURED_CHAT) + @SendTo(SECURED_CHAT_HISTORY) + public OutputMessage sendAll(Message msg) throws Exception { OutputMessage out = new OutputMessage(msg.getFrom(), msg.getText(), new SimpleDateFormat("HH:mm").format(new Date())); return out; } + + /** + * Example of sending message to specific user using 'convertAndSendToUser()' and '/queue' + */ + @MessageMapping(SECURED_CHAT_ROOM) + public void sendSpecific(@Payload Message msg, Principal user, @Header("simpSessionId") String sessionId) throws Exception { + OutputMessage out = new OutputMessage(msg.getFrom(), msg.getText(), new SimpleDateFormat("HH:mm").format(new Date())); + simpMessagingTemplate.convertAndSendToUser(msg.getTo(), SECURED_CHAT_SPECIFIC_USER, out); + } } diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/transfer/socket/Message.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/transfer/socket/Message.java index 024b386164..fd5d632fce 100644 --- a/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/transfer/socket/Message.java +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/transfer/socket/Message.java @@ -3,6 +3,7 @@ package com.baeldung.springsecuredsockets.transfer.socket; public class Message { private String from; + private String to; private String text; public String getFrom() { @@ -11,6 +12,13 @@ public class Message { public void setFrom(String from) { this.from = from; } + + public String getTo() { + return to; + } + public void setTo(String to) { + this.to = to; + } public String getText() { return text; } diff --git a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/denied.jsp b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/denied.jsp index d61148b04f..e4c40da0a8 100644 --- a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/denied.jsp +++ b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/denied.jsp @@ -4,15 +4,20 @@ - - Spring Secured Sockets - " rel="stylesheet"> - - - -

ACCESS DENIED!

-
-
- Click to login. - + + + Spring Secured Sockets + " rel="stylesheet"> + " rel="stylesheet"> + + + + +
+
+

ACCESS DENIED!

+ Click to login. +
+
+ \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/index.jsp b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/index.jsp index d83338680e..d1435e8c2b 100644 --- a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/index.jsp +++ b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/index.jsp @@ -4,27 +4,32 @@ - - Spring Secured Sockets - " rel="stylesheet"> - - - - - - - - - - - - -

Welcome!

-
- {{greeting}} -
-
- Click to login. - + + Spring Secured Sockets + " rel="stylesheet"> + " rel="stylesheet"> + + + + + + + + + + + + + + +
+
+

Welcome!

+ {{greeting}} + Click to login. +
+
+ + \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/login.jsp b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/login.jsp index d50059c674..76e3334bbe 100644 --- a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/login.jsp +++ b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/login.jsp @@ -4,42 +4,50 @@ - - Spring Secured Sockets - " rel="stylesheet"> - - - - - - - - - - - - - - -

JSP Login Form


-
- - - - - - - - - - - - -
User:
Password:
-
+ + Spring Secured Sockets + " rel="stylesheet"> + " rel="stylesheet"> + + + + + + + + + + + + + + + +
+
+ +

JSP Login Form

+
+ + + + + + + + + + + + +
User:
Password:
+
+ +
+
+ + + - - - \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/socket.jsp b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/socket.jsp index b5807d74a6..ccd48672a1 100644 --- a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/socket.jsp +++ b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/socket.jsp @@ -4,48 +4,56 @@ - - Spring Secured Sockets - " rel="stylesheet"> - - - - - - - - - - - - - - -

Socket Chat

- -
-
- -
-
-
- - - -
-
-
- - -

-
-
+ + Spring Secured Sockets + " rel="stylesheet"> + " rel="stylesheet"> -Click to go back! -Click to start over (you will still be authenticated)! + + + + + + + + + + + + + + + + +
+
+

Socket Chat

+
+

(Chat With Everyone)

+ + + +
+
+

(Chat With A Specific User)

+ + + +
+
+

(Send and See Messages)

+ + +
+ +
+ Click to go back! + Click to start over (you will still be authenticated)! +
+
+ + + - - - \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/success.jsp b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/success.jsp index 27e8f7aa44..4cff3f7a25 100644 --- a/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/success.jsp +++ b/spring-security-mvc-socket/src/main/webapp/WEB-INF/jsp/success.jsp @@ -4,28 +4,33 @@ - - Spring Secured Sockets - " rel="stylesheet"> - - - - - - - - - - - - -

Congrats! You've logged in.

+ + Spring Secured Sockets + " rel="stylesheet"> + " rel="stylesheet"> - Click to chat! - Click to start over (you will still be authenticated)! + + + - - - + + + + + + + + + +
+ +
+ + + \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/indexController.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/indexController.js index 04c02e339b..5de3dcc78b 100644 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/indexController.js +++ b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/indexController.js @@ -1,12 +1,13 @@ 'use strict'; angularApp - .controller('indexController', function ($scope) { - $scope.greeting = ''; +.controller('indexController', function ($scope) { - $scope.initialize = function () { - $scope.greeting = "Howdy!" - }; + $scope.greeting = ''; - $scope.initialize(); - }); \ No newline at end of file + $scope.initialize = function () { + $scope.greeting = "Howdy!" + }; + + $scope.initialize(); +}); \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/loginController.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/loginController.js deleted file mode 100644 index 07187d5327..0000000000 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/loginController.js +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; - -angularApp - .controller('loginController', function ($scope) { - - }); diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/socketController.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/socketController.js index 395bf85c4a..ff000c251e 100644 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/socketController.js +++ b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/socketController.js @@ -1,37 +1,52 @@ 'use strict'; angularApp - .controller('socketController', function ($scope, SocketService) { +.controller('socketController', function ($scope, SocketService) { - $scope.stompClient = null; - $scope.sendEndpoint = '/secured/chat'; - $scope.subscribeEndpoint = '/secured/history'; - $scope.elems = { - connect: 'connect', - from: 'from', - text: 'text', - disconnect: 'disconnect', - conversationDiv: 'conversationDiv', - response: 'response' - }; + /** + * URL mapping endpoints. + */ - $scope.connect = function (context) { - $scope.sendEndpoint = '/secured/chat'; - $scope.sendEndpoint = context + $scope.sendEndpoint ; - $scope.stompClient = SocketService.connect($scope.sendEndpoint, $scope.elems); - }; + var SECURED_CHAT = '/secured/chat'; + var SECURED_CHAT_HISTORY = '/secured/history'; + var SECURED_CHAT_ROOM = '/secured/room'; + var SECURED_CHAT_SPECIFIC_USER = '/secured/user/queue/specific-user'; - $scope.subscribe = function () { - $scope.stompClient.subscribe($scope.subscribeEndpoint, function (msgOut) { - SocketService.messageOut(JSON.parse(msgOut.body), $scope.elems); - }); - }; + var opts = { + from: 'from', + to: 'to', + text: 'text', + disconnect: 'disconnect', + conversationDiv: 'conversationDiv', + response: 'response' + }; - $scope.disconnect = function () { - SocketService.disconnect($scope.elems, $scope.stompClient); - }; + $scope.sendEndpoint = ''; + $scope.stompClient = null; - $scope.sendMessage = function () { - SocketService.sendMessage( $scope.elems, $scope.stompClient, $scope.sendEndpoint); - }; - }); \ No newline at end of file + /** + * Broadcast to All Users. + */ + + $scope.connectAll = function (context) { + $scope.sendEndpoint = context + SECURED_CHAT; + $scope.stompClient = SocketService.connect($scope.sendEndpoint , opts, true); + }; + + $scope.subscribeAll = function () { SocketService.subscribeToAll($scope.stompClient, SECURED_CHAT_HISTORY, opts); }; + + /** + * Broadcast to Specific User. + */ + + $scope.connectSpecific = function (context) { + $scope.sendEndpoint = context + SECURED_CHAT_ROOM; + $scope.stompClient = SocketService.connect(context + SECURED_CHAT_ROOM, opts, false); + }; + + $scope.subscribeSpecific = function () { SocketService.subscribeToSpecific($scope.stompClient, SECURED_CHAT_SPECIFIC_USER, opts); }; + + $scope.disconnect = function () { SocketService.disconnect(opts, $scope.stompClient); }; + + $scope.sendMessage = function () { SocketService.sendMessage(opts, $scope.stompClient, $scope.sendEndpoint); }; +}); \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/successController.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/successController.js index ccb972202d..6926239202 100644 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/successController.js +++ b/spring-security-mvc-socket/src/main/webapp/resources/scripts/controllers/successController.js @@ -1,13 +1,12 @@ 'use strict'; angularApp - .controller('successController', function ($scope) { +.controller('successController', function ($scope) { + $scope.successMsg = ''; - $scope.successMsg = ''; + $scope.initialize = function () { + $scope.successMsg = "You've logged in!"; + }; - $scope.initialize = function () { - $scope.successMsg = "You've logged in!"; - }; - - $scope.initialize(); - }); + $scope.initialize(); +}); diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/routes/router.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/routes/router.js index 70644f996d..004c7b331a 100644 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/routes/router.js +++ b/spring-security-mvc-socket/src/main/webapp/resources/scripts/routes/router.js @@ -1,19 +1,16 @@ 'use strict'; angularApp - .config(function ($routeProvider) { - $routeProvider - .when('/index', { - controller: 'indexController' - }) - .when('/login', { - controller: 'loginController' - }) - .when('/sockets', { - controller: 'socketController' - }) - .when('/success', { - controller: 'successController' - }) - .otherwise('/index'); - }); \ No newline at end of file +.config(function ($routeProvider) { + $routeProvider + .when('/index', { + controller: 'indexController' + }) + .when('/sockets', { + controller: 'socketController' + }) + .when('/success', { + controller: 'successController' + }) + .otherwise('/index'); +}); \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/scripts/services/SocketService.js b/spring-security-mvc-socket/src/main/webapp/resources/scripts/services/SocketService.js index 56930808a3..ba3f397f89 100644 --- a/spring-security-mvc-socket/src/main/webapp/resources/scripts/services/SocketService.js +++ b/spring-security-mvc-socket/src/main/webapp/resources/scripts/services/SocketService.js @@ -1,46 +1,99 @@ 'use strict'; +var idHelper = function (context) { + return document.getElementById(context); +}; + function SocketService() { - var that = this, - idHelper = function(context) { - return document.getElementById(context); - }; - that.setConnected = function (elems, connected) { - idHelper(elems.connect).disabled = connected; - idHelper(elems.disconnect).disabled = !connected; - idHelper(elems.conversationDiv).style.visibility = connected ? 'visible' : 'hidden'; - idHelper(elems.response).innerHTML = ''; + var that = this; + + that.sessionId = ''; + + /** + * Generic methods. + */ + + that.setConnected = function (opts, connected) { + idHelper('connectAll').disabled = connected; + idHelper('connectSpecific').disabled = connected; + idHelper(opts.disconnect).disabled = !connected; + idHelper(opts.response).innerHTML = ''; + idHelper('subscribeAll').disabled = !connected; + idHelper('subscribeSpecific').disabled = !connected; + }; + + that.connect = function (endpoint, opts, isBroadcastAll) { + var socket = new SockJS(endpoint), stompClient = Stomp.over(socket); + stompClient.connect({}, function (frame) { + that.setConnected(opts, true); + + if (!isBroadcastAll) { + var url = stompClient.ws._transport.url; + console.log(stompClient.ws._transport.url); + url = url.replace("ws://localhost:8080/spring-security-mvc-socket/secured/room/", ""); + url = url.replace("/websocket", ""); + url = url.replace(/^[0-9]+\//, ""); + console.log("Your current session is: " + url); + that.sessionId = url; + } + + }); + return stompClient; + }; + + that.disconnect = function (opts, stompClient) { + if (stompClient !== null && stompClient !== undefined) { stompClient.disconnect(); } + that.setConnected(opts, false); + }; + + that.sendMessage = function (opts, stompClient, endpoint) { + var to = idHelper(opts.to).value; + var from = idHelper(opts.from).value; + + var msg = { + 'from': (from === undefined || from === null ) ? to : from, + 'to': (to === undefined || to === null ) ? "ALL" : to, + 'text': idHelper(opts.text).value }; - that.connect = function (endpoint, elems) { - var socket = new SockJS(endpoint), stompClient = Stomp.over(socket); - stompClient.connect({}, function (frame) { - that.setConnected(elems, true); - }); - return stompClient; - }; + console.log(JSON.stringify(msg)); + stompClient.send(endpoint, {}, JSON.stringify(msg)); + }; - that.disconnect = function (elems, stompClient) { - if (stompClient !== null && stompClient !== undefined) stompClient.disconnect(); - that.setConnected(elems, false); - }; + that.messageOut = function (msg, opts) { + var r = idHelper(opts.response), p = document.createElement('p'); + p.style.wordWrap = 'break-word'; + p.appendChild(document.createTextNode(msg.from + ': ' + msg.text + ' (' + msg.time + ')')); + r.appendChild(p); + }; + + /** + * Broadcast to All Users. + */ + + that.subscribeToAll = function(client, url, opts) { + idHelper('subscribeAll').disabled = true; + idHelper('subscribeSpecific').disabled = true; + client.subscribe(url, function (msgOut) { + that.messageOut(JSON.parse(msgOut.body), opts); + }); + }; + + /** + * Broadcast to Specific User. + */ + + that.subscribeToSpecific = function(client, url, opts) { + idHelper('subscribeAll').disabled = true; + idHelper('subscribeSpecific').disabled = true; + client.subscribe(url + "-user" + that.sessionId, function (msgOut) { + that.messageOut(JSON.parse(msgOut.body), opts); + }); + }; - that.sendMessage = function (elems, stompClient, endpoint) { - stompClient.send(endpoint, {}, - JSON.stringify({ - 'from': idHelper(elems.from).value, - 'text': idHelper(elems.text).value - })); - }; - that.messageOut = function (msg, elems) { - var r = idHelper(elems.response), p = document.createElement('p'); - p.style.wordWrap = 'break-word'; - p.appendChild(document.createTextNode(msg.from + ': ' + msg.text + ' (' + msg.time + ')')); - r.appendChild(p); - }; } angularApp - .service('SocketService', SocketService); \ No newline at end of file +.service('SocketService', SocketService); \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/app.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/app.css new file mode 100644 index 0000000000..ee2cb4d717 --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/app.css @@ -0,0 +1,70 @@ +@import url('https://fonts.googleapis.com/css?family=Poiret+One'); + +html, body, main { + padding: 0; + margin: 0; + position: absolute; + height: 100%; + width: 100%; + color: white; + font-family: 'Poiret One', sans-serif; + opacity:.95; +} + +main { + background-size: cover; +} + +main > div.wrapper { + position: relative; + left: 15%; + top: 15%; + width: 40%; +} + +h1 { + text-transform: uppercase; + font-size: 48px; + letter-spacing: 2px; +} + +span, a { + font-size: 24px; + letter-spacing: 1px; +} + +a { + text-decoration: none; + color: white; +} + +a:hover { + color: lightgray; +} + +input { + width: 225px; + background: none !important; + border-radius: 25px; + background-color: #fff; + color: white; + outline: none; + margin-left: 2px; + padding-left: 25px; + padding-bottom: 9px; + padding-top: 9px; +} + +button { + width: 100px; + align-content: center; + text-align: center; + background: transparent; + border-radius: 25px; + padding: 6px; + color: white; +} + +button:hover { + opacity: .6; +} \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/denied.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/denied.css new file mode 100644 index 0000000000..3956715e4a --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/denied.css @@ -0,0 +1,3 @@ +main { + background-color: black; +} diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/index.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/index.css new file mode 100644 index 0000000000..b6da4814b7 --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/index.css @@ -0,0 +1,7 @@ +main { + background-color: black; +} + +span, a { + top: 175px; +} \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/login.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/login.css new file mode 100644 index 0000000000..197f3e41bb --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/login.css @@ -0,0 +1,17 @@ +main { + background-color: black; +} + +input[type="submit"] { + width: 100px; + align-content: center; + text-align: center; + background: transparent; + border-radius: 25px; + padding: 6px; + color: white; +} + +input[type="submit"]:hover { + opacity:.6; +} \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/socket.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/socket.css new file mode 100644 index 0000000000..aa483fd684 --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/socket.css @@ -0,0 +1,19 @@ +main { + background-color: black; +} + +div#all, div#specific, div#conversationDiv { + border: 1px solid white; + border-radius: 25px; + padding: 15px; + margin: 15px +} + +button[disabled=true] { + color: lightgray; + border: 1px solid lightgray; +} + +button[disabled=true]:hover { + opacity: 1; +} \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/webapp/resources/styles/success.css b/spring-security-mvc-socket/src/main/webapp/resources/styles/success.css new file mode 100644 index 0000000000..3956715e4a --- /dev/null +++ b/spring-security-mvc-socket/src/main/webapp/resources/styles/success.css @@ -0,0 +1,3 @@ +main { + background-color: black; +} diff --git a/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml b/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml index aac6fe9843..0f1cfa27ac 100644 --- a/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml +++ b/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml @@ -2,11 +2,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 - com.baeldung spring-swagger-codegen-api-client jar spring-swagger-codegen-api-client - 0.0.1-SNAPSHOT https://github.com/swagger-api/swagger-codegen Swagger Java @@ -14,9 +12,7 @@ scm:git:git@github.com:swagger-api/swagger-codegen.git https://github.com/swagger-api/swagger-codegen - - 2.2.0 - + Unlicense @@ -58,20 +54,6 @@ pertest
- - maven-dependency-plugin - - - package - - copy-dependencies - - - ${project.build.directory}/lib - - - - diff --git a/spring-thymeleaf/README.md b/spring-thymeleaf/README.md index 0dcb742c09..f9e7dd90a7 100644 --- a/spring-thymeleaf/README.md +++ b/spring-thymeleaf/README.md @@ -17,6 +17,7 @@ - [Working With Arrays in Thymeleaf](http://www.baeldung.com/thymeleaf-arrays) - [Spring with Thymeleaf Pagination for a List](http://www.baeldung.com/spring-thymeleaf-pagination) - [Working with Select and Option in Thymeleaf](http://www.baeldung.com/thymeleaf-select-option) +- [Working With Custom HTML Attributes in Thymeleaf](https://www.baeldung.com/thymeleaf-custom-html-attributes) ### Build the Project diff --git a/spring-vault/pom.xml b/spring-vault/pom.xml new file mode 100644 index 0000000000..31616cdb16 --- /dev/null +++ b/spring-vault/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + com.baeldung + spring-vault + 0.0.1-SNAPSHOT + jar + spring-vault + Spring Vault sample project + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.vault + spring-vault-core + ${spring.vault.core.version} + + + com.fasterxml.jackson.core + jackson-databind + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + + + + + + + + UTF-8 + 2.0.1.RELEASE + + diff --git a/spring-vault/src/main/java/org/baeldung/springvault/Credentials.java b/spring-vault/src/main/java/org/baeldung/springvault/Credentials.java new file mode 100644 index 0000000000..f90ab66a4e --- /dev/null +++ b/spring-vault/src/main/java/org/baeldung/springvault/Credentials.java @@ -0,0 +1,30 @@ +package org.baeldung.springvault; + +public class Credentials { + + private String username; + private String password; + + public Credentials() { + + } + + public Credentials(String username, String password) { + this.username = username; + this.password = password; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + @Override + public String toString() { + return "Credential [username=" + username + ", password=" + password + "]"; + } + +} diff --git a/spring-vault/src/main/java/org/baeldung/springvault/CredentialsService.java b/spring-vault/src/main/java/org/baeldung/springvault/CredentialsService.java new file mode 100644 index 0000000000..87c24bd947 --- /dev/null +++ b/spring-vault/src/main/java/org/baeldung/springvault/CredentialsService.java @@ -0,0 +1,50 @@ +package org.baeldung.springvault; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.vault.authentication.TokenAuthentication; +import org.springframework.vault.client.VaultEndpoint; +import org.springframework.vault.core.VaultTemplate; +import org.springframework.vault.core.env.VaultPropertySource; +import org.springframework.vault.support.VaultResponse; +import org.springframework.vault.support.VaultResponseSupport; + +/** + * Sample service to demonstrate storing and retrieval of secrets. + * + * NOTE: We need to configure Vault and provide the Vault uri in the properties file. + * + */ +@Service +public class CredentialsService { + + @Autowired + private VaultTemplate vaultTemplate; + + /** + * To Secure Credentials + * @param credentials + * @return VaultResponse + * @throws URISyntaxException + */ + public void secureCredentials(Credentials credentials) throws URISyntaxException { + + vaultTemplate.write("credentials/myapp", credentials); + } + + /** + * To Retrieve Credentials + * @return Credentials + * @throws URISyntaxException + */ + public Credentials accessCredentials() throws URISyntaxException { + + VaultResponseSupport response = vaultTemplate.read("credentials/myapp", Credentials.class); + return response.getData(); + } + +} diff --git a/spring-vault/src/main/java/org/baeldung/springvault/SpringVaultApplication.java b/spring-vault/src/main/java/org/baeldung/springvault/SpringVaultApplication.java new file mode 100644 index 0000000000..916a809be5 --- /dev/null +++ b/spring-vault/src/main/java/org/baeldung/springvault/SpringVaultApplication.java @@ -0,0 +1,17 @@ +package org.baeldung.springvault; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Sample application to demonstrate basics of Spring Vault + * + */ +@SpringBootApplication +public class SpringVaultApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringVaultApplication.class, args); + } + +} diff --git a/spring-vault/src/main/java/org/baeldung/springvault/VaultConfig.java b/spring-vault/src/main/java/org/baeldung/springvault/VaultConfig.java new file mode 100644 index 0000000000..106a1a3d42 --- /dev/null +++ b/spring-vault/src/main/java/org/baeldung/springvault/VaultConfig.java @@ -0,0 +1,26 @@ +package org.baeldung.springvault; + +import org.springframework.context.annotation.Configuration; +import org.springframework.vault.authentication.ClientAuthentication; +import org.springframework.vault.authentication.TokenAuthentication; +import org.springframework.vault.client.VaultEndpoint; +import org.springframework.vault.config.AbstractVaultConfiguration; + +/** + * Example class to configure Vault beans using AbstractVaultConfiguration + * + */ +@Configuration +public class VaultConfig extends AbstractVaultConfiguration { + + @Override + public ClientAuthentication clientAuthentication() { + return new TokenAuthentication("00000000-0000-0000-0000-000000000000"); + } + + @Override + public VaultEndpoint vaultEndpoint() { + return VaultEndpoint.create("host", 8020); + } + +} diff --git a/spring-vault/src/main/java/org/baeldung/springvault/VaultEnvironmentConfig.java b/spring-vault/src/main/java/org/baeldung/springvault/VaultEnvironmentConfig.java new file mode 100644 index 0000000000..6c796bc718 --- /dev/null +++ b/spring-vault/src/main/java/org/baeldung/springvault/VaultEnvironmentConfig.java @@ -0,0 +1,21 @@ +package org.baeldung.springvault; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; +import org.springframework.vault.authentication.ClientAuthentication; +import org.springframework.vault.authentication.TokenAuthentication; +import org.springframework.vault.client.VaultEndpoint; +import org.springframework.vault.config.AbstractVaultConfiguration; +import org.springframework.vault.config.EnvironmentVaultConfiguration; + +/** + * Example class to configure Vault beans using EnvironmentVaultConfiguration + * + */ +@Configuration +@PropertySource(value = { "vault-config.properties" }) +@Import(value = EnvironmentVaultConfiguration.class) +public class VaultEnvironmentConfig { + +} diff --git a/spring-vault/src/main/resources/vault-config.properties b/spring-vault/src/main/resources/vault-config.properties new file mode 100644 index 0000000000..68a080ebe8 --- /dev/null +++ b/spring-vault/src/main/resources/vault-config.properties @@ -0,0 +1,2 @@ +vault.uri=https://localhost:8200 +vault.token=00000000-0000-0000-0000-000000000000 \ No newline at end of file diff --git a/spring-vault/src/test/java/org/baeldung/springvault/VaultInitializer.java b/spring-vault/src/test/java/org/baeldung/springvault/VaultInitializer.java new file mode 100644 index 0000000000..c7db5eb199 --- /dev/null +++ b/spring-vault/src/test/java/org/baeldung/springvault/VaultInitializer.java @@ -0,0 +1,113 @@ +package org.baeldung.springvault; + +import java.io.BufferedReader; +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Map; + +/** + * + * This is a test class to initialize Vault. + */ +public class VaultInitializer implements Closeable { + + private static final String UNSEAL_KEY = "Unseal Key:"; + private static final String ROOT_TOKEN = "Root Token:"; + + private Process vaultProcess; + private String unSealKey; + private String rootToken; + + public String getRootToken() { + return rootToken; + } + + public String getUnSealKey() { + return unSealKey; + } + + public static final VaultInitializer initializeValut() { + VaultInitializer vaultProcess = new VaultInitializer(); + vaultProcess.start(); + // Secrets is by default enabled. + vaultProcess.enableSecrets(); + return vaultProcess; + } + + @SuppressWarnings("unused") + private void enableSecrets() { + System.out.println("Enabling Secrets at path credentials/myapp..."); + ProcessBuilder pb = new ProcessBuilder("vault", "secrets", "enable", "-path=credentials/myapp", "kv"); + Map map = pb.environment(); + map.put("VAULT_ADDR", "http://127.0.0.1:8200"); + try { + Process p = pb.inheritIO() + .start(); + p.waitFor(); + } catch (IOException e) { + System.out.println("unable to enableSecrets" + e); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public void start() { + System.out.println("starting vault"); + // This starts the vault server. + ProcessBuilder pb = new ProcessBuilder("vault", "server", "-dev"); + + try { + vaultProcess = pb.start(); + // wait for initialization to complete. + Thread.sleep(5000); + } catch (IOException e) { + System.out.println("unable to start vault in new process" + e); + } catch (InterruptedException e) { + System.out.println("Thread interrupted " + e); + } + extractUnsealKeyAndToken(); + } + + /** + * To get the root token which is generated every time server is initialized. + */ + private void extractUnsealKeyAndToken() { + BufferedReader reader = new BufferedReader(new InputStreamReader(vaultProcess.getInputStream())); + StringBuilder builder = new StringBuilder(); + String line = null; + boolean tokenExtracted = false; + try { + while ((line = reader.readLine()) != null) { + builder.append(line); + builder.append(System.getProperty("line.separator")); + if (line.contains(UNSEAL_KEY)) { + String tmp = line.replace(UNSEAL_KEY, ""); + unSealKey = tmp.trim(); + } else if (line.contains(ROOT_TOKEN)) { + String tmp = line.replace(ROOT_TOKEN, ""); + rootToken = tmp.trim(); + tokenExtracted = true; + } + if (tokenExtracted) + break; + System.out.println(line); + } + } catch (IOException e) { + System.out.println("unable to read vault output" + e); + } + + String result = builder.toString(); + + System.out.println("Unseal Key {}" + unSealKey); + System.out.println("Root Token {}" + rootToken); + System.out.println(result); + } + + @Override + public void close() throws IOException { + + System.out.println("stoping vault"); + vaultProcess.destroy(); + } +} diff --git a/spring-vault/src/test/java/org/baeldung/springvault/VaultIntegrationTest.java b/spring-vault/src/test/java/org/baeldung/springvault/VaultIntegrationTest.java new file mode 100644 index 0000000000..75c700ee26 --- /dev/null +++ b/spring-vault/src/test/java/org/baeldung/springvault/VaultIntegrationTest.java @@ -0,0 +1,80 @@ +package org.baeldung.springvault; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.annotation.Order; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; +import org.springframework.vault.authentication.TokenAuthentication; +import org.springframework.vault.client.VaultEndpoint; +import org.springframework.vault.core.VaultTemplate; +import org.springframework.vault.support.VaultResponse; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = CredentialsService.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ContextConfiguration(classes = VaultTestConfiguration.class, loader = AnnotationConfigContextLoader.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@DirtiesContext(classMode = ClassMode.AFTER_CLASS) +public class VaultIntegrationTest { + + @Autowired + private CredentialsService credentialsService; + + /** + * Test to secure credentials. + * + * @throws URISyntaxException + */ + @Test + public void givenCredentials_whenSecureCredentials_thenCredentialsSecured() throws URISyntaxException { + try { + // Given + Credentials credentials = new Credentials("username", "password"); + + // When + credentialsService.secureCredentials(credentials); + + } catch (Exception e) { + e.printStackTrace(); + } + + } + + /** + * Test to access credentials + * @throws URISyntaxException + */ + @Test + public void whenAccessCredentials_thenCredentialsRetrieved() throws URISyntaxException { + + // Given + Credentials credentials = credentialsService.accessCredentials(); + + // Then + assertNotNull(credentials); + assertEquals("username", credentials.getUsername()); + assertEquals("password", credentials.getPassword()); + } + +} diff --git a/spring-vault/src/test/java/org/baeldung/springvault/VaultTestConfiguration.java b/spring-vault/src/test/java/org/baeldung/springvault/VaultTestConfiguration.java new file mode 100644 index 0000000000..09a1445788 --- /dev/null +++ b/spring-vault/src/test/java/org/baeldung/springvault/VaultTestConfiguration.java @@ -0,0 +1,29 @@ +package org.baeldung.springvault; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.vault.authentication.TokenAuthentication; +import org.springframework.vault.client.VaultEndpoint; +import org.springframework.vault.core.VaultTemplate; + +@Configuration +public class VaultTestConfiguration { + + @Bean + public VaultInitializer vaultInitializer() { + VaultInitializer vaultInitializer = VaultInitializer.initializeValut(); + return vaultInitializer; + } + + @Bean + public VaultTemplate vaultTemplate() throws URISyntaxException { + + VaultInitializer vaultInitializer = vaultInitializer(); + VaultTemplate vaultTemplate = new VaultTemplate(VaultEndpoint.from(new URI("http://localhost:8200")), new TokenAuthentication(vaultInitializer.getRootToken())); + return vaultTemplate; + + } +} diff --git a/testing-modules/README.md b/testing-modules/README.md index b269f547ec..b189b9819e 100644 --- a/testing-modules/README.md +++ b/testing-modules/README.md @@ -13,3 +13,4 @@ - [Headers, Cookies and Parameters with REST-assured](http://www.baeldung.com/rest-assured-header-cookie-parameter) - [JSON Schema Validation with REST-assured](http://www.baeldung.com/rest-assured-json-schema) - [Testing Callbacks with Mockito](http://www.baeldung.com/mockito-callbacks) +- [Running JUnit Tests in Parallel with Maven](https://www.baeldung.com/maven-junit-parallel-tests) diff --git a/testing-modules/gatling/README.md b/testing-modules/gatling/README.md index 5c81c4bd6a..941ab75f06 100644 --- a/testing-modules/gatling/README.md +++ b/testing-modules/gatling/README.md @@ -1,2 +1,5 @@ ### Relevant Articles: - [Intro to Gatling](http://www.baeldung.com/introduction-to-gatling) + +### Running a simualtion +- To run a simulation use "simulation" profile, command - `mvn install -Psimulation -Dgib.enabled=false` diff --git a/testing-modules/gatling/pom.xml b/testing-modules/gatling/pom.xml index 1afaefd47e..8d4c89ec62 100644 --- a/testing-modules/gatling/pom.xml +++ b/testing-modules/gatling/pom.xml @@ -7,13 +7,13 @@ gatling 1.0-SNAPSHOT - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ - - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + io.gatling.highcharts @@ -68,6 +68,14 @@ +
+
+ + + + simulation + + io.gatling gatling-maven-plugin @@ -78,11 +86,16 @@ execute + + true + + + @@ -113,10 +126,10 @@ 1.8 1.8 UTF-8 - 2.11.12 - 2.2.5 + 2.12.6 + 2.3.1 3.2.2 - 2.2.1 + 2.2.4 diff --git a/testing-modules/junit-5/README.md b/testing-modules/junit-5/README.md index 5a73bca4d6..40c4e8cde9 100644 --- a/testing-modules/junit-5/README.md +++ b/testing-modules/junit-5/README.md @@ -14,4 +14,5 @@ - [Migrating from JUnit 4 to JUnit 5](http://www.baeldung.com/junit-5-migration) - [JUnit5 Programmatic Extension Registration with @RegisterExtension](http://www.baeldung.com/junit-5-registerextension-annotation) - [The Order of Tests in JUnit](http://www.baeldung.com/junit-5-test-order) - +- [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java) +- [Testing an Abstract Class With JUnit](https://www.baeldung.com/junit-test-abstract-class) diff --git a/testing-modules/mockito/pom.xml b/testing-modules/mockito/pom.xml index fab2fcc795..acdd7eb2ac 100644 --- a/testing-modules/mockito/pom.xml +++ b/testing-modules/mockito/pom.xml @@ -14,6 +14,27 @@
+ + + org.springframework + spring-core + ${spring-framework.version} + + + org.springframework + spring-context + ${spring-framework.version} + + + org.springframework.data + spring-data-jpa + ${spring-data.version} + + + org.eclipse.persistence + javax.persistence + 2.1.1 + @@ -46,35 +67,15 @@ org.springframework.boot spring-boot-starter - LATEST + ${spring-boot.version} test org.springframework.boot spring-boot-starter-test - LATEST + ${spring-boot.version} test - - org.springframework - spring-core - LATEST - - - org.springframework - spring-context - LATEST - - - org.eclipse.persistence - javax.persistence - 2.1.1 - - - org.springframework.data - spring-data-jpa - LATEST - @@ -88,6 +89,9 @@ + 2.0.4.RELEASE + 5.0.8.RELEASE + 2.0.9.RELEASE 19.0 3.5 diff --git a/testing-modules/testing/README.md b/testing-modules/testing/README.md index 6f13f45194..849b578a55 100644 --- a/testing-modules/testing/README.md +++ b/testing-modules/testing/README.md @@ -20,3 +20,4 @@ - [Custom Assertions with AssertJ](http://www.baeldung.com/assertj-custom-assertion) - [Using Conditions with AssertJ](http://www.baeldung.com/assertj-conditions) - [Guide to JavaFaker](https://www.baeldung.com/java-faker) +- [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java) diff --git a/testing-modules/testng/pom.xml b/testing-modules/testng/pom.xml index 028db38727..a9f680aafb 100644 --- a/testing-modules/testng/pom.xml +++ b/testing-modules/testng/pom.xml @@ -38,24 +38,6 @@ true - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - -
diff --git a/vaadin-spring/README.md b/vaadin-spring/README.md new file mode 100644 index 0000000000..347c92d7b5 --- /dev/null +++ b/vaadin-spring/README.md @@ -0,0 +1,3 @@ +### Relevant articles + +- [Sample Application with Spring Boot and Vaadin](https://www.baeldung.com/spring-boot-vaadin) diff --git a/vaadin-spring/pom.xml b/vaadin-spring/pom.xml deleted file mode 100644 index 09f79f2676..0000000000 --- a/vaadin-spring/pom.xml +++ /dev/null @@ -1,64 +0,0 @@ - - - 4.0.0 - - com.baeldung - vaadin-spring - 0.1.0 - - - parent-boot-2 - com.baeldung - 0.0.1-SNAPSHOT - ../parent-boot-2 - - - - 1.8 - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - com.vaadin - vaadin-spring-boot-starter - - - com.h2database - h2 - - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - com.vaadin - vaadin-bom - 10.0.1 - pom - import - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - diff --git a/vaadin-spring/src/main/resources/logback.xml b/vaadin-spring/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/vaadin-spring/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/vaadin/README.md b/vaadin/README.md index 0dfeea8996..43d2cc8961 100644 --- a/vaadin/README.md +++ b/vaadin/README.md @@ -1,3 +1,4 @@ ## Relevant articles: - [Introduction to Vaadin](http://www.baeldung.com/vaadin) +- [Sample Application with Spring Boot and Vaadin](https://www.baeldung.com/spring-boot-vaadin) diff --git a/vaadin/pom.xml b/vaadin/pom.xml index ce2cd592d7..c34ffa3636 100644 --- a/vaadin/pom.xml +++ b/vaadin/pom.xml @@ -8,21 +8,30 @@ war 1.0-SNAPSHOT Vaadin - - 3 - - + + parent-boot-2 com.baeldung - parent-modules - 1.0.0-SNAPSHOT + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + com.vaadin + vaadin-bom + ${vaadin.version} + pom + import + + + + javax.servlet javax.servlet-api - ${servlet.version} provided @@ -41,6 +50,25 @@ com.vaadin vaadin-themes + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.vaadin + vaadin-spring-boot-starter + ${vaadin-spring-boot-starter.version} + + + com.h2database + h2 + + + org.springframework.boot + spring-boot-starter-test + test + @@ -97,21 +125,13 @@ 2 + + org.springframework.boot + spring-boot-maven-plugin + - - - - com.vaadin - vaadin-bom - ${vaadin.version} - pom - import - - - - vaadin-addons @@ -166,6 +186,7 @@ 3.0.1 7.7.10 8.0.6 + 10.0.1 9.3.9.v20160517 UTF-8 1.8 diff --git a/vaadin-spring/src/main/java/com/baeldung/Application.java b/vaadin/src/main/java/com/baeldung/Application.java similarity index 100% rename from vaadin-spring/src/main/java/com/baeldung/Application.java rename to vaadin/src/main/java/com/baeldung/Application.java diff --git a/vaadin-spring/src/main/java/com/baeldung/Employee.java b/vaadin/src/main/java/com/baeldung/Employee.java similarity index 100% rename from vaadin-spring/src/main/java/com/baeldung/Employee.java rename to vaadin/src/main/java/com/baeldung/Employee.java diff --git a/vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java b/vaadin/src/main/java/com/baeldung/EmployeeEditor.java similarity index 100% rename from vaadin-spring/src/main/java/com/baeldung/EmployeeEditor.java rename to vaadin/src/main/java/com/baeldung/EmployeeEditor.java diff --git a/vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java b/vaadin/src/main/java/com/baeldung/EmployeeRepository.java similarity index 100% rename from vaadin-spring/src/main/java/com/baeldung/EmployeeRepository.java rename to vaadin/src/main/java/com/baeldung/EmployeeRepository.java diff --git a/vaadin-spring/src/main/java/com/baeldung/MainView.java b/vaadin/src/main/java/com/baeldung/MainView.java similarity index 100% rename from vaadin-spring/src/main/java/com/baeldung/MainView.java rename to vaadin/src/main/java/com/baeldung/MainView.java diff --git a/xml/pom.xml b/xml/pom.xml index dba0308337..5490f3a89f 100644 --- a/xml/pom.xml +++ b/xml/pom.xml @@ -1,8 +1,6 @@ 4.0.0 - - com.baeldung xml 0.1-SNAPSHOT xml @@ -168,23 +166,7 @@ template-binding.xml - true - - - - process-classes - - bind - - - - - - org.jibx - maven-jibx-plugin - 1.3.1 - - src/main/resources + src/main/resources *-binding.xml @@ -192,6 +174,14 @@ + process-classes + process-classes + + bind + + + + process-test-classes process-test-classes test-bind