From d4486bbaa1892adc8771dfd517f3378c0a1f9268 Mon Sep 17 00:00:00 2001 From: Benoit Orihuela Date: Tue, 31 Oct 2023 10:59:19 +0100 Subject: [PATCH] feat: switch to Jakarta EE API for XML binding --- nifi-file-identity-provider/pom.xml | 13 +++++- .../authentication/file/CredentialsStore.java | 42 +++++++------------ .../file/TestCredentialsStore.java | 5 ++- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/nifi-file-identity-provider/pom.xml b/nifi-file-identity-provider/pom.xml index c15fd3c..93d6d17 100644 --- a/nifi-file-identity-provider/pom.xml +++ b/nifi-file-identity-provider/pom.xml @@ -117,7 +117,7 @@ org.glassfish.jaxb jaxb-runtime - 2.3.5 + 4.0.4 @@ -144,6 +144,17 @@ nifi-properties 1.23.2 + + jakarta.xml.bind + jakarta.xml.bind-api + 4.0.0 + + + com.sun.xml.bind + jaxb-impl + 4.0.0 + runtime + ch.qos.logback logback-classic diff --git a/nifi-file-identity-provider/src/main/java/io/egm/nifi/authentication/file/CredentialsStore.java b/nifi-file-identity-provider/src/main/java/io/egm/nifi/authentication/file/CredentialsStore.java index 1ab934e..25a290e 100644 --- a/nifi-file-identity-provider/src/main/java/io/egm/nifi/authentication/file/CredentialsStore.java +++ b/nifi-file-identity-provider/src/main/java/io/egm/nifi/authentication/file/CredentialsStore.java @@ -16,29 +16,22 @@ package io.egm.nifi.authentication.file; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.InvalidObjectException; -import java.util.List; -import javax.xml.XMLConstants; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBElement; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; -import javax.xml.bind.ValidationEvent; -import javax.xml.bind.ValidationEventHandler; -import javax.xml.transform.stream.StreamSource; -import javax.xml.validation.Schema; -import javax.xml.validation.SchemaFactory; - import io.egm.nifi.authentication.file.generated.ObjectFactory; import io.egm.nifi.authentication.file.generated.UserCredentials; import io.egm.nifi.authentication.file.generated.UserCredentialsList; - +import jakarta.xml.bind.*; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; +import javax.xml.XMLConstants; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.InvalidObjectException; +import java.util.List; + /** * Data access for a simple local XML credentials file. The credentials file @@ -55,7 +48,7 @@ public class CredentialsStore { private static final JAXBContext JAXB_CONTEXT = initializeJaxbContext(); private static final ObjectFactory factory = new ObjectFactory(); - private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); private File credentialsFile; private long credentialsListLastModified; private UserCredentialsList credentialsList = factory.createUserCredentialsList(); @@ -68,12 +61,7 @@ private static JAXBContext initializeJaxbContext() { } } - private static ValidationEventHandler defaultValidationEventHandler = new ValidationEventHandler() { - @Override - public boolean handleEvent(ValidationEvent event) { - return false; - } - }; + private static final ValidationEventHandler defaultValidationEventHandler = event -> false; static UserCredentialsList loadCredentialsList(String filePath) throws Exception { final File credentialsFile = new File(filePath); @@ -90,8 +78,7 @@ static UserCredentialsList loadCredentialsList(File credentialsFile, ValidationE unmarshaller.setEventHandler(validationEventHandler); final JAXBElement element = unmarshaller.unmarshal(new StreamSource(credentialsFile), UserCredentialsList.class); - UserCredentialsList credentialsList = element.getValue(); - return credentialsList; + return element.getValue(); } else { final String notFoundMessage = "The credentials configuration file was not found at: " + credentialsFile.getAbsolutePath(); @@ -110,8 +97,7 @@ static void saveCredentialsList(UserCredentialsList credentialsList, File saveFi public static CredentialsStore fromFile(String filePath) throws Exception { UserCredentialsList credentialsList = loadCredentialsList(filePath); - CredentialsStore credStore = new CredentialsStore(credentialsList); - return credStore; + return new CredentialsStore(credentialsList); } public static CredentialsStore fromFile(File file) throws Exception { diff --git a/nifi-file-identity-provider/src/test/java/io/egm/nifi/authentication/file/TestCredentialsStore.java b/nifi-file-identity-provider/src/test/java/io/egm/nifi/authentication/file/TestCredentialsStore.java index b7576cd..6c46a44 100644 --- a/nifi-file-identity-provider/src/test/java/io/egm/nifi/authentication/file/TestCredentialsStore.java +++ b/nifi-file-identity-provider/src/test/java/io/egm/nifi/authentication/file/TestCredentialsStore.java @@ -18,6 +18,7 @@ import io.egm.nifi.authentication.file.generated.UserCredentials; import io.egm.nifi.authentication.file.generated.UserCredentialsList; +import jakarta.xml.bind.UnmarshalException; import org.apache.commons.io.FileUtils; import org.junit.jupiter.api.Test; @@ -59,7 +60,7 @@ public void testLoadCredentialsFile() throws Exception { public void testLoadInvalidCredentialsFileMessaging() throws Exception { try { CredentialsStore.loadCredentialsList(TEST_INVALID_CREDENTIALS_FILE); - } catch (javax.xml.bind.UnmarshalException unmarshalEx) { + } catch (UnmarshalException unmarshalEx) { String exceptionMessage = unmarshalEx.toString(); assertTrue(exceptionMessage.contains("invalid_credentials")); assertTrue(exceptionMessage.contains(TEST_INVALID_CREDENTIALS_FILE)); @@ -71,7 +72,7 @@ public void testLoadInvalidDuplicateUserCredentialsFileMessaging() { try { CredentialsStore.loadCredentialsList(TEST_DUPLICATE_USER_CREDENTIALS_FILE); fail("Duplicate user in credentials file should throw an exception"); - } catch (javax.xml.bind.UnmarshalException unmarshalEx) { + } catch (UnmarshalException unmarshalEx) { String exceptionMessage = unmarshalEx.toString(); assertTrue(exceptionMessage.contains("unique")); assertTrue(exceptionMessage.contains(TEST_DUPLICATE_USER_CREDENTIALS_FILE));