Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/eugenp/tutorials into swa…
Browse files Browse the repository at this point in the history
…gger-spring-sec-3
  • Loading branch information
abh1navv committed Oct 19, 2024
2 parents 46985f4 + 07e7651 commit 19337b7
Show file tree
Hide file tree
Showing 931 changed files with 69,626 additions and 2,288 deletions.
16 changes: 16 additions & 0 deletions algorithms-modules/algorithms-miscellaneous-10/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<artifactId>algorithms-miscellaneous-9</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>algorithms-miscellaneous-9</name>

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

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baeldung.algorithms.firstduplicate;

import java.util.HashSet;

public class FirstDuplicate {

public int firstDuplicateBruteForce(int[] arr) {
int minIndex = arr.length;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
minIndex = Math.min(minIndex, j);
break;
}
}
}
return minIndex == arr.length ? -1 : minIndex;
}

public int firstDuplicateHashSet(int[] arr) {
HashSet<Integer> firstDuplicateSet = new HashSet<>();
for (int i = 0; i < arr.length; i++) {
if (firstDuplicateSet.contains(arr[i])) {
return i;
}
firstDuplicateSet.add(arr[i]);
}
return -1;
}

public int firstDuplicateArrayIndexing(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int val = Math.abs(arr[i]) - 1;
if (arr[val] < 0) {
return i;
}
arr[val] = -arr[val];
}
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baeldung.algorithms.firstduplicate;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class FirstDuplicateUnitTest {

@Test
public void givenArray_whenUsingBruteForce_thenFindFirstDuplicate() {
FirstDuplicate firstDuplicate = new FirstDuplicate();

assertEquals(4, firstDuplicate.firstDuplicateBruteForce(new int[] { 2, 1, 3, 5, 3, 2 }));
assertEquals(-1, firstDuplicate.firstDuplicateBruteForce(new int[] { 1, 2, 3, 4, 5 }));
assertEquals(2, firstDuplicate.firstDuplicateBruteForce(new int[] { 2, 1, 1, 2 }));
assertEquals(1, firstDuplicate.firstDuplicateBruteForce(new int[] { 1, 1 }));
assertEquals(-1, firstDuplicate.firstDuplicateBruteForce(new int[] {}));
}

@Test
public void givenArray_whenUsingHashSet_thenFindFirstDuplicate() {
FirstDuplicate firstDuplicate = new FirstDuplicate();

assertEquals(4, firstDuplicate.firstDuplicateHashSet(new int[] { 2, 1, 3, 5, 3, 2 }));
assertEquals(-1, firstDuplicate.firstDuplicateHashSet(new int[] { 1, 2, 3, 4, 5 }));
assertEquals(2, firstDuplicate.firstDuplicateHashSet(new int[] { 2, 1, 1, 2 }));
assertEquals(1, firstDuplicate.firstDuplicateHashSet(new int[] { 1, 1 }));
assertEquals(-1, firstDuplicate.firstDuplicateHashSet(new int[] {}));
}

@Test
public void givenArray_whenUsingArrayIndexing_thenFindFirstDuplicate() {
FirstDuplicate firstDuplicate = new FirstDuplicate();

assertEquals(4, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 2, 1, 3, 5, 3, 2 }));
assertEquals(-1, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 1, 2, 3, 4, 5 }));
assertEquals(2, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 2, 1, 1, 2 }));
assertEquals(1, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 1, 1 }));
assertEquals(-1, firstDuplicate.firstDuplicateArrayIndexing(new int[] {}));
}
}

9 changes: 7 additions & 2 deletions algorithms-modules/algorithms-sorting-2/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
### Relevant Articles:

- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers)
- [Guide to In-Place Sorting Algorithm Works with a Java Implementation](https://www.baeldung.com/java-in-place-sorting)
- [Partitioning and Sorting Arrays with Many Repeated Entries with Java Examples](https://www.baeldung.com/java-sorting-arrays-with-repeated-entries)
- [Gravity/Bead Sort in Java](https://www.baeldung.com/java-gravity-bead-sort)
- More articles: [[<-- prev]](/algorithms-sorting)
- [Selection Sort in Java](https://www.baeldung.com/java-selection-sort)
- [Bubble Sort in Java](https://www.baeldung.com/java-bubble-sort)
- [Insertion Sort in Java](https://www.baeldung.com/java-insertion-sort)
- [Heap Sort in Java](https://www.baeldung.com/java-heap-sort)
- [Counting Sort in Java](https://www.baeldung.com/java-counting-sort)
- [Radix Sort in Java](https://www.baeldung.com/java-radix-sort)
- More articles: [[<-- prev]](/algorithms-modules/algorithms-sorting)
10 changes: 2 additions & 8 deletions algorithms-modules/algorithms-sorting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
This module contains articles about sorting algorithms.

### Relevant articles:

- [Bubble Sort in Java](https://www.baeldung.com/java-bubble-sort)
- [Merge Sort in Java](https://www.baeldung.com/java-merge-sort)
- [Quicksort Algorithm Implementation in Java](https://www.baeldung.com/java-quicksort)
- [Insertion Sort in Java](https://www.baeldung.com/java-insertion-sort)
- [Heap Sort in Java](https://www.baeldung.com/java-heap-sort)
- [Shell Sort in Java](https://www.baeldung.com/java-shell-sort)
- [Counting Sort in Java](https://www.baeldung.com/java-counting-sort)
- [Selection Sort in Java](https://www.baeldung.com/java-selection-sort)
- [Radix Sort in Java](https://www.baeldung.com/java-radix-sort)
- [Bucket Sort in Java](https://www.baeldung.com/java-bucket-sort)
- More articles: [[next -->]](/algorithms-sorting-2)
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)
- More articles: [[next -->]](/algorithms-modules/algorithms-sorting-2)
18 changes: 10 additions & 8 deletions apache-httpclient4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-spring-5</artifactId>
<artifactId>parent-spring-6</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-spring-5</relativePath>
<relativePath>../parent-spring-6</relativePath>
</parent>

<dependencies>
Expand Down Expand Up @@ -91,8 +91,8 @@
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
<version>${httpcore.version}</version>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -165,9 +165,9 @@
</dependency>
<!-- web -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>${jakarta.servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -228,14 +228,16 @@

<properties>
<spring-oxm.version>6.1.4</spring-oxm.version>
<spring-security.version>6.3.1</spring-security.version>
<!-- util -->
<commons-codec.version>1.16.0</commons-codec.version>
<httpasyncclient.version>4.1.5</httpasyncclient.version>
<!-- testing -->
<wiremock.version>3.9.1</wiremock.version>
<httpcore.version>4.4.16</httpcore.version>
<httpcore.version>5.3</httpcore.version>
<httpclient.version>4.5.14</httpclient.version>
<mockserver.version>5.11.2</mockserver.version>
<jakarta.servlet-api.version>6.1.0</jakarta.servlet-api.version>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.apache.hc.core5.http.protocol.BasicHttpContext;
import org.apache.hc.core5.http.protocol.HttpContext;

public class HttpComponentsClientHttpRequestFactoryBasicAuth extends HttpComponentsClientHttpRequestFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import java.io.IOException;

public class CustomFilter extends GenericFilterBean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/securityNone")
.requestMatchers("/securityNone")
.permitAll()
.anyRequest()
.authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class ClientLiveTest extends GetRequestMockServer {


@Test
/*@Test
void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_2() throws GeneralSecurityException {
final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
Expand All @@ -56,7 +56,7 @@ void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_2() throws Gene
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(simplePathUrl, HttpMethod.GET, null, String.class);
assertThat(response.getStatusCode().value(), equalTo(200));
}
}*/

@Test
void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws IOException {
Expand All @@ -72,7 +72,7 @@ void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws I
}
}

@Test
/*@Test
void givenAcceptingAllCertificates_whenUsingRestTemplate_thenCorrect() {
final CloseableHttpClient httpClient = HttpClients.custom()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
Expand All @@ -82,7 +82,7 @@ void givenAcceptingAllCertificates_whenUsingRestTemplate_thenCorrect() {
final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(simplePathUrl, HttpMethod.GET, null, String.class);
assertThat(response.getStatusCode().value(), equalTo(200));
}
}*/

@Test
void whenHttpsUrlIsConsumed_thenException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RestClientV4LiveManualTest {

final String urlOverHttps = "http://localhost:8082/httpclient-simple/api/bars/1";

@Test
/*@Test
void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_2() throws GeneralSecurityException {
final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
Expand All @@ -59,7 +59,7 @@ void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_2() throws Gene
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
assertThat(response.getStatusCode().value(), equalTo(200));
}
}*/

@Test
void givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumed_thenCorrect() throws IOException {
Expand All @@ -70,15 +70,15 @@ void givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumed_thenCorrect()
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

@Test
/*@Test
void givenAcceptingAllCertificatesUsing4_4_whenUsingRestTemplate_thenCorrect(){
final CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
assertThat(response.getStatusCode().value(), equalTo(200));
}
}*/

@Test
void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
Expand Down
1 change: 1 addition & 0 deletions apache-kafka-3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ You can build the project from the command line using: *mvn clean install*, or i
- [Get Last N Messages in Apache Kafka Topic](https://www.baeldung.com/java-apache-kafka-get-last-n-messages)
- [Get Partition Count for a Topic in Kafka](https://www.baeldung.com/java-kafka-partition-count-topic)
- [Retries With Kafka Producer](https://www.baeldung.com/kafka-producer-retries)
- [Handling Kafka Producer TimeOutException with Java](https://www.baeldung.com/java-kafka-timeoutexception)

3 changes: 2 additions & 1 deletion apache-libraries-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
- [Introduction to Apache BVal](https://www.baeldung.com/apache-bval)
- [Building a Microservice with Apache Meecrowave](https://www.baeldung.com/apache-meecrowave)
- [A Quick Guide to Apache Geode](https://www.baeldung.com/apache-geode)
- More articles: [[<-- prev]](../apache-libraries)
- [Convert Avro File to JSON File in Java](https://www.baeldung.com/java-avro-json)
- More articles: [[<-- prev]](../apache-libraries)
Loading

0 comments on commit 19337b7

Please sign in to comment.