diff --git a/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/maximumsubarray/KadaneAlgorithm.java b/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/maximumsubarray/KadaneAlgorithm.java index 202912a1af..15e813f680 100644 --- a/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/maximumsubarray/KadaneAlgorithm.java +++ b/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/maximumsubarray/KadaneAlgorithm.java @@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory; public class KadaneAlgorithm { - private Logger logger = LoggerFactory.getLogger(BruteForceAlgorithm.class.getName()); + private Logger logger = LoggerFactory.getLogger(KadaneAlgorithm.class.getName()); public int maxSubArraySum(int[] arr) { @@ -14,15 +14,15 @@ public class KadaneAlgorithm { int end = 0; int maxSoFar = arr[0], maxEndingHere = arr[0]; + for (int i = 1; i < size; i++) { - - if (arr[i] > maxEndingHere + arr[i]) { - start = i; + maxEndingHere = maxEndingHere + arr[i]; + if (arr[i] > maxEndingHere) { maxEndingHere = arr[i]; - } else { - maxEndingHere = maxEndingHere + arr[i]; + if (maxSoFar < maxEndingHere) { + start = i; + } } - if (maxSoFar < maxEndingHere) { maxSoFar = maxEndingHere; end = i; diff --git a/algorithms-modules/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/maximumsubarray/KadaneAlgorithmUnitTest.java b/algorithms-modules/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/maximumsubarray/KadaneAlgorithmUnitTest.java index 8dcc81bc5b..b0ce689645 100644 --- a/algorithms-modules/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/maximumsubarray/KadaneAlgorithmUnitTest.java +++ b/algorithms-modules/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/maximumsubarray/KadaneAlgorithmUnitTest.java @@ -7,7 +7,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; class KadaneAlgorithmUnitTest { @Test - void givenArrayWithNegativeNumberWhenMaximumSubarrayThenReturns6() { + void givenArrayWithNegativeNumberWhenMaximumSubarrayThenReturnsExpectedResult() { //given int[] arr = new int[] { -3, 1, -8, 4, -1, 2, 1, -5, 5 }; //when @@ -27,7 +27,7 @@ class KadaneAlgorithmUnitTest { //then assertEquals(-1, maxSum); } - + @Test void givenArrayWithAllPosiitveNumbersWhenMaximumSubarrayThenReturnsExpectedResult() { //given @@ -39,4 +39,15 @@ class KadaneAlgorithmUnitTest { assertEquals(10, maxSum); } + @Test + void givenArrayToTestStartIndexWhenMaximumSubarrayThenReturnsExpectedResult() { + //given + int[] arr = new int[] { 1, 2, -1, 3, -6, -2 }; + //when + KadaneAlgorithm algorithm = new KadaneAlgorithm(); + int maxSum = algorithm.maxSubArraySum(arr); + //then + assertEquals(5, maxSum); + } + } \ No newline at end of file diff --git a/algorithms-modules/algorithms-miscellaneous-7/README.md b/algorithms-modules/algorithms-miscellaneous-7/README.md index ab07d655f9..82d9df9292 100644 --- a/algorithms-modules/algorithms-miscellaneous-7/README.md +++ b/algorithms-modules/algorithms-miscellaneous-7/README.md @@ -3,4 +3,5 @@ - [Algorithm to Identify and Validate a Credit Card Number](https://www.baeldung.com/java-validate-cc-number) - [Find the N Most Frequent Elements in a Java Array](https://www.baeldung.com/java-n-most-frequent-elements-array) - [Getting Pixel Array From Image in Java](https://www.baeldung.com/java-getting-pixel-array-from-image) +- [Calculate Distance Between Two Coordinates in Java](https://www.baeldung.com/java-find-distance-between-points) - More articles: [[<-- prev]](/algorithms-miscellaneous-6) diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/latlondistance/EquirectangularApproximation.java b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/latlondistance/EquirectangularApproximation.java new file mode 100644 index 0000000000..e42475c17e --- /dev/null +++ b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/latlondistance/EquirectangularApproximation.java @@ -0,0 +1,19 @@ +package com.baeldung.algorithms.latlondistance; + +public class EquirectangularApproximation { + + private static final int EARTH_RADIUS = 6371; // Approx Earth radius in KM + + public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) { + double lat1Rad = Math.toRadians(lat1); + double lat2Rad = Math.toRadians(lat2); + double lon1Rad = Math.toRadians(lon1); + double lon2Rad = Math.toRadians(lon2); + + double x = (lon2Rad - lon1Rad) * Math.cos((lat1Rad + lat2Rad) / 2); + double y = (lat2Rad - lat1Rad); + double distance = Math.sqrt(x * x + y * y) * EARTH_RADIUS; + + return distance; + } +} \ No newline at end of file diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/latlondistance/HaversineDistance.java b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/latlondistance/HaversineDistance.java new file mode 100644 index 0000000000..69074ec559 --- /dev/null +++ b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/latlondistance/HaversineDistance.java @@ -0,0 +1,24 @@ +package com.baeldung.algorithms.latlondistance; + +public class HaversineDistance { + private static final int EARTH_RADIUS = 6371; // Approx Earth radius in KM + + public static double calculateDistance(double startLat, double startLong, + double endLat, double endLong) { + + double dLat = Math.toRadians((endLat - startLat)); + double dLong = Math.toRadians((endLong - startLong)); + + startLat = Math.toRadians(startLat); + endLat = Math.toRadians(endLat); + + double a = haversine(dLat) + Math.cos(startLat) * Math.cos(endLat) * haversine(dLong); + double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + + return EARTH_RADIUS * c; + } + + public static double haversine(double val) { + return Math.pow(Math.sin(val / 2), 2); + } +} diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/latlondistance/VincentyDistance.java b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/latlondistance/VincentyDistance.java new file mode 100644 index 0000000000..7d0b0b907f --- /dev/null +++ b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/latlondistance/VincentyDistance.java @@ -0,0 +1,53 @@ +package com.baeldung.algorithms.latlondistance; + +public class VincentyDistance { + + // Constants for WGS84 ellipsoid model of Earth + private static final double SEMI_MAJOR_AXIS_MT = 6378137; + private static final double SEMI_MINOR_AXIS_MT = 6356752.314245; + private static final double FLATTENING = 1 / 298.257223563; + private static final double ERROR_TOLERANCE = 1e-12; + + public static double calculateDistance(double latitude1, double longitude1, double latitude2, double longitude2) { + double U1 = Math.atan((1 - FLATTENING) * Math.tan(Math.toRadians(latitude1))); + double U2 = Math.atan((1 - FLATTENING) * Math.tan(Math.toRadians(latitude2))); + + double sinU1 = Math.sin(U1); + double cosU1 = Math.cos(U1); + double sinU2 = Math.sin(U2); + double cosU2 = Math.cos(U2); + + double longitudeDifference = Math.toRadians(longitude2 - longitude1); + double previousLongitudeDifference; + + double sinSigma, cosSigma, sigma, sinAlpha, cosSqAlpha, cos2SigmaM; + + do { + sinSigma = Math.sqrt(Math.pow(cosU2 * Math.sin(longitudeDifference), 2) + + Math.pow(cosU1 * sinU2 - sinU1 * cosU2 * Math.cos(longitudeDifference), 2)); + cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * Math.cos(longitudeDifference); + sigma = Math.atan2(sinSigma, cosSigma); + sinAlpha = cosU1 * cosU2 * Math.sin(longitudeDifference) / sinSigma; + cosSqAlpha = 1 - Math.pow(sinAlpha, 2); + cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha; + if (Double.isNaN(cos2SigmaM)) { + cos2SigmaM = 0; + } + previousLongitudeDifference = longitudeDifference; + double C = FLATTENING / 16 * cosSqAlpha * (4 + FLATTENING * (4 - 3 * cosSqAlpha)); + longitudeDifference = Math.toRadians(longitude2 - longitude1) + (1 - C) * FLATTENING * sinAlpha * + (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * Math.pow(cos2SigmaM, 2)))); + } while (Math.abs(longitudeDifference - previousLongitudeDifference) > ERROR_TOLERANCE); + + double uSq = cosSqAlpha * (Math.pow(SEMI_MAJOR_AXIS_MT, 2) - Math.pow(SEMI_MINOR_AXIS_MT, 2)) / Math.pow(SEMI_MINOR_AXIS_MT, 2); + + double A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq))); + double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq))); + + double deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * Math.pow(cos2SigmaM, 2)) - + B / 6 * cos2SigmaM * (-3 + 4 * Math.pow(sinSigma, 2)) * (-3 + 4 * Math.pow(cos2SigmaM, 2)))); + + double distanceMt = SEMI_MINOR_AXIS_MT * A * (sigma - deltaSigma); + return distanceMt / 1000; + } +} \ No newline at end of file diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/latlondistance/GeoDistanceUnitTest.java b/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/latlondistance/GeoDistanceUnitTest.java new file mode 100644 index 0000000000..9e72f86287 --- /dev/null +++ b/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/latlondistance/GeoDistanceUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.algorithms.latlondistance; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class GeoDistanceUnitTest { + @Test + public void testCalculateDistance() { + double lat1 = 40.714268; // New York + double lon1 = -74.005974; + double lat2 = 34.0522; // Los Angeles + double lon2 = -118.2437; + + double equirectangularDistance = EquirectangularApproximation.calculateDistance(lat1, lon1, lat2, lon2); + double haversineDistance = HaversineDistance.calculateDistance(lat1, lon1, lat2, lon2); + double vincentyDistance = VincentyDistance.calculateDistance(lat1, lon1, lat2, lon2); + + double expectedDistance = 3944; + assertTrue(Math.abs(equirectangularDistance - expectedDistance) < 100); + assertTrue(Math.abs(haversineDistance - expectedDistance) < 10); + assertTrue(Math.abs(vincentyDistance - expectedDistance) < 0.5); + + } + +} \ No newline at end of file diff --git a/apache-httpclient-2/src/main/java/com/baeldung/tlsversion/ClientTlsVersionExamples.java b/apache-httpclient-2/src/main/java/com/baeldung/tlsversion/ClientTlsVersionExamples.java index c58763b1c0..818d0c3cd9 100644 --- a/apache-httpclient-2/src/main/java/com/baeldung/tlsversion/ClientTlsVersionExamples.java +++ b/apache-httpclient-2/src/main/java/com/baeldung/tlsversion/ClientTlsVersionExamples.java @@ -1,36 +1,43 @@ package com.baeldung.tlsversion; -import javax.net.ssl.SSLSocket; - -import org.apache.http.HttpEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.ssl.SSLContexts; -import org.apache.http.util.EntityUtils; - import java.io.IOException; +import javax.net.ssl.SSLSocket; + +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.config.TlsConfig; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; +import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.ssl.TLS; +import org.apache.hc.core5.ssl.SSLContexts; +import org.apache.hc.core5.util.Timeout; + public class ClientTlsVersionExamples { - public static CloseableHttpClient setViaSocketFactory() { - SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( - SSLContexts.createDefault(), - new String[] { "TLSv1.2", "TLSv1.3" }, - null, - SSLConnectionSocketFactory.getDefaultHostnameVerifier()); + final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create() + .setDefaultTlsConfig(TlsConfig.custom() + .setHandshakeTimeout(Timeout.ofSeconds(30)) + .setSupportedProtocols(TLS.V_1_2, TLS.V_1_3) + .build()) + .build(); - return HttpClients.custom().setSSLSocketFactory(sslsf).build(); + return HttpClients.custom() + .setConnectionManager(cm) + .build(); } public static CloseableHttpClient setTlsVersionPerConnection() { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContexts.createDefault()) { - @Override protected void prepareSocket(SSLSocket socket) { - String hostname = socket.getInetAddress().getHostName(); + String hostname = socket.getInetAddress() + .getHostName(); if (hostname.endsWith("internal.system.com")) { socket.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" }); } else { @@ -39,7 +46,14 @@ public class ClientTlsVersionExamples { } }; - return HttpClients.custom().setSSLSocketFactory(sslsf).build(); + HttpClientConnectionManager connManager = PoolingHttpClientConnectionManagerBuilder.create() + .setSSLSocketFactory(sslsf) + .build(); + + return HttpClients.custom() + .setConnectionManager(connManager) + .build(); + } // To configure the TLS versions for the client, set the https.protocols system property during runtime. @@ -47,15 +61,11 @@ public class ClientTlsVersionExamples { public static CloseableHttpClient setViaSystemProperties() { return HttpClients.createSystem(); // Alternatively: - // return HttpClients.custom().useSystemProperties().build(); + //return HttpClients.custom().useSystemProperties().build(); } public static void main(String[] args) throws IOException { - // Alternatively: - // CloseableHttpClient httpClient = setTlsVersionPerConnection(); - // CloseableHttpClient httpClient = setViaSystemProperties(); - try (CloseableHttpClient httpClient = setViaSocketFactory(); - CloseableHttpResponse response = httpClient.execute(new HttpGet("https://httpbin.org/"))) { + try (CloseableHttpClient httpClient = setViaSocketFactory(); CloseableHttpResponse response = httpClient.execute(new HttpGet("https://httpbin.org/"))) { HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); diff --git a/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/HttpClientConnectionManagementLiveTest.java b/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/HttpClientConnectionManagementLiveTest.java index 57a8f0a806..9d5294aa7e 100644 --- a/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/HttpClientConnectionManagementLiveTest.java +++ b/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/HttpClientConnectionManagementLiveTest.java @@ -1,69 +1,71 @@ package com.baeldung.httpclient.conn; -import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Iterator; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; -import org.apache.http.HeaderElement; -import org.apache.http.HeaderElementIterator; -import org.apache.http.HttpClientConnection; -import org.apache.http.HttpException; -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.protocol.HttpClientContext; -import org.apache.http.config.SocketConfig; -import org.apache.http.conn.ConnectionKeepAliveStrategy; -import org.apache.http.conn.ConnectionPoolTimeoutException; -import org.apache.http.conn.ConnectionRequest; -import org.apache.http.conn.routing.HttpRoute; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.impl.conn.BasicHttpClientConnectionManager; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.apache.http.message.BasicHeaderElementIterator; -import org.apache.http.protocol.HTTP; -import org.apache.http.protocol.HttpContext; -import org.apache.http.protocol.HttpCoreContext; -import org.apache.http.protocol.HttpRequestExecutor; -import org.apache.http.util.EntityUtils; -import org.junit.Ignore; +import org.apache.hc.client5.http.ConnectionKeepAliveStrategy; +import org.apache.hc.client5.http.HttpRoute; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.config.ConnectionConfig; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; +import org.apache.hc.client5.http.io.ConnectionEndpoint; +import org.apache.hc.client5.http.io.LeaseRequest; +import org.apache.hc.core5.http.HeaderElement; +import org.apache.hc.core5.http.HeaderElements; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.message.MessageSupport; +import org.apache.hc.core5.http.message.StatusLine; +import org.apache.hc.core5.http.protocol.BasicHttpContext; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.apache.hc.core5.pool.PoolStats; +import org.apache.hc.core5.util.Args; +import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; +import org.junit.Assert; import org.junit.Test; public class HttpClientConnectionManagementLiveTest { // Example 2.1. Getting a Connection Request for a Low Level Connection (HttpClientConnection) @Test - public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws ConnectionPoolTimeoutException, InterruptedException, ExecutionException { - try (BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager()) { - HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80)); - final ConnectionRequest connRequest = connManager.requestConnection(route, null); - assertNotNull(connRequest.get(1000, TimeUnit.SECONDS)); - } + public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws ExecutionException, InterruptedException, TimeoutException { + BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager(); + HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 443)); + final LeaseRequest connRequest = connMgr.lease("some-id", route, null); + assertNotNull(connRequest.get(Timeout.ZERO_MILLISECONDS)); + connMgr.close(); } // Example 3.1. Setting the PoolingHttpClientConnectionManager on a HttpClient @Test - public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws ClientProtocolException, IOException { + public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws IOException { PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager(); CloseableHttpClient client = HttpClients.custom() .setConnectionManager(poolingConnManager) .build(); client.execute(new HttpGet("https://www.baeldung.com")); - assertTrue(poolingConnManager.getTotalStats() .getLeased() == 1); + client.close(); + poolingConnManager.close(); } // Example 3.2. Using Two HttpClients to Connect to One Target Host Each @Test - public final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws InterruptedException { + public final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws InterruptedException, IOException { HttpGet get1 = new HttpGet("https://www.baeldung.com"); HttpGet get2 = new HttpGet("https://www.google.com"); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); @@ -81,38 +83,52 @@ public class HttpClientConnectionManagementLiveTest { thread1.join(); thread2.join(); - assertTrue(connManager.getTotalStats() + Assert.assertTrue(connManager.getTotalStats() .getLeased() == 0); + client1.close(); + client2.close(); + connManager.close(); } // Example 4.1. Increasing the Number of Connections that Can be Open and Managed Beyond the default Limits @Test - public final void whenIncreasingConnectionPool_thenNoEceptions() { - try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) { - connManager.setMaxTotal(5); - connManager.setDefaultMaxPerRoute(4); - HttpHost host = new HttpHost("www.baeldung.com", 80); - connManager.setMaxPerRoute(new HttpRoute(host), 5); - } + public final void whenIncreasingConnectionPool_thenNoExceptions() { + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); + connManager.setMaxTotal(5); + connManager.setDefaultMaxPerRoute(4); + HttpHost host = new HttpHost("www.baeldung.com", 80); + connManager.setMaxPerRoute(new HttpRoute(host), 5); + connManager.close(); } // Example 4.2. Using Threads to Execute Connections @Test - public final void whenExecutingSameRequestsInDifferentThreads_thenExecuteReuqest() throws InterruptedException { + public final void whenExecutingSameRequestsInDifferentThreads_thenExecuteRequest() throws InterruptedException, IOException { HttpGet get = new HttpGet("http://www.baeldung.com"); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); CloseableHttpClient client = HttpClients.custom() .setConnectionManager(connManager) .build(); - MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client, get); - MultiHttpClientConnThread thread2 = new MultiHttpClientConnThread(client, get); - MultiHttpClientConnThread thread3 = new MultiHttpClientConnThread(client, get); + MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client, get, connManager); + MultiHttpClientConnThread thread2 = new MultiHttpClientConnThread(client, get, connManager); + MultiHttpClientConnThread thread3 = new MultiHttpClientConnThread(client, get, connManager); + MultiHttpClientConnThread thread4 = new MultiHttpClientConnThread(client, get, connManager); + MultiHttpClientConnThread thread5 = new MultiHttpClientConnThread(client, get, connManager); + MultiHttpClientConnThread thread6 = new MultiHttpClientConnThread(client, get, connManager); thread1.start(); thread2.start(); thread3.start(); + thread4.start(); + thread5.start(); + thread6.start(); thread1.join(); thread2.join(); thread3.join(); + thread4.join(); + thread5.join(); + thread6.join(); + client.close(); + connManager.close(); } // Example 5.1. A Custom Keep Alive Strategy @@ -120,22 +136,19 @@ public class HttpClientConnectionManagementLiveTest { public final void whenCustomizingKeepAliveStrategy_thenNoExceptions() { final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() { @Override - public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContext myContext) { - final HeaderElementIterator it = new BasicHeaderElementIterator(myResponse.headerIterator(HTTP.CONN_KEEP_ALIVE)); - while (it.hasNext()) { - final HeaderElement he = it.nextElement(); - final String param = he.getName(); - final String value = he.getValue(); - if ((value != null) && param.equalsIgnoreCase("timeout")) { - return Long.parseLong(value) * 1000; + public TimeValue getKeepAliveDuration(HttpResponse response, HttpContext context) { + Args.notNull(response, "HTTP response"); + final Iterator it = MessageSupport.iterate(response, HeaderElements.KEEP_ALIVE); + final HeaderElement he = it.next(); + final String param = he.getName(); + final String value = he.getValue(); + if (value != null && param.equalsIgnoreCase("timeout")) { + try { + return TimeValue.ofSeconds(Long.parseLong(value)); + } catch (final NumberFormatException ignore) { } } - final HttpHost target = (HttpHost) myContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); - if ("localhost".equalsIgnoreCase(target.getHostName())) { - return 10 * 1000; - } else { - return 5 * 1000; - } + return TimeValue.ofSeconds(5); } }; @@ -144,42 +157,38 @@ public class HttpClientConnectionManagementLiveTest { .setKeepAliveStrategy(myStrategy) .setConnectionManager(connManager) .build(); + connManager.close(); } - // Example 6.1. BasicHttpClientConnectionManager Connection Reuse + //Example 6.1. BasicHttpClientConnectionManager Connection Reuse @Test - public final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException { - BasicHttpClientConnectionManager basicConnManager = new BasicHttpClientConnectionManager(); - HttpClientContext context = HttpClientContext.create(); - - // low level + public final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws InterruptedException, ExecutionException, TimeoutException, IOException, URISyntaxException { + BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager(); HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 443)); - ConnectionRequest connRequest = basicConnManager.requestConnection(route, null); - HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS); - basicConnManager.connect(conn, route, 1000, context); - basicConnManager.routeComplete(conn, route, context); + final HttpContext context = new BasicHttpContext(); - HttpRequestExecutor exeRequest = new HttpRequestExecutor(); - context.setTargetHost((new HttpHost("www.baeldung.com", 80))); - HttpGet get = new HttpGet("http://www.baeldung.com"); - exeRequest.execute(get, conn, context); + final LeaseRequest connRequest = connMgr.lease("some-id", route, null); + final ConnectionEndpoint endpoint = connRequest.get(Timeout.ZERO_MILLISECONDS); + connMgr.connect(endpoint, Timeout.ZERO_MILLISECONDS, context); - basicConnManager.releaseConnection(conn, null, 1, TimeUnit.SECONDS); + connMgr.release(endpoint, null, TimeValue.ZERO_MILLISECONDS); - // high level CloseableHttpClient client = HttpClients.custom() - .setConnectionManager(basicConnManager) + .setConnectionManager(connMgr) .build(); - client.execute(get); + HttpGet httpGet = new HttpGet("https://www.example.com"); + client.execute(httpGet, context, response -> response); + client.close(); + connMgr.close(); } // Example 6.2. PoolingHttpClientConnectionManager: Re-Using Connections with Threads @Test - public final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException { - HttpGet get = new HttpGet("http://echo.200please.com"); + public final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException, IOException { + HttpGet get = new HttpGet("http://www.baeldung.com"); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); - connManager.setDefaultMaxPerRoute(5); - connManager.setMaxTotal(5); + connManager.setDefaultMaxPerRoute(6); + connManager.setMaxTotal(6); CloseableHttpClient client = HttpClients.custom() .setConnectionManager(connManager) .build(); @@ -193,48 +202,71 @@ public class HttpClientConnectionManagementLiveTest { for (MultiHttpClientConnThread thread : threads) { thread.join(1000); } + client.close(); + connManager.close(); } // Example 7.1. Setting Socket Timeout to 5 Seconds @Test - public final void whenConfiguringTimeOut_thenNoExceptions() { - HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80)); - try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) { - connManager.setSocketConfig(route.getTargetHost(), SocketConfig.custom() - .setSoTimeout(5000) - .build()); - assertTrue(connManager.getSocketConfig(route.getTargetHost()) - .getSoTimeout() == 5000); - } + public final void whenConfiguringTimeOut_thenNoExceptions() throws ExecutionException, InterruptedException, TimeoutException, IOException { + final HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80)); + final HttpContext context = new BasicHttpContext(); + final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); + + final ConnectionConfig connConfig = ConnectionConfig.custom() + .setSocketTimeout(5, TimeUnit.SECONDS) + .build(); + + connManager.setDefaultConnectionConfig(connConfig); + + final LeaseRequest leaseRequest = connManager.lease("id1", route, null); + final ConnectionEndpoint endpoint = leaseRequest.get(Timeout.ZERO_MILLISECONDS); + connManager.connect(endpoint, null, context); + connManager.close(); } // Example 8.1. Setting the HttpClient to Check for Stale Connections @Test - public final void whenHttpClientChecksStaleConns_thenNoExceptions() { - PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); - HttpClients.custom() - .setDefaultRequestConfig(RequestConfig.custom() - .setStaleConnectionCheckEnabled(true) - .build()) + public final void whenEvictIdealConn_thenNoExceptions() throws InterruptedException, IOException { + final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); + connManager.setMaxTotal(100); + try (final CloseableHttpClient httpclient = HttpClients.custom() .setConnectionManager(connManager) - .build(); - } + .evictExpiredConnections() + .evictIdleConnections(TimeValue.ofSeconds(2)) + .build()) { + // create an array of URIs to perform GETs on + final String[] urisToGet = { "http://hc.apache.org/", "http://hc.apache.org/httpcomponents-core-ga/"}; - // Example 8.2. Using a Stale Connection Monitor Thread - @Test - public final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws InterruptedException { - PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); - HttpClients.custom() - .setConnectionManager(connManager) - .build(); - IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(connManager); - staleMonitor.start(); - staleMonitor.join(1000); + for (final String requestURI : urisToGet) { + final HttpGet request = new HttpGet(requestURI); + + System.out.println("Executing request " + request.getMethod() + " " + request.getRequestUri()); + + httpclient.execute(request, response -> { + System.out.println("----------------------------------------"); + System.out.println(request + "->" + new StatusLine(response)); + EntityUtils.consume(response.getEntity()); + return null; + }); + } + + final PoolStats stats1 = connManager.getTotalStats(); + System.out.println("Connections kept alive: " + stats1.getAvailable()); + + // Sleep 10 sec and let the connection evict or do its job + Thread.sleep(4000); + + final PoolStats stats2 = connManager.getTotalStats(); + System.out.println("Connections kept alive: " + stats2.getAvailable()); + + connManager.close(); + } } // Example 9.1. Closing Connection and Releasing Resources - @Test(expected = IllegalStateException.class) - public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions1() throws InterruptedException, ExecutionException, IOException, HttpException { + @Test + public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions1() throws IOException { PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); CloseableHttpClient client = HttpClients.custom() .setConnectionManager(connManager) @@ -246,16 +278,11 @@ public class HttpClientConnectionManagementLiveTest { response.close(); client.close(); connManager.close(); - connManager.shutdown(); - - client.execute(get); - - assertTrue(response.getEntity() == null); } @Test // Example 3.2. TESTER VERSION - public final void whenTwoConnectionsForTwoRequests_thenTwoConnectionsAreLeased() throws InterruptedException { + public final void whenTwoConnectionsForTwoRequests_thenTwoConnectionsAreLeased() throws InterruptedException, IOException { HttpGet get1 = new HttpGet("https://www.baeldung.com"); HttpGet get2 = new HttpGet("https://www.google.com"); @@ -273,77 +300,11 @@ public class HttpClientConnectionManagementLiveTest { thread2.start(); thread1.join(); thread2.join(1000); - assertTrue(poolingConnManager.getTotalStats() + Assert.assertTrue(poolingConnManager.getTotalStats() .getLeased() == 2); - } - @Test - // Example 4.2 Tester Version - public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimit() throws InterruptedException { - PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager(); - CloseableHttpClient client = HttpClients.custom() - .setConnectionManager(poolingConnManager) - .build(); - final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager); - final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager); - final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager); - thread1.start(); - thread2.start(); - thread3.start(); - thread1.join(10000); - thread2.join(10000); - thread3.join(10000); - } - - @Test - // 6.2 TESTER VERSION - public final void whenConnectionsNeededGreaterThanMaxTotal_thenReuseConnections() throws InterruptedException { - PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager(); - poolingConnManager.setDefaultMaxPerRoute(5); - poolingConnManager.setMaxTotal(5); - CloseableHttpClient client = HttpClients.custom() - .setConnectionManager(poolingConnManager) - .build(); - final MultiHttpClientConnThread[] threads = new MultiHttpClientConnThread[10]; - int countConnMade = 0; - for (int i = 0; i < threads.length; i++) { - threads[i] = new MultiHttpClientConnThread(client, new HttpGet("http://www.baeldung.com/"), poolingConnManager); - } - for (final MultiHttpClientConnThread thread : threads) { - thread.start(); - } - for (final MultiHttpClientConnThread thread : threads) { - thread.join(10000); - countConnMade++; - if (countConnMade == 0) { - assertTrue(thread.getLeasedConn() == 5); - } - } - } - - @Test - @Ignore("Very Long Running") - // 8.2 TESTER VERSION - public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConns() throws InterruptedException { - PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager(); - CloseableHttpClient client = HttpClients.custom() - .setConnectionManager(poolingConnManager) - .build(); - final IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(poolingConnManager); - final HttpGet get = new HttpGet("http://google.com"); - final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager); - final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager); - final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager); - staleMonitor.start(); - thread1.start(); - thread1.join(); - thread2.start(); - thread2.join(); - thread3.start(); - assertTrue(poolingConnManager.getTotalStats() - .getAvailable() == 1); - thread3.join(32000); - assertTrue(poolingConnManager.getTotalStats() - .getAvailable() == 0); + client1.close(); + client2.close(); + poolingConnManager.close(); } } diff --git a/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/MultiHttpClientConnThread.java b/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/MultiHttpClientConnThread.java index acadd1f240..16bb49123a 100644 --- a/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/MultiHttpClientConnThread.java +++ b/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/MultiHttpClientConnThread.java @@ -2,12 +2,12 @@ package com.baeldung.httpclient.conn; import java.io.IOException; -import org.apache.http.HttpResponse; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.apache.http.util.EntityUtils; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,22 +45,21 @@ public class MultiHttpClientConnThread extends Thread { try { logger.debug("Thread Running: " + getName()); - logger.debug("Thread Running: " + getName()); if (connManager != null) { logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased()); logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable()); } - final HttpResponse response = client.execute(get); + HttpEntity entity = client.execute(get).getEntity(); if (connManager != null) { leasedConn = connManager.getTotalStats().getLeased(); logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased()); logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable()); } + EntityUtils.consume(entity); - EntityUtils.consume(response.getEntity()); } catch (final IOException ex) { logger.error("", ex); } diff --git a/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/TesterVersion_MultiHttpClientConnThread.java b/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/TesterVersion_MultiHttpClientConnThread.java index a50858672e..6703d6880c 100644 --- a/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/TesterVersion_MultiHttpClientConnThread.java +++ b/apache-httpclient/src/test/java/com/baeldung/httpclient/conn/TesterVersion_MultiHttpClientConnThread.java @@ -2,10 +2,9 @@ package com.baeldung.httpclient.conn; import java.io.IOException; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/apache-httpclient4/src/main/java/com/baeldung/tlsversion/ClientTlsVersionExamples.java b/apache-httpclient4/src/main/java/com/baeldung/tlsversion/ClientTlsVersionExamples.java new file mode 100644 index 0000000000..c58763b1c0 --- /dev/null +++ b/apache-httpclient4/src/main/java/com/baeldung/tlsversion/ClientTlsVersionExamples.java @@ -0,0 +1,64 @@ +package com.baeldung.tlsversion; + +import javax.net.ssl.SSLSocket; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContexts; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; + +public class ClientTlsVersionExamples { + + public static CloseableHttpClient setViaSocketFactory() { + SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( + SSLContexts.createDefault(), + new String[] { "TLSv1.2", "TLSv1.3" }, + null, + SSLConnectionSocketFactory.getDefaultHostnameVerifier()); + + return HttpClients.custom().setSSLSocketFactory(sslsf).build(); + } + + public static CloseableHttpClient setTlsVersionPerConnection() { + SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContexts.createDefault()) { + + @Override + protected void prepareSocket(SSLSocket socket) { + String hostname = socket.getInetAddress().getHostName(); + if (hostname.endsWith("internal.system.com")) { + socket.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" }); + } else { + socket.setEnabledProtocols(new String[] { "TLSv1.3" }); + } + } + }; + + return HttpClients.custom().setSSLSocketFactory(sslsf).build(); + } + + // To configure the TLS versions for the client, set the https.protocols system property during runtime. + // For example: java -Dhttps.protocols=TLSv1.1,TLSv1.2,TLSv1.3 -jar webClient.jar + public static CloseableHttpClient setViaSystemProperties() { + return HttpClients.createSystem(); + // Alternatively: + // return HttpClients.custom().useSystemProperties().build(); + } + + public static void main(String[] args) throws IOException { + // Alternatively: + // CloseableHttpClient httpClient = setTlsVersionPerConnection(); + // CloseableHttpClient httpClient = setViaSystemProperties(); + try (CloseableHttpClient httpClient = setViaSocketFactory(); + CloseableHttpResponse response = httpClient.execute(new HttpGet("https://httpbin.org/"))) { + + HttpEntity entity = response.getEntity(); + EntityUtils.consume(entity); + } + } +} \ No newline at end of file diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/HttpClientConnectionManagementLiveTest.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/HttpClientConnectionManagementLiveTest.java new file mode 100644 index 0000000000..c894d72af0 --- /dev/null +++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/HttpClientConnectionManagementLiveTest.java @@ -0,0 +1,349 @@ +package com.baeldung.httpclient.httpclient.conn; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import org.apache.http.HeaderElement; +import org.apache.http.HeaderElementIterator; +import org.apache.http.HttpClientConnection; +import org.apache.http.HttpException; +import org.apache.http.HttpHost; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.config.SocketConfig; +import org.apache.http.conn.ConnectionKeepAliveStrategy; +import org.apache.http.conn.ConnectionPoolTimeoutException; +import org.apache.http.conn.ConnectionRequest; +import org.apache.http.conn.routing.HttpRoute; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.BasicHttpClientConnectionManager; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.message.BasicHeaderElementIterator; +import org.apache.http.protocol.HTTP; +import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.HttpCoreContext; +import org.apache.http.protocol.HttpRequestExecutor; +import org.apache.http.util.EntityUtils; +import org.junit.Ignore; +import org.junit.Test; + +public class HttpClientConnectionManagementLiveTest { + + // Example 2.1. Getting a Connection Request for a Low Level Connection (HttpClientConnection) + @Test + public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws ConnectionPoolTimeoutException, InterruptedException, ExecutionException { + try (BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager()) { + HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80)); + final ConnectionRequest connRequest = connManager.requestConnection(route, null); + assertNotNull(connRequest.get(1000, TimeUnit.SECONDS)); + } + } + + // Example 3.1. Setting the PoolingHttpClientConnectionManager on a HttpClient + @Test + public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws ClientProtocolException, IOException { + PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager(); + CloseableHttpClient client = HttpClients.custom() + .setConnectionManager(poolingConnManager) + .build(); + client.execute(new HttpGet("https://www.baeldung.com")); + + assertTrue(poolingConnManager.getTotalStats() + .getLeased() == 1); + } + + // Example 3.2. Using Two HttpClients to Connect to One Target Host Each + @Test + public final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws InterruptedException { + HttpGet get1 = new HttpGet("https://www.baeldung.com"); + HttpGet get2 = new HttpGet("https://www.google.com"); + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); + CloseableHttpClient client1 = HttpClients.custom() + .setConnectionManager(connManager) + .build(); + CloseableHttpClient client2 = HttpClients.custom() + .setConnectionManager(connManager) + .build(); + + MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client1, get1); + MultiHttpClientConnThread thread2 = new MultiHttpClientConnThread(client2, get2); + thread1.start(); + thread2.start(); + thread1.join(); + thread2.join(); + + assertTrue(connManager.getTotalStats() + .getLeased() == 0); + } + + // Example 4.1. Increasing the Number of Connections that Can be Open and Managed Beyond the default Limits + @Test + public final void whenIncreasingConnectionPool_thenNoEceptions() { + try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) { + connManager.setMaxTotal(5); + connManager.setDefaultMaxPerRoute(4); + HttpHost host = new HttpHost("www.baeldung.com", 80); + connManager.setMaxPerRoute(new HttpRoute(host), 5); + } + } + + // Example 4.2. Using Threads to Execute Connections + @Test + public final void whenExecutingSameRequestsInDifferentThreads_thenExecuteReuqest() throws InterruptedException { + HttpGet get = new HttpGet("http://www.baeldung.com"); + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); + CloseableHttpClient client = HttpClients.custom() + .setConnectionManager(connManager) + .build(); + MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client, get); + MultiHttpClientConnThread thread2 = new MultiHttpClientConnThread(client, get); + MultiHttpClientConnThread thread3 = new MultiHttpClientConnThread(client, get); + thread1.start(); + thread2.start(); + thread3.start(); + thread1.join(); + thread2.join(); + thread3.join(); + } + + // Example 5.1. A Custom Keep Alive Strategy + @Test + public final void whenCustomizingKeepAliveStrategy_thenNoExceptions() { + final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() { + @Override + public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContext myContext) { + final HeaderElementIterator it = new BasicHeaderElementIterator(myResponse.headerIterator(HTTP.CONN_KEEP_ALIVE)); + while (it.hasNext()) { + final HeaderElement he = it.nextElement(); + final String param = he.getName(); + final String value = he.getValue(); + if ((value != null) && param.equalsIgnoreCase("timeout")) { + return Long.parseLong(value) * 1000; + } + } + final HttpHost target = (HttpHost) myContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); + if ("localhost".equalsIgnoreCase(target.getHostName())) { + return 10 * 1000; + } else { + return 5 * 1000; + } + } + + }; + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); + HttpClients.custom() + .setKeepAliveStrategy(myStrategy) + .setConnectionManager(connManager) + .build(); + } + + // Example 6.1. BasicHttpClientConnectionManager Connection Reuse + @Test + public final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException { + BasicHttpClientConnectionManager basicConnManager = new BasicHttpClientConnectionManager(); + HttpClientContext context = HttpClientContext.create(); + + // low level + HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 443)); + ConnectionRequest connRequest = basicConnManager.requestConnection(route, null); + HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS); + basicConnManager.connect(conn, route, 1000, context); + basicConnManager.routeComplete(conn, route, context); + + HttpRequestExecutor exeRequest = new HttpRequestExecutor(); + context.setTargetHost((new HttpHost("www.baeldung.com", 80))); + HttpGet get = new HttpGet("http://www.baeldung.com"); + exeRequest.execute(get, conn, context); + + basicConnManager.releaseConnection(conn, null, 1, TimeUnit.SECONDS); + + // high level + CloseableHttpClient client = HttpClients.custom() + .setConnectionManager(basicConnManager) + .build(); + client.execute(get); + } + + // Example 6.2. PoolingHttpClientConnectionManager: Re-Using Connections with Threads + @Test + public final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException { + HttpGet get = new HttpGet("http://echo.200please.com"); + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); + connManager.setDefaultMaxPerRoute(5); + connManager.setMaxTotal(5); + CloseableHttpClient client = HttpClients.custom() + .setConnectionManager(connManager) + .build(); + MultiHttpClientConnThread[] threads = new MultiHttpClientConnThread[10]; + for (int i = 0; i < threads.length; i++) { + threads[i] = new MultiHttpClientConnThread(client, get, connManager); + } + for (MultiHttpClientConnThread thread : threads) { + thread.start(); + } + for (MultiHttpClientConnThread thread : threads) { + thread.join(1000); + } + } + + // Example 7.1. Setting Socket Timeout to 5 Seconds + @Test + public final void whenConfiguringTimeOut_thenNoExceptions() { + HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80)); + try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) { + connManager.setSocketConfig(route.getTargetHost(), SocketConfig.custom() + .setSoTimeout(5000) + .build()); + assertTrue(connManager.getSocketConfig(route.getTargetHost()) + .getSoTimeout() == 5000); + } + } + + // Example 8.1. Setting the HttpClient to Check for Stale Connections + @Test + public final void whenHttpClientChecksStaleConns_thenNoExceptions() { + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); + HttpClients.custom() + .setDefaultRequestConfig(RequestConfig.custom() + .setStaleConnectionCheckEnabled(true) + .build()) + .setConnectionManager(connManager) + .build(); + } + + // Example 8.2. Using a Stale Connection Monitor Thread + @Test + public final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws InterruptedException { + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); + HttpClients.custom() + .setConnectionManager(connManager) + .build(); + IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(connManager); + staleMonitor.start(); + staleMonitor.join(1000); + } + + // Example 9.1. Closing Connection and Releasing Resources + @Test(expected = IllegalStateException.class) + public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions1() throws InterruptedException, ExecutionException, IOException, HttpException { + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); + CloseableHttpClient client = HttpClients.custom() + .setConnectionManager(connManager) + .build(); + final HttpGet get = new HttpGet("http://google.com"); + CloseableHttpResponse response = client.execute(get); + + EntityUtils.consume(response.getEntity()); + response.close(); + client.close(); + connManager.close(); + connManager.shutdown(); + + client.execute(get); + + assertTrue(response.getEntity() == null); + } + + @Test + // Example 3.2. TESTER VERSION + public final void whenTwoConnectionsForTwoRequests_thenTwoConnectionsAreLeased() throws InterruptedException { + HttpGet get1 = new HttpGet("https://www.baeldung.com"); + HttpGet get2 = new HttpGet("https://www.google.com"); + + PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager(); + final CloseableHttpClient client1 = HttpClients.custom() + .setConnectionManager(poolingConnManager) + .build(); + final CloseableHttpClient client2 = HttpClients.custom() + .setConnectionManager(poolingConnManager) + .build(); + + final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client1, get1, poolingConnManager); + final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client2, get2, poolingConnManager); + thread1.start(); + thread2.start(); + thread1.join(); + thread2.join(1000); + assertTrue(poolingConnManager.getTotalStats() + .getLeased() == 2); + } + + @Test + // Example 4.2 Tester Version + public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimit() throws InterruptedException { + PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager(); + CloseableHttpClient client = HttpClients.custom() + .setConnectionManager(poolingConnManager) + .build(); + final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager); + final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager); + final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager); + thread1.start(); + thread2.start(); + thread3.start(); + thread1.join(10000); + thread2.join(10000); + thread3.join(10000); + } + + @Test + // 6.2 TESTER VERSION + public final void whenConnectionsNeededGreaterThanMaxTotal_thenReuseConnections() throws InterruptedException { + PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager(); + poolingConnManager.setDefaultMaxPerRoute(5); + poolingConnManager.setMaxTotal(5); + CloseableHttpClient client = HttpClients.custom() + .setConnectionManager(poolingConnManager) + .build(); + final MultiHttpClientConnThread[] threads = new MultiHttpClientConnThread[10]; + int countConnMade = 0; + for (int i = 0; i < threads.length; i++) { + threads[i] = new MultiHttpClientConnThread(client, new HttpGet("http://www.baeldung.com/"), poolingConnManager); + } + for (final MultiHttpClientConnThread thread : threads) { + thread.start(); + } + for (final MultiHttpClientConnThread thread : threads) { + thread.join(10000); + countConnMade++; + if (countConnMade == 0) { + assertTrue(thread.getLeasedConn() == 5); + } + } + } + + @Test + @Ignore("Very Long Running") + // 8.2 TESTER VERSION + public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConns() throws InterruptedException { + PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager(); + CloseableHttpClient client = HttpClients.custom() + .setConnectionManager(poolingConnManager) + .build(); + final IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(poolingConnManager); + final HttpGet get = new HttpGet("http://google.com"); + final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager); + final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager); + final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager); + staleMonitor.start(); + thread1.start(); + thread1.join(); + thread2.start(); + thread2.join(); + thread3.start(); + assertTrue(poolingConnManager.getTotalStats() + .getAvailable() == 1); + thread3.join(32000); + assertTrue(poolingConnManager.getTotalStats() + .getAvailable() == 0); + } +} diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/IdleConnectionMonitorThread.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/IdleConnectionMonitorThread.java new file mode 100644 index 0000000000..4bf97e4fa6 --- /dev/null +++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/IdleConnectionMonitorThread.java @@ -0,0 +1,41 @@ +package com.baeldung.httpclient.httpclient.conn; + +import java.util.concurrent.TimeUnit; + +import org.apache.http.conn.HttpClientConnectionManager; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; + +public class IdleConnectionMonitorThread extends Thread { + private final HttpClientConnectionManager connMgr; + private volatile boolean shutdown; + + IdleConnectionMonitorThread(final PoolingHttpClientConnectionManager connMgr) { + super(); + this.connMgr = connMgr; + } + + // API + + @Override + public final void run() { + try { + while (!shutdown) { + synchronized (this) { + wait(1000); + connMgr.closeExpiredConnections(); + connMgr.closeIdleConnections(30, TimeUnit.SECONDS); + } + } + } catch (final InterruptedException ex) { + shutdown(); + } + } + + private void shutdown() { + shutdown = true; + synchronized (this) { + notifyAll(); + } + } + +} diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/MultiHttpClientConnThread.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/MultiHttpClientConnThread.java new file mode 100644 index 0000000000..4183094621 --- /dev/null +++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/MultiHttpClientConnThread.java @@ -0,0 +1,69 @@ +package com.baeldung.httpclient.httpclient.conn; + +import java.io.IOException; + +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MultiHttpClientConnThread extends Thread { + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private final CloseableHttpClient client; + private final HttpGet get; + + private PoolingHttpClientConnectionManager connManager; + private int leasedConn; + + MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) { + this.client = client; + this.get = get; + this.connManager = connManager; + leasedConn = 0; + } + + MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get) { + this.client = client; + this.get = get; + } + + // API + + final int getLeasedConn() { + return leasedConn; + } + + // + + @Override + public final void run() { + try { + logger.debug("Thread Running: " + getName()); + + logger.debug("Thread Running: " + getName()); + + if (connManager != null) { + logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased()); + logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable()); + } + + final HttpResponse response = client.execute(get); + + if (connManager != null) { + leasedConn = connManager.getTotalStats().getLeased(); + logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased()); + logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable()); + } + + EntityUtils.consume(response.getEntity()); + } catch (final IOException ex) { + logger.error("", ex); + } + } + +} diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/TesterVersion_MultiHttpClientConnThread.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/TesterVersion_MultiHttpClientConnThread.java new file mode 100644 index 0000000000..5e2710342d --- /dev/null +++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/conn/TesterVersion_MultiHttpClientConnThread.java @@ -0,0 +1,46 @@ +package com.baeldung.httpclient.httpclient.conn; + +import java.io.IOException; + +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Preconditions; + +public class TesterVersion_MultiHttpClientConnThread extends Thread { + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private final CloseableHttpClient client; + private final HttpGet get; + private PoolingHttpClientConnectionManager connManager; + + TesterVersion_MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) { + this.client = client; + this.get = get; + this.connManager = Preconditions.checkNotNull(connManager); + } + + // + + @Override + public final void run() { + try { + logger.debug("Thread Running: " + getName()); + + logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased()); + logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable()); + + client.execute(get); + + logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased()); + logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable()); + } catch (final IOException ex) { + logger.error("", ex); + } + } + +} diff --git a/apache-kafka-2/README.md b/apache-kafka-2/README.md index 81239d4a7b..dc675a0811 100644 --- a/apache-kafka-2/README.md +++ b/apache-kafka-2/README.md @@ -12,3 +12,4 @@ You can build the project from the command line using: *mvn clean install*, or i - [Is a Key Required as Part of Sending Messages to Kafka?](https://www.baeldung.com/java-kafka-message-key) - [Read Data From the Beginning Using Kafka Consumer API](https://www.baeldung.com/java-kafka-consumer-api-read) - [Get Partition Count for a Topic in Kafka](https://www.baeldung.com/java-kafka-partition-count-topic) +- [bootstrap-server in Kafka Configuration](https://www.baeldung.com/java-kafka-bootstrap-server) diff --git a/apache-poi-2/README.md b/apache-poi-2/README.md index 0132147201..65641e7c37 100644 --- a/apache-poi-2/README.md +++ b/apache-poi-2/README.md @@ -13,4 +13,5 @@ This module contains articles about Apache POI. - [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas) - [Set the Date Format Using Apache POI](https://www.baeldung.com/java-apache-poi-date-format) - [Replacing Variables in a Document Template with Java](https://www.baeldung.com/java-replace-pattern-word-document-doc-docx) +- [Lock Header Rows With Apache POI](https://www.baeldung.com/java-apache-poi-lock-header-rows) - More articles: [[<-- prev]](../apache-poi) diff --git a/aws-modules/aws-s3-update-object/pom.xml b/aws-modules/aws-s3-update-object/pom.xml index b44cdb8c6a..574a63977b 100644 --- a/aws-modules/aws-s3-update-object/pom.xml +++ b/aws-modules/aws-s3-update-object/pom.xml @@ -2,26 +2,21 @@ 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.1.2 - - - com.baeldung aws-s3-update-object 0.0.1-SNAPSHOT aws-s3-update-object Project demonstrating overwriting of S3 objects - - 17 - + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + org.springframework.boot spring-boot-starter-web - org.springframework.boot spring-boot-starter-test @@ -30,10 +25,9 @@ com.amazonaws aws-java-sdk - 1.12.523 + ${aws-java-sdk-version} - @@ -42,5 +36,7 @@ - + + 1.12.523 + diff --git a/aws-modules/aws-s3-update-object/src/main/java/com/baeldung/awss3updateobject/service/FileService.java b/aws-modules/aws-s3-update-object/src/main/java/com/baeldung/awss3updateobject/service/FileService.java index 8f3458d060..23eaad7913 100644 --- a/aws-modules/aws-s3-update-object/src/main/java/com/baeldung/awss3updateobject/service/FileService.java +++ b/aws-modules/aws-s3-update-object/src/main/java/com/baeldung/awss3updateobject/service/FileService.java @@ -7,13 +7,13 @@ import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*; -import jakarta.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import javax.annotation.PostConstruct; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; diff --git a/aws-modules/aws-s3-update-object/src/test/java/com/baeldung/awss3updateobject/controller/FileControllerUnitTest.java b/aws-modules/aws-s3-update-object/src/test/java/com/baeldung/awss3updateobject/controller/FileControllerUnitTest.java index ec2385f62b..823391c139 100644 --- a/aws-modules/aws-s3-update-object/src/test/java/com/baeldung/awss3updateobject/controller/FileControllerUnitTest.java +++ b/aws-modules/aws-s3-update-object/src/test/java/com/baeldung/awss3updateobject/controller/FileControllerUnitTest.java @@ -11,6 +11,7 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.multipart.MultipartFile; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -34,28 +35,28 @@ public class FileControllerUnitTest { @Test public void givenValidMultipartFile_whenUploadedViaEndpoint_thenCorrectPathIsReturned() throws Exception { - MockMultipartFile multipartFile = new MockMultipartFile("multipartFile", "test.txt", - "text/plain", "test data".getBytes()); + MockMultipartFile multipartFile = new MockMultipartFile("file", "test.txt", "text/plain", "sample file content".getBytes()); + String expectedResult = "File Uploaded Successfully"; - when(fileService.uploadFile(any(MultipartFile.class))).thenReturn("/documents/test.txt"); + when(fileService.uploadFile(multipartFile)).thenReturn(expectedResult); - mockMvc.perform(multipart("/file/upload").file(multipartFile)) + mockMvc.perform(multipart("/api/v1/file/upload").file(multipartFile)) .andExpect(status().isOk()) - .andExpect(content().string("/documents/test.txt")); + .andExpect(content().string(expectedResult)); } @Test public void givenValidMultipartFileAndExistingPath_whenUpdatedViaEndpoint_thenSamePathIsReturned() throws Exception { - MockMultipartFile multipartFile = new MockMultipartFile("multipartFile", "test.txt", - "text/plain", "test update data".getBytes()); - String existingFilePath = "/documents/existingFile.txt"; + MockMultipartFile multipartFile = new MockMultipartFile("file", "test.txt", "text/plain", "updated file content".getBytes()); + String filePath = "some/path/to/file"; + String expectedResult = "File Updated Successfully"; - when(fileService.updateFile(any(MultipartFile.class), eq(existingFilePath))).thenReturn(existingFilePath); + when(fileService.updateFile(multipartFile, filePath)).thenReturn(expectedResult); - mockMvc.perform(multipart("/file/update") + mockMvc.perform(multipart("/api/v1/file/update") .file(multipartFile) - .param("exitingFilePath", existingFilePath)) + .param("filePath", filePath)) .andExpect(status().isOk()) - .andExpect(content().string(existingFilePath)); + .andExpect(content().string(expectedResult)); } } \ No newline at end of file diff --git a/aws-modules/aws-s3-update-object/src/test/java/com/baeldung/awss3updateobject/service/FileServiceUnitTest.java b/aws-modules/aws-s3-update-object/src/test/java/com/baeldung/awss3updateobject/service/FileServiceUnitTest.java index 3ccd41820e..90ed77b148 100644 --- a/aws-modules/aws-s3-update-object/src/test/java/com/baeldung/awss3updateobject/service/FileServiceUnitTest.java +++ b/aws-modules/aws-s3-update-object/src/test/java/com/baeldung/awss3updateobject/service/FileServiceUnitTest.java @@ -41,6 +41,7 @@ public class FileServiceUnitTest { @Test public void givenValidFile_whenUploaded_thenKeyMatchesDocumentPath() throws Exception { when(multipartFile.getName()).thenReturn("testFile"); + when(multipartFile.getOriginalFilename()).thenReturn("testFile"); when(multipartFile.getContentType()).thenReturn("application/pdf"); when(multipartFile.getSize()).thenReturn(1024L); when(multipartFile.getInputStream()).thenReturn(mock(InputStream.class)); @@ -57,6 +58,7 @@ public class FileServiceUnitTest { @Test public void givenValidFile_whenUploadFailsDueToNoBucket_thenExceptionIsThrown() throws Exception { when(multipartFile.getName()).thenReturn("testFile"); + when(multipartFile.getOriginalFilename()).thenReturn("testFile"); when(multipartFile.getContentType()).thenReturn("application/pdf"); when(multipartFile.getSize()).thenReturn(1024L); when(multipartFile.getInputStream()).thenReturn(mock(InputStream.class)); diff --git a/aws-modules/aws-s3/README.md b/aws-modules/aws-s3/README.md index 3389fdf454..59c9f893d9 100644 --- a/aws-modules/aws-s3/README.md +++ b/aws-modules/aws-s3/README.md @@ -8,4 +8,5 @@ This module contains articles about Simple Storage Service (S3) on AWS - [Multipart Uploads in Amazon S3 with Java](https://www.baeldung.com/aws-s3-multipart-upload) - [Using the JetS3t Java Client With Amazon S3](https://www.baeldung.com/jets3t-amazon-s3) - [Check if a Specified Key Exists in a Given S3 Bucket Using Java](https://www.baeldung.com/java-aws-s3-check-specified-key-exists) -- [Listing All AWS S3 Objects in a Bucket Using Java](https://www.baeldung.com/java-aws-s3-list-bucket-objects) \ No newline at end of file +- [Listing All AWS S3 Objects in a Bucket Using Java](https://www.baeldung.com/java-aws-s3-list-bucket-objects) +- [Update an Existing Amazon S3 Object Using Java](https://www.baeldung.com/java-update-amazon-s3-object) diff --git a/aws-modules/aws-s3/src/main/java/com/baeldung/s3/RenameObjectService.java b/aws-modules/aws-s3/src/main/java/com/baeldung/s3/RenameObjectService.java new file mode 100644 index 0000000000..0ca586c73b --- /dev/null +++ b/aws-modules/aws-s3/src/main/java/com/baeldung/s3/RenameObjectService.java @@ -0,0 +1,79 @@ +package com.baeldung.s3; + +import java.util.List; + +import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.CopyObjectRequest; +import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; +import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; +import software.amazon.awssdk.services.s3.model.ListObjectsV2Response; +import software.amazon.awssdk.services.s3.model.S3Object; + +public class RenameObjectService { + + private S3Client s3Client; + + public RenameObjectService(S3Client s3Client) { + this.s3Client = s3Client; + } + + public RenameObjectService() { + init(); + } + + public void init() { + this.s3Client = S3Client.builder() + .region(Region.US_EAST_1) + .credentialsProvider(ProfileCredentialsProvider.create("default")) + .build(); + } + + public void renameFile(String bucketName, String keyName, String destinationKeyName) { + CopyObjectRequest copyObjRequest = CopyObjectRequest.builder() + .sourceBucket(bucketName) + .sourceKey(keyName) + .destinationBucket(destinationKeyName) + .destinationKey(bucketName) + .build(); + s3Client.copyObject(copyObjRequest); + DeleteObjectRequest deleteRequest = DeleteObjectRequest.builder() + .bucket(bucketName) + .key(keyName) + .build(); + s3Client.deleteObject(deleteRequest); + } + + public void renameFolder(String bucketName, String sourceFolderKey, String destinationFolderKey) { + ListObjectsV2Request listRequest = ListObjectsV2Request.builder() + .bucket(bucketName) + .prefix(sourceFolderKey) + .build(); + + ListObjectsV2Response listResponse = s3Client.listObjectsV2(listRequest); + List objects = listResponse.contents(); + + for (S3Object s3Object : objects) { + String newKey = destinationFolderKey + s3Object.key() + .substring(sourceFolderKey.length()); + + // Copy object to destination folder + CopyObjectRequest copyRequest = CopyObjectRequest.builder() + .sourceBucket(bucketName) + .sourceKey(s3Object.key()) + .destinationBucket(bucketName) + .destinationKey(newKey) + .build(); + s3Client.copyObject(copyRequest); + + // Delete object from source folder + DeleteObjectRequest deleteRequest = DeleteObjectRequest.builder() + .bucket(bucketName) + .key(s3Object.key()) + .build(); + s3Client.deleteObject(deleteRequest); + } + } + +} diff --git a/aws-modules/pom.xml b/aws-modules/pom.xml index 02473815b5..b94faafa86 100644 --- a/aws-modules/pom.xml +++ b/aws-modules/pom.xml @@ -19,6 +19,7 @@ aws-miscellaneous aws-reactive aws-s3 + aws-s3-update-object diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/multipleorwithif/MultipleOrWithIfUnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/multipleorwithif/MultipleOrWithIfUnitTest.java new file mode 100644 index 0000000000..09b8d3c2b1 --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/multipleorwithif/MultipleOrWithIfUnitTest.java @@ -0,0 +1,126 @@ +package com.baeldung.multipleorwithif; + +import static java.time.Month.DECEMBER; +import static java.time.Month.NOVEMBER; +import static java.time.Month.OCTOBER; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.in; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.time.Month; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.function.Predicate; + +import org.junit.jupiter.api.Test; + +class MultipleOrWithIfUnitTest { + + private final Random rand = new Random(); + + final Set months = Set.of(OCTOBER, NOVEMBER, DECEMBER); + + @Test + public void givenIfStatement_whenMultipleOrOperator_thenAssert() { + assertTrue(multipleOrOperatorIf(monthIn())); + assertFalse(multipleOrOperatorIf(monthNotIn())); + } + + boolean multipleOrOperatorIf(Month month) { + if (month == OCTOBER || month == NOVEMBER || month == DECEMBER) { + return true; + } + return false; + } + + @Test + public void givenSwitch_whenMultipleCase_thenBreakAndAssert() { + assertTrue(switchMonth(monthIn())); + assertFalse(switchMonth(monthNotIn())); + } + + boolean switchMonth(Month month) { + return switch (month) { + case OCTOBER, NOVEMBER, DECEMBER -> true; + default -> false; + }; + } + + @Test + public void givenAllowedValuesList_whenContains_thenAssert() { + assertTrue(contains(monthIn())); + assertFalse(contains(monthNotIn())); + } + + @Test + public void givenPredicates_whenTestMultipleOr_thenAssert() { + assertTrue(predicateWithIf(monthIn())); + assertFalse(predicateWithIf(monthNotIn())); + } + + @Test + public void givenInputList_whenFilterWithPredicate_thenAssert() { + + List list = List.of(monthIn(), monthIn(), monthNotIn()); + + list.stream() + .filter(this::predicateWithIf) + .forEach(m -> assertThat(m, is(in(months)))); + } + + Predicate orPredicate() { + Predicate predicate = x -> x == OCTOBER; + Predicate predicate1 = x -> x == NOVEMBER; + Predicate predicate2 = x -> x == DECEMBER; + + return predicate.or(predicate1) + .or(predicate2); + } + + boolean predicateWithIf(Month month) { + if (orPredicate().test(month)) { + return true; + } + return false; + } + + @Test + public void givenContainsInSetPredicate_whenTestPredicate_thenAssert() { + Predicate collectionPredicate = this::contains; + + assertTrue(collectionPredicate.test(monthIn())); + assertFalse(collectionPredicate.test(monthNotIn())); + } + + @Test + public void givenInputList_whenFilterWithContains_thenAssert() { + + List monthList = List.of(monthIn(), monthIn(), monthNotIn()); + + monthList.stream() + .filter(this::contains) + .forEach(m -> assertThat(m, is(in(months)))); + } + + private boolean contains(Month month) { + if (months.contains(month)) { + return true; + } + return false; + } + + private Month monthIn() { + return Month.of(rand.ints(10, 13) + .findFirst() + .orElse(10)); + } + + private Month monthNotIn() { + return Month.of(rand.ints(1, 10) + .findFirst() + .orElse(1)); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-arrays-convert/README.md b/core-java-modules/core-java-arrays-convert/README.md index b68773463d..dcaaaac12e 100644 --- a/core-java-modules/core-java-arrays-convert/README.md +++ b/core-java-modules/core-java-arrays-convert/README.md @@ -9,3 +9,4 @@ This module contains articles about arrays conversion in Java - [Converting a String Array Into an int Array in Java](https://www.baeldung.com/java-convert-string-array-to-int-array) - [Convert Java Array to Iterable](https://www.baeldung.com/java-array-convert-to-iterable) - [Converting an int[] to HashSet in Java](https://www.baeldung.com/java-converting-int-array-to-hashset) +- [Convert an ArrayList of String to a String Array in Java](https://www.baeldung.com/java-convert-string-arraylist-array) diff --git a/core-java-modules/core-java-arrays-operations-advanced/README.md b/core-java-modules/core-java-arrays-operations-advanced/README.md index e3465c9fa3..b379958f37 100644 --- a/core-java-modules/core-java-arrays-operations-advanced/README.md +++ b/core-java-modules/core-java-arrays-operations-advanced/README.md @@ -13,3 +13,4 @@ This module contains articles about advanced operations on arrays in Java. They - [Performance of System.arraycopy() vs. Arrays.copyOf()](https://www.baeldung.com/java-system-arraycopy-arrays-copyof-performance) - [Slicing Arrays in Java](https://www.baeldung.com/java-slicing-arrays) - [Combining Two or More Byte Arrays](https://www.baeldung.com/java-concatenate-byte-arrays) +- [Calculating the Sum of Two Arrays in Java](https://www.baeldung.com/java-sum-arrays-element-wise) diff --git a/core-java-modules/core-java-collections-conversions-2/README.md b/core-java-modules/core-java-collections-conversions-2/README.md index fe17af0a7a..e8d008104c 100644 --- a/core-java-modules/core-java-collections-conversions-2/README.md +++ b/core-java-modules/core-java-collections-conversions-2/README.md @@ -12,4 +12,6 @@ This module contains articles about conversions among Collection types and array - [Convert a List of Integers to a List of Strings](https://www.baeldung.com/java-convert-list-integers-to-list-strings) - [Combining Two Lists Into a Map in Java](https://www.baeldung.com/java-combine-two-lists-into-map) - [Convert a List of Strings to a List of Integers](https://www.baeldung.com/java-convert-list-strings-to-integers) +- [Convert List to Long[] Array in Java](https://www.baeldung.com/java-convert-list-object-to-long-array) +- [Get the First n Elements of a List Into an Array](https://www.baeldung.com/java-take-start-elements-list-array) - More articles: [[<-- prev]](../core-java-collections-conversions) diff --git a/core-java-modules/core-java-collections-maps-3/README.md b/core-java-modules/core-java-collections-maps-3/README.md index 68df2b9556..0d07bde8c1 100644 --- a/core-java-modules/core-java-collections-maps-3/README.md +++ b/core-java-modules/core-java-collections-maps-3/README.md @@ -9,4 +9,5 @@ This module contains articles about Map data structures in Java. - [Collections.synchronizedMap vs. ConcurrentHashMap](https://www.baeldung.com/java-synchronizedmap-vs-concurrenthashmap) - [Java HashMap Load Factor](https://www.baeldung.com/java-hashmap-load-factor) - [Converting Java Properties to HashMap](https://www.baeldung.com/java-convert-properties-to-hashmap) +- [Get Values and Keys as ArrayList From a HashMap](https://www.baeldung.com/java-values-keys-arraylists-hashmap) - More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-2) diff --git a/core-java-modules/core-java-collections-maps-6/README.md b/core-java-modules/core-java-collections-maps-6/README.md index bebcdea82b..d4f432bdcb 100644 --- a/core-java-modules/core-java-collections-maps-6/README.md +++ b/core-java-modules/core-java-collections-maps-6/README.md @@ -8,3 +8,4 @@ - [How to Modify a Key in a HashMap?](https://www.baeldung.com/java-hashmap-modify-key) - [Converting String or String Array to Map in Java](https://www.baeldung.com/java-convert-string-to-map) - [Remove Duplicate Values From HashMap in Java](https://www.baeldung.com/java-hashmap-delete-duplicates) +- [Sorting Java Map in Descending Order](https://www.baeldung.com/java-sort-map-descending) diff --git a/core-java-modules/core-java-collections-set-2/pom.xml b/core-java-modules/core-java-collections-set-2/pom.xml index 680c01d8ca..d1a108f8bb 100644 --- a/core-java-modules/core-java-collections-set-2/pom.xml +++ b/core-java-modules/core-java-collections-set-2/pom.xml @@ -20,6 +20,17 @@ ${junit-platform.version} test + + org.testng + testng + 7.7.0 + test + + + com.google.guava + guava + 32.1.1-jre + diff --git a/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/cartesianproduct/CartesianProduct.java b/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/cartesianproduct/CartesianProduct.java new file mode 100644 index 0000000000..d4725d74f1 --- /dev/null +++ b/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/cartesianproduct/CartesianProduct.java @@ -0,0 +1,75 @@ +package com.baeldung.cartesianproduct; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.google.common.collect.Sets; + +public class CartesianProduct { + public List> getCartesianProductIterative(List> sets) { + List> result = new ArrayList<>(); + if(sets == null || sets.isEmpty()) { + return result; + } + int totalSets = sets.size(); + int totalCombinations = 1 << totalSets; + for(int i = 0; i < totalCombinations; i++) { + List combination = new ArrayList<>(); + for(int j = 0; j < totalSets; j++) { + if (((i >> j) & 1) == 1) { + combination.add(sets.get(j).get(0)); + } else { + combination.add(sets.get(j).get(1)); + } + } + result.add(combination); + } + return result; + } + + public List> getCartesianProductRecursive(List> sets) { + List> result = new ArrayList<>(); + getCartesianProductRecursiveHelper(sets, 0, new ArrayList<>(), result); + return result; + } + + private void getCartesianProductRecursiveHelper(List> sets, int index, List current, List> result) { + if(index == sets.size()) { + result.add(new ArrayList<>(current)); + return; + } + List currentSet = sets.get(index); + for(Object element: currentSet) { + current.add(element); + getCartesianProductRecursiveHelper(sets, index+1, current, result); + current.remove(current.size() - 1); + } + } + + public List> getCartesianProductUsingStreams(List> sets) { + return cartesianProduct(sets,0).collect(Collectors.toList()); + } + + public Stream> cartesianProduct(List> sets, int index) { + if(index == sets.size()) { + List emptyList = new ArrayList<>(); + return Stream.of(emptyList); + } + List currentSet = sets.get(index); + return currentSet.stream().flatMap(element -> cartesianProduct(sets, index+1) + .map(list -> { + List newList = new ArrayList<>(list); + newList.add(0, element); return newList; + })); + } + + public List> getCartesianProductUsingGuava(List> sets) { + Set> cartesianProduct = Sets.cartesianProduct(sets); + List> cartesianList = new ArrayList<>(cartesianProduct); + return cartesianList; + } + +} diff --git a/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/cartesianproduct/CartesianProductUnitTest.java b/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/cartesianproduct/CartesianProductUnitTest.java new file mode 100644 index 0000000000..cc9c01fb5f --- /dev/null +++ b/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/cartesianproduct/CartesianProductUnitTest.java @@ -0,0 +1,93 @@ +package com.baeldung.cartesianproduct; + +import static org.testng.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.testng.annotations.Test; + +public class CartesianProductUnitTest { + private CartesianProduct cp = new CartesianProduct(); + List> sets = Arrays.asList( + Arrays.asList(10, 20), + Arrays.asList("John", "Jack"), + Arrays.asList('I', 'J') + ); + + @Test + public void whenUsingStreams_thenCalculateCartesianProduct() { + List> expected = Arrays.asList( + Arrays.asList(10, "John", 'I'), + Arrays.asList(10, "John", 'J'), + Arrays.asList(10, "Jack", 'I'), + Arrays.asList(10, "Jack", 'J'), + Arrays.asList(20, "John", 'I'), + Arrays.asList(20, "John", 'J'), + Arrays.asList(20, "Jack", 'I'), + Arrays.asList(20, "Jack", 'J') + ); + List> cartesianProduct = cp.getCartesianProductUsingStreams(sets); + + assertEquals(expected, cartesianProduct); + } + + @Test + public void whenUsingRecursion_thenCalculateCartesianProduct() { + List> expected = Arrays.asList( + Arrays.asList(10, "John", 'I'), + Arrays.asList(10, "John", 'J'), + Arrays.asList(10, "Jack", 'I'), + Arrays.asList(10, "Jack", 'J'), + Arrays.asList(20, "John", 'I'), + Arrays.asList(20, "John", 'J'), + Arrays.asList(20, "Jack", 'I'), + Arrays.asList(20, "Jack", 'J') + ); + List> cartesianProduct = cp.getCartesianProductRecursive(sets); + + assertEquals(expected, cartesianProduct); + } + + @Test + public void whenUsingIterativeApproach_thenCalculateCartesianProduct() { + List> expected = Arrays.asList( + Arrays.asList(20, "Jack", 'J'), + Arrays.asList(10, "Jack", 'J'), + Arrays.asList(20, "John", 'J'), + Arrays.asList(10, "John", 'J'), + Arrays.asList(20, "Jack", 'I'), + Arrays.asList(10, "Jack", 'I'), + Arrays.asList(20, "John", 'I'), + Arrays.asList(10, "John", 'I') + ); + List> cartesianProduct = cp.getCartesianProductIterative(sets); + + assertEquals(expected, cartesianProduct); + } + + @Test + public void whenUsingGuava_thenCalculateCartesianProduct() { + List> sets = new ArrayList<>(); + sets.add(new HashSet<>(Arrays.asList(10, 20))); + sets.add(new HashSet<>(Arrays.asList("John", "Jack"))); + sets.add(new HashSet<>(Arrays.asList('I', 'J'))); + + List> expected = Arrays.asList( + Arrays.asList(20, "John", 'I'), + Arrays.asList(20, "John", 'J'), + Arrays.asList(20, "Jack", 'I'), + Arrays.asList(20, "Jack", 'J'), + Arrays.asList(10, "John", 'I'), + Arrays.asList(10, "John", 'J'), + Arrays.asList(10, "Jack", 'I'), + Arrays.asList(10, "Jack", 'J') + ); + List> cartesianProduct = cp.getCartesianProductUsingGuava(sets); + + assertEquals(expected, cartesianProduct); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java b/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java index 44f84ad77c..093cfdbc81 100644 --- a/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java +++ b/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java @@ -7,31 +7,37 @@ import java.util.concurrent.Phaser; class LongRunningAction implements Runnable { - private static Logger log = LoggerFactory.getLogger(LongRunningAction.class); - private String threadName; - private Phaser ph; + private static final Logger log = LoggerFactory.getLogger(LongRunningAction.class); + private final String threadName; + private final Phaser ph; LongRunningAction(String threadName, Phaser ph) { this.threadName = threadName; this.ph = ph; + + this.randomWait(); + ph.register(); + log.info("Thread {} registered during phase {}", threadName, ph.getPhase()); } @Override public void run() { - log.info("This is phase {}", ph.getPhase()); - log.info("Thread {} before long running action", threadName); - + log.info("Thread {} BEFORE long running action in phase {}", threadName, ph.getPhase()); + ph.arriveAndAwaitAdvance(); + + randomWait(); + + log.info("Thread {} AFTER long running action in phase {}", threadName, ph.getPhase()); + ph.arriveAndDeregister(); + } + + // Simulating real work + private void randomWait() { try { - Thread.sleep(2000); + Thread.sleep((long) (Math.random() * 100)); } catch (InterruptedException e) { e.printStackTrace(); } - - log.debug("Thread {} action completed and waiting for others", threadName); - ph.arriveAndAwaitAdvance(); - log.debug("Thread {} proceeding in phase {}", threadName, ph.getPhase()); - - ph.arriveAndDeregister(); } } \ No newline at end of file diff --git a/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java b/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java index 384a1837c1..9cb863073e 100644 --- a/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java +++ b/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java @@ -7,8 +7,6 @@ import org.junit.runners.MethodSorters; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Phaser; import static junit.framework.TestCase.assertEquals; @@ -16,38 +14,32 @@ import static junit.framework.TestCase.assertEquals; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class PhaserUnitTest { - private static Logger log = LoggerFactory.getLogger(PhaserUnitTest.class); + private static final Logger log = LoggerFactory.getLogger(PhaserUnitTest.class); @Test - public void givenPhaser_whenCoordinateWorksBetweenThreads_thenShouldCoordinateBetweenMultiplePhases() { - //given - ExecutorService executorService = Executors.newCachedThreadPool(); + public void givenPhaser_whenCoordinateWorksBetweenThreads_thenShouldCoordinateBetweenMultiplePhases() throws InterruptedException { Phaser ph = new Phaser(1); assertEquals(0, ph.getPhase()); - //when - executorService.submit(new LongRunningAction("thread-1", ph)); - executorService.submit(new LongRunningAction("thread-2", ph)); - executorService.submit(new LongRunningAction("thread-3", ph)); + new Thread(new LongRunningAction("thread-1", ph)).start(); + new Thread(new LongRunningAction("thread-2", ph)).start(); + new Thread(new LongRunningAction("thread-3", ph)).start(); - //then - log.debug("Thread {} waiting for others", Thread.currentThread().getName()); + log.info("Thread {} waiting for others", Thread.currentThread().getName()); ph.arriveAndAwaitAdvance(); - log.debug("Thread {} proceeding in phase {}", Thread.currentThread().getName(), ph.getPhase()); - + log.info("Thread {} proceeding in phase {}", Thread.currentThread().getName(), ph.getPhase()); assertEquals(1, ph.getPhase()); - //and - executorService.submit(new LongRunningAction("thread-4", ph)); - executorService.submit(new LongRunningAction("thread-5", ph)); + new Thread(new LongRunningAction("thread-4", ph)).start(); + new Thread(new LongRunningAction("thread-5", ph)).start(); - log.debug("Thread {} waiting for others", Thread.currentThread().getName()); + log.info("Thread {} waiting for new phase", Thread.currentThread().getName()); ph.arriveAndAwaitAdvance(); - log.debug("Thread {} proceeding in phase {}", Thread.currentThread().getName(), ph.getPhase()); - + log.info("Thread {} proceeding in phase {}", Thread.currentThread().getName(), ph.getPhase()); assertEquals(2, ph.getPhase()); - ph.arriveAndDeregister(); + Thread.sleep(1000); + assertEquals(true, ph.isTerminated()); } } diff --git a/core-java-modules/core-java-concurrency-basic/README.md b/core-java-modules/core-java-concurrency-basic/README.md index e5c061710c..a133beb1fe 100644 --- a/core-java-modules/core-java-concurrency-basic/README.md +++ b/core-java-modules/core-java-concurrency-basic/README.md @@ -11,4 +11,5 @@ This module contains articles about basic Java concurrency - [Runnable vs. Callable in Java](https://www.baeldung.com/java-runnable-callable) - [What Is Thread-Safety and How to Achieve It?](https://www.baeldung.com/java-thread-safety) - [How to Get Notified When a Task Completes in Java Executors](https://www.baeldung.com/java-executors-task-completed-notification) +- [Difference Between Future, CompletableFuture, and Rxjava’s Observable](https://www.baeldung.com/java-future-completablefuture-rxjavas-observable) - [[Next -->]](/core-java-modules/core-java-concurrency-basic-2) diff --git a/core-java-modules/core-java-conditionals/README.md b/core-java-modules/core-java-conditionals/README.md index ae5694c3ba..828f3484f1 100644 --- a/core-java-modules/core-java-conditionals/README.md +++ b/core-java-modules/core-java-conditionals/README.md @@ -3,3 +3,4 @@ This module contains articles about Java Conditionals. ### Relevant articles: +- [Guide to the yield Keyword in Java](https://www.baeldung.com/java-yield-switch) diff --git a/core-java-modules/core-java-datetime-conversion/README.md b/core-java-modules/core-java-datetime-conversion/README.md index 11e4348838..d3a3dae728 100644 --- a/core-java-modules/core-java-datetime-conversion/README.md +++ b/core-java-modules/core-java-datetime-conversion/README.md @@ -8,3 +8,5 @@ This module contains articles about converting between Java date and time object - [Convert Date to LocalDate or LocalDateTime and Back](http://www.baeldung.com/java-date-to-localdate-and-localdatetime) - [Convert Between java.time.Instant and java.sql.Timestamp](https://www.baeldung.com/java-time-instant-to-java-sql-timestamp) - [Convert Between LocalDateTime and ZonedDateTime](https://www.baeldung.com/java-localdatetime-zoneddatetime) +- [Conversion From 12-Hour Time to 24-Hour Time in Java](https://www.baeldung.com/java-convert-time-format) +- [Convert Epoch Time to LocalDate and LocalDateTime](https://www.baeldung.com/java-convert-epoch-localdate) diff --git a/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/socketexception/SslServer.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/socketexception/SslServer.java new file mode 100644 index 0000000000..ae783fa8bd --- /dev/null +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/socketexception/SslServer.java @@ -0,0 +1,32 @@ +package com.baeldung.socketexception; + +import java.io.IOException; + +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; + +public class SslServer { + + public static void createSSLSocketWithEnabledProtocols(SSLServerSocketFactory socketFactory, int port, String[] enabledProtocols) { + SSLServerSocket serverSocket = null; + + try { + serverSocket = (SSLServerSocket) socketFactory.createServerSocket(port); + // Set the enabled protocols + serverSocket.setEnabledProtocols(enabledProtocols); + System.out.println("Server is running on port " + port); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (serverSocket != null) { + try { + serverSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + +} diff --git a/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/socketexception/SocketExceptionHandlingUnitTest.java b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/socketexception/SocketExceptionHandlingUnitTest.java index 08b21c6299..197046d273 100644 --- a/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/socketexception/SocketExceptionHandlingUnitTest.java +++ b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/socketexception/SocketExceptionHandlingUnitTest.java @@ -1,25 +1,33 @@ package com.baeldung.socketexception; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import java.io.IOException; import java.net.SocketException; import java.util.concurrent.Executors; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; + import org.junit.BeforeClass; import org.junit.Test; public class SocketExceptionHandlingUnitTest { + private static final int PORT = 6699; @BeforeClass public static void runServer() throws IOException, InterruptedException { Executors.newSingleThreadExecutor() - .submit(() -> new SocketServer().start(6699)); + .submit(() -> new SocketServer().start(PORT)); Thread.sleep(100); } @Test public void givenRunningServer_whenConnectToClosedSocket_thenHandleException() throws IOException { SocketClient client = new SocketClient(); - client.startConnection("127.0.0.1", 6699); + client.startConnection("127.0.0.1", PORT); try { client.sendMessage("hi"); client.sendMessage("hi again"); @@ -28,4 +36,22 @@ public class SocketExceptionHandlingUnitTest { } } + @Test + public void givenRunningServer_whenConnectToSocket_thenHandleConnectionResetException() throws IOException { + // Enable multiple SSL/TLS protocols + String[] enabledProtocols = { "TLSv1.2", "TLSv1.3", "TLSv1.1", "TLSv1", "SSLv3", "SSLv3" }; + SSLServerSocketFactory mockFactory = mock(SSLServerSocketFactory.class); + SSLServerSocket mockServerSocket = mock(SSLServerSocket.class); + + // Set up the mock factory to return the mock server socket + when(mockFactory.createServerSocket(PORT)).thenReturn(mockServerSocket); + + // Call the method being tested + SslServer.createSSLSocketWithEnabledProtocols(mockFactory, PORT, enabledProtocols); + + // Verify that setEnabledProtocols and close were called + verify(mockServerSocket).setEnabledProtocols(enabledProtocols); + verify(mockServerSocket).close(); + } + } diff --git a/core-java-modules/core-java-io/pom.xml b/core-java-modules/core-java-io/pom.xml index a59ac619bd..8364f36874 100644 --- a/core-java-modules/core-java-io/pom.xml +++ b/core-java-modules/core-java-io/pom.xml @@ -38,9 +38,9 @@ ${fscontext.version} - javax.activation - activation - 1.1 + org.eclipse.angus + angus-activation + ${angus-activation.version} test @@ -73,10 +73,6 @@ org.apache.maven.plugins maven-javadoc-plugin ${maven-javadoc-plugin.version} - - ${maven.compiler.source} - ${maven.compiler.target} - @@ -131,13 +127,15 @@ - 3.0.0-M1 - 2.4.0 + 3.5.0 + 2.7.1 - 1.18 + 2.8.0 0.1.5 - 3.1.0 + 3.3.0 4.4.2 + 2.1.2 + 2.0.1 \ No newline at end of file diff --git a/core-java-modules/core-java-io/src/test/java/com/baeldung/mimetype/MimeTypeUnitTest.java b/core-java-modules/core-java-io/src/test/java/com/baeldung/mimetype/MimeTypeUnitTest.java index bf916e39e7..4c382d07a6 100644 --- a/core-java-modules/core-java-io/src/test/java/com/baeldung/mimetype/MimeTypeUnitTest.java +++ b/core-java-modules/core-java-io/src/test/java/com/baeldung/mimetype/MimeTypeUnitTest.java @@ -10,7 +10,7 @@ import java.net.URLConnection; import java.nio.file.Files; import java.nio.file.Path; -import javax.activation.MimetypesFileTypeMap; +import jakarta.activation.MimetypesFileTypeMap; import org.apache.tika.Tika; import org.junit.Test; diff --git a/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/magicsquare/README.md b/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/magicsquare/README.md new file mode 100644 index 0000000000..b2f7ece88a --- /dev/null +++ b/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/magicsquare/README.md @@ -0,0 +1,2 @@ +## Relevant Articles +- [Creating a Magic Square in Java](https://www.baeldung.com/java-magic-square) diff --git a/core-java-modules/core-java-lang-oop-generics/README.md b/core-java-modules/core-java-lang-oop-generics/README.md index 720ba9dcfd..3e33ba5315 100644 --- a/core-java-modules/core-java-lang-oop-generics/README.md +++ b/core-java-modules/core-java-lang-oop-generics/README.md @@ -9,3 +9,4 @@ This module contains articles about generics in Java - [Super Type Tokens in Java Generics](https://www.baeldung.com/java-super-type-tokens) - [Java Warning “unchecked conversion”](https://www.baeldung.com/java-unchecked-conversion) - [Java Warning “Unchecked Cast”](https://www.baeldung.com/java-warning-unchecked-cast) +- [What Does the Holder Class Do in Java?](https://www.baeldung.com/java-holder-class) diff --git a/core-java-modules/core-java-lang-oop-inheritance/README.md b/core-java-modules/core-java-lang-oop-inheritance/README.md index c87bdf13d7..430f88e717 100644 --- a/core-java-modules/core-java-lang-oop-inheritance/README.md +++ b/core-java-modules/core-java-lang-oop-inheritance/README.md @@ -12,3 +12,4 @@ This module contains articles about inheritance in Java - [Guide to Inheritance in Java](https://www.baeldung.com/java-inheritance) - [Object Type Casting in Java](https://www.baeldung.com/java-type-casting) - [Variable and Method Hiding in Java](https://www.baeldung.com/java-variable-method-hiding) +- [Inner Classes Vs. Subclasses in Java](https://www.baeldung.com/java-inner-classes-vs-subclasses) diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/EmailNotifier.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/EmailNotifier.java index 1ade675e87..9f3e140926 100644 --- a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/EmailNotifier.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/EmailNotifier.java @@ -1,11 +1,12 @@ package com.baeldung.subclassinnerclass; -import java.util.HashMap; - public class EmailNotifier extends Notifier { @Override void notify(Message e) { - // Provide email specific implementation here + // connect to the email connector + EmailConnector emailConnector = new EmailConnector(); + emailConnector.connect(); + // send email } // Inner class for email connection @@ -13,5 +14,9 @@ public class EmailNotifier extends Notifier { private String emailHost; private int emailPort; // Getter Setters + + private void connect() { + // connect to the smtp server + } } } diff --git a/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/modulo/ModuloUnitTest.java b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/modulo/ModuloUnitTest.java index 8b3685adf3..0a9418cb17 100644 --- a/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/modulo/ModuloUnitTest.java +++ b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/modulo/ModuloUnitTest.java @@ -1,54 +1,77 @@ package com.baeldung.modulo; +import static org.junit.Assert.assertEquals; + import org.junit.Test; import static org.assertj.core.api.Java6Assertions.*; public class ModuloUnitTest { - @Test - public void whenIntegerDivision_thenLosesRemainder(){ - assertThat(11 / 4).isEqualTo(2); - } - - @Test - public void whenDoubleDivision_thenKeepsRemainder(){ - assertThat(11 / 4.0).isEqualTo(2.75); - } - - @Test - public void whenModulo_thenReturnsRemainder(){ - assertThat(11 % 4).isEqualTo(3); - } - - @Test(expected = ArithmeticException.class) - public void whenDivisionByZero_thenArithmeticException(){ - double result = 1 / 0; - } - - @Test(expected = ArithmeticException.class) - public void whenModuloByZero_thenArithmeticException(){ - double result = 1 % 0; - } - - @Test - public void whenDivisorIsOddAndModulusIs2_thenResultIs1(){ - assertThat(3 % 2).isEqualTo(1); - } - - @Test - public void whenDivisorIsEvenAndModulusIs2_thenResultIs0(){ - assertThat(4 % 2).isEqualTo(0); - } - - @Test - public void whenItemsIsAddedToCircularQueue_thenNoArrayIndexOutOfBounds(){ - int QUEUE_CAPACITY= 10; - int[] circularQueue = new int[QUEUE_CAPACITY]; - int itemsInserted = 0; - for (int value = 0; value < 1000; value++) { - int writeIndex = ++itemsInserted % QUEUE_CAPACITY; - circularQueue[writeIndex] = value; + @Test + public void whenIntegerDivision_thenLosesRemainder() { + assertThat(11 / 4).isEqualTo(2); + } + + @Test + public void whenDoubleDivision_thenKeepsRemainder() { + assertThat(11 / 4.0).isEqualTo(2.75); + } + + @Test + public void whenModulo_thenReturnsRemainder() { + assertThat(11 % 4).isEqualTo(3); + } + + @Test(expected = ArithmeticException.class) + public void whenDivisionByZero_thenArithmeticException() { + double result = 1 / 0; + } + + @Test(expected = ArithmeticException.class) + public void whenModuloByZero_thenArithmeticException() { + double result = 1 % 0; + } + + @Test + public void whenDivisorIsOddAndModulusIs2_thenResultIs1() { + assertThat(3 % 2).isEqualTo(1); + } + + @Test + public void whenDivisorIsEvenAndModulusIs2_thenResultIs0() { + assertThat(4 % 2).isEqualTo(0); + } + + @Test + public void whenDividendIsNegativeAndModulusIs2_thenResultIsNegative() { + assertEquals(-1, -9 % 2); + } + + @Test + public void whenDividendIsNegativeAndRemainderIsCheckedForNegativeValue_thenResultIsPositive() { + int remainder = -9 % 2; + + if (remainder < 0) { + remainder += 2; + } + assertEquals(1, remainder); + } + + @Test + public void whenDividendIsNegativeAndUsesMathClass_thenResultIsPositive() { + int remainder = Math.floorMod(-9, 2); + assertEquals(1, remainder); + } + + @Test + public void whenItemsIsAddedToCircularQueue_thenNoArrayIndexOutOfBounds() { + int QUEUE_CAPACITY = 10; + int[] circularQueue = new int[QUEUE_CAPACITY]; + int itemsInserted = 0; + for (int value = 0; value < 1000; value++) { + int writeIndex = ++itemsInserted % QUEUE_CAPACITY; + circularQueue[writeIndex] = value; + } } - } } diff --git a/core-java-modules/core-java-lang/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java b/core-java-modules/core-java-lang/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java index dac05a85b1..612fd7e097 100644 --- a/core-java-modules/core-java-lang/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java +++ b/core-java-modules/core-java-lang/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java @@ -30,13 +30,13 @@ public class Java8ComparatorUnitTest { System.out.println("************** Java 8 Comaparator **************"); Comparator byRanking = (Player player1, Player player2) -> Integer.compare(player1.getRanking(), player2.getRanking()); - System.out.println("Before Sorting : " + footballTeam); Collections.sort(footballTeam, byRanking); - System.out.println("After Sorting : " + footballTeam); - assertEquals(footballTeam.get(0) - .getName(), "Steven"); - assertEquals(footballTeam.get(2) - .getRanking(), 67); + assertEquals(footballTeam.get(0).getName(), "Steven"); + assertEquals(footballTeam.get(0).getRanking(), 45); + assertEquals(footballTeam.get(1).getName(), "John"); + assertEquals(footballTeam.get(1).getRanking(), 59); + assertEquals(footballTeam.get(2).getName(), "Roger"); + assertEquals(footballTeam.get(2).getRanking(), 67); } @Test @@ -45,24 +45,24 @@ public class Java8ComparatorUnitTest { System.out.println("********* byRanking *********"); Comparator byRanking = Comparator.comparing(Player::getRanking); - System.out.println("Before Sorting : " + footballTeam); Collections.sort(footballTeam, byRanking); - System.out.println("After Sorting : " + footballTeam); - assertEquals(footballTeam.get(0) - .getName(), "Steven"); - assertEquals(footballTeam.get(2) - .getRanking(), 67); - + assertEquals(footballTeam.get(0).getName(), "Steven"); + assertEquals(footballTeam.get(0).getRanking(), 45); + assertEquals(footballTeam.get(1).getName(), "John"); + assertEquals(footballTeam.get(1).getRanking(), 59); + assertEquals(footballTeam.get(2).getName(), "Roger"); + assertEquals(footballTeam.get(2).getRanking(), 67); + System.out.println("********* byAge *********"); Comparator byAge = Comparator.comparing(Player::getAge); - System.out.println("Before Sorting : " + footballTeam); Collections.sort(footballTeam, byAge); - System.out.println("After Sorting : " + footballTeam); - assertEquals(footballTeam.get(0) - .getName(), "Roger"); - assertEquals(footballTeam.get(2) - .getRanking(), 45); + assertEquals(footballTeam.get(0).getName(), "Roger"); + assertEquals(footballTeam.get(0).getAge(), 20); + assertEquals(footballTeam.get(1).getName(), "John"); + assertEquals(footballTeam.get(1).getAge(), 22); + assertEquals(footballTeam.get(2).getName(), "Steven"); + assertEquals(footballTeam.get(2).getAge(), 24); } } diff --git a/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/indexesofmatches/IndexesOfMatchesUnitTest.java b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/indexesofmatches/IndexesOfMatchesUnitTest.java new file mode 100644 index 0000000000..7fb2afcdea --- /dev/null +++ b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/indexesofmatches/IndexesOfMatchesUnitTest.java @@ -0,0 +1,75 @@ +package com.baeldung.regex.indexesofmatches; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.junit.jupiter.api.Test; + +public class IndexesOfMatchesUnitTest { + private static final String INPUT = "This line contains , , and ."; + + @Test + void whenUsingNorCharClass_thenGetExpectedTexts() { + Pattern pattern = Pattern.compile("<[^>]*>"); + Matcher matcher = pattern.matcher(INPUT); + List result = new ArrayList<>(); + while (matcher.find()) { + result.add(matcher.group()); + } + assertThat(result).containsExactly("", "", ""); + } + + @Test + void whenCallingMatcherEnd_thenGetIndexesAfterTheMatchSequence() { + Pattern pattern = Pattern.compile("456"); + Matcher matcher = pattern.matcher("0123456789"); + String result = null; + int startIdx = -1; + int endIdx = -1; + if (matcher.find()) { + result = matcher.group(); + startIdx = matcher.start(); + endIdx = matcher.end(); + } + assertThat(result).isEqualTo("456"); + assertThat(startIdx).isEqualTo(4); + assertThat(endIdx).isEqualTo(7); + } + + @Test + void whenUsingMatcherStartAndEnd_thenGetIndexesOfMatches() { + Pattern pattern = Pattern.compile("<[^>]*>"); + Matcher matcher = pattern.matcher(INPUT); + List result = new ArrayList<>(); + Map indexesOfMatches = new LinkedHashMap<>(); + while (matcher.find()) { + result.add(matcher.group()); + indexesOfMatches.put(matcher.start(), matcher.end()); + } + assertThat(result).containsExactly("", "", ""); + assertThat(indexesOfMatches.entrySet()).map(entry -> INPUT.substring(entry.getKey(), entry.getValue())) + .containsExactly("", "", ""); + } + + @Test + void whenUsingMatcherStartAndEndWithGroupIdx_thenGetIndexesOfMatches() { + Pattern pattern = Pattern.compile("<([^>]*)>"); + Matcher matcher = pattern.matcher(INPUT); + List result = new ArrayList<>(); + Map indexesOfMatches = new LinkedHashMap<>(); + while (matcher.find()) { + result.add(matcher.group(1)); + indexesOfMatches.put(matcher.start(1), matcher.end(1)); + } + assertThat(result).containsExactly("the first value", "the second value", "the third value"); + + assertThat(indexesOfMatches.entrySet()).map(entry -> INPUT.substring(entry.getKey(), entry.getValue())) + .containsExactly("the first value", "the second value", "the third value"); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/squarebrackets/ExtractTextBetweenSquareBracketsUnitTest.java b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/squarebrackets/ExtractTextBetweenSquareBracketsUnitTest.java new file mode 100644 index 0000000000..2ea80a1f69 --- /dev/null +++ b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/squarebrackets/ExtractTextBetweenSquareBracketsUnitTest.java @@ -0,0 +1,98 @@ +package com.baeldung.regex.squarebrackets; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.junit.jupiter.api.Test; + +import com.google.common.collect.Lists; + +public class ExtractTextBetweenSquareBracketsUnitTest { + static final String INPUT1 = "some text [THE IMPORTANT MESSAGE] something else"; + static final String EXPECTED1 = "THE IMPORTANT MESSAGE"; + + static final String INPUT2 = "[La La Land], [The last Emperor], and [Life of Pi] are all great movies."; + static final List EXPECTED2 = Lists.newArrayList("La La Land", "The last Emperor", "Life of Pi"); + + @Test + void whenUsingDotStarOnInput1_thenGetExpectedResult() { + String result = null; + String rePattern = "\\[(.*)]"; + Pattern p = Pattern.compile(rePattern); + Matcher m = p.matcher(INPUT1); + if (m.find()) { + result = m.group(1); + } + assertThat(result).isEqualTo(EXPECTED1); + } + + @Test + void whenUsingCharClassOnInput1_thenGetExpectedResult() { + String result = null; + String rePattern = "\\[([^]]*)"; + Pattern p = Pattern.compile(rePattern); + Matcher m = p.matcher(INPUT1); + if (m.find()) { + result = m.group(1); + } + assertThat(result).isEqualTo(EXPECTED1); + } + + @Test + void whenUsingSplitOnInput1_thenGetExpectedResult() { + String[] strArray = INPUT1.split("[\\[\\]]", -1); + String result = strArray.length == 3 ? strArray[1] : null; + + assertThat(result).isEqualTo(EXPECTED1); + } + + @Test + void whenUsingSplitWithLimit_thenGetExpectedResult() { + String[] strArray = "[THE IMPORTANT MESSAGE]".split("[\\[\\]]"); + assertThat(strArray).hasSize(2) + .containsExactly("", "THE IMPORTANT MESSAGE"); + + strArray = "[THE IMPORTANT MESSAGE]".split("[\\[\\]]", -1); + assertThat(strArray).hasSize(3) + .containsExactly("", "THE IMPORTANT MESSAGE", ""); + } + + @Test + void whenUsingNonGreedyOnInput2_thenGetExpectedResult() { + List result = new ArrayList<>(); + String rePattern = "\\[(.*?)]"; + Pattern p = Pattern.compile(rePattern); + Matcher m = p.matcher(INPUT2); + while (m.find()) { + result.add(m.group(1)); + } + assertThat(result).isEqualTo(EXPECTED2); + } + + @Test + void whenUsingCharClassOnInput2_thenGetExpectedResult() { + List result = new ArrayList<>(); + String rePattern = "\\[([^]]*)"; + Pattern p = Pattern.compile(rePattern); + Matcher m = p.matcher(INPUT2); + while (m.find()) { + result.add(m.group(1)); + } + assertThat(result).isEqualTo(EXPECTED2); + } + + @Test + void whenUsingSplitInput2_thenGetExpectedResult() { + List result = new ArrayList<>(); + String[] strArray = INPUT2.split("[\\[\\]]", -1); + for (int i = 1; i < strArray.length; i += 2) { + result.add(strArray[i]); + } + assertThat(result).isEqualTo(EXPECTED2); + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-security-3/pom.xml b/core-java-modules/core-java-security-3/pom.xml index ad9feeb36a..b979b56658 100644 --- a/core-java-modules/core-java-security-3/pom.xml +++ b/core-java-modules/core-java-security-3/pom.xml @@ -33,7 +33,7 @@ com.google.guava guava - ${google-guava.version} + ${guava.version} org.springframework.security @@ -47,7 +47,6 @@ 1.15 2.3.1 6.0.3 - 31.0.1-jre \ No newline at end of file diff --git a/core-java-modules/core-java-streams-4/pom.xml b/core-java-modules/core-java-streams-4/pom.xml index fe791ebd42..0b9b3569f1 100644 --- a/core-java-modules/core-java-streams-4/pom.xml +++ b/core-java-modules/core-java-streams-4/pom.xml @@ -39,7 +39,7 @@ org.assertj assertj-core - 3.23.1 + ${assertj.version} test @@ -55,7 +55,7 @@ org.apache.commons commons-lang3 - 3.12.0 + ${commons-lang3.version} test @@ -81,7 +81,7 @@ com.google.guava guava - ${google.guava.version} + ${guava.version} com.oath.cyclops @@ -114,6 +114,7 @@ + 3.23.1 3.1 12 12 @@ -123,7 +124,6 @@ 1.0.0-alpha-4 3.5.1 4.4 - 31.1-jre 10.4.1 diff --git a/core-java-modules/core-java-streams/pom.xml b/core-java-modules/core-java-streams/pom.xml index b0794829c2..c7282dda21 100644 --- a/core-java-modules/core-java-streams/pom.xml +++ b/core-java-modules/core-java-streams/pom.xml @@ -14,33 +14,6 @@ - - org.openjdk.jmh - jmh-core - ${jmh-core.version} - - - org.openjdk.jmh - jmh-generator-annprocess - ${jmh-generator.version} - provided - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - log4j - log4j - ${log4j.version} - - - org.projectlombok - lombok - ${lombok.version} - provided - com.codepoetics protonpack @@ -57,17 +30,7 @@ ${streamex.version} - org.aspectj - aspectjrt - ${asspectj.version} - - - org.aspectj - aspectjweaver - ${asspectj.version} - - - pl.touk + com.pivovarit throwing-function ${throwing-function.version} @@ -96,15 +59,13 @@ - 0.9.0 - 1.15 - 0.6.5 - 2.10 - 1.3 - 1.8.9 - 3.1 - 1.8 - 1.8 + 0.10.4 + 1.16 + 0.8.1 + 1.5.1 + + 17 + 17 \ No newline at end of file diff --git a/core-java-modules/core-java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java b/core-java-modules/core-java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java index 5ad875f61e..3f3922e015 100644 --- a/core-java-modules/core-java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java +++ b/core-java-modules/core-java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java @@ -1,8 +1,8 @@ package com.baeldung.stream.filter; import org.junit.jupiter.api.Test; -import pl.touk.throwing.ThrowingPredicate; -import pl.touk.throwing.exception.WrappedException; +import com.pivovarit.function.ThrowingPredicate; +import com.pivovarit.function.exception.WrappedException; import java.io.IOException; import java.util.Arrays; @@ -156,5 +156,4 @@ public class StreamFilterUnitTest { }) .collect(Collectors.toList())).isInstanceOf(RuntimeException.class); } - -} +} \ No newline at end of file diff --git a/core-java-modules/core-java-string-apis-2/pom.xml b/core-java-modules/core-java-string-apis-2/pom.xml index 51ab3bc1f8..814d301532 100644 --- a/core-java-modules/core-java-string-apis-2/pom.xml +++ b/core-java-modules/core-java-string-apis-2/pom.xml @@ -22,7 +22,7 @@ org.apache.commons commons-lang3 - ${commons.lang3.version} + ${commons-lang3.version} com.ibm.icu @@ -42,8 +42,6 @@ - 3.12.0 - 31.1-jre 61.1 diff --git a/core-java-modules/core-java-string-conversions-3/README.md b/core-java-modules/core-java-string-conversions-3/README.md index 96799d1660..4b348dd555 100644 --- a/core-java-modules/core-java-string-conversions-3/README.md +++ b/core-java-modules/core-java-string-conversions-3/README.md @@ -1,3 +1,4 @@ ## Relevant Articles - [Object.toString() vs String.valueOf()](https://www.baeldung.com/java-object-tostring-vs-string-valueof) - [Convert String to Int Using Encapsulation](https://www.baeldung.com/java-encapsulation-convert-string-to-int) +- [HashMap with Multiple Values for the Same Key](https://www.baeldung.com/java-hashmap-multiple-values-per-key) diff --git a/di-modules/avaje/README.md b/di-modules/avaje/README.md index ea914e551f..f0fa9f058e 100644 --- a/di-modules/avaje/README.md +++ b/di-modules/avaje/README.md @@ -4,4 +4,4 @@ This module contains articles about Avaje ### Relevant articles: -- [Introduction to Avaje Inject](https://www.baeldung.com/avaje-inject/intro) \ No newline at end of file +- [Introduction to Avaje Inject](https://www.baeldung.com/avaje-inject) diff --git a/docker-modules/docker-caching/multi-module-caching/pom.xml b/docker-modules/docker-caching/multi-module-caching/pom.xml index b64cf1a8b8..60f14a17e5 100644 --- a/docker-modules/docker-caching/multi-module-caching/pom.xml +++ b/docker-modules/docker-caching/multi-module-caching/pom.xml @@ -27,7 +27,7 @@ UTF-8 1.8 - 31.1-jre + 32.1.2-jre \ No newline at end of file diff --git a/docker-modules/docker-caching/single-module-caching/pom.xml b/docker-modules/docker-caching/single-module-caching/pom.xml index a388c7563f..0e5174b7ca 100644 --- a/docker-modules/docker-caching/single-module-caching/pom.xml +++ b/docker-modules/docker-caching/single-module-caching/pom.xml @@ -49,7 +49,7 @@ 8 8 UTF-8 - 31.1-jre + 32.1.2-jre \ No newline at end of file diff --git a/jackson-modules/jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java b/jackson-modules/jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java index 047d53ab62..4dc9e40292 100644 --- a/jackson-modules/jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java +++ b/jackson-modules/jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java @@ -20,6 +20,7 @@ import org.joda.time.DateTimeZone; import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.util.StdDateFormat; @@ -67,6 +68,8 @@ public class JacksonDateUnitTest { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.findAndRegisterModules(); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + objectMapper.enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID); + objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE); ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC")); String converted = objectMapper.writeValueAsString(now); diff --git a/javaxval-2/README.md b/javaxval-2/README.md index b7603d9e84..3ae53ebd94 100644 --- a/javaxval-2/README.md +++ b/javaxval-2/README.md @@ -7,4 +7,5 @@ This module contains articles about Bean Validation. - [Guide to ParameterMessageInterpolator](https://www.baeldung.com/hibernate-parametermessageinterpolator) - [Hibernate Validator Annotation Processor in Depth](https://www.baeldung.com/hibernate-validator-annotation-processor) - [Object Validation After Deserialization](https://www.baeldung.com/java-object-validation-deserialization) +- [Java Validation List Annotations](https://www.baeldung.com/java-validation-list-annotations) - More articles: [[<-- prev]](../javaxval) diff --git a/javaxval/README.md b/javaxval/README.md index 7420580f8e..be8c3b51b7 100644 --- a/javaxval/README.md +++ b/javaxval/README.md @@ -3,7 +3,7 @@ This module contains articles about Bean Validation. ### Relevant Articles: -- [Java Bean Validation Basics](https://www.baeldung.com/javax-validation) +- [Java Bean Validation Basics](https://www.baeldung.com/java-validation) - [Validating Container Elements with Jakarta Bean Validation 3.0](https://www.baeldung.com/bean-validation-container-elements) - [Validations for Enum Types](https://www.baeldung.com/javax-validations-enums) - [Javax BigDecimal Validation](https://www.baeldung.com/javax-bigdecimal-validation) diff --git a/json-modules/json-2/README.md b/json-modules/json-2/README.md index a29484d9fc..f41a7047fa 100644 --- a/json-modules/json-2/README.md +++ b/json-modules/json-2/README.md @@ -13,5 +13,6 @@ This module contains articles about JSON. - [Getting a Value in JSONObject](https://www.baeldung.com/java-jsonobject-get-value) - [Pretty-Print a JSON in Java](https://www.baeldung.com/java-json-pretty-print) - [Remove Whitespaces From a JSON in Java](https://www.baeldung.com/java-json-minify-remove-whitespaces) +- [Programmatic Generation of JSON Schemas in Java](https://www.baeldung.com/java-json-schema-create-automatically) - More Articles: [[<-- prev]](/json-modules/json) diff --git a/json-modules/json-2/pom.xml b/json-modules/json-2/pom.xml index 82fe689ebf..7253088516 100644 --- a/json-modules/json-2/pom.xml +++ b/json-modules/json-2/pom.xml @@ -1,7 +1,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"> 4.0.0 json-2 0.0.1-SNAPSHOT @@ -123,6 +123,26 @@ javax.annotation-api 1.3.2 + + com.github.victools + jsonschema-generator + ${jsonschema-generator.version} + + + com.github.victools + jsonschema-module-jackson + ${jsonschema-generator.version} + + + jakarta.validation + jakarta.validation-api + ${jakarta.validation.version} + + + com.github.victools + jsonschema-module-jakarta-validation + ${jsonschema-generator.version} + @@ -162,6 +182,55 @@ + + + com.github.victools + jsonschema-maven-plugin + ${jsonschema-generator.version} + + + + generate + + + + + + com.baeldung.jsonschemageneration.plugin + + + com.baeldung.jsonschemageneration.plugin.Person + + DRAFT_2020_12 + src/main/resources/schemas + {1}/{0}.json + true + + PLAIN_JSON + + + + + SCHEMA_VERSION_INDICATOR + + + + Jackson + + + + + + JakartaValidation + + + + + + + + + @@ -173,6 +242,8 @@ 0.1.1 0.4.2 0.13.0 + 4.31.1 + 3.0.2 \ No newline at end of file diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/AdvancedArticle.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/AdvancedArticle.java new file mode 100644 index 0000000000..07e00bba0f --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/AdvancedArticle.java @@ -0,0 +1,70 @@ +package com.baeldung.jsonschemageneration.configuration; + +import java.sql.Timestamp; +import java.util.Date; +import java.util.UUID; + +enum Area { + JAVA("JAVA"), KOTLIN("KOTLIN"), SCALA("SCALA"), LINUX("LINUX"); + + private final String area; + + Area(String area) { + this.area = area; + } + + public String getArea() { + return area; + } +} + +public class AdvancedArticle { + private UUID id; + private String title; + + private String content; + + @AllowedTypes({ Timestamp.class, Date.class }) + private Object createdAt; + private Area area; + + public Area getArea() { + return area; + } + + public void setArea(Area area) { + this.area = area; + } + + public Object getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Object createdAt) { + this.createdAt = createdAt; + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/AdvancedConfigurationSchemaGenerator.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/AdvancedConfigurationSchemaGenerator.java new file mode 100644 index 0000000000..3fbf6bac6e --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/AdvancedConfigurationSchemaGenerator.java @@ -0,0 +1,39 @@ +package com.baeldung.jsonschemageneration.configuration; + +import com.fasterxml.jackson.databind.JsonNode; +import com.github.victools.jsonschema.generator.Option; +import com.github.victools.jsonschema.generator.OptionPreset; +import com.github.victools.jsonschema.generator.SchemaGenerator; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfig; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; +import com.github.victools.jsonschema.generator.SchemaVersion; + +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class AdvancedConfigurationSchemaGenerator { + public static void main(String[] args) { + + SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON); + + configBuilder.forFields().withInstanceAttributeOverride((node, field, context) -> node.put("readOnly", field.getDeclaredType().isInstanceOf(UUID.class))); + + configBuilder.forFields() + .withTargetTypeOverridesResolver(field -> Optional.ofNullable(field.getAnnotationConsideringFieldAndGetterIfSupported(AllowedTypes.class)) + .map(AllowedTypes::value) + .map(Stream::of) + .map(stream -> stream.map(subtype -> field.getContext().resolve(subtype))) + .map(stream -> stream.collect(Collectors.toList())) + .orElse(null)); + + SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES).build(); + + SchemaGenerator generator = new SchemaGenerator(config); + JsonNode jsonSchema = generator.generateSchema(AdvancedArticle.class); + + System.out.println(jsonSchema.toPrettyString()); + } + +} diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/AllowedTypes.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/AllowedTypes.java new file mode 100644 index 0000000000..9c2ae2780d --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/AllowedTypes.java @@ -0,0 +1,13 @@ +package com.baeldung.jsonschemageneration.configuration; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface AllowedTypes { + Class[] value(); +} + diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/IndividualConfigurationSchemaGenerator.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/IndividualConfigurationSchemaGenerator.java new file mode 100644 index 0000000000..3e1b556311 --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/configuration/IndividualConfigurationSchemaGenerator.java @@ -0,0 +1,44 @@ +package com.baeldung.jsonschemageneration.configuration; + +import com.baeldung.jsonschemageneration.recursive.Author; +import com.fasterxml.jackson.databind.JsonNode; +import com.github.victools.jsonschema.generator.Option; +import com.github.victools.jsonschema.generator.OptionPreset; +import com.github.victools.jsonschema.generator.SchemaGenerator; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfig; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; +import com.github.victools.jsonschema.generator.SchemaVersion; + +import javax.annotation.Nullable; +import javax.validation.constraints.NotNull; +import java.time.Instant; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class IndividualConfigurationSchemaGenerator { + public static void main(String[] args) { + + SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON); + + configBuilder.forFields().withRequiredCheck(field -> field.getAnnotationConsideringFieldAndGetter(Nullable.class) == null).withArrayUniqueItemsResolver(scope -> scope.getType().getErasedType() == (List.class) ? true : null); + + configBuilder.forMethods().withRequiredCheck(method -> method.getAnnotationConsideringFieldAndGetter(NotNull.class) != null); + + configBuilder.forTypesInGeneral() + .withArrayUniqueItemsResolver(scope -> scope.getType().getErasedType() == (List.class) ? true : null) + .withDefaultResolver(scope -> scope.getType().getErasedType() == List.class ? Collections.EMPTY_LIST : null) + .withDefaultResolver(scope -> scope.getType().getErasedType() == Date.class ? Date.from(Instant.now()) : null) + .withEnumResolver(scope -> scope.getType().getErasedType().isEnum() ? Stream.of(scope.getType().getErasedType().getEnumConstants()).map(v -> ((Enum) v).name()).collect(Collectors.toList()) : null); + + SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES).build(); + + SchemaGenerator generator = new SchemaGenerator(config); + JsonNode jsonSchema = generator.generateSchema(Author.class); + + System.out.println(jsonSchema.toPrettyString()); + } + +} diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/modules/JacksonModuleSchemaGenerator.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/modules/JacksonModuleSchemaGenerator.java new file mode 100644 index 0000000000..eda5015400 --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/modules/JacksonModuleSchemaGenerator.java @@ -0,0 +1,68 @@ +package com.baeldung.jsonschemageneration.modules; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.JsonNode; +import com.github.victools.jsonschema.generator.SchemaGenerator; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; +import com.github.victools.jsonschema.module.jackson.JacksonModule; + +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import static com.github.victools.jsonschema.generator.Option.EXTRA_OPEN_API_FORMAT_VALUES; +import static com.github.victools.jsonschema.generator.OptionPreset.PLAIN_JSON; +import static com.github.victools.jsonschema.generator.SchemaVersion.DRAFT_2020_12; +import static com.github.victools.jsonschema.module.jackson.JacksonOption.RESPECT_JSONPROPERTY_REQUIRED; + +public class JacksonModuleSchemaGenerator { + public static void main(String[] args) { + + JacksonModule module = new JacksonModule(RESPECT_JSONPROPERTY_REQUIRED); + SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(DRAFT_2020_12, PLAIN_JSON).with(module).with(EXTRA_OPEN_API_FORMAT_VALUES); + + SchemaGenerator generator = new SchemaGenerator(configBuilder.build()); + JsonNode jsonSchema = generator.generateSchema(Person.class); + + System.out.println(jsonSchema.toPrettyString()); + } + + static class Person { + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + UUID id; + + @JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true) + String name; + + @JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true) + String surname; + + @JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true) + Address address; + + @JsonIgnore + String fullName; + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + Date createdAt; + + @JsonProperty(access = JsonProperty.Access.READ_WRITE) + List friends; + + } + + static class Address { + + @JsonProperty() + String street; + + @JsonProperty(required = true) + String city; + + @JsonProperty(required = true) + String country; + } + +} diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/modules/JakartaValidationModuleSchemaGenerator.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/modules/JakartaValidationModuleSchemaGenerator.java new file mode 100644 index 0000000000..ca19d35357 --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/modules/JakartaValidationModuleSchemaGenerator.java @@ -0,0 +1,63 @@ +package com.baeldung.jsonschemageneration.modules; + +import com.fasterxml.jackson.databind.JsonNode; +import com.github.victools.jsonschema.generator.SchemaGenerator; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; +import com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationModule; +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Null; +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Size; + +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import static com.github.victools.jsonschema.generator.OptionPreset.PLAIN_JSON; +import static com.github.victools.jsonschema.generator.SchemaVersion.DRAFT_2020_12; +import static com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationOption.INCLUDE_PATTERN_EXPRESSIONS; +import static com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationOption.NOT_NULLABLE_FIELD_IS_REQUIRED; + +public class JakartaValidationModuleSchemaGenerator { + public static void main(String[] args) { + + JakartaValidationModule module = new JakartaValidationModule(NOT_NULLABLE_FIELD_IS_REQUIRED, INCLUDE_PATTERN_EXPRESSIONS); + SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(DRAFT_2020_12, PLAIN_JSON).with(module); + + SchemaGenerator generator = new SchemaGenerator(configBuilder.build()); + JsonNode jsonSchema = generator.generateSchema(Person.class); + + System.out.println(jsonSchema.toPrettyString()); + } + + static class Person { + + @NotNull UUID id; + + @NotNull String name; + + @NotNull @Email @Pattern(regexp = "\\b[A-Za-z0-9._%+-]+@baeldung\\.com\\b") String email; + + @NotNull String surname; + + @NotNull Address address; + + @Null String fullName; + + @NotNull Date createdAt; + + @Size(max = 10) List friends; + + } + + static class Address { + + @Null String street; + + @NotNull String city; + + @NotNull String country; + } + +} diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/plugin/Person.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/plugin/Person.java new file mode 100644 index 0000000000..f87dcd5981 --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/plugin/Person.java @@ -0,0 +1,61 @@ +package com.baeldung.jsonschemageneration.plugin; + +import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Null; +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Size; + +import java.util.Date; +import java.util.List; +import java.util.UUID; + +public class Person { + + @NotNull + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + UUID id; + + @NotNull + @JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true) + String name; + + @NotNull + @JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true) + @Email @Pattern(regexp = "\\b[A-Za-z0-9._%+-]+@baeldung\\.com\\b") String email; + + @NotNull + @JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true) + + String surname; + + @NotNull + @JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true) + Address address; + + @Null String fullName; + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + Date createdAt; + + @Size(max = 10) + @JsonProperty(access = JsonProperty.Access.READ_WRITE) + List friends; + +} + +class Address { + + @Null + @JsonProperty + String street; + + @NotNull + @JsonProperty(required = true) + String city; + + @NotNull + @JsonProperty(required = true) + String country; +} \ No newline at end of file diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/recursive/Author.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/recursive/Author.java new file mode 100644 index 0000000000..a19afb3f6f --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/recursive/Author.java @@ -0,0 +1,12 @@ +package com.baeldung.jsonschemageneration.recursive; + +import java.util.List; +import java.util.UUID; + +public class Author { + private UUID id; + private String name; + private String role; + + private List articles; +} diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/recursive/AuthoredArticle.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/recursive/AuthoredArticle.java new file mode 100644 index 0000000000..3ab7464aaf --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/recursive/AuthoredArticle.java @@ -0,0 +1,68 @@ +package com.baeldung.jsonschemageneration.recursive; + +import java.util.Date; +import java.util.UUID; + +enum Area { + JAVA("JAVA"), KOTLIN("KOTLIN"), SCALA("SCALA"), LINUX("LINUX"); + + private final String area; + + Area(String area) { + this.area = area; + } + + public String getArea() { + return area; + } +} + +public class AuthoredArticle { + private UUID id; + private String title; + private String content; + private Date createdAt; + private Area area; + + private Author author; + + public Area getArea() { + return area; + } + + public void setArea(Area area) { + this.area = area; + } + + public Date getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/recursive/RecursiveSchemaGenerator.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/recursive/RecursiveSchemaGenerator.java new file mode 100644 index 0000000000..2047dac53f --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/recursive/RecursiveSchemaGenerator.java @@ -0,0 +1,23 @@ +package com.baeldung.jsonschemageneration.recursive; + +import com.fasterxml.jackson.databind.JsonNode; +import com.github.victools.jsonschema.generator.Option; +import com.github.victools.jsonschema.generator.OptionPreset; +import com.github.victools.jsonschema.generator.SchemaGenerator; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfig; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; +import com.github.victools.jsonschema.generator.SchemaVersion; + +public class RecursiveSchemaGenerator { + public static void main(String[] args) { + + SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON); + SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES).without(Option.FLATTENED_ENUMS_FROM_TOSTRING).build(); + + SchemaGenerator generator = new SchemaGenerator(config); + JsonNode jsonSchema = generator.generateSchema(AuthoredArticle.class); + + System.out.println(jsonSchema.toPrettyString()); + } + +} diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/simple/Article.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/simple/Article.java new file mode 100644 index 0000000000..480dc2e425 --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/simple/Article.java @@ -0,0 +1,66 @@ +package com.baeldung.jsonschemageneration.simple; + +import java.util.Date; +import java.util.UUID; + +enum Area { + JAVA("JAVA"), KOTLIN("KOTLIN"), SCALA("SCALA"), LINUX("LINUX"); + + private final String area; + + Area(String area) { + this.area = area; + } + + public String getArea() { + return area; + } +} + +public class Article { + private UUID id; + private String title; + private String content; + private Date createdAt; + private Area area; + + public Area getArea() { + return area; + } + + public void setArea(Area area) { + this.area = area; + } + + public Date getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/simple/SimpleSchemaGenerator.java b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/simple/SimpleSchemaGenerator.java new file mode 100644 index 0000000000..daea643b1b --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonschemageneration/simple/SimpleSchemaGenerator.java @@ -0,0 +1,23 @@ +package com.baeldung.jsonschemageneration.simple; + +import com.fasterxml.jackson.databind.JsonNode; +import com.github.victools.jsonschema.generator.Option; +import com.github.victools.jsonschema.generator.OptionPreset; +import com.github.victools.jsonschema.generator.SchemaGenerator; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfig; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; +import com.github.victools.jsonschema.generator.SchemaVersion; + +public class SimpleSchemaGenerator { + public static void main(String[] args) { + + SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON); + SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES).without(Option.FLATTENED_ENUMS_FROM_TOSTRING).build(); + + SchemaGenerator generator = new SchemaGenerator(config); + JsonNode jsonSchema = generator.generateSchema(Article.class); + + System.out.println(jsonSchema.toPrettyString()); + } + +} diff --git a/json-modules/json-2/src/main/resources/schemas/com/baeldung/jsonschemageneration/plugin/Person.json b/json-modules/json-2/src/main/resources/schemas/com/baeldung/jsonschemageneration/plugin/Person.json new file mode 100644 index 0000000000..d52a527684 --- /dev/null +++ b/json-modules/json-2/src/main/resources/schemas/com/baeldung/jsonschemageneration/plugin/Person.json @@ -0,0 +1,54 @@ +{ + "$defs" : { + "Address" : { + "type" : "object", + "properties" : { + "city" : { + "type" : "string" + }, + "country" : { + "type" : "string" + }, + "street" : { + "type" : [ "string", "null" ] + } + }, + "required" : [ "city", "country" ], + "additionalProperties" : false + } + }, + "type" : "object", + "properties" : { + "address" : { + "$ref" : "#/$defs/Address" + }, + "createdAt" : { + "type" : "string", + "readOnly" : true + }, + "email" : { + "type" : "string", + "format" : "email", + "pattern" : "\\b[A-Za-z0-9._%+-]+@baeldung\\.com\\b" + }, + "friends" : { + "maxItems" : 10, + "type" : "array", + "items" : { + "$ref" : "#" + } + }, + "id" : { + "type" : "string", + "readOnly" : true + }, + "name" : { + "type" : "string" + }, + "surname" : { + "type" : "string" + } + }, + "required" : [ "address", "email", "id", "name", "surname" ], + "additionalProperties" : false +} \ No newline at end of file diff --git a/json-modules/json-arrays/README.md b/json-modules/json-arrays/README.md new file mode 100644 index 0000000000..f119467046 --- /dev/null +++ b/json-modules/json-arrays/README.md @@ -0,0 +1,2 @@ +## Relevant Articles +- [How to Check if a Value Exists in a JSON Array for a Particular Key](https://www.baeldung.com/java-json-array-check-key-value-pair) diff --git a/json-modules/json-arrays/pom.xml b/json-modules/json-arrays/pom.xml new file mode 100644 index 0000000000..0eefbc86fc --- /dev/null +++ b/json-modules/json-arrays/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + org.baeldung + json-arrays + json-arrays + + + com.baeldung + json-modules + 1.0.0-SNAPSHOT + + + + + org.json + json + ${json.version} + + + com.google.code.gson + gson + ${gson.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + javax.json.bind + javax.json.bind-api + ${jsonb-api.version} + + + + + 1.0 + 20230227 + 2.8.5 + 1.1.2 + 2.28.0 + + + diff --git a/json-modules/json-arrays/src/test/java/com.baeldung.checkforkey/CheckForKeyUnitTest.java b/json-modules/json-arrays/src/test/java/com.baeldung.checkforkey/CheckForKeyUnitTest.java new file mode 100644 index 0000000000..6e1cbb7ab0 --- /dev/null +++ b/json-modules/json-arrays/src/test/java/com.baeldung.checkforkey/CheckForKeyUnitTest.java @@ -0,0 +1,47 @@ +package com.baeldung.checkforkey; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Objects; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; + +public class CheckForKeyUnitTest { + + private final String exampleJson = "[{\"colour\":\"red\"},{\"colour\":\"blue\"},{\"colour\":\"green\"}]"; + + @Test + public void givenJsonArray_whenUsingJackson_thenDetectKeyInArray() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode tree = objectMapper.readTree(exampleJson); + + Stream s = StreamSupport.stream(tree.spliterator(), false); + boolean result = s.map(entry -> entry.get("colour")) + .filter(Objects::nonNull) + .anyMatch(colour -> "green".equals(colour.asText())); + assertTrue(result); + } + + @Test + public void givenJsonArray_whenUsingGson_thenDetectKeyInArray() { + Gson gson = new Gson(); + JsonArray parsed = gson.fromJson(exampleJson, JsonArray.class); + + Stream s = StreamSupport.stream(parsed.spliterator(), false); + boolean result = s.map(entry -> entry.getAsJsonObject() + .get("colour")) + .filter(Objects::nonNull) + .anyMatch(colour -> "green".equals(colour.getAsString())); + assertTrue(result); + } + +} diff --git a/json-modules/pom.xml b/json-modules/pom.xml index 82314e0edf..306b404049 100644 --- a/json-modules/pom.xml +++ b/json-modules/pom.xml @@ -16,6 +16,7 @@ json json-2 + json-arrays json-conversion json-path gson diff --git a/kubernetes-modules/README.md b/kubernetes-modules/README.md new file mode 100644 index 0000000000..cb52c12465 --- /dev/null +++ b/kubernetes-modules/README.md @@ -0,0 +1,2 @@ +## Relevant Articles +- [Guide to Eclipse JKube](https://www.baeldung.com/ops/eclipse-jkube) diff --git a/lombok-modules/lombok-2/pom.xml b/lombok-modules/lombok-2/pom.xml index 88fbaf530f..ea562ad896 100644 --- a/lombok-modules/lombok-2/pom.xml +++ b/lombok-modules/lombok-2/pom.xml @@ -25,10 +25,58 @@ jackson-databind ${jackson.version} + + com.google.code.gson + gson + ${gson.version} + + + javax.annotation + javax.annotation-api + ${javax.annotation} + + + io.swagger + swagger-annotations + ${swagger.annotation} + org.springframework.boot spring-boot-starter-web + + + + org.openapitools + openapi-generator-maven-plugin + ${openapi.version} + + + + generate + + + ${project.basedir}/src/main/resources/bookapi.yml + java + com.baeldung.openapi.model + + @lombok.Data @lombok.NoArgsConstructor @lombok.AllArgsConstructor + + false + false + false + + + + + + + + 4.2.3 + 2.10.1 + 1.3.2 + 1.6.2 + \ No newline at end of file diff --git a/lombok-modules/lombok-2/src/main/java/com/baeldung/lombok/openapiandlombook/Book.java b/lombok-modules/lombok-2/src/main/java/com/baeldung/lombok/openapiandlombook/Book.java new file mode 100644 index 0000000000..2da2e74a40 --- /dev/null +++ b/lombok-modules/lombok-2/src/main/java/com/baeldung/lombok/openapiandlombook/Book.java @@ -0,0 +1,12 @@ +package com.baeldung.lombok.openapiandlombook; + +@lombok.Data +@lombok.NoArgsConstructor +@lombok.AllArgsConstructor +public class Book { + + private Long id; + private String name; + private String author; + +} diff --git a/lombok-modules/lombok-2/src/main/resources/bookapi.yml b/lombok-modules/lombok-2/src/main/resources/bookapi.yml new file mode 100644 index 0000000000..e9b63b2f85 --- /dev/null +++ b/lombok-modules/lombok-2/src/main/resources/bookapi.yml @@ -0,0 +1,39 @@ +openapi: 3.0.2 +info: + version: 1.0.0 + title: Book Store + license: + name: MIT +paths: + /books: + get: + tags: + - book + summary: Get All Books + responses: + 200: + description: successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/Book' + + 404: + description: Book not found + content: { } +components: + schemas: + Book: + type: object + required: + - id + - name + - author + properties: + id: + type: integer + format: int64 + name: + type: string + author: + type: string diff --git a/lombok-modules/lombok/pom.xml b/lombok-modules/lombok/pom.xml index 6ba90f33b5..a04233bc2b 100644 --- a/lombok-modules/lombok/pom.xml +++ b/lombok-modules/lombok/pom.xml @@ -43,7 +43,6 @@ - edge-SNAPSHOT 1.0.0.Final 1.18.20.0 23.0.0 diff --git a/lombok-modules/pom.xml b/lombok-modules/pom.xml index 5100ed1790..46d91462fb 100644 --- a/lombok-modules/pom.xml +++ b/lombok-modules/pom.xml @@ -16,7 +16,7 @@ - + lombok lombok-2 diff --git a/mapstruct/src/main/java/com/baeldung/expression/dto/LicenseDto.java b/mapstruct/src/main/java/com/baeldung/expression/dto/LicenseDto.java new file mode 100644 index 0000000000..cd6e9bad68 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/expression/dto/LicenseDto.java @@ -0,0 +1,21 @@ +package com.baeldung.expression.dto; + +import java.time.LocalDateTime; +import java.util.UUID; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +@AllArgsConstructor +@Data +@Builder +public class LicenseDto { + + private UUID id; + + private LocalDateTime startDate; + + private LocalDateTime endDate; + +} diff --git a/mapstruct/src/main/java/com/baeldung/expression/mapper/LicenseMapper.java b/mapstruct/src/main/java/com/baeldung/expression/mapper/LicenseMapper.java new file mode 100644 index 0000000000..100588b45d --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/expression/mapper/LicenseMapper.java @@ -0,0 +1,43 @@ +package com.baeldung.expression.mapper; + +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; + +import org.mapstruct.AfterMapping; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.MappingTarget; + +import com.baeldung.expression.dto.LicenseDto; +import com.baeldung.expression.model.License; + +@Mapper +public interface LicenseMapper { + + @Mapping(target = "startDate", expression = "java(mapStartDate(licenseDto))") + @Mapping(target = "endDate", ignore = true) + @Mapping(target = "active", constant = "true") + @Mapping(target = "renewalRequired", conditionExpression = "java(isEndDateInTwoWeeks(licenseDto))", source = ".") + License toLicense(LicenseDto licenseDto); + + @AfterMapping + default void afterMapping(LicenseDto licenseDto, @MappingTarget License license) { + OffsetDateTime endDate = licenseDto.getEndDate() != null ? licenseDto.getEndDate() + .atOffset(ZoneOffset.UTC) : OffsetDateTime.now() + .plusYears(1); + license.setEndDate(endDate); + } + + default OffsetDateTime mapStartDate(LicenseDto licenseDto) { + return licenseDto.getStartDate() != null ? licenseDto.getStartDate() + .atOffset(ZoneOffset.UTC) : OffsetDateTime.now(); + } + + default boolean isEndDateInTwoWeeks(LicenseDto licenseDto) { + return licenseDto.getEndDate() != null && Duration.between(licenseDto.getEndDate(), LocalDateTime.now()) + .toDays() <= 14; + } + +} diff --git a/mapstruct/src/main/java/com/baeldung/expression/model/License.java b/mapstruct/src/main/java/com/baeldung/expression/model/License.java new file mode 100644 index 0000000000..9e87be03d4 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/expression/model/License.java @@ -0,0 +1,21 @@ +package com.baeldung.expression.model; + +import java.time.OffsetDateTime; +import java.util.UUID; + +import lombok.Data; + +@Data +public class License { + + private UUID id; + + private OffsetDateTime startDate; + + private OffsetDateTime endDate; + + private boolean active; + + private boolean renewalRequired; + +} diff --git a/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java new file mode 100644 index 0000000000..98f704e61c --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/expression/mapper/LicenseMapperUnitTest.java @@ -0,0 +1,64 @@ +package com.baeldung.expression.mapper; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +import org.junit.jupiter.api.Test; +import org.mapstruct.factory.Mappers; + +import com.baeldung.expression.dto.LicenseDto; +import com.baeldung.expression.model.License; + +class LicenseMapperUnitTest { + + LicenseMapper licenseMapper = Mappers.getMapper(LicenseMapper.class); + + @Test + void givenLicenseDtoWithStartDateAndWithoutEndDate_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithDefaultEndDate() { + License license = licenseMapper.toLicense(LicenseDto.builder() + .startDate(LocalDateTime.now()) + .build()); + assertThat(license).isNotNull(); + assertThat(license.getEndDate() + .toLocalDate()).isEqualTo(LocalDate.now() + .plusYears(1)); + } + + @Test + void givenLicenseDtoWithEndDateAndWithoutStartDate_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithDefaultStartDate() { + License license = licenseMapper.toLicense(LicenseDto.builder() + .endDate(LocalDateTime.now() + .plusYears(2)) + .build()); + assertThat(license).isNotNull(); + assertThat(license.getStartDate() + .toLocalDate()).isEqualTo(LocalDate.now()); + } + + @Test + void givenLicenseDtoWithoutStartDateAndEndDate_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithDefaultDetails() { + License license = licenseMapper.toLicense(LicenseDto.builder() + .build()); + assertThat(license).isNotNull(); + assertThat(license.getStartDate() + .toLocalDate()).isEqualTo(LocalDate.now()); + assertThat(license.getEndDate() + .toLocalDate()).isEqualTo(LocalDate.now() + .plusYears(1)); + assertThat(license.isActive()).isTrue(); + assertThat(license.isRenewalRequired()).isFalse(); + } + + @Test + void givenLicenseDtoWithEndDateInTwoWeeks_WhenMapperMethodIsInvoked_ThenLicenseShouldBePopulatedWithRenewalRequiredSetToTrue() { + License license = licenseMapper.toLicense(LicenseDto.builder() + .endDate(LocalDateTime.now() + .plusDays(10)) + .build()); + assertThat(license).isNotNull(); + assertThat(license.isRenewalRequired()).isTrue(); + } + +} \ No newline at end of file diff --git a/maven-modules/maven-build-optimization/README.md b/maven-modules/maven-build-optimization/README.md new file mode 100644 index 0000000000..c1764b67ac --- /dev/null +++ b/maven-modules/maven-build-optimization/README.md @@ -0,0 +1,2 @@ +## Relevant Articles +- [How to Speed Up Maven Build](https://www.baeldung.com/maven-fast-build) diff --git a/pants/README.md b/pants/README.md new file mode 100644 index 0000000000..a37d2e3d31 --- /dev/null +++ b/pants/README.md @@ -0,0 +1,2 @@ +## Relevant Articles +- [Introduction to the Pants Build Tool](https://www.baeldung.com/ops/pants-build-tool-guide) diff --git a/persistence-modules/core-java-persistence-2/README.md b/persistence-modules/core-java-persistence-2/README.md index afabf9ecb3..f9da4947a7 100644 --- a/persistence-modules/core-java-persistence-2/README.md +++ b/persistence-modules/core-java-persistence-2/README.md @@ -9,3 +9,4 @@ - [Get the Number of Rows in a ResultSet](https://www.baeldung.com/java-resultset-number-of-rows) - [Converting a JDBC ResultSet to JSON in Java](https://www.baeldung.com/java-jdbc-convert-resultset-to-json) - [Guide to MicroStream](https://www.baeldung.com/microstream-intro) +- [Executing SQL Script File in Java](https://www.baeldung.com/java-run-sql-script) diff --git a/persistence-modules/core-java-persistence-2/pom.xml b/persistence-modules/core-java-persistence-2/pom.xml index 0bec7a808f..2013bdb6af 100644 --- a/persistence-modules/core-java-persistence-2/pom.xml +++ b/persistence-modules/core-java-persistence-2/pom.xml @@ -70,6 +70,16 @@ ${assertj.version} test + + org.mybatis + mybatis + ${mybatis.version} + + + org.springframework + spring-jdbc + ${spring-jdbc.version} + @@ -80,6 +90,8 @@ 20220320 07.00.00-MS-GA 2.1.214 + 5.3.29 + 3.5.7 \ No newline at end of file diff --git a/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/script/MyBatisScriptUtility.java b/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/script/MyBatisScriptUtility.java new file mode 100644 index 0000000000..8a9bbb6e20 --- /dev/null +++ b/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/script/MyBatisScriptUtility.java @@ -0,0 +1,13 @@ +package com.baeldung.script; + +import org.apache.ibatis.jdbc.ScriptRunner; +import java.sql.Connection; + +public class MyBatisScriptUtility { + public static void runScript(String path, Connection connection) throws Exception { + ScriptRunner scriptRunner = new ScriptRunner(connection); + scriptRunner.setSendFullScript(false); + scriptRunner.setStopOnError(true); + scriptRunner.runScript(new java.io.FileReader(path)); + } +} diff --git a/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/script/SpringScriptUtility.java b/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/script/SpringScriptUtility.java new file mode 100644 index 0000000000..cf70dfa890 --- /dev/null +++ b/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/script/SpringScriptUtility.java @@ -0,0 +1,29 @@ +package com.baeldung.script; + +import org.springframework.core.io.PathResource; +import org.springframework.core.io.support.EncodedResource; +import org.springframework.jdbc.datasource.init.ScriptUtils; + +import java.sql.Connection; + +public class SpringScriptUtility { + public static void runScript(String path, Connection connection) { + boolean continueOrError = false; + boolean ignoreFailedDrops = false; + String commentPrefix = "--"; + String separator = ";"; + String blockCommentStartDelimiter = "/*"; + String blockCommentEndDelimiter = "*/"; + + ScriptUtils.executeSqlScript( + connection, + new EncodedResource(new PathResource(path)), + continueOrError, + ignoreFailedDrops, + commentPrefix, + separator, + blockCommentStartDelimiter, + blockCommentEndDelimiter + ); + } +} diff --git a/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/script/SqlScriptBatchExecutor.java b/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/script/SqlScriptBatchExecutor.java new file mode 100644 index 0000000000..b9da968abe --- /dev/null +++ b/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/script/SqlScriptBatchExecutor.java @@ -0,0 +1,74 @@ +package com.baeldung.script; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class SqlScriptBatchExecutor { + private static final Log logger = LogFactory.getLog(SqlScriptBatchExecutor.class); + private static final Pattern COMMENT_PATTERN = Pattern.compile("--.*|/\\*(.|[\\r\\n])*?\\*/"); + public static void executeBatchedSQL(String scriptFilePath, Connection connection, int batchSize) throws Exception { + List sqlStatements = parseSQLScript(scriptFilePath); + executeSQLBatches(connection, sqlStatements, batchSize); + } + private static List parseSQLScript(String scriptFilePath) throws IOException { + List sqlStatements = new ArrayList<>(); + + try (BufferedReader reader = new BufferedReader(new FileReader(scriptFilePath))) { + StringBuilder currentStatement = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + Matcher commentMatcher = COMMENT_PATTERN.matcher(line); + line = commentMatcher.replaceAll(""); + line = line.trim(); + + if (line.isEmpty()) { + continue; + } + + currentStatement.append(line).append(" "); + + if (line.endsWith(";")) { + sqlStatements.add(currentStatement.toString()); + logger.info(currentStatement.toString()); + currentStatement.setLength(0); + } + } + } catch (IOException e) { + throw e; + } + return sqlStatements; + } + + private static void executeSQLBatches(Connection connection, List sqlStatements, int batchSize) + throws SQLException { + int count = 0; + Statement statement = connection.createStatement(); + + for (String sql : sqlStatements) { + statement.addBatch(sql); + count++; + + if (count % batchSize == 0) { + logger.info("Executing batch"); + statement.executeBatch(); + statement.clearBatch(); + } + } + // Execute any remaining statements + if (count % batchSize != 0) { + statement.executeBatch(); + } + connection.commit(); + } +} diff --git a/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/script/MyBatisScriptUtilityUnitTest.java b/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/script/MyBatisScriptUtilityUnitTest.java new file mode 100644 index 0000000000..98f82cfbd5 --- /dev/null +++ b/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/script/MyBatisScriptUtilityUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.script; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.jupiter.api.AfterAll; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; + +public class MyBatisScriptUtilityUnitTest { + private static final Log logger = LogFactory.getLog(MyBatisScriptUtilityUnitTest.class); + private static Connection connection = null; + private static final String JDBC_URL = "jdbc:h2:mem:testdb1"; + private static final String USERNAME = "user"; + private static final String PASSWORD = "password"; + + @Before + public void prepareConnection() throws Exception { + connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD); + } + + @AfterAll + public static void closeConnection() throws Exception { + connection.close(); + } + + @Test + public void givenConnectionObject_whenSQLFile_thenExecute() throws Exception { + String path = new File(ClassLoader.getSystemClassLoader() + .getResource("employee.sql").getFile()).toPath().toString(); + MyBatisScriptUtility.runScript(path, connection); + + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT COUNT(1) FROM employees"); + if (resultSet.next()) { + int count = resultSet.getInt(1); + Assert.assertEquals("Incorrect number of records inserted", 20, count); + } + } +} diff --git a/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/script/SpringScriptUtilityUnitTest.java b/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/script/SpringScriptUtilityUnitTest.java new file mode 100644 index 0000000000..be6e39ca31 --- /dev/null +++ b/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/script/SpringScriptUtilityUnitTest.java @@ -0,0 +1,47 @@ +package com.baeldung.script; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.jupiter.api.AfterAll; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; + +public class SpringScriptUtilityUnitTest { + private static final Log logger = LogFactory.getLog(SpringScriptUtilityUnitTest.class); + + private static Connection connection = null; + private static final String JDBC_URL = "jdbc:h2:mem:testdb2"; + private static final String USERNAME = "user"; + private static final String PASSWORD = "password"; + + @Before + public void prepareConnection() throws Exception { + connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD); + } + + @AfterAll + public static void closeConnection() throws Exception { + connection.close(); + } + + @Test + public void givenConnectionObject_whenSQLFile_thenExecute() throws Exception { + String path = new File(ClassLoader.getSystemClassLoader() + .getResource("employee.sql").getFile()).toPath().toString(); + SpringScriptUtility.runScript(path, connection); + + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT COUNT(1) FROM employees"); + if (resultSet.next()) { + int count = resultSet.getInt(1); + Assert.assertEquals("Incorrect number of records inserted", 20, count); + } + } +} diff --git a/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/script/SqlScriptBatchExecutorUnitTest.java b/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/script/SqlScriptBatchExecutorUnitTest.java new file mode 100644 index 0000000000..3b1210fd00 --- /dev/null +++ b/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/script/SqlScriptBatchExecutorUnitTest.java @@ -0,0 +1,47 @@ +package com.baeldung.script; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.jupiter.api.AfterAll; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; + +public class SqlScriptBatchExecutorUnitTest { + private static final Log logger = LogFactory.getLog(SqlScriptBatchExecutorUnitTest.class); + private static Connection connection = null; + private static final String JDBC_URL = "jdbc:h2:mem:testdb3"; + private static final String USERNAME = "user"; + private static final String PASSWORD = "password"; + + @Before + public void prepareConnection() throws Exception { + connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD); + } + + @AfterAll + public static void closeConnection() throws Exception { + connection.close(); + } + + @Test + public void givenConnectionObject_whenSQLFile_thenExecute() throws Exception { + String path = new File(ClassLoader.getSystemClassLoader() + .getResource("employee.sql").getFile()).toPath().toString(); + SqlScriptBatchExecutor.executeBatchedSQL(path, connection, 10); + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT COUNT(1) FROM employees"); + + if (resultSet.next()) { + int count = resultSet.getInt(1); + Assert.assertEquals("Incorrect number of records inserted", 20, count); + } + } + +} diff --git a/persistence-modules/core-java-persistence-2/src/test/resources/employee.sql b/persistence-modules/core-java-persistence-2/src/test/resources/employee.sql new file mode 100644 index 0000000000..c532458163 --- /dev/null +++ b/persistence-modules/core-java-persistence-2/src/test/resources/employee.sql @@ -0,0 +1,75 @@ +/** +Script Name : Create employees script +Author: Parthiv Pradhan + +**/ + +-- Create the employees table if it doesn't exist +CREATE TABLE employees ( + id INT PRIMARY KEY, + first_name VARCHAR(50), + last_name VARCHAR(50), + department VARCHAR(50), + salary DECIMAL(10, 2) +); + +-- Insert employee records +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (1, 'John', 'Doe', 'HR', 50000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (2, 'Jane', 'Smith', 'IT', 60000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (3, 'Michael', 'Johnson', 'Finance', 55000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (4, 'Emily', 'Williams', 'Marketing', 52000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (5, 'David', 'Brown', 'IT', 65000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (6, 'Sarah', 'Miller', 'Finance', 58000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (7, 'Robert', 'Jones', 'HR', 53000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (8, 'Jessica', 'Davis', 'Marketing', 51000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (9, 'William', 'Wilson', 'IT', 59000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (10, 'Jennifer', 'Taylor', 'Finance', 57000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (11, 'Daniel', 'Anderson', 'Marketing', 54000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (12, 'Linda', 'Martinez', 'HR', 52000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (13, 'Christopher', 'Lopez', 'IT', 62000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (14, 'Karen', 'Hernandez', 'Finance', 56000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (15, 'Mark', 'Garcia', 'Marketing', 53000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (16, 'Patricia', 'Lee', 'HR', 51000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (17, 'Anthony', 'Clark', 'IT', 60000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (18, 'Maria', 'Lewis', 'Finance', 59000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (19, 'Paul', 'Walker', 'Marketing', 55000.00); + +INSERT INTO employees (id, first_name, last_name, department, salary) +VALUES (20, 'Ruth', 'Young', 'HR', 54000.00); diff --git a/persistence-modules/hibernate-enterprise/pom.xml b/persistence-modules/hibernate-enterprise/pom.xml index eaa7b0e765..954f8f40d3 100644 --- a/persistence-modules/hibernate-enterprise/pom.xml +++ b/persistence-modules/hibernate-enterprise/pom.xml @@ -82,7 +82,6 @@ 8.0.32 2.6.0 0.9 - 1.14.2 \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-off.properties b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-off.properties index b030527cca..df322cd15e 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-off.properties +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-off.properties @@ -5,7 +5,7 @@ spring.datasource.password= spring.jpa.hibernate.ddl-auto=create-drop spring.h2.console.enabled=true spring.h2.console.path=/h2-console -spring.sql.init.data-locations=data-trans.sql +spring.sql.init.data-locations=classpath:data-trans.sql logging.level.org.hibernate.SQL=INFO logging.level.org.hibernate.type=INFO diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-on.properties b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-on.properties index 972a7bb0f3..e5c488f3be 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-on.properties +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-lazy-load-no-trans-on.properties @@ -5,7 +5,7 @@ spring.datasource.password= spring.jpa.hibernate.ddl-auto=create-drop spring.h2.console.enabled=true spring.h2.console.path=/h2-console -spring.sql.init.data-locations=data-trans.sql +spring.sql.init.data-locations=classpath:data-trans.sql logging.level.org.hibernate.SQL=INFO logging.level.org.hibernate.type=INFO diff --git a/pom.xml b/pom.xml index d2504118a4..8f1671ea3c 100644 --- a/pom.xml +++ b/pom.xml @@ -427,7 +427,6 @@ spring-security-modules/spring-security-ldap - spring-soap spring-swagger-codegen video-tutorials @@ -475,6 +474,7 @@ apache-spark jenkins-modules + jhipster-6 jhipster-modules @@ -594,7 +594,6 @@ spring-security-modules/spring-security-ldap - spring-soap spring-swagger-codegen video-tutorials @@ -634,6 +633,7 @@ apache-spark jenkins-modules + jhipster-6 jhipster-modules @@ -788,7 +788,6 @@ guava-modules kubernetes-modules libraries-concurrency - jhipster-6 libraries-testing maven-modules optaplanner @@ -908,6 +907,7 @@ spring-kafka spring-native + spring-soap spring-security-modules spring-protobuf spring-quartz @@ -1059,7 +1059,6 @@ guava-modules kubernetes-modules libraries-concurrency - jhipster-6 libraries-testing maven-modules optaplanner @@ -1180,6 +1179,7 @@ spring-kafka spring-native + spring-soap spring-security-modules spring-protobuf spring-quartz @@ -1261,7 +1261,7 @@ 2.2 1.3 4.4.0 - 1.12.13 + 1.14.6 @@ -1278,7 +1278,7 @@ 1.36 2.21.0 4.4 - 2.11.0 + 2.13.0 2.6 3.13.0 1.5.0 @@ -1288,7 +1288,7 @@ 2.3.3 1.2 2.15.2 - 1.4 + 1.5 1.9.2 5.9.2 1.3.2 @@ -1300,7 +1300,7 @@ 3.19.0 1.18.28 2.1.214 - 31.1-jre + 32.1.2-jre 3.2.2 diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index a2febc8156..251dba0316 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -44,7 +44,7 @@ spring-boot-graphql spring-boot-groovy - + spring-boot-jasypt spring-boot-jsp spring-boot-keycloak spring-boot-keycloak-2 @@ -91,6 +91,7 @@ spring-boot-3-native spring-boot-3-observation spring-boot-3-test-pitfalls + spring-boot-3-testcontainers spring-boot-resilience4j spring-boot-properties spring-boot-properties-2 diff --git a/spring-boot-modules/spring-boot-3-testcontainers/README.md b/spring-boot-modules/spring-boot-3-testcontainers/README.md new file mode 100644 index 0000000000..5616cce48b --- /dev/null +++ b/spring-boot-modules/spring-boot-3-testcontainers/README.md @@ -0,0 +1 @@ +## Relevant Articles diff --git a/spring-boot-modules/spring-boot-3-testcontainers/pom.xml b/spring-boot-modules/spring-boot-3-testcontainers/pom.xml new file mode 100644 index 0000000000..173fb8795c --- /dev/null +++ b/spring-boot-modules/spring-boot-3-testcontainers/pom.xml @@ -0,0 +1,102 @@ + + + 4.0.0 + spring-boot-3-testcontainers + 0.0.1-SNAPSHOT + spring-boot-3-testcontainers + Testcontainer Improvements in Spring Boot 3 + + + com.baeldung + parent-boot-3 + 0.0.1-SNAPSHOT + ../../parent-boot-3 + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.springframework.boot + spring-boot-starter-test + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + + org.springframework.boot + spring-boot-testcontainers + + + + org.testcontainers + mongodb + ${testcontainers.version} + test + + + org.testcontainers + testcontainers + ${testcontainers.version} + test + + + org.testcontainers + junit-jupiter + ${testcontainers.version} + test + + + + io.rest-assured + rest-assured + ${rest-assured.version} + test + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + 3.0.0-M7 + 1.18.3 + 5.3.1 + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/Application.java b/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/Application.java new file mode 100644 index 0000000000..9a00bfebf2 --- /dev/null +++ b/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/Application.java @@ -0,0 +1,14 @@ +package com.baeldung.testcontainers; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/support/MiddleEarthCharacter.java b/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/support/MiddleEarthCharacter.java new file mode 100644 index 0000000000..4780a2f66a --- /dev/null +++ b/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/support/MiddleEarthCharacter.java @@ -0,0 +1,13 @@ +package com.baeldung.testcontainers.support; + +import java.util.UUID; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document("characters") +public record MiddleEarthCharacter(@Id String id, String name, String race) { + public MiddleEarthCharacter(String name, String race) { + this(UUID.randomUUID().toString(), name, race); + } +} diff --git a/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/support/MiddleEarthCharactersController.java b/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/support/MiddleEarthCharactersController.java new file mode 100644 index 0000000000..3b7bfddbef --- /dev/null +++ b/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/support/MiddleEarthCharactersController.java @@ -0,0 +1,30 @@ +package com.baeldung.testcontainers.support; + +import java.util.List; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("characters") +public class MiddleEarthCharactersController { + private final MiddleEarthCharactersRepository repository; + + public MiddleEarthCharactersController(MiddleEarthCharactersRepository repository) { + this.repository = repository; + } + + @GetMapping + public List findByRace(@RequestParam String race) { + return repository.findAllByRace(race); + } + + @PostMapping + public MiddleEarthCharacter save(@RequestBody MiddleEarthCharacter character) { + return repository.save(character); + } +} diff --git a/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/support/MiddleEarthCharactersRepository.java b/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/support/MiddleEarthCharactersRepository.java new file mode 100644 index 0000000000..a668650670 --- /dev/null +++ b/spring-boot-modules/spring-boot-3-testcontainers/src/main/java/com/baeldung/testcontainers/support/MiddleEarthCharactersRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.testcontainers.support; + +import java.util.List; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface MiddleEarthCharactersRepository extends MongoRepository { + List findAllByRace(String race); +} diff --git a/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/DynamicPropertiesIntegrationTest.java b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/DynamicPropertiesIntegrationTest.java new file mode 100644 index 0000000000..8689b10110 --- /dev/null +++ b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/DynamicPropertiesIntegrationTest.java @@ -0,0 +1,59 @@ +package com.baeldung.testcontainers; + +import static io.restassured.RestAssured.when; +import static org.hamcrest.Matchers.hasItems; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; +import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS; + +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; + +import com.baeldung.testcontainers.support.MiddleEarthCharacter; +import com.baeldung.testcontainers.support.MiddleEarthCharactersRepository; + +@Testcontainers +@SpringBootTest(webEnvironment = DEFINED_PORT) +@DirtiesContext(classMode = AFTER_CLASS) +class DynamicPropertiesIntegrationTest { + @Container + static MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10")); + + @DynamicPropertySource + static void setProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl); + } + + @Autowired + private MiddleEarthCharactersRepository repository; + + @BeforeEach + void beforeEach() { + repository.deleteAll(); + } + + @Test + void whenRequestingHobbits_thenReturnFrodoAndSam() { + repository.saveAll(List.of( + new MiddleEarthCharacter("Frodo", "hobbit"), + new MiddleEarthCharacter("Samwise", "hobbit"), + new MiddleEarthCharacter("Aragon", "human"), + new MiddleEarthCharacter("Gandalf", "wizzard") + )); + + when().get("/characters?race=hobbit") + .then().statusCode(200) + .and().body("name", hasItems("Frodo", "Samwise")); + } + +} diff --git a/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/LocalDevApplication.java b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/LocalDevApplication.java new file mode 100644 index 0000000000..a94c0f772a --- /dev/null +++ b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/LocalDevApplication.java @@ -0,0 +1,29 @@ +package com.baeldung.testcontainers; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.devtools.restart.RestartScope; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.context.annotation.Bean; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.utility.DockerImageName; + +class LocalDevApplication { + + public static void main(String[] args) { + SpringApplication.from(Application::main) + .with(LocalDevTestcontainersConfig.class) + .run(args); + } + + @TestConfiguration(proxyBeanMethods = false) + static class LocalDevTestcontainersConfig { + @Bean + @RestartScope + @ServiceConnection + public MongoDBContainer mongoDBContainer() { + return new MongoDBContainer(DockerImageName.parse("mongo:4.0.10")); + } + } + +} diff --git a/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/ServiceConnectionIntegrationTest.java b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/ServiceConnectionIntegrationTest.java new file mode 100644 index 0000000000..500d6d2e61 --- /dev/null +++ b/spring-boot-modules/spring-boot-3-testcontainers/src/test/java/com/baeldung/testcontainers/ServiceConnectionIntegrationTest.java @@ -0,0 +1,55 @@ +package com.baeldung.testcontainers; + +import static io.restassured.RestAssured.when; +import static org.hamcrest.Matchers.hasItems; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; +import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS; + +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.test.annotation.DirtiesContext; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; + +import com.baeldung.testcontainers.support.MiddleEarthCharacter; +import com.baeldung.testcontainers.support.MiddleEarthCharactersRepository; + +@Testcontainers +@SpringBootTest(webEnvironment = DEFINED_PORT) +@DirtiesContext(classMode = AFTER_CLASS) +class ServiceConnectionIntegrationTest { + + @Container + @ServiceConnection + static MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10")); + + @Autowired + private MiddleEarthCharactersRepository repository; + + @BeforeEach + void beforeEach() { + repository.deleteAll(); + } + + @Test + void whenRequestingHobbits_thenReturnFrodoAndSam() { + repository.saveAll(List.of( + new MiddleEarthCharacter("Frodo", "hobbit"), + new MiddleEarthCharacter("Samwise", "hobbit"), + new MiddleEarthCharacter("Aragon", "human"), + new MiddleEarthCharacter("Gandalf", "wizzard") + )); + + when().get("/characters?race=hobbit") + .then().statusCode(200) + .and().body("name", hasItems("Frodo", "Samwise")); + } + +} diff --git a/spring-boot-modules/spring-boot-jasypt/pom.xml b/spring-boot-modules/spring-boot-jasypt/pom.xml index 8595b9c639..b83162fb04 100644 --- a/spring-boot-modules/spring-boot-jasypt/pom.xml +++ b/spring-boot-modules/spring-boot-jasypt/pom.xml @@ -50,4 +50,11 @@ 2.0.0 + + + spring-milestone + Spring Milestone + https://repo.spring.io/milestone + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableCredentials.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableCredentials.java index 72c45afbb2..d7e948ca9a 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableCredentials.java +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableCredentials.java @@ -1,6 +1,7 @@ package com.baeldung.configurationproperties; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.bind.ConstructorBinding; @ConfigurationProperties(prefix = "mail.credentials") public class ImmutableCredentials { @@ -9,12 +10,19 @@ public class ImmutableCredentials { private final String username; private final String password; + @ConstructorBinding public ImmutableCredentials(String authMethod, String username, String password) { this.authMethod = authMethod; this.username = username; this.password = password; } + public ImmutableCredentials(String username, String password) { + this.username = username; + this.password = password; + this.authMethod = "Default"; + } + public String getAuthMethod() { return authMethod; } diff --git a/spring-boot-modules/spring-boot-telegram/.gitignore b/spring-boot-modules/spring-boot-telegram/.gitignore new file mode 100644 index 0000000000..a8e6c9dbce --- /dev/null +++ b/spring-boot-modules/spring-boot-telegram/.gitignore @@ -0,0 +1,35 @@ +HELP.md +target/ +.mvn +mvnw +mvnw.cmd +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/spring-boot-modules/spring-boot-telegram/README.md b/spring-boot-modules/spring-boot-telegram/README.md new file mode 100644 index 0000000000..4cd6560bc0 --- /dev/null +++ b/spring-boot-modules/spring-boot-telegram/README.md @@ -0,0 +1,2 @@ +## Relevant Articles +- [Creating a Telegram Bot with Spring Boot](https://www.baeldung.com/spring-boot-telegram-bot) diff --git a/spring-boot-modules/spring-boot-telegram/pom.xml b/spring-boot-modules/spring-boot-telegram/pom.xml new file mode 100644 index 0000000000..b960137449 --- /dev/null +++ b/spring-boot-modules/spring-boot-telegram/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + com.baelding + spring-boot-telegram + 0.0.1-SNAPSHOT + spring-boot-telegram + Demo project for Spring Boot with Spring Data Redis + + + com.baeldung + parent-boot-3 + 0.0.1-SNAPSHOT + ../../parent-boot-3 + + + + + org.telegram + telegrambots-spring-boot-starter + 6.7.0 + + + org.telegram + telegrambots-abilities + 6.7.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + 17 + + + diff --git a/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/Constants.java b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/Constants.java new file mode 100644 index 0000000000..5c529bf15c --- /dev/null +++ b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/Constants.java @@ -0,0 +1,9 @@ +package com.baeldung.telegram; + +public class Constants { + + public static final String START_DESCRIPTION = "Starts the bot"; + + public static final String CHAT_STATES = "chatStates"; + public static final String START_TEXT = "Welcome to Baeldung Pizza Bot.\nPlease enter your name"; +} diff --git a/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/KeyboardFactory.java b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/KeyboardFactory.java new file mode 100644 index 0000000000..367c5a4c7c --- /dev/null +++ b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/KeyboardFactory.java @@ -0,0 +1,30 @@ +package com.baeldung.telegram; + +import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard; +import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup; +import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow; + +import java.util.List; + +public class KeyboardFactory { +public static ReplyKeyboard getPizzaToppingsKeyboard() { + KeyboardRow row = new KeyboardRow(); + row.add("Margherita"); + row.add("Pepperoni"); + return new ReplyKeyboardMarkup(List.of(row)); +} + + public static ReplyKeyboard getPizzaOrDrinkKeyboard(){ + KeyboardRow row = new KeyboardRow(); + row.add("Pizza"); + row.add("Drink"); + return new ReplyKeyboardMarkup(List.of(row)); + } + + public static ReplyKeyboard getYesOrNo() { + KeyboardRow row = new KeyboardRow(); + row.add("Yes"); + row.add("No"); + return new ReplyKeyboardMarkup(List.of(row)); + } +} diff --git a/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/PizzaBot.java b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/PizzaBot.java new file mode 100644 index 0000000000..802cb586d7 --- /dev/null +++ b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/PizzaBot.java @@ -0,0 +1,48 @@ +package com.baeldung.telegram; + +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; +import org.telegram.abilitybots.api.bot.AbilityBot; +import org.telegram.abilitybots.api.bot.BaseAbilityBot; +import org.telegram.abilitybots.api.objects.Ability; +import org.telegram.abilitybots.api.objects.Flag; +import org.telegram.abilitybots.api.objects.Reply; +import org.telegram.telegrambots.meta.api.objects.Update; + +import java.util.function.BiConsumer; + +import static org.telegram.abilitybots.api.objects.Locality.USER; +import static org.telegram.abilitybots.api.objects.Privacy.PUBLIC; +import static org.telegram.abilitybots.api.util.AbilityUtils.getChatId; + +@Component +public class PizzaBot extends AbilityBot { + + private final ResponseHandler responseHandler; + + public PizzaBot(Environment environment) { + super(environment.getProperty("BOT_TOKEN"), "baeldungbot"); + responseHandler = new ResponseHandler(silent, db); + } + +public Ability startBot() { + return Ability + .builder() + .name("start") + .info(Constants.START_DESCRIPTION) + .locality(USER) + .privacy(PUBLIC) + .action(ctx -> responseHandler.replyToStart(ctx.chatId())) + .build(); +} + +public Reply replyToButtons() { + BiConsumer action = (abilityBot, upd) -> responseHandler.replyToButtons(getChatId(upd), upd.getMessage()); + return Reply.of(action, Flag.TEXT,upd -> responseHandler.userIsActive(getChatId(upd))); +} + +@Override +public long creatorId() { + return 1L; +} +} diff --git a/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/ResponseHandler.java b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/ResponseHandler.java new file mode 100644 index 0000000000..862edc962c --- /dev/null +++ b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/ResponseHandler.java @@ -0,0 +1,132 @@ +package com.baeldung.telegram; + +import org.telegram.abilitybots.api.db.DBContext; +import org.telegram.abilitybots.api.sender.SilentSender; +import org.telegram.telegrambots.meta.api.methods.send.SendMessage; +import org.telegram.telegrambots.meta.api.objects.Message; +import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard; +import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardRemove; + +import java.util.Map; + +import static com.baeldung.telegram.Constants.START_TEXT; +import static com.baeldung.telegram.UserState.*; + +public class ResponseHandler { + private final SilentSender sender; + private final Map chatStates; + + public ResponseHandler(SilentSender sender, DBContext db) { + this.sender = sender; + chatStates = db.getMap(Constants.CHAT_STATES); + } + + public void replyToStart(long chatId) { + SendMessage message = new SendMessage(); + message.setChatId(chatId); + message.setText(START_TEXT); + sender.execute(message); + chatStates.put(chatId, AWAITING_NAME); + } + +public void replyToButtons(long chatId, Message message) { + if (message.getText().equalsIgnoreCase("/stop")) { + stopChat(chatId); + } + + switch (chatStates.get(chatId)) { + case AWAITING_NAME -> replyToName(chatId, message); + case FOOD_DRINK_SELECTION -> replyToFoodDrinkSelection(chatId, message); + case PIZZA_TOPPINGS -> replyToPizzaToppings(chatId, message); + case AWAITING_CONFIRMATION -> replyToOrder(chatId, message); + default -> unexpectedMessage(chatId); + } +} + + private void unexpectedMessage(long chatId) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setChatId(chatId); + sendMessage.setText("I did not expect that."); + sender.execute(sendMessage); + } + +private void stopChat(long chatId) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setChatId(chatId); + sendMessage.setText("Thank you for your order. See you soon!\nPress /start to order again"); + chatStates.remove(chatId); + sendMessage.setReplyMarkup(new ReplyKeyboardRemove(true)); + sender.execute(sendMessage); +} + + private void replyToOrder(long chatId, Message message) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setChatId(chatId); + if ("yes".equalsIgnoreCase(message.getText())) { + sendMessage.setText("We will deliver it soon. Thank you!\nOrder another?"); + sendMessage.setReplyMarkup(KeyboardFactory.getPizzaOrDrinkKeyboard()); + sender.execute(sendMessage); + chatStates.put(chatId, FOOD_DRINK_SELECTION); + } else if ("no".equalsIgnoreCase(message.getText())) { + stopChat(chatId); + } else { + sendMessage.setText("Please select yes or no"); + sendMessage.setReplyMarkup(KeyboardFactory.getYesOrNo()); + sender.execute(sendMessage); + } + } + + private void replyToPizzaToppings(long chatId, Message message) { + if ("margherita".equalsIgnoreCase(message.getText())) { + promptWithKeyboardForState(chatId, "You selected Margherita Pizza.\nWe will deliver it soon. Thank you!\nOrder again?", + KeyboardFactory.getYesOrNo(), AWAITING_CONFIRMATION); + } else if ("pepperoni".equalsIgnoreCase(message.getText())) { + promptWithKeyboardForState(chatId, "We finished the Pepperoni Pizza.\nSelect another Topping", + KeyboardFactory.getPizzaToppingsKeyboard(), PIZZA_TOPPINGS); + } else { + SendMessage sendMessage = new SendMessage(); + sendMessage.setChatId(chatId); + sendMessage.setText("We don't sell " + message.getText() + " Pizza.\nSelect the toppings!"); + sendMessage.setReplyMarkup(KeyboardFactory.getPizzaToppingsKeyboard()); + sender.execute(sendMessage); + } + } + + private void promptWithKeyboardForState(long chatId, String text, ReplyKeyboard YesOrNo, UserState awaitingReorder) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setChatId(chatId); + sendMessage.setText(text); + sendMessage.setReplyMarkup(YesOrNo); + sender.execute(sendMessage); + chatStates.put(chatId, awaitingReorder); + } + +private void replyToFoodDrinkSelection(long chatId, Message message) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setChatId(chatId); + if ("drink".equalsIgnoreCase(message.getText())) { + sendMessage.setText("We don't sell drinks.\nBring your own drink!! :)"); + sendMessage.setReplyMarkup(KeyboardFactory.getPizzaOrDrinkKeyboard()); + sender.execute(sendMessage); + } else if ("pizza".equalsIgnoreCase(message.getText())) { + sendMessage.setText("We love Pizza in here.\nSelect the toppings!"); + sendMessage.setReplyMarkup(KeyboardFactory.getPizzaToppingsKeyboard()); + sender.execute(sendMessage); + chatStates.put(chatId, UserState.PIZZA_TOPPINGS); + } else { + sendMessage.setText("We don't sell " + message.getText() + ". Please select from the options below."); + sendMessage.setReplyMarkup(KeyboardFactory.getPizzaOrDrinkKeyboard()); + sender.execute(sendMessage); + } +} + +private void replyToName(long chatId, Message message) { + promptWithKeyboardForState(chatId, "Hello " + message.getText() + ". What would you like to have?", + KeyboardFactory.getPizzaOrDrinkKeyboard(), + UserState.FOOD_DRINK_SELECTION); +} + + public boolean userIsActive(Long chatId) { + return chatStates.containsKey(chatId); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/SpringBootTelegramApplication.java b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/SpringBootTelegramApplication.java new file mode 100644 index 0000000000..041478f42b --- /dev/null +++ b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/SpringBootTelegramApplication.java @@ -0,0 +1,24 @@ +package com.baeldung.telegram; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; +import org.telegram.abilitybots.api.bot.AbilityBot; +import org.telegram.telegrambots.meta.TelegramBotsApi; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; +import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; + +@SpringBootApplication +public class SpringBootTelegramApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext ctx = SpringApplication.run(SpringBootTelegramApplication.class, args); + try { + TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class); + botsApi.registerBot(ctx.getBean("pizzaBot", AbilityBot.class)); + } catch (TelegramApiException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/UserState.java b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/UserState.java new file mode 100644 index 0000000000..a0b53a6a02 --- /dev/null +++ b/spring-boot-modules/spring-boot-telegram/src/main/java/com/baeldung/telegram/UserState.java @@ -0,0 +1,5 @@ +package com.baeldung.telegram; + +public enum UserState { + AWAITING_NAME, FOOD_DRINK_SELECTION, PIZZA_TOPPINGS, AWAITING_CONFIRMATION +} diff --git a/spring-boot-modules/spring-boot-telegram/src/main/resources/application.properties b/spring-boot-modules/spring-boot-telegram/src/main/resources/application.properties new file mode 100644 index 0000000000..6b2753c3a8 --- /dev/null +++ b/spring-boot-modules/spring-boot-telegram/src/main/resources/application.properties @@ -0,0 +1,2 @@ +server.port=8081 +BOT_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \ No newline at end of file diff --git a/spring-cloud-modules/spring-cloud-bootstrap/application-config/book-service.properties b/spring-cloud-modules/spring-cloud-bootstrap/application-config/book-service.properties index 2ea30b9ab7..597899bd16 100644 --- a/spring-cloud-modules/spring-cloud-bootstrap/application-config/book-service.properties +++ b/spring-cloud-modules/spring-cloud-bootstrap/application-config/book-service.properties @@ -10,8 +10,8 @@ eureka.client.registryFetchIntervalSeconds = 5 management.security.sessions=never -logging.level.org.springframework.web.=debug -logging.level.org.springframework.security=debug +logging.level.org.springframework.web.=INFO +logging.level.org.springframework.security=INFO spring.redis.host=localhost spring.redis.port=6379 diff --git a/spring-cloud-modules/spring-cloud-bootstrap/application-config/gateway.properties b/spring-cloud-modules/spring-cloud-bootstrap/application-config/gateway.properties index 42e114450d..44d5267d10 100644 --- a/spring-cloud-modules/spring-cloud-bootstrap/application-config/gateway.properties +++ b/spring-cloud-modules/spring-cloud-bootstrap/application-config/gateway.properties @@ -6,8 +6,8 @@ eureka.client.registryFetchIntervalSeconds = 5 management.security.sessions=always -logging.level.org.springframework.web.=debug -logging.level.org.springframework.security=debug +logging.level.org.springframework.web.=INFO +logging.level.org.springframework.security=INFO spring.redis.host=localhost spring.redis.port=6379 diff --git a/spring-cloud-modules/spring-cloud-bootstrap/application-config/rating-service.properties b/spring-cloud-modules/spring-cloud-bootstrap/application-config/rating-service.properties index 059b87e4e7..89f8ee40fe 100644 --- a/spring-cloud-modules/spring-cloud-bootstrap/application-config/rating-service.properties +++ b/spring-cloud-modules/spring-cloud-bootstrap/application-config/rating-service.properties @@ -10,8 +10,8 @@ eureka.client.registryFetchIntervalSeconds = 5 management.security.sessions=never -logging.level.org.springframework.web.=debug -logging.level.org.springframework.security=debug +logging.level.org.springframework.web.=INFO +logging.level.org.springframework.security=INFO spring.redis.host=localhost spring.redis.port=6379 diff --git a/spring-cloud-modules/spring-cloud-bootstrap/application-config/zipkin.properties b/spring-cloud-modules/spring-cloud-bootstrap/application-config/zipkin.properties index ca3aed2263..1a689123cb 100644 --- a/spring-cloud-modules/spring-cloud-bootstrap/application-config/zipkin.properties +++ b/spring-cloud-modules/spring-cloud-bootstrap/application-config/zipkin.properties @@ -4,4 +4,4 @@ server.port=9411 eureka.client.region = default eureka.client.registryFetchIntervalSeconds = 5 -logging.level.org.springframework.web=debug +logging.level.org.springframework.web=INFO diff --git a/spring-cloud-modules/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/pom.xml b/spring-cloud-modules/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/pom.xml index ba923c4ae6..3e2b1632c7 100644 --- a/spring-cloud-modules/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/pom.xml +++ b/spring-cloud-modules/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/pom.xml @@ -46,7 +46,6 @@ 1.3.1.RELEASE Edgware.SR6 5.2.12.Final - 1.11.20 3.1.0 diff --git a/spring-ejb-modules/wildfly/wildfly-jpa/src/main/resources/logback.xml b/spring-cloud-modules/spring-cloud-eureka/spring-cloud-eureka-feign-client-integration-test/src/test/resources/logback-test.xml similarity index 74% rename from spring-ejb-modules/wildfly/wildfly-jpa/src/main/resources/logback.xml rename to spring-cloud-modules/spring-cloud-eureka/spring-cloud-eureka-feign-client-integration-test/src/test/resources/logback-test.xml index 7d900d8ea8..fb1f109a18 100644 --- a/spring-ejb-modules/wildfly/wildfly-jpa/src/main/resources/logback.xml +++ b/spring-cloud-modules/spring-cloud-eureka/spring-cloud-eureka-feign-client-integration-test/src/test/resources/logback-test.xml @@ -7,6 +7,10 @@ + + + + diff --git a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/resources/application.properties b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/resources/application.properties index aa0dc6a382..0a31d79ee0 100644 --- a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/resources/application.properties +++ b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/resources/application.properties @@ -4,7 +4,7 @@ feign.okhttp.enabled=true server.port=8085 spring.main.allow-bean-definition-overriding=true -logging.level.com.baeldung.cloud.openfeign.client=DEBUG +logging.level.com.baeldung.cloud.openfeign.client=INFO feign.hystrix.enabled=true spring.cloud.openfeign.client.config.postClient.url=https://jsonplaceholder.typicode.com/posts/ \ No newline at end of file diff --git a/spring-ejb-modules/wildfly/wildfly-ejb/src/main/resources/logback.xml b/spring-cloud-modules/spring-cloud-stream/spring-cloud-stream-rabbit/src/test/resources/logback-test.xml similarity index 71% rename from spring-ejb-modules/wildfly/wildfly-ejb/src/main/resources/logback.xml rename to spring-cloud-modules/spring-cloud-stream/spring-cloud-stream-rabbit/src/test/resources/logback-test.xml index 7d900d8ea8..233cfa7e23 100644 --- a/spring-ejb-modules/wildfly/wildfly-ejb/src/main/resources/logback.xml +++ b/spring-cloud-modules/spring-cloud-stream/spring-cloud-stream-rabbit/src/test/resources/logback-test.xml @@ -7,6 +7,10 @@ + + + + diff --git a/spring-ejb-modules/wildfly/wildfly-ejb-interfaces/src/main/resources/logback.xml b/spring-cloud-modules/spring-cloud-task/springcloudtasksink/src/test/resources/logback-test.xml similarity index 83% rename from spring-ejb-modules/wildfly/wildfly-ejb-interfaces/src/main/resources/logback.xml rename to spring-cloud-modules/spring-cloud-task/springcloudtasksink/src/test/resources/logback-test.xml index 7d900d8ea8..2a5eefdd1f 100644 --- a/spring-ejb-modules/wildfly/wildfly-ejb-interfaces/src/main/resources/logback.xml +++ b/spring-cloud-modules/spring-cloud-task/springcloudtasksink/src/test/resources/logback-test.xml @@ -7,6 +7,8 @@ + + diff --git a/spring-cloud-modules/spring-cloud-zuul-fallback/spring-cloud-zuul-fallback-weather-service/src/test/resources/logback-test.xml b/spring-cloud-modules/spring-cloud-zuul-fallback/spring-cloud-zuul-fallback-weather-service/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..4ff950099a --- /dev/null +++ b/spring-cloud-modules/spring-cloud-zuul-fallback/spring-cloud-zuul-fallback-weather-service/src/test/resources/logback-test.xml @@ -0,0 +1,17 @@ + + + + + # Pattern of log message for console appender + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + \ No newline at end of file diff --git a/spring-ejb-modules/wildfly/pom.xml b/spring-ejb-modules/wildfly/pom.xml index f122e99001..412fa1b244 100644 --- a/spring-ejb-modules/wildfly/pom.xml +++ b/spring-ejb-modules/wildfly/pom.xml @@ -16,11 +16,6 @@ - widlfly-web - wildfly-ear - wildfly-jpa - wildfly-ejb-interfaces - wildfly-ejb wildfly-mdb diff --git a/spring-ejb-modules/wildfly/widlfly-web/pom.xml b/spring-ejb-modules/wildfly/widlfly-web/pom.xml deleted file mode 100644 index 46c3f7d0bc..0000000000 --- a/spring-ejb-modules/wildfly/widlfly-web/pom.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - 4.0.0 - widlfly-web - widlfly-web - war - - - com.baeldung.wildfly - wildfly - 0.0.1-SNAPSHOT - - - - - - javax - javaee-api - ${javaee-api.version} - provided - - - - com.baeldung.wildfly - wildfly-jpa - ${wildlfy.version} - - - - com.baeldung.wildfly - wildfly-ejb-interfaces - ${wildlfy.version} - - - - com.baeldung.wildfly - wildfly-ejb - ${wildlfy.version} - - - - - 3.3.2 - - \ No newline at end of file diff --git a/spring-ejb-modules/wildfly/widlfly-web/src/main/java/TestEJBServlet.java b/spring-ejb-modules/wildfly/widlfly-web/src/main/java/TestEJBServlet.java deleted file mode 100644 index 57376e9c4a..0000000000 --- a/spring-ejb-modules/wildfly/widlfly-web/src/main/java/TestEJBServlet.java +++ /dev/null @@ -1,41 +0,0 @@ - -import java.io.IOException; -import java.io.PrintWriter; -import java.util.List; - -import javax.ejb.EJB; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import model.User; -import wildfly.beans.UserBeanLocal; - -/** - * Servlet implementation class TestEJBServlet - */ -public class TestEJBServlet extends HttpServlet { - - @EJB - private UserBeanLocal userBean; - - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - List users = userBean.getUsers(); - - PrintWriter out = response.getWriter(); - - out.println(""); - out.println(""); - for (User user : users) { - out.print(user.getUsername()); - out.print(" " + user.getEmail() + "
"); - } - out.println(""); - out.println(""); - } - - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - doGet(request, response); - } -} diff --git a/spring-ejb-modules/wildfly/widlfly-web/src/main/java/TestJPAServlet.java b/spring-ejb-modules/wildfly/widlfly-web/src/main/java/TestJPAServlet.java deleted file mode 100644 index 609366c53a..0000000000 --- a/spring-ejb-modules/wildfly/widlfly-web/src/main/java/TestJPAServlet.java +++ /dev/null @@ -1,54 +0,0 @@ - -import java.io.IOException; -import java.util.List; - -import javax.annotation.Resource; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.Query; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.transaction.UserTransaction; - -import model.User; - -/** - * Servlet implementation class TestJPAServlet - */ -public class TestJPAServlet extends HttpServlet { - private static final long serialVersionUID = 1L; - @PersistenceContext(unitName = "wildfly-jpa") - EntityManager em; - - @Resource - UserTransaction tx; - - /** - * @see HttpServlet#HttpServlet() - */ - public TestJPAServlet() { - super(); - // TODO Auto-generated constructor stub - } - - /** - * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) - */ - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - Query q = em.createNamedQuery("User.findAll"); - List users = q.getResultList(); - response.getWriter() - .append("JPA users returned: " + users.size()); - } - - /** - * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) - */ - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // TODO Auto-generated method stub - doGet(request, response); - } - -} diff --git a/spring-ejb-modules/wildfly/widlfly-web/src/main/resources/logback.xml b/spring-ejb-modules/wildfly/widlfly-web/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/spring-ejb-modules/wildfly/widlfly-web/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-ejb-modules/wildfly/widlfly-web/src/main/webapp/WEB-INF/web.xml b/spring-ejb-modules/wildfly/widlfly-web/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 5b3f664443..0000000000 --- a/spring-ejb-modules/wildfly/widlfly-web/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - widlfly-web - - index.html - index.htm - index.jsp - default.html - default.htm - default.jsp - - - - TestJPAServlet - TestJPAServlet - TestJPAServlet - - - TestJPAServlet - /TestJPAServlet - - - - TestEJBServlet - TestEJBServlet - TestEJBServlet - - - TestEJBServlet - /TestEJBServlet - - \ No newline at end of file diff --git a/spring-ejb-modules/wildfly/wildfly-ear/pom.xml b/spring-ejb-modules/wildfly/wildfly-ear/pom.xml deleted file mode 100644 index da321cb9eb..0000000000 --- a/spring-ejb-modules/wildfly/wildfly-ear/pom.xml +++ /dev/null @@ -1,73 +0,0 @@ - - - 4.0.0 - wildfly-ear - wildfly-ear - ear - - - com.baeldung.wildfly - wildfly - 0.0.1-SNAPSHOT - - - - - - com.baeldung.wildfly - widlfly-web - ${wildlfy.version} - war - - - - com.baeldung.wildfly - wildfly-jpa - ${wildlfy.version} - - - - com.baeldung.wildfly - wildfly-ejb - - - - com.baeldung.wildfly - wildfly-ejb-interfaces - - - - - - - maven-ear-plugin - ${maven-ear-plugin.version} - - lib/ - ${defaultLibBundleDir.version} - - - com.baeldung.wildfly - widlfly-web - /wildfly - - - - - - org.wildfly.plugins - wildfly-maven-plugin - ${wildfly-maven-plugin.version} - - - - - - 2.10.1 - 1.2.0.Final - 7 - - - \ No newline at end of file diff --git a/spring-ejb-modules/wildfly/wildfly-ejb-interfaces/pom.xml b/spring-ejb-modules/wildfly/wildfly-ejb-interfaces/pom.xml deleted file mode 100644 index bc9159b667..0000000000 --- a/spring-ejb-modules/wildfly/wildfly-ejb-interfaces/pom.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - 4.0.0 - wildfly-ejb-interfaces - wildfly-ejb-interfaces - - - com.baeldung.wildfly - wildfly - 0.0.1-SNAPSHOT - - - - - - javax.ejb - javax.ejb-api - ${javax.ejb-api.version} - provided - - - - com.baeldung.wildfly - wildfly-jpa - ${wildlfy.version} - - - - - 3.2 - - - \ No newline at end of file diff --git a/spring-ejb-modules/wildfly/wildfly-ejb-interfaces/src/main/java/wildfly/beans/UserBeanLocal.java b/spring-ejb-modules/wildfly/wildfly-ejb-interfaces/src/main/java/wildfly/beans/UserBeanLocal.java deleted file mode 100644 index 16930cb5b9..0000000000 --- a/spring-ejb-modules/wildfly/wildfly-ejb-interfaces/src/main/java/wildfly/beans/UserBeanLocal.java +++ /dev/null @@ -1,13 +0,0 @@ -package wildfly.beans; - -import java.util.List; - -import javax.ejb.Local; - -import model.User; - -@Local -public interface UserBeanLocal { - - List getUsers(); -} diff --git a/spring-ejb-modules/wildfly/wildfly-ejb-interfaces/src/main/java/wildfly/beans/UserBeanRemote.java b/spring-ejb-modules/wildfly/wildfly-ejb-interfaces/src/main/java/wildfly/beans/UserBeanRemote.java deleted file mode 100644 index 5b57d4283f..0000000000 --- a/spring-ejb-modules/wildfly/wildfly-ejb-interfaces/src/main/java/wildfly/beans/UserBeanRemote.java +++ /dev/null @@ -1,13 +0,0 @@ -package wildfly.beans; - -import java.util.List; - -import javax.ejb.Remote; - -import model.User; - -@Remote -public interface UserBeanRemote { - - List getUsers(); -} diff --git a/spring-ejb-modules/wildfly/wildfly-ejb/pom.xml b/spring-ejb-modules/wildfly/wildfly-ejb/pom.xml deleted file mode 100644 index 36574bf984..0000000000 --- a/spring-ejb-modules/wildfly/wildfly-ejb/pom.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - 4.0.0 - wildfly-ejb - wildfly-ejb - ejb - - - com.baeldung.wildfly - wildfly - 0.0.1-SNAPSHOT - - - - - - javax.ejb - javax.ejb-api - ${ejb.version} - provided - - - - javax - javaee-api - provided - - - - org.hibernate - hibernate-core - - - - com.baeldung.wildfly - wildfly-ejb-interfaces - - - - - - - org.apache.maven.plugins - maven-ejb-plugin - - ${ejb.version} - - - - - - - 3.2 - - - \ No newline at end of file diff --git a/spring-ejb-modules/wildfly/wildfly-ejb/src/main/java/wildfly/beans/UserBean.java b/spring-ejb-modules/wildfly/wildfly-ejb/src/main/java/wildfly/beans/UserBean.java deleted file mode 100644 index 07e3cbcb32..0000000000 --- a/spring-ejb-modules/wildfly/wildfly-ejb/src/main/java/wildfly/beans/UserBean.java +++ /dev/null @@ -1,24 +0,0 @@ -package wildfly.beans; - -import java.util.List; - -import javax.ejb.Stateless; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - -import model.User; - -/** - * Session Bean implementation class UserBean - */ -@Stateless -public class UserBean implements UserBeanRemote, UserBeanLocal { - @PersistenceContext(unitName = "wildfly-jpa") - private EntityManager em; - - @Override - public List getUsers() { - return em.createNamedQuery("User.findAll") - .getResultList(); - } -} diff --git a/spring-ejb-modules/wildfly/wildfly-jpa/pom.xml b/spring-ejb-modules/wildfly/wildfly-jpa/pom.xml deleted file mode 100644 index 603e337510..0000000000 --- a/spring-ejb-modules/wildfly/wildfly-jpa/pom.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - 4.0.0 - wildfly-jpa - wildfly-jpa - - - com.baeldung.wildfly - wildfly - 0.0.1-SNAPSHOT - - - - - - - org.hibernate - hibernate-core - provided - - - - \ No newline at end of file diff --git a/spring-ejb-modules/wildfly/wildfly-jpa/src/main/java/model/User.java b/spring-ejb-modules/wildfly/wildfly-jpa/src/main/java/model/User.java deleted file mode 100644 index 3a3f95bf8c..0000000000 --- a/spring-ejb-modules/wildfly/wildfly-jpa/src/main/java/model/User.java +++ /dev/null @@ -1,51 +0,0 @@ -package model; - -import java.io.Serializable; -import javax.persistence.*; - -/** - * The persistent class for the users database table. - * - */ -@Entity -@Table(name = "users") -@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u") -public class User implements Serializable { - private static final long serialVersionUID = 1L; - - @Id - private String username; - - private String email; - - @Column(name = "postal_number") - private Integer postalNumber; - - public User() { - } - - public String getUsername() { - return this.username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getEmail() { - return this.email; - } - - public void setEmail(String email) { - this.email = email; - } - - public Integer getPostalNumber() { - return this.postalNumber; - } - - public void setPostalNumber(Integer postalNumber) { - this.postalNumber = postalNumber; - } - -} \ No newline at end of file diff --git a/spring-ejb-modules/wildfly/wildfly-jpa/src/main/resources/META-INF/persistence.xml b/spring-ejb-modules/wildfly/wildfly-jpa/src/main/resources/META-INF/persistence.xml deleted file mode 100644 index 2aa6bc2cd7..0000000000 --- a/spring-ejb-modules/wildfly/wildfly-jpa/src/main/resources/META-INF/persistence.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - java:/H2DS - model.User - - - - - - - - diff --git a/spring-ejb-modules/wildfly/wildfly-jpa/src/main/resources/data.sql b/spring-ejb-modules/wildfly/wildfly-jpa/src/main/resources/data.sql deleted file mode 100644 index 03eafa534e..0000000000 --- a/spring-ejb-modules/wildfly/wildfly-jpa/src/main/resources/data.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO users (username, email, postal_number) VALUES ('user1', 'user1@baeldung.com', 1000), ('user2', 'user2@baeldung.com', 2); \ No newline at end of file diff --git a/spring-integration/src/test/resources/logback-test.xml b/spring-integration/src/test/resources/logback-test.xml index 8f1be4eb7a..98bfc86d71 100644 --- a/spring-integration/src/test/resources/logback-test.xml +++ b/spring-integration/src/test/resources/logback-test.xml @@ -9,6 +9,8 @@ + + diff --git a/spring-soap/.gitignore b/spring-soap/.gitignore index cce17abdb9..2b04fd0719 100644 --- a/spring-soap/.gitignore +++ b/spring-soap/.gitignore @@ -1,2 +1,4 @@ /target/ sun-jaxb.episode + +**/gen/** \ No newline at end of file diff --git a/spring-soap/pom.xml b/spring-soap/pom.xml index 8188178d61..594eb3dd19 100644 --- a/spring-soap/pom.xml +++ b/spring-soap/pom.xml @@ -30,6 +30,22 @@ spring-boot-starter-test test + + + jakarta.xml.bind + jakarta.xml.bind-api + 4.0.0 + + + + org.glassfish.jaxb + jaxb-runtime + + + + javax.xml.bind + jaxb-api + @@ -42,7 +58,7 @@ org.codehaus.mojo jaxb2-maven-plugin - 1.6 + 3.1.0 xjc @@ -52,8 +68,10 @@ - ${project.basedir}/src/main/resources/ - ${project.basedir}/src/main/java + + src/main/resources/countries.xsd + + src/main/java false @@ -61,7 +79,7 @@ org.jvnet.jaxb2.maven2 maven-jaxb2-plugin - 0.14.0 + 0.15.3 @@ -83,4 +101,7 @@ + + 17 + \ No newline at end of file diff --git a/spring-soap/src/main/java/com/baeldung/springsoap/CountryEndpoint.java b/spring-soap/src/main/java/com/baeldung/springsoap/CountryEndpoint.java index 745131767a..49020cec21 100644 --- a/spring-soap/src/main/java/com/baeldung/springsoap/CountryEndpoint.java +++ b/spring-soap/src/main/java/com/baeldung/springsoap/CountryEndpoint.java @@ -1,12 +1,14 @@ package com.baeldung.springsoap; -import com.baeldung.springsoap.gen.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ws.server.endpoint.annotation.Endpoint; import org.springframework.ws.server.endpoint.annotation.PayloadRoot; import org.springframework.ws.server.endpoint.annotation.RequestPayload; import org.springframework.ws.server.endpoint.annotation.ResponsePayload; +import com.baeldung.springsoap.client.gen.GetCountryRequest; +import com.baeldung.springsoap.client.gen.GetCountryResponse; + @Endpoint public class CountryEndpoint { diff --git a/spring-soap/src/main/java/com/baeldung/springsoap/CountryRepository.java b/spring-soap/src/main/java/com/baeldung/springsoap/CountryRepository.java index 8a0f58a64e..183027cdb6 100644 --- a/spring-soap/src/main/java/com/baeldung/springsoap/CountryRepository.java +++ b/spring-soap/src/main/java/com/baeldung/springsoap/CountryRepository.java @@ -1,12 +1,15 @@ package com.baeldung.springsoap; -import com.baeldung.springsoap.gen.*; +import java.util.HashMap; +import java.util.Map; + +import javax.annotation.PostConstruct; + import org.springframework.stereotype.Component; import org.springframework.util.Assert; -import javax.annotation.PostConstruct; -import java.util.HashMap; -import java.util.Map; +import com.baeldung.springsoap.client.gen.Country; +import com.baeldung.springsoap.client.gen.Currency; @Component public class CountryRepository { diff --git a/spring-soap/src/main/java/com/baeldung/springsoap/WebServiceConfig.java b/spring-soap/src/main/java/com/baeldung/springsoap/WebServiceConfig.java index 930a961208..57636e9891 100644 --- a/spring-soap/src/main/java/com/baeldung/springsoap/WebServiceConfig.java +++ b/spring-soap/src/main/java/com/baeldung/springsoap/WebServiceConfig.java @@ -17,11 +17,11 @@ import org.springframework.xml.xsd.XsdSchema; public class WebServiceConfig extends WsConfigurerAdapter { @Bean - public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) { + public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); servlet.setTransformWsdlLocations(true); - return new ServletRegistrationBean(servlet, "/ws/*"); + return new ServletRegistrationBean<>(servlet, "/ws/*"); } @Bean(name = "countries") diff --git a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/Country.java b/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/Country.java deleted file mode 100644 index bb196d625d..0000000000 --- a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/Country.java +++ /dev/null @@ -1,139 +0,0 @@ - -package com.baeldung.springsoap.client.gen; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlSchemaType; -import javax.xml.bind.annotation.XmlType; - - -/** - *

Java class for country complex type. - * - *

The following schema fragment specifies the expected content contained within this class. - * - *

- * <complexType name="country">
- *   <complexContent>
- *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
- *       <sequence>
- *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
- *         <element name="population" type="{http://www.w3.org/2001/XMLSchema}int"/>
- *         <element name="capital" type="{http://www.w3.org/2001/XMLSchema}string"/>
- *         <element name="currency" type="{http://www.baeldung.com/springsoap/gen}currency"/>
- *       </sequence>
- *     </restriction>
- *   </complexContent>
- * </complexType>
- * 
- * - * - */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "country", propOrder = { - "name", - "population", - "capital", - "currency" -}) -public class Country { - - @XmlElement(required = true) - protected String name; - protected int population; - @XmlElement(required = true) - protected String capital; - @XmlElement(required = true) - @XmlSchemaType(name = "string") - protected Currency currency; - - /** - * Gets the value of the name property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getName() { - return name; - } - - /** - * Sets the value of the name property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setName(String value) { - this.name = value; - } - - /** - * Gets the value of the population property. - * - */ - public int getPopulation() { - return population; - } - - /** - * Sets the value of the population property. - * - */ - public void setPopulation(int value) { - this.population = value; - } - - /** - * Gets the value of the capital property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getCapital() { - return capital; - } - - /** - * Sets the value of the capital property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setCapital(String value) { - this.capital = value; - } - - /** - * Gets the value of the currency property. - * - * @return - * possible object is - * {@link Currency } - * - */ - public Currency getCurrency() { - return currency; - } - - /** - * Sets the value of the currency property. - * - * @param value - * allowed object is - * {@link Currency } - * - */ - public void setCurrency(Currency value) { - this.currency = value; - } - -} diff --git a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/Currency.java b/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/Currency.java deleted file mode 100644 index 023a8103e5..0000000000 --- a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/Currency.java +++ /dev/null @@ -1,40 +0,0 @@ - -package com.baeldung.springsoap.client.gen; - -import javax.xml.bind.annotation.XmlEnum; -import javax.xml.bind.annotation.XmlType; - - -/** - *

Java class for currency. - * - *

The following schema fragment specifies the expected content contained within this class. - *

- *

- * <simpleType name="currency">
- *   <restriction base="{http://www.w3.org/2001/XMLSchema}string">
- *     <enumeration value="GBP"/>
- *     <enumeration value="EUR"/>
- *     <enumeration value="PLN"/>
- *   </restriction>
- * </simpleType>
- * 
- * - */ -@XmlType(name = "currency") -@XmlEnum -public enum Currency { - - GBP, - EUR, - PLN; - - public String value() { - return name(); - } - - public static Currency fromValue(String v) { - return valueOf(v); - } - -} diff --git a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/GetCountryRequest.java b/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/GetCountryRequest.java deleted file mode 100644 index dcd5b1f08b..0000000000 --- a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/GetCountryRequest.java +++ /dev/null @@ -1,64 +0,0 @@ - -package com.baeldung.springsoap.client.gen; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlType; - - -/** - *

Java class for anonymous complex type. - * - *

The following schema fragment specifies the expected content contained within this class. - * - *

- * <complexType>
- *   <complexContent>
- *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
- *       <sequence>
- *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
- *       </sequence>
- *     </restriction>
- *   </complexContent>
- * </complexType>
- * 
- * - * - */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "", propOrder = { - "name" -}) -@XmlRootElement(name = "getCountryRequest") -public class GetCountryRequest { - - @XmlElement(required = true) - protected String name; - - /** - * Gets the value of the name property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getName() { - return name; - } - - /** - * Sets the value of the name property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setName(String value) { - this.name = value; - } - -} diff --git a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/GetCountryResponse.java b/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/GetCountryResponse.java deleted file mode 100644 index 11135c32e1..0000000000 --- a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/GetCountryResponse.java +++ /dev/null @@ -1,64 +0,0 @@ - -package com.baeldung.springsoap.client.gen; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlType; - - -/** - *

Java class for anonymous complex type. - * - *

The following schema fragment specifies the expected content contained within this class. - * - *

- * <complexType>
- *   <complexContent>
- *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
- *       <sequence>
- *         <element name="country" type="{http://www.baeldung.com/springsoap/gen}country"/>
- *       </sequence>
- *     </restriction>
- *   </complexContent>
- * </complexType>
- * 
- * - * - */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "", propOrder = { - "country" -}) -@XmlRootElement(name = "getCountryResponse") -public class GetCountryResponse { - - @XmlElement(required = true) - protected Country country; - - /** - * Gets the value of the country property. - * - * @return - * possible object is - * {@link Country } - * - */ - public Country getCountry() { - return country; - } - - /** - * Sets the value of the country property. - * - * @param value - * allowed object is - * {@link Country } - * - */ - public void setCountry(Country value) { - this.country = value; - } - -} diff --git a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/ObjectFactory.java b/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/ObjectFactory.java deleted file mode 100644 index e6d56d5aba..0000000000 --- a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/ObjectFactory.java +++ /dev/null @@ -1,56 +0,0 @@ - -package com.baeldung.springsoap.client.gen; - -import javax.xml.bind.annotation.XmlRegistry; - - -/** - * This object contains factory methods for each - * Java content interface and Java element interface - * generated in the com.baeldung.springsoap.client.gen package. - *

An ObjectFactory allows you to programatically - * construct new instances of the Java representation - * for XML content. The Java representation of XML - * content can consist of schema derived interfaces - * and classes representing the binding of schema - * type definitions, element declarations and model - * groups. Factory methods for each of these are - * provided in this class. - * - */ -@XmlRegistry -public class ObjectFactory { - - - /** - * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.baeldung.springsoap.client.gen - * - */ - public ObjectFactory() { - } - - /** - * Create an instance of {@link GetCountryRequest } - * - */ - public GetCountryRequest createGetCountryRequest() { - return new GetCountryRequest(); - } - - /** - * Create an instance of {@link GetCountryResponse } - * - */ - public GetCountryResponse createGetCountryResponse() { - return new GetCountryResponse(); - } - - /** - * Create an instance of {@link Country } - * - */ - public Country createCountry() { - return new Country(); - } - -} diff --git a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/package-info.java b/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/package-info.java deleted file mode 100644 index 9432e0c328..0000000000 --- a/spring-soap/src/main/java/com/baeldung/springsoap/client/gen/package-info.java +++ /dev/null @@ -1,2 +0,0 @@ -@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.baeldung.com/springsoap/gen", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) -package com.baeldung.springsoap.client.gen; diff --git a/spring-soap/src/test/java/com/baeldung/springsoap/ApplicationIntegrationTest.java b/spring-soap/src/test/java/com/baeldung/springsoap/ApplicationIntegrationTest.java index 3b071c286f..1150b8d91b 100644 --- a/spring-soap/src/test/java/com/baeldung/springsoap/ApplicationIntegrationTest.java +++ b/spring-soap/src/test/java/com/baeldung/springsoap/ApplicationIntegrationTest.java @@ -1,18 +1,19 @@ package com.baeldung.springsoap; -import com.baeldung.springsoap.gen.GetCountryRequest; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.oxm.jaxb.Jaxb2Marshaller; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.ClassUtils; import org.springframework.ws.client.core.WebServiceTemplate; -import static org.assertj.core.api.Assertions.assertThat; +import com.baeldung.springsoap.client.gen.GetCountryRequest; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @@ -20,7 +21,8 @@ public class ApplicationIntegrationTest { private Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); - @LocalServerPort private int port = 0; + @LocalServerPort + private int port = 0; @Before public void init() throws Exception { @@ -34,6 +36,8 @@ public class ApplicationIntegrationTest { GetCountryRequest request = new GetCountryRequest(); request.setName("Spain"); - assertThat(ws.marshalSendAndReceive("http://localhost:" + port + "/ws", request)).isNotNull(); + Object response = ws.marshalSendAndReceive("http://localhost:" + port + "/ws", request); + + assertThat(response).isNotNull(); } } diff --git a/web-modules/javax-servlets-2/pom.xml b/web-modules/javax-servlets-2/pom.xml index 81616e0055..9ba12352fd 100644 --- a/web-modules/javax-servlets-2/pom.xml +++ b/web-modules/javax-servlets-2/pom.xml @@ -22,6 +22,11 @@ commons-fileupload ${commons-fileupload.version} + + org.apache.commons + commons-text + ${commons-text.version} + javax.servlet @@ -76,6 +81,16 @@ + + org.eclipse.jetty + jetty-maven-plugin + ${jetty-maven-plugin.version} + + + / + + + @@ -84,6 +99,8 @@ 1.49 5.3.20 2.22.2 + 10.0.4 + 1.10.0 \ No newline at end of file diff --git a/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/LanguageServlet.java b/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/LanguageServlet.java new file mode 100644 index 0000000000..983066ec51 --- /dev/null +++ b/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/LanguageServlet.java @@ -0,0 +1,21 @@ +package com.baeldung.setparam; + +import java.io.IOException; +import java.util.Locale; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.*; + +@WebServlet(name = "LanguageServlet", urlPatterns = "/setparam/lang") +public class LanguageServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + + SetParameterRequestWrapper requestWrapper = new SetParameterRequestWrapper(request); + requestWrapper.setParameter("locale", Locale.getDefault().getLanguage()); + request.getRequestDispatcher("/setparam/3rd_party_module.jsp").forward(requestWrapper, response); + } + +} \ No newline at end of file diff --git a/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/SanitizeParametersFilter.java b/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/SanitizeParametersFilter.java new file mode 100644 index 0000000000..3f93591bd9 --- /dev/null +++ b/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/SanitizeParametersFilter.java @@ -0,0 +1,17 @@ +package com.baeldung.setparam; + +import java.io.IOException; +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; + +@WebFilter(urlPatterns = { "/setparam/with-sanitize.jsp" }) +public class SanitizeParametersFilter implements Filter { + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest httpReq = (HttpServletRequest) request; + chain.doFilter(new SanitizeParametersRequestWrapper(httpReq), response); + } + +} \ No newline at end of file diff --git a/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/SanitizeParametersRequestWrapper.java b/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/SanitizeParametersRequestWrapper.java new file mode 100644 index 0000000000..e4c2870ca6 --- /dev/null +++ b/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/SanitizeParametersRequestWrapper.java @@ -0,0 +1,46 @@ +package com.baeldung.setparam; + +import java.util.*; +import java.util.stream.Collectors; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; + +import org.apache.commons.text.StringEscapeUtils; + +public class SanitizeParametersRequestWrapper extends HttpServletRequestWrapper { + + private final Map sanitizedMap; + + public SanitizeParametersRequestWrapper(HttpServletRequest request) { + super(request); + sanitizedMap = Collections.unmodifiableMap( + request.getParameterMap().entrySet().stream() + .collect(Collectors.toMap( + Map.Entry::getKey, + entry -> Arrays.stream(entry.getValue()) + .map(StringEscapeUtils::escapeHtml4) + .toArray(String[]::new) + ))); + } + + @Override + public Map getParameterMap() { + return sanitizedMap; + } + + @Override + public String[] getParameterValues(String name) { + return Optional.ofNullable(getParameterMap().get(name)) + .map(values -> Arrays.copyOf(values, values.length)) + .orElse(null); + } + + @Override + public String getParameter(String name) { + return Optional.ofNullable(getParameterValues(name)) + .map(values -> values[0]) + .orElse(null); + } + +} \ No newline at end of file diff --git a/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/SetParameterRequestWrapper.java b/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/SetParameterRequestWrapper.java new file mode 100644 index 0000000000..7b2ab4597c --- /dev/null +++ b/web-modules/javax-servlets-2/src/main/java/com/baeldung/setparam/SetParameterRequestWrapper.java @@ -0,0 +1,40 @@ +package com.baeldung.setparam; + +import java.util.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; + +public class SetParameterRequestWrapper extends HttpServletRequestWrapper { + + private final Map paramMap; + + public SetParameterRequestWrapper(HttpServletRequest request) { + super(request); + paramMap = new HashMap<>(request.getParameterMap()); + } + + @Override + public Map getParameterMap() { + return Collections.unmodifiableMap(paramMap); + } + + @Override + public String[] getParameterValues(String name) { + return Optional.ofNullable(getParameterMap().get(name)) + .map(values -> Arrays.copyOf(values, values.length)) + .orElse(null); + } + + @Override + public String getParameter(String name) { + return Optional.ofNullable(getParameterValues(name)) + .map(values -> values[0]) + .orElse(null); + } + + public void setParameter(String name, String value) { + paramMap.put(name, new String[] {value}); + } + +} \ No newline at end of file diff --git a/web-modules/javax-servlets-2/src/main/webapp/setparam/3rd_party_module.jsp b/web-modules/javax-servlets-2/src/main/webapp/setparam/3rd_party_module.jsp new file mode 100644 index 0000000000..9ab4fd2c00 --- /dev/null +++ b/web-modules/javax-servlets-2/src/main/webapp/setparam/3rd_party_module.jsp @@ -0,0 +1,13 @@ +<%@ page import="java.util.*"%> + + + 3rd party Module + + + <% + String localeStr = request.getParameter("locale"); + Locale currentLocale = (localeStr != null ? new Locale(localeStr) : null); + %> + The language you have selected: <%=currentLocale != null ? currentLocale.getDisplayLanguage(currentLocale) : " None"%> + + \ No newline at end of file diff --git a/web-modules/javax-servlets-2/src/main/webapp/setparam/with-sanitize.jsp b/web-modules/javax-servlets-2/src/main/webapp/setparam/with-sanitize.jsp new file mode 100644 index 0000000000..68704d6133 --- /dev/null +++ b/web-modules/javax-servlets-2/src/main/webapp/setparam/with-sanitize.jsp @@ -0,0 +1,10 @@ + + + Sanitized request parameter + + + The text below comes from request parameter "input": +
+ <%=request.getParameter("input")%> + + \ No newline at end of file diff --git a/web-modules/javax-servlets-2/src/main/webapp/setparam/without-sanitize.jsp b/web-modules/javax-servlets-2/src/main/webapp/setparam/without-sanitize.jsp new file mode 100644 index 0000000000..f5437164ef --- /dev/null +++ b/web-modules/javax-servlets-2/src/main/webapp/setparam/without-sanitize.jsp @@ -0,0 +1,10 @@ + + + Non sanitized request parameter + + + The text below comes from request parameter "input": +
+ <%=request.getParameter("input")%> + + \ No newline at end of file diff --git a/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/LanguageServletIntegrationTest.java b/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/LanguageServletIntegrationTest.java new file mode 100644 index 0000000000..893ba06ce4 --- /dev/null +++ b/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/LanguageServletIntegrationTest.java @@ -0,0 +1,34 @@ +package com.baeldung.setparam; + +import static org.junit.Assert.assertTrue; + +import java.util.Locale; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.Test; + +public class LanguageServletIntegrationTest { + + @Test + public void whenGetRequestUsingHttpClient_thenResponseBodyContainsDefaultLanguage() throws Exception { + + // When + HttpClient client = HttpClientBuilder.create().build(); + HttpGet method = new HttpGet("http://localhost:8080/setparam/lang"); + HttpResponse httpResponse = client.execute(method); + + // Then + Locale defaultLocale = Locale.getDefault(); + String expectedLanguage = defaultLocale.getDisplayLanguage(defaultLocale); + + HttpEntity entity = httpResponse.getEntity(); + String responseBody = EntityUtils.toString(entity, "UTF-8"); + assertTrue(responseBody.contains("The language you have selected: " + expectedLanguage)); + } + +} diff --git a/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/SanitizeParametersRequestIntegrationTest.java b/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/SanitizeParametersRequestIntegrationTest.java new file mode 100644 index 0000000000..047d5431f6 --- /dev/null +++ b/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/SanitizeParametersRequestIntegrationTest.java @@ -0,0 +1,40 @@ +package com.baeldung.setparam; + +import static org.junit.Assert.assertTrue; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.BeforeClass; +import org.junit.Test; + +public class SanitizeParametersRequestIntegrationTest { + + private static String PARAM_INPUT; + + @BeforeClass + public static void init() throws UnsupportedEncodingException { + PARAM_INPUT = URLEncoder.encode("", "UTF-8"); + } + + @Test + public void whenInputParameterContainsXss_thenResponseBodyContainsSanitizedValue() throws Exception { + + // When + HttpClient client = HttpClientBuilder.create().build(); + HttpGet method = new HttpGet(String.format("http://localhost:8080/setparam/with-sanitize.jsp?input=%s", PARAM_INPUT)); + HttpResponse httpResponse = client.execute(method); + + // Then + HttpEntity entity = httpResponse.getEntity(); + String responseBody = EntityUtils.toString(entity, "UTF-8"); + assertTrue(responseBody.contains("<script>alert('Hello');</script>")); + } + +} \ No newline at end of file diff --git a/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/SanitizeParametersRequestWrapperUnitTest.java b/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/SanitizeParametersRequestWrapperUnitTest.java new file mode 100644 index 0000000000..4f246e0953 --- /dev/null +++ b/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/SanitizeParametersRequestWrapperUnitTest.java @@ -0,0 +1,60 @@ +package com.baeldung.setparam; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class SanitizeParametersRequestWrapperUnitTest { + + private String NEW_VALUE = "NEW VALUE"; + + private Map parameterMap; + + @Mock + private HttpServletRequest request; + + @Before + public void initBeforeEachTest() { + parameterMap = new HashMap<>(); + parameterMap.put("input", new String[] {""}); + when(request.getParameterMap()).thenReturn(parameterMap); + } + + @Test + public void whenGetParameterViaWrapper_thenParameterReturnedIsSanitized() { + SanitizeParametersRequestWrapper wrapper = new SanitizeParametersRequestWrapper(request); + String actualValue = wrapper.getParameter("input"); + + assertEquals(actualValue, "<script>alert('Hello');</script>"); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenPutValueToWrapperParameterMap_thenThrowsUnsupportedOperationException() { + SanitizeParametersRequestWrapper wrapper = new SanitizeParametersRequestWrapper(request); + Map wrapperParamMap = wrapper.getParameterMap(); + wrapperParamMap.put("input", new String[] {NEW_VALUE}); + } + + @Test + public void whenSetValueToWrapperParametersStringArray_thenThe2ndCallShouldNotEqualToNewValue() { + SanitizeParametersRequestWrapper wrapper = new SanitizeParametersRequestWrapper(request); + String[] firstCallValues = wrapper.getParameterValues("input"); + + firstCallValues[0] = NEW_VALUE; + String[] secondCallValues = wrapper.getParameterValues("input"); + assertNotEquals(firstCallValues, secondCallValues); + } + +} \ No newline at end of file diff --git a/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/SetParameterRequestWrapperUnitTest.java b/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/SetParameterRequestWrapperUnitTest.java new file mode 100644 index 0000000000..32343abb93 --- /dev/null +++ b/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/SetParameterRequestWrapperUnitTest.java @@ -0,0 +1,61 @@ +package com.baeldung.setparam; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class SetParameterRequestWrapperUnitTest { + + private String NEW_VALUE = "NEW VALUE"; + + private Map parameterMap; + + @Mock + private HttpServletRequest request; + + @Before + public void initBeforeEachTest() { + parameterMap = new HashMap<>(); + parameterMap.put("input", new String[] {"inputValue"}); + when(request.getParameterMap()).thenReturn(parameterMap); + } + + @Test + public void whenSetParameterViaWrapper_thenGetParameterShouldReturnTheSameValue() { + SetParameterRequestWrapper wrapper = new SetParameterRequestWrapper(request); + wrapper.setParameter("newInput", "newInputValue"); + String actualValue = wrapper.getParameter("newInput"); + + assertEquals(actualValue, "newInputValue"); + } + + @Test(expected = UnsupportedOperationException.class) + public void whenPutValueToWrapperParameterMap_thenThrowsUnsupportedOperationException() { + SetParameterRequestWrapper wrapper = new SetParameterRequestWrapper(request); + Map wrapperParamMap = wrapper.getParameterMap(); + wrapperParamMap.put("input", new String[] {NEW_VALUE}); + } + + @Test + public void whenSetValueToWrapperParametersStringArray_thenThe2ndCallShouldNotEqualToNewValue() { + SetParameterRequestWrapper wrapper = new SetParameterRequestWrapper(request); + String[] firstCallValues = wrapper.getParameterValues("input"); + + firstCallValues[0] = NEW_VALUE; + String[] secondCallValues = wrapper.getParameterValues("input"); + assertNotEquals(firstCallValues, secondCallValues); + } + +} \ No newline at end of file diff --git a/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/UnsanitizedParametersRequestIntegrationTest.java b/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/UnsanitizedParametersRequestIntegrationTest.java new file mode 100644 index 0000000000..a00944b6a4 --- /dev/null +++ b/web-modules/javax-servlets-2/src/test/java/com/baeldung/setparam/UnsanitizedParametersRequestIntegrationTest.java @@ -0,0 +1,42 @@ +package com.baeldung.setparam; + +import static org.junit.Assert.assertTrue; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.BeforeClass; +import org.junit.Test; + +public class UnsanitizedParametersRequestIntegrationTest { + + private static final String TAG_SCRIPT = ""; + + private static String PARAM_INPUT; + + @BeforeClass + public static void init() throws UnsupportedEncodingException { + PARAM_INPUT = URLEncoder.encode(TAG_SCRIPT, "UTF-8"); + } + + @Test + public void whenInputParameterContainsXss_thenResponseBodyContainsUnsanitizedValue() throws Exception { + + // When + HttpClient client = HttpClientBuilder.create().build(); + HttpGet method = new HttpGet(String.format("http://localhost:8080/setparam/without-sanitize.jsp?input=%s", PARAM_INPUT)); + HttpResponse httpResponse = client.execute(method); + + // Then + HttpEntity entity = httpResponse.getEntity(); + String responseBody = EntityUtils.toString(entity, "UTF-8"); + assertTrue(responseBody.contains(TAG_SCRIPT)); + } + +} \ No newline at end of file diff --git a/web-modules/pom.xml b/web-modules/pom.xml index c009837186..2dcec681ad 100644 --- a/web-modules/pom.xml +++ b/web-modules/pom.xml @@ -26,7 +26,7 @@ jee-7 jooby linkrest - + ratpack