diff --git a/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpclient/basicauthentication/HttpClientBasicAuthentication.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpclient/basicauthentication/HttpClientBasicAuthentication.java new file mode 100644 index 0000000000..3ade3bd553 --- /dev/null +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpclient/basicauthentication/HttpClientBasicAuthentication.java @@ -0,0 +1,67 @@ +package com.baeldung.httpclient.basicauthentication; + +import java.io.IOException; +import java.net.Authenticator; +import java.net.PasswordAuthentication; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodyHandlers; +import java.util.Base64; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HttpClientBasicAuthentication { + + private static final Logger logger = LoggerFactory.getLogger(HttpClientBasicAuthentication.class); + + public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException { + useClientWithAuthenticator(); + useClientWithHeaders(); + } + + private static void useClientWithAuthenticator() throws URISyntaxException, IOException, InterruptedException { + HttpClient client = HttpClient.newBuilder() + .authenticator(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication("postman", "password".toCharArray()); + } + }) + .build(); + + HttpRequest request = HttpRequest.newBuilder() + .GET() + .uri(new URI("https://postman-echo.com/basic-auth")) + .build(); + + HttpResponse response = client.send(request, BodyHandlers.ofString()); + + logger.info("Status using authenticator {}", response.statusCode()); + } + + private static void useClientWithHeaders() throws IOException, InterruptedException, URISyntaxException { + HttpClient client = HttpClient.newBuilder() + .build(); + + HttpRequest request = HttpRequest.newBuilder() + .GET() + .uri(new URI("https://postman-echo.com/basic-auth")) + .header("Authorization", getBasicAuthenticationHeader("postman", "password")) + .build(); + + HttpResponse response = client.send(request, BodyHandlers.ofString()); + + logger.info("Status using headers: {}", response.statusCode()); + } + + private static final String getBasicAuthenticationHeader(String username, String password) { + String valueToEncode = username + ":" + password; + return "Basic " + Base64.getEncoder() + .encodeToString(valueToEncode.getBytes()); + } + +} diff --git a/core-java-modules/core-java-collections-list-3/README.md b/core-java-modules/core-java-collections-list-3/README.md index 967e148a99..bcc8b3f3ed 100644 --- a/core-java-modules/core-java-collections-list-3/README.md +++ b/core-java-modules/core-java-collections-list-3/README.md @@ -11,4 +11,5 @@ This module contains articles about the Java List collection - [Filtering a Java Collection by a List](https://www.baeldung.com/java-filter-collection-by-list) - [How to Count Duplicate Elements in Arraylist](https://www.baeldung.com/java-count-duplicate-elements-arraylist) - [Finding the Differences Between Two Lists in Java](https://www.baeldung.com/java-lists-difference) +- [List vs. ArrayList in Java](https://www.baeldung.com/java-list-vs-arraylist) - [[<-- Prev]](/core-java-modules/core-java-collections-list-2) diff --git a/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/subtractdays/SubtractDaysFromDateUnitTest.java b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/subtractdays/SubtractDaysFromDateUnitTest.java index 9afb6e8db0..91cd29cd54 100644 --- a/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/subtractdays/SubtractDaysFromDateUnitTest.java +++ b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/subtractdays/SubtractDaysFromDateUnitTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import java.text.SimpleDateFormat; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -15,7 +16,7 @@ public class SubtractDaysFromDateUnitTest { @Test public void givenLocalDateTime_whenSubtractingFiveDays_dateIsChangedCorrectly() { - LocalDate localDateTime = LocalDate.of(2022, 4, 20); + LocalDateTime localDateTime = LocalDateTime.of(2022, 4, 20, 0, 0); localDateTime = localDateTime.minusDays(5); diff --git a/core-java-modules/core-java-lang-4/README.md b/core-java-modules/core-java-lang-4/README.md index a145633d2b..befef0b6eb 100644 --- a/core-java-modules/core-java-lang-4/README.md +++ b/core-java-modules/core-java-lang-4/README.md @@ -11,3 +11,4 @@ This module contains articles about core features in the Java language - [Fixing the “Declared package does not match the expected package” Error](https://www.baeldung.com/java-declared-expected-package-error) - [Chaining Constructors in Java](https://www.baeldung.com/java-chain-constructors) - [Difference Between POJO, JavaBeans, DTO and VO](https://www.baeldung.com/java-pojo-javabeans-dto-vo) +- [Implements vs. Extends in Java](https://www.baeldung.com/java-implements-vs-extends) diff --git a/jackson-modules/jackson-annotations/README.md b/jackson-modules/jackson-annotations/README.md index 783a06605b..3b6cd6f20b 100644 --- a/jackson-modules/jackson-annotations/README.md +++ b/jackson-modules/jackson-annotations/README.md @@ -7,3 +7,4 @@ This module contains articles about Jackson annotations. - [More Jackson Annotations](https://www.baeldung.com/jackson-advanced-annotations) - [Jackson – Bidirectional Relationships](https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion) - [Jackson JSON Views](https://www.baeldung.com/jackson-json-view-annotation) +- [Deduction-Based Polymorphism in Jackson 2.12](https://www.baeldung.com/jackson-deduction-based-polymorphism) diff --git a/java-numbers-4/README.md b/java-numbers-4/README.md index 95d46203ab..267639a829 100644 --- a/java-numbers-4/README.md +++ b/java-numbers-4/README.md @@ -6,3 +6,4 @@ - [Guide to Java BigInteger](https://www.baeldung.com/java-biginteger) - [Automorphic Numbers in Java](https://www.baeldung.com/java-automorphic-numbers) - [Convert Byte Size Into a Human-Readable Format in Java](https://www.baeldung.com/java-human-readable-byte-size) +- [Convert boolean to int in Java](https://www.baeldung.com/java-boolean-to-int) diff --git a/persistence-modules/hibernate-exceptions/README.md b/persistence-modules/hibernate-exceptions/README.md index 2e5a98c2f8..c485de2f34 100644 --- a/persistence-modules/hibernate-exceptions/README.md +++ b/persistence-modules/hibernate-exceptions/README.md @@ -5,3 +5,4 @@ - [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception) - [Hibernate’s “Object References an Unsaved Transient Instance” Error](https://www.baeldung.com/hibernate-unsaved-transient-instance-error) - [EntityNotFoundException in Hibernate](https://www.baeldung.com/hibernate-entitynotfoundexception) +- [Hibernate’s “Not-Null Property References a Null or Transient Value” Error](https://www.baeldung.com/hibernate-not-null-error) diff --git a/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java b/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java index 240e54ab99..df17e27351 100644 --- a/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java +++ b/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java @@ -7,7 +7,7 @@ import com.mongodb.ServerAddress; public class ConnectionCheck { - public static void checkingConnection() { + public static MongoClient checkingConnection() { MongoClientOptions.Builder builder = MongoClientOptions.builder(); @@ -27,9 +27,10 @@ public class ConnectionCheck { System.out.println(db.getStats()); } catch (Exception e) { System.out.println("MongoDB Server is Down"); - mongoClient.close(); } + return mongoClient; + } public static void main(String[] args) { @@ -42,5 +43,4 @@ public class ConnectionCheck { } -} - +} \ No newline at end of file diff --git a/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java b/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java index 69f356997c..32014a4473 100644 --- a/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java +++ b/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java @@ -3,45 +3,22 @@ package com.baeldung.mongo; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import java.io.IOException; - -import org.junit.BeforeClass; import org.junit.Test; +import com.baeldung.ConnectionCheck; import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; -import com.mongodb.ServerAddress; public class ConnectionCheckLiveTest { - private static MongoClient mongoClient; - private static MongoClientOptions.Builder builder; - private static ServerAddress ServerAddress; - - @BeforeClass - public static void setup() throws IOException { - if (mongoClient == null) { - - builder = MongoClientOptions.builder(); - builder.connectionsPerHost(100); - builder.maxWaitTime(60000); - builder.connectTimeout(1500); - builder.socketTimeout(60000); - builder.socketKeepAlive(true); - - ServerAddress = new ServerAddress("localhost", 27017); - mongoClient = new MongoClient(ServerAddress, builder.build()); - - } - } + ConnectionCheck ConnectionCheck = new ConnectionCheck(); @Test public void givenMongoClient_whenConnectionCheck_thenCheckingForConnectionPoint() { + MongoClient mongoClient = ConnectionCheck.checkingConnection(); String connectionPoint = mongoClient.getConnectPoint(); assertNotNull(connectionPoint); assertFalse(connectionPoint.isEmpty()); } -} - +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-migrator-demo/README.md b/spring-boot-modules/spring-boot-properties-migrator-demo/README.md new file mode 100644 index 0000000000..c00eae2681 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-migrator-demo/README.md @@ -0,0 +1,3 @@ + +### Relevant Articles: +- [Spring Boot Configuration Properties Migrator](https://www.baeldung.com/spring-boot-properties-migrator) diff --git a/spring-cloud/spring-cloud-eureka/README.md b/spring-cloud/spring-cloud-eureka/README.md index 5fc96256f4..0029ea60f6 100644 --- a/spring-cloud/spring-cloud-eureka/README.md +++ b/spring-cloud/spring-cloud-eureka/README.md @@ -1,3 +1,4 @@ ### Relevant Articles: - [Introduction to Spring Cloud Netflix – Eureka](http://www.baeldung.com/spring-cloud-netflix-eureka) - [Integration Tests With Spring Cloud Netflix and Feign](https://www.baeldung.com/spring-cloud-feign-integration-tests) +- [Spring Cloud – Disable Discovery Clients with Profiles](https://www.baeldung.com/spring-cloud-disable-discovery-clients)