Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>apache-poi-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-poi-3</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>${poi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-scratchpad</artifactId>
|
||||
<version>${poi.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<poi.version>5.2.3</poi.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.poi.excel.expandcolumn;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ExpandColumnUnitTest {
|
||||
|
||||
private Workbook workbook;
|
||||
private Sheet sheet;
|
||||
|
||||
@BeforeEach
|
||||
void prepareSpreadsheet() {
|
||||
workbook = new XSSFWorkbook();
|
||||
sheet = workbook.createSheet();
|
||||
|
||||
Row headerRow = sheet.createRow(0);
|
||||
Cell headerCell1 = headerRow.createCell(0);
|
||||
headerCell1.setCellValue("Full Name");
|
||||
Cell headerCell2 = headerRow.createCell(1);
|
||||
headerCell2.setCellValue("Abbreviation");
|
||||
|
||||
Row dataRow = sheet.createRow(1);
|
||||
Cell dataCell1 = dataRow.createCell(0);
|
||||
dataCell1.setCellValue("Java Virtual Machine");
|
||||
Cell dataCell2 = dataRow.createCell(1);
|
||||
dataCell2.setCellValue("JVM");
|
||||
|
||||
dataRow = sheet.createRow(2);
|
||||
dataCell1 = dataRow.createCell(0);
|
||||
dataCell1.setCellValue("Java Runtime Environment");
|
||||
dataCell2 = dataRow.createCell(1);
|
||||
dataCell2.setCellValue("JRE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenSetColumnWidth_thenColumnSetToTheSpecifiedWidth() {
|
||||
|
||||
Row row = sheet.getRow(2);
|
||||
String cellValue = row.getCell(0).getStringCellValue();
|
||||
int targetWidth = cellValue.length() * 256;
|
||||
|
||||
sheet.setColumnWidth(0, targetWidth);
|
||||
|
||||
assertEquals(targetWidth, sheet.getColumnWidth(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenAutoSizeColumn_thenColumnExpands() {
|
||||
|
||||
int originalWidth = sheet.getColumnWidth(0);
|
||||
|
||||
sheet.autoSizeColumn(0);
|
||||
|
||||
assertThat(sheet.getColumnWidth(0)).isGreaterThan(originalWidth);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() throws IOException {
|
||||
workbook.close();
|
||||
}
|
||||
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package com.baeldung.mergeandremoveduplicate;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class MergeArraysAndRemoveDuplicate {
|
||||
|
||||
public static int[] removeDuplicateOnSortedArray(int[] arr) {
|
||||
// Initialize a new array to store unique elements
|
||||
int[] uniqueArray = new int[arr.length];
|
||||
uniqueArray[0] = arr[0];
|
||||
int uniqueCount = 1;
|
||||
|
||||
// Iterate through the sorted array to remove duplicates
|
||||
for (int i = 1; i < arr.length; i++) {
|
||||
if (arr[i] != arr[i - 1]) {
|
||||
uniqueArray[uniqueCount] = arr[i];
|
||||
uniqueCount++;
|
||||
}
|
||||
}
|
||||
int[] truncatedArray = new int[uniqueCount];
|
||||
System.arraycopy(uniqueArray, 0, truncatedArray, 0, uniqueCount);
|
||||
return truncatedArray;
|
||||
}
|
||||
|
||||
public static int[] mergeAndRemoveDuplicatesUsingStream(int[] arr1, int[] arr2) {
|
||||
Stream<Integer> s1 = Arrays.stream(arr1).boxed();
|
||||
Stream<Integer> s2 = Arrays.stream(arr2).boxed();
|
||||
return Stream.concat(s1, s2)
|
||||
.distinct()
|
||||
.mapToInt(Integer::intValue)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
public static int[] mergeAndRemoveDuplicatesUsingSet(int[] arr1, int[] arr2) {
|
||||
int[] mergedArr = mergeArrays(arr1, arr2);
|
||||
Set<Integer> uniqueInts = new HashSet<>();
|
||||
|
||||
for (int el : mergedArr) {
|
||||
uniqueInts.add(el);
|
||||
}
|
||||
|
||||
return getArrayFromSet(uniqueInts);
|
||||
}
|
||||
|
||||
private static int[] getArrayFromSet(Set<Integer> set) {
|
||||
int[] mergedArrWithoutDuplicated = new int[set.size()];
|
||||
int i = 0;
|
||||
for (Integer el: set) {
|
||||
mergedArrWithoutDuplicated[i] = el;
|
||||
i++;
|
||||
}
|
||||
return mergedArrWithoutDuplicated;
|
||||
}
|
||||
|
||||
public static int[] mergeAndRemoveDuplicates(int[] arr1, int[] arr2) {
|
||||
return removeDuplicate(mergeArrays(arr1, arr2));
|
||||
}
|
||||
|
||||
public static int[] mergeAndRemoveDuplicatesOnSortedArray(int[] arr1, int[] arr2) {
|
||||
return removeDuplicateOnSortedArray(mergeArrays(arr1, arr2));
|
||||
}
|
||||
|
||||
private static int[] mergeArrays(int[] arr1, int[] arr2) {
|
||||
int[] mergedArrays = new int[arr1.length + arr2.length];
|
||||
System.arraycopy(arr1, 0, mergedArrays, 0, arr1.length);
|
||||
System.arraycopy(arr2, 0, mergedArrays, arr1.length, arr2.length);
|
||||
|
||||
return mergedArrays;
|
||||
}
|
||||
private static int[] removeDuplicate(int[] arr) {
|
||||
int[] withoutDuplicates = new int[arr.length];
|
||||
int i = 0;
|
||||
|
||||
for(int element : arr) {
|
||||
if(!isElementPresent(withoutDuplicates, element)) {
|
||||
withoutDuplicates[i] = element;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
int[] truncatedArray = new int[i];
|
||||
System.arraycopy(withoutDuplicates, 0, truncatedArray, 0, i);
|
||||
return truncatedArray;
|
||||
}
|
||||
|
||||
private static boolean isElementPresent(int[] arr, int element) {
|
||||
for(int el : arr) {
|
||||
if(el == element) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.mergeandremoveduplicate;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
public class MergeArraysAndRemoveDuplicateUnitTest {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(MergeArraysAndRemoveDuplicateUnitTest.class);
|
||||
|
||||
private static String getCommaDelimited(int[] arr) {
|
||||
return Arrays.stream(arr)
|
||||
.mapToObj(Integer::toString)
|
||||
.collect(Collectors.joining(", "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNoLibraryAndUnSortedArrays_whenArr1andArr2_thenMergeAndRemoveDuplicates() {
|
||||
int[] arr1 = {3, 2, 1, 4, 5, 6, 8, 7, 9};
|
||||
int[] arr2 = {8, 9, 10, 11, 12, 13, 15, 14, 15, 14, 16, 17};
|
||||
int[] expectedArr = {3, 2, 1, 4, 5, 6, 8, 7, 9, 10, 11, 12, 13, 15, 14, 16, 17};
|
||||
|
||||
int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicates(arr1, arr2);
|
||||
|
||||
//merged array maintains the order of the elements in the arrays
|
||||
assertArrayEquals(expectedArr, mergedArr);
|
||||
|
||||
logger.info(getCommaDelimited(mergedArr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNoLibraryAndSortedArrays_whenArr1andArr2_thenMergeAndRemoveDuplicates() {
|
||||
int[] arr1 = {1, 2, 3, 4, 5, 5, 6, 7, 7, 8};
|
||||
int[] arr2 = {8, 9, 10, 11, 12, 13, 14, 15, 15, 16, 17};
|
||||
int[] expectedArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
|
||||
|
||||
int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicatesOnSortedArray(arr1, arr2);
|
||||
|
||||
assertArrayEquals(expectedArr, mergedArr);
|
||||
|
||||
logger.info(getCommaDelimited(mergedArr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSet_whenArr1andArr2_thenMergeAndRemoveDuplicates() {
|
||||
int[] arr1 = {3, 2, 1, 4, 5, 6, 8, 7, 9};
|
||||
int[] arr2 = {8, 9, 10, 11, 12, 13, 15, 14, 15, 14, 16, 17};
|
||||
int[] expectedArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
|
||||
|
||||
int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicatesUsingSet(arr1, arr2);
|
||||
|
||||
assertArrayEquals(expectedArr, mergedArr);
|
||||
|
||||
logger.info(getCommaDelimited(mergedArr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStream_whenArr1andArr2_thenMergeAndRemoveDuplicates() {
|
||||
int[] arr1 = {3, 2, 1, 4, 5, 6, 8, 7, 9};
|
||||
int[] arr2 = {8, 9, 10, 11, 12, 13, 15, 14, 15, 14, 16, 17};
|
||||
int[] expectedArr = {3, 2, 1, 4, 5, 6, 8, 7, 9, 10, 11, 12, 13, 15, 14, 16, 17};
|
||||
|
||||
int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicatesUsingStream(arr1, arr2);
|
||||
|
||||
assertArrayEquals(expectedArr, mergedArr);
|
||||
|
||||
logger.info(getCommaDelimited(mergedArr));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?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>core-java-arrays-operations-advanced-2</artifactId>
|
||||
<name>core-java-arrays-operations-advanced-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+1
-1
@@ -62,4 +62,4 @@ public class MiddleOfArray {
|
||||
return array[mid];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -86,4 +86,4 @@ public class MiddleOfArrayUnitTest {
|
||||
MiddleOfArray middleOfArray = new MiddleOfArray();
|
||||
Assert.assertEquals(expectMedian, middleOfArray.medianOfArray(array, 0, 100));
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -53,7 +53,7 @@ public class IdentityHashMapDemonstrator {
|
||||
}
|
||||
}
|
||||
|
||||
private static class Book {
|
||||
static class Book {
|
||||
String title;
|
||||
int year;
|
||||
|
||||
|
||||
+43
@@ -1,8 +1,11 @@
|
||||
package com.baeldung.map.identity;
|
||||
|
||||
import com.baeldung.map.identity.IdentityHashMapDemonstrator.Book;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.IdentityHashMap;
|
||||
import org.junit.jupiter.api.condition.EnabledForJreRange;
|
||||
import org.junit.jupiter.api.condition.JRE;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@@ -18,4 +21,44 @@ public class IdentityHashMapDemonstratorUnitTest {
|
||||
assertEquals("Fantasy", identityHashMap.get("genre"));
|
||||
assertEquals("Drama", identityHashMap.get(newGenreKey));
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledForJreRange(max = JRE.JAVA_19)
|
||||
public void removeEntryComparingValueByEquality() {
|
||||
Book book = new Book("A Passage to India", 1924);
|
||||
IdentityHashMap<Book, String> identityHashMap = new IdentityHashMap<>(10);
|
||||
identityHashMap.put(book, "A great work of fiction");
|
||||
identityHashMap.remove(book, new String("A great work of fiction"));
|
||||
assertEquals(null, identityHashMap.get(book));
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledForJreRange(max = JRE.JAVA_19)
|
||||
public void replaceEntryComparingValueByEquality() {
|
||||
Book book = new Book("A Passage to India", 1924);
|
||||
IdentityHashMap<Book, String> identityHashMap = new IdentityHashMap<>(10);
|
||||
identityHashMap.put(book, "A great work of fiction");
|
||||
identityHashMap.replace(book, new String("A great work of fiction"), "One of the greatest books");
|
||||
assertEquals("One of the greatest books", identityHashMap.get(book));
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledForJreRange(min = JRE.JAVA_20)
|
||||
public void dontRemoveEntryComparingValueByEquality() {
|
||||
Book book = new Book("A Passage to India", 1924);
|
||||
IdentityHashMap<Book, String> identityHashMap = new IdentityHashMap<>(10);
|
||||
identityHashMap.put(book, "A great work of fiction");
|
||||
identityHashMap.remove(book, new String("A great work of fiction"));
|
||||
assertEquals("A great work of fiction", identityHashMap.get(book));
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledForJreRange(min = JRE.JAVA_20)
|
||||
public void dontReplaceEntryComparingValueByEquality() {
|
||||
Book book = new Book("A Passage to India", 1924);
|
||||
IdentityHashMap<Book, String> identityHashMap = new IdentityHashMap<>(10);
|
||||
identityHashMap.put(book, "A great work of fiction");
|
||||
identityHashMap.replace(book, new String("A great work of fiction"), "One of the greatest books");
|
||||
assertEquals("A great work of fiction", identityHashMap.get(book));
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -14,7 +14,7 @@ import org.apache.tika.mime.MimeTypeException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.j256.simplemagic.ContentInfo;
|
||||
import com.j256.simplemagic.ContentType;
|
||||
|
||||
public class ExtensionFromMimeTypeUnitTest {
|
||||
private static final String IMAGE_JPEG_MIME_TYPE = "image/jpeg";
|
||||
@@ -39,8 +39,7 @@ public class ExtensionFromMimeTypeUnitTest {
|
||||
@Test
|
||||
public void whenUsingMimetypesFileTypeMap_thenGetFileExtension() {
|
||||
List<String> expectedExtensions = Arrays.asList("jpeg", "jpg", "jpe");
|
||||
ContentInfo contentInfo = new ContentInfo("", IMAGE_JPEG_MIME_TYPE, "", true);
|
||||
String[] detectedExtensions = contentInfo.getFileExtensions();
|
||||
String[] detectedExtensions = ContentType.fromMimeType(IMAGE_JPEG_MIME_TYPE).getFileExtensions();
|
||||
assertThat(detectedExtensions).containsExactlyElementsOf(expectedExtensions);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ public class EchoIntegrationTest {
|
||||
|
||||
Executors.newSingleThreadExecutor()
|
||||
.submit(() -> new EchoServer().start(port));
|
||||
Thread.sleep(500);
|
||||
Thread.sleep(2000);
|
||||
}
|
||||
|
||||
private EchoClient client = new EchoClient();
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ public class GreetServerIntegrationTest {
|
||||
|
||||
Executors.newSingleThreadExecutor()
|
||||
.submit(() -> new GreetServer().start(port));
|
||||
Thread.sleep(500);
|
||||
Thread.sleep(2000);
|
||||
}
|
||||
|
||||
@Before
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ public class SocketEchoMultiIntegrationTest {
|
||||
s.close();
|
||||
|
||||
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(port));
|
||||
Thread.sleep(500);
|
||||
Thread.sleep(2000);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -65,6 +65,7 @@
|
||||
<module>core-java-arrays-convert</module>
|
||||
<module>core-java-arrays-operations-basic</module>
|
||||
<module>core-java-arrays-operations-advanced</module>
|
||||
<module>core-java-arrays-operations-advanced-2</module>
|
||||
<module>core-java-booleans</module>
|
||||
<module>core-java-char</module>
|
||||
<module>core-java-collections</module>
|
||||
|
||||
@@ -816,6 +816,7 @@
|
||||
<module>apache-olingo</module>
|
||||
|
||||
<module>apache-poi-2</module>
|
||||
<module>apache-poi-3</module>
|
||||
<module>apache-thrift</module>
|
||||
<module>apache-tika</module>
|
||||
|
||||
@@ -1089,6 +1090,7 @@
|
||||
<module>apache-olingo</module>
|
||||
|
||||
<module>apache-poi-2</module>
|
||||
<module>apache-poi-3</module>
|
||||
<module>apache-thrift</module>
|
||||
<module>apache-tika</module>
|
||||
|
||||
|
||||
@@ -10,9 +10,10 @@
|
||||
<description>This is simple boot application demonstrating a custom auto-configuration</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -66,6 +67,13 @@
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.autoconfiguration.example.AutoconfigurationApplication</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
+4
-3
@@ -3,10 +3,10 @@ package com.baeldung.autoconfiguration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionMessage.Style;
|
||||
@@ -20,7 +20,6 @@ import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -31,7 +30,9 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
@Configuration
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
|
||||
@AutoConfiguration
|
||||
@ConditionalOnClass(DataSource.class)
|
||||
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
|
||||
@PropertySource("classpath:mysql.properties")
|
||||
|
||||
+4
-2
@@ -1,11 +1,13 @@
|
||||
package com.baeldung.autoconfiguration.annotationprocessor;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "database")
|
||||
public class DatabaseProperties {
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
package com.baeldung.autoconfiguration.example;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class MyUser {
|
||||
|
||||
-1
@@ -1 +0,0 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfiguration.MySQLAutoconfiguration
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
com.baeldung.autoconfiguration.MySQLAutoconfiguration
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
usemysql=local
|
||||
|
||||
mysql-hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
|
||||
mysql-hibernate.dialect=org.hibernate.dialect.MySQLDialect
|
||||
mysql-hibernate.show_sql=true
|
||||
mysql-hibernate.hbm2ddl.auto=create-drop
|
||||
@@ -27,7 +27,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.ulisesbocchio</groupId>
|
||||
<artifactId>jasypt-spring-boot-starter</artifactId>
|
||||
<artifactId>jasypt-spring-boot-starter</artifactId>
|
||||
<version>${jasypt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -46,15 +46,7 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestone</id>
|
||||
<name>Spring Milestone</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<jasypt.version>2.0.0</jasypt.version>
|
||||
<jasypt.version>3.0.5</jasypt.version>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant Articles
|
||||
@@ -0,0 +1,56 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.1.3</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<artifactId>springbootsslbundles</artifactId>
|
||||
<name>spring-boot-ssl-bundles</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Module for showing usage of SSL Bundles</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5-fluent</artifactId>
|
||||
<version>${apache.client5.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<apache.client5.version>5.0.3</apache.client5.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.springbootsslbundles;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.boot.ssl.SslBundles;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SSLBundlesApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SSLBundlesApp.class, args);
|
||||
}
|
||||
@Bean
|
||||
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder, SslBundles sslBundles) {
|
||||
return restTemplateBuilder.setSslBundle(sslBundles.getBundle("secure-service")).build();
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.springbootsslbundles;
|
||||
|
||||
import org.apache.hc.client5.http.classic.HttpClient;
|
||||
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.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ssl.NoSuchSslBundleException;
|
||||
import org.springframework.boot.ssl.SslBundle;
|
||||
import org.springframework.boot.ssl.SslBundles;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
@Component
|
||||
public class SecureRestTemplateConfig {
|
||||
private final SSLContext sslContext;
|
||||
|
||||
@Autowired
|
||||
public SecureRestTemplateConfig(SslBundles sslBundles) throws NoSuchSslBundleException {
|
||||
SslBundle sslBundle = sslBundles.getBundle("secure-service");
|
||||
this.sslContext = sslBundle.createSslContext();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestTemplate secureRestTemplate() {
|
||||
final SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create().setSslContext(this.sslContext).build();
|
||||
final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create().setSSLSocketFactory(sslSocketFactory).build();
|
||||
HttpClient httpClient = HttpClients.custom().setConnectionManager(cm).evictExpiredConnections().build();
|
||||
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
|
||||
return new RestTemplate(factory);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.springbootsslbundles;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Service
|
||||
public class SecureServiceRestApi {
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
public SecureServiceRestApi(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
public String fetchData(String dataId) {
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
"https://secure-service.com/api/data/{id}",
|
||||
HttpMethod.GET,
|
||||
null,
|
||||
String.class,
|
||||
dataId
|
||||
);
|
||||
return response.getBody();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
spring:
|
||||
ssl:
|
||||
bundle:
|
||||
jks:
|
||||
secure-service:
|
||||
key:
|
||||
alias: "secure-service"
|
||||
keystore:
|
||||
location: "classpath:keystore.p12"
|
||||
password: "FooBar"
|
||||
type: "PKCS12"
|
||||
@@ -0,0 +1,31 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
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
|
||||
-----END CERTIFICATE-----
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user