Merge branch 'eugenp:master' into master
This commit is contained in:
commit
5ceb08d88e
|
@ -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<String> 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<String> 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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
|||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
### Relevant Articles:
|
||||
- [Spring Boot Configuration Properties Migrator](https://www.baeldung.com/spring-boot-properties-migrator)
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue