|
| 1 | +package software.amazon.cryptography.dbencryptionsdk.dynamodb.enhancedclient; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter; |
| 7 | +import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType; |
| 8 | +import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; |
| 9 | +import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbAttribute; |
| 10 | +import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean; |
| 11 | +import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbConvertedBy; |
| 12 | +import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey; |
| 13 | +import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey; |
| 14 | +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; |
| 15 | + |
| 16 | +/** |
| 17 | + * This is a valid use of DynamoDbEncryption annotations attributes with |
| 18 | + * DynamoDbConvertedBy.<p> |
| 19 | + * The DynamoDbEncryption annotations are placed on elements that are converted |
| 20 | + * to Maps.<p> |
| 21 | + * In this case, only {@code nestedEncrypted} will be written to the DynamoDB Table as a |
| 22 | + * binary. {@code nestedSigned} and {@code nestedIgnored} are recorded as DynamoDB Maps. |
| 23 | + */ |
| 24 | +@DynamoDbBean |
| 25 | +public class AnnotatedConvertedBy { |
| 26 | + private String partitionKey; |
| 27 | + private int sortKey; |
| 28 | + private ConvertedByNestedBean nestedEncrypted; |
| 29 | + private ConvertedByNestedBean nestedSigned; |
| 30 | + private ConvertedByNestedBean nestedIgnored; |
| 31 | + |
| 32 | + @DynamoDbPartitionKey |
| 33 | + @DynamoDbAttribute(value = "partition_key") |
| 34 | + public String getPartitionKey() { |
| 35 | + return this.partitionKey; |
| 36 | + } |
| 37 | + public void setPartitionKey(String partitionKey) { |
| 38 | + this.partitionKey = partitionKey; |
| 39 | + } |
| 40 | + |
| 41 | + @DynamoDbSortKey |
| 42 | + @DynamoDbAttribute(value = "sort_key") |
| 43 | + public int getSortKey() { |
| 44 | + return this.sortKey; |
| 45 | + } |
| 46 | + public void setSortKey(int sortKey) { |
| 47 | + this.sortKey = sortKey; |
| 48 | + } |
| 49 | + |
| 50 | + @DynamoDbConvertedBy(ConvertedByNestedBean.NestedBeanConverter.class) |
| 51 | + @DynamoDbAttribute("nestedEncrypted") |
| 52 | + public ConvertedByNestedBean getNestedEncrypted() { |
| 53 | + return this.nestedEncrypted; |
| 54 | + } |
| 55 | + public void setNestedEncrypted(ConvertedByNestedBean nested) { |
| 56 | + this.nestedEncrypted = nested; |
| 57 | + } |
| 58 | + |
| 59 | + @DynamoDbConvertedBy(ConvertedByNestedBean.NestedBeanConverter.class) |
| 60 | + @DynamoDbEncryptionSignOnly |
| 61 | + @DynamoDbAttribute("nestedSigned") |
| 62 | + public ConvertedByNestedBean getNestedSigned() { |
| 63 | + return this.nestedSigned; |
| 64 | + } |
| 65 | + public void setNestedSigned(ConvertedByNestedBean nested) { |
| 66 | + this.nestedSigned = nested; |
| 67 | + } |
| 68 | + |
| 69 | + @DynamoDbConvertedBy(ConvertedByNestedBean.NestedBeanConverter.class) |
| 70 | + @DynamoDbEncryptionDoNothing |
| 71 | + @DynamoDbAttribute("nestedIgnored") |
| 72 | + public ConvertedByNestedBean getNestedIgnored() { |
| 73 | + return this.nestedIgnored; |
| 74 | + } |
| 75 | + public void setNestedIgnored(ConvertedByNestedBean nested) { |
| 76 | + this.nestedIgnored = nested; |
| 77 | + } |
| 78 | + |
| 79 | + public static class ConvertedByNestedBean { |
| 80 | + private String id; |
| 81 | + private String firstName; |
| 82 | + private String lastName; |
| 83 | + |
| 84 | + // A Default Empty constructor is needed by the Enhanced Client |
| 85 | + public ConvertedByNestedBean() {}; |
| 86 | + |
| 87 | + public ConvertedByNestedBean(String id, String firstName, String lastName) { |
| 88 | + this.id = id; |
| 89 | + this.firstName = firstName; |
| 90 | + this.lastName = lastName; |
| 91 | + } |
| 92 | + |
| 93 | + public String getId() { return this.id; } |
| 94 | + public void setId(String id) { this.id = id; } |
| 95 | + |
| 96 | + public String getFirstName() { return firstName; } |
| 97 | + public void setFirstName(String firstName) { this.firstName = firstName; } |
| 98 | + |
| 99 | + public String getLastName() { return lastName; } |
| 100 | + public void setLastName(String lastName) { this.lastName = lastName; } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean equals(Object obj) { |
| 104 | + if (this == obj) return true; |
| 105 | + if (obj == null) return false; |
| 106 | + if (getClass() != obj.getClass()) return false; |
| 107 | + ConvertedByNestedBean other = (ConvertedByNestedBean) obj; |
| 108 | + if (id == null) { |
| 109 | + if (other.getId() != null) return false; |
| 110 | + } else if (!id.equals(other.getId())) return false; |
| 111 | + if (firstName == null) { |
| 112 | + if (other.getFirstName() != null) return false; |
| 113 | + } else if (!firstName.equals(other.getFirstName())) return false; |
| 114 | + if (lastName == null) { |
| 115 | + if (other.getLastName() != null) return false; |
| 116 | + } else if (!lastName.equals(other.getLastName())) return false; |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + public static final class NestedBeanConverter implements AttributeConverter<ConvertedByNestedBean> { |
| 121 | + @Override |
| 122 | + public AttributeValue transformFrom(ConvertedByNestedBean ConvertedByNestedBean) { |
| 123 | + Map<String, AttributeValue> map = new HashMap<>(3); |
| 124 | + map.put("id", AttributeValue.fromS(ConvertedByNestedBean.getId())); |
| 125 | + map.put("firstName", AttributeValue.fromS(ConvertedByNestedBean.getFirstName())); |
| 126 | + map.put("lastName", AttributeValue.fromS(ConvertedByNestedBean.getLastName())); |
| 127 | + return AttributeValue.fromM(map); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public ConvertedByNestedBean transformTo(AttributeValue attributeValue) { |
| 132 | + Map<String, AttributeValue> map = attributeValue.m(); |
| 133 | + return new ConvertedByNestedBean( |
| 134 | + map.get("id").s(), |
| 135 | + map.get("firstName").s(), |
| 136 | + map.get("lastName").s()); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public EnhancedType<ConvertedByNestedBean> type() { |
| 141 | + return EnhancedType.of(ConvertedByNestedBean.class); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public AttributeValueType attributeValueType() { |
| 146 | + return AttributeValueType.M; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments