@Test public void deserialize() throws IOException { Map<String, JsonNode> kids = new HashMap<>(); kids.put(READONLY, node); kids.put(DISPOSED, node); kids.put(ENCRYPTED_BYTES, node); kids.put(BASE64_SHA1_HASH, node); ObjectNode tree = new ObjectNode(JsonNodeFactory.instance, kids); String testString = "randomTestString"; byte[] encryptedBytes = EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(testString.getBytes()); String encryptedString = Base64.getEncoder().encodeToString(encryptedBytes); when(jp.readValueAsTree()).thenReturn(tree); when(node.asText()).thenReturn(encryptedString); assertEquals(Boolean.FALSE, ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), READONLY)); kids.remove(READONLY); assertEquals(Boolean.FALSE, ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), DISPOSED)); kids.remove(DISPOSED); assertEquals(encryptedString, ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), BASE64_SHA1_HASH)); kids.remove(BASE64_SHA1_HASH); GuardedString expected = new GuardedString(new String(testString.getBytes()).toCharArray()); assertTrue(EqualsBuilder.reflectionEquals(ReflectionTestUtils.getField(expected, ENCRYPTED_BYTES), ReflectionTestUtils.getField(deserializer.deserialize(jp, ctx), ENCRYPTED_BYTES))); }
@Override public GuardedString deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException { ObjectNode tree = jp.readValueAsTree(); boolean readOnly = false; if (tree.has(READONLY)) { readOnly = tree.get(READONLY).asBoolean(); } boolean disposed = false; if (tree.has(DISPOSED)) { disposed = tree.get(DISPOSED).asBoolean(); } byte[] encryptedBytes = null; if (tree.has(ENCRYPTED_BYTES)) { encryptedBytes = Base64.getDecoder().decode(tree.get(ENCRYPTED_BYTES).asText()); } String base64SHA1Hash = null; if (tree.has(BASE64_SHA1_HASH)) { base64SHA1Hash = tree.get(BASE64_SHA1_HASH).asText(); } final byte[] clearBytes = EncryptorFactory.getInstance().getDefaultEncryptor().decrypt(encryptedBytes); GuardedString dest = new GuardedString(new String(clearBytes).toCharArray()); try { Field field = GuardedString.class.getDeclaredField(READONLY); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, readOnly); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, readOnly, e); } try { Field field = GuardedString.class.getDeclaredField(DISPOSED); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, disposed); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, disposed, e); } if (base64SHA1Hash != null) { try { Field field = GuardedString.class.getDeclaredField(BASE64_SHA1_HASH); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, base64SHA1Hash); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, base64SHA1Hash, e); } } return dest; }
GuardedStringDeserializer extends JsonDeserializer<GuardedString> { @Override public GuardedString deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException { ObjectNode tree = jp.readValueAsTree(); boolean readOnly = false; if (tree.has(READONLY)) { readOnly = tree.get(READONLY).asBoolean(); } boolean disposed = false; if (tree.has(DISPOSED)) { disposed = tree.get(DISPOSED).asBoolean(); } byte[] encryptedBytes = null; if (tree.has(ENCRYPTED_BYTES)) { encryptedBytes = Base64.getDecoder().decode(tree.get(ENCRYPTED_BYTES).asText()); } String base64SHA1Hash = null; if (tree.has(BASE64_SHA1_HASH)) { base64SHA1Hash = tree.get(BASE64_SHA1_HASH).asText(); } final byte[] clearBytes = EncryptorFactory.getInstance().getDefaultEncryptor().decrypt(encryptedBytes); GuardedString dest = new GuardedString(new String(clearBytes).toCharArray()); try { Field field = GuardedString.class.getDeclaredField(READONLY); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, readOnly); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, readOnly, e); } try { Field field = GuardedString.class.getDeclaredField(DISPOSED); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, disposed); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, disposed, e); } if (base64SHA1Hash != null) { try { Field field = GuardedString.class.getDeclaredField(BASE64_SHA1_HASH); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, base64SHA1Hash); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, base64SHA1Hash, e); } } return dest; } }
GuardedStringDeserializer extends JsonDeserializer<GuardedString> { @Override public GuardedString deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException { ObjectNode tree = jp.readValueAsTree(); boolean readOnly = false; if (tree.has(READONLY)) { readOnly = tree.get(READONLY).asBoolean(); } boolean disposed = false; if (tree.has(DISPOSED)) { disposed = tree.get(DISPOSED).asBoolean(); } byte[] encryptedBytes = null; if (tree.has(ENCRYPTED_BYTES)) { encryptedBytes = Base64.getDecoder().decode(tree.get(ENCRYPTED_BYTES).asText()); } String base64SHA1Hash = null; if (tree.has(BASE64_SHA1_HASH)) { base64SHA1Hash = tree.get(BASE64_SHA1_HASH).asText(); } final byte[] clearBytes = EncryptorFactory.getInstance().getDefaultEncryptor().decrypt(encryptedBytes); GuardedString dest = new GuardedString(new String(clearBytes).toCharArray()); try { Field field = GuardedString.class.getDeclaredField(READONLY); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, readOnly); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, readOnly, e); } try { Field field = GuardedString.class.getDeclaredField(DISPOSED); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, disposed); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, disposed, e); } if (base64SHA1Hash != null) { try { Field field = GuardedString.class.getDeclaredField(BASE64_SHA1_HASH); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, base64SHA1Hash); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, base64SHA1Hash, e); } } return dest; }  }
GuardedStringDeserializer extends JsonDeserializer<GuardedString> { @Override public GuardedString deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException { ObjectNode tree = jp.readValueAsTree(); boolean readOnly = false; if (tree.has(READONLY)) { readOnly = tree.get(READONLY).asBoolean(); } boolean disposed = false; if (tree.has(DISPOSED)) { disposed = tree.get(DISPOSED).asBoolean(); } byte[] encryptedBytes = null; if (tree.has(ENCRYPTED_BYTES)) { encryptedBytes = Base64.getDecoder().decode(tree.get(ENCRYPTED_BYTES).asText()); } String base64SHA1Hash = null; if (tree.has(BASE64_SHA1_HASH)) { base64SHA1Hash = tree.get(BASE64_SHA1_HASH).asText(); } final byte[] clearBytes = EncryptorFactory.getInstance().getDefaultEncryptor().decrypt(encryptedBytes); GuardedString dest = new GuardedString(new String(clearBytes).toCharArray()); try { Field field = GuardedString.class.getDeclaredField(READONLY); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, readOnly); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, readOnly, e); } try { Field field = GuardedString.class.getDeclaredField(DISPOSED); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, disposed); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, disposed, e); } if (base64SHA1Hash != null) { try { Field field = GuardedString.class.getDeclaredField(BASE64_SHA1_HASH); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, base64SHA1Hash); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, base64SHA1Hash, e); } } return dest; }  @Override GuardedString deserialize(final JsonParser jp, final DeserializationContext ctx); }
GuardedStringDeserializer extends JsonDeserializer<GuardedString> { @Override public GuardedString deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException { ObjectNode tree = jp.readValueAsTree(); boolean readOnly = false; if (tree.has(READONLY)) { readOnly = tree.get(READONLY).asBoolean(); } boolean disposed = false; if (tree.has(DISPOSED)) { disposed = tree.get(DISPOSED).asBoolean(); } byte[] encryptedBytes = null; if (tree.has(ENCRYPTED_BYTES)) { encryptedBytes = Base64.getDecoder().decode(tree.get(ENCRYPTED_BYTES).asText()); } String base64SHA1Hash = null; if (tree.has(BASE64_SHA1_HASH)) { base64SHA1Hash = tree.get(BASE64_SHA1_HASH).asText(); } final byte[] clearBytes = EncryptorFactory.getInstance().getDefaultEncryptor().decrypt(encryptedBytes); GuardedString dest = new GuardedString(new String(clearBytes).toCharArray()); try { Field field = GuardedString.class.getDeclaredField(READONLY); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, readOnly); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, readOnly, e); } try { Field field = GuardedString.class.getDeclaredField(DISPOSED); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, disposed); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, disposed, e); } if (base64SHA1Hash != null) { try { Field field = GuardedString.class.getDeclaredField(BASE64_SHA1_HASH); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, dest, base64SHA1Hash); } catch (Exception e) { LOG.error(LOG_ERROR_MESSAGE, base64SHA1Hash, e); } } return dest; }  @Override GuardedString deserialize(final JsonParser jp, final DeserializationContext ctx);  }