From dbe4db7491d0c598f48170b337fb3d0db7622afa Mon Sep 17 00:00:00 2001 From: Kalin Chan Date: Tue, 31 May 2022 10:03:30 +0100 Subject: [PATCH] Fixes #1050 Signed-off-by: Kalin Chan --- .../tck/core/rest/jsonb/cdi/KeysProducer.java | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/core-profile-tck/tck/src/main/java/ee/jakarta/tck/core/rest/jsonb/cdi/KeysProducer.java b/core-profile-tck/tck/src/main/java/ee/jakarta/tck/core/rest/jsonb/cdi/KeysProducer.java index a20077fa36..9cde182218 100644 --- a/core-profile-tck/tck/src/main/java/ee/jakarta/tck/core/rest/jsonb/cdi/KeysProducer.java +++ b/core-profile-tck/tck/src/main/java/ee/jakarta/tck/core/rest/jsonb/cdi/KeysProducer.java @@ -11,18 +11,21 @@ package ee.jakarta.tck.core.rest.jsonb.cdi; +import jakarta.annotation.PostConstruct; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Produces; + +import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.security.KeyFactory; +import java.security.NoSuchAlgorithmException; import java.security.PublicKey; import java.security.spec.EncodedKeySpec; +import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; -import jakarta.annotation.PostConstruct; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.enterprise.inject.Produces; - @ApplicationScoped public class KeysProducer { @@ -33,22 +36,26 @@ public class KeysProducer { * @throws Exception - on failure to read or initialize public key */ @PostConstruct - private void loadKeys() throws Exception { - byte[] pubKeyData; - try (InputStream keyIS = getClass().getResourceAsStream("/key.pub")) { - if (keyIS == null) { - throw new IllegalStateException("Failed to find /key.pub"); + private void loadKeys() { + try { + byte[] pubKeyData; + try (InputStream keyIS = getClass().getResourceAsStream("/key.pub")) { + if (keyIS == null) { + throw new IllegalStateException("Failed to find /key.pub"); + } + pubKeyData = keyIS.readAllBytes(); } - pubKeyData = keyIS.readAllBytes(); + String pubKeyString = new String(pubKeyData, StandardCharsets.UTF_8); + System.out.println(pubKeyString); + byte[] keyData = Base64.getDecoder().decode(pubKeyString); + EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyData); + System.out.println(publicKeySpec); + + KeyFactory keyFactory = KeyFactory.getInstance("EC"); + publicKey = keyFactory.generatePublic(publicKeySpec); + } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException exception) { + System.out.println(exception.getStackTrace()); } - String pubKeyString = new String(pubKeyData, StandardCharsets.UTF_8); - System.out.println(pubKeyString); - byte[] keyData = Base64.getDecoder().decode(pubKeyString); - EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyData); - System.out.println(publicKeySpec); - - KeyFactory keyFactory = KeyFactory.getInstance("EC"); - publicKey = keyFactory.generatePublic(publicKeySpec); } @Produces