|
8 | 8 | import java.util.Map; |
9 | 9 | import java.util.Set; |
10 | 10 |
|
11 | | -import org.slf4j.Logger; |
12 | | -import org.slf4j.LoggerFactory; |
13 | | - |
14 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
15 | 11 | import com.yoti.api.client.Attribute; |
16 | 12 | import com.yoti.api.client.spi.remote.proto.AttrProto; |
17 | 13 | import com.yoti.api.client.spi.remote.proto.AttrProto.Anchor; |
18 | | -import com.yoti.api.client.spi.remote.proto.ContentTypeProto.ContentType; |
19 | 14 | import com.yoti.api.client.spi.remote.util.AnchorCertificateParser; |
20 | | -import com.yoti.api.client.spi.remote.util.AnchorType; |
21 | 15 | import com.yoti.api.client.spi.remote.util.AnchorCertificateParser.AnchorVerifierSourceData; |
| 16 | +import com.yoti.api.client.spi.remote.util.AnchorType; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
22 | 21 |
|
23 | 22 | public class AttributeConverter { |
24 | 23 |
|
25 | 24 | private static final ObjectMapper JSON_MAPPER = new ObjectMapper(); |
26 | 25 | private static final Logger LOG = LoggerFactory.getLogger(AttributeConverter.class); |
27 | 26 |
|
28 | | - |
29 | | - public static Attribute convertAttribute(AttrProto.Attribute attribute) throws ParseException, IOException{ |
30 | | - if (ContentType.STRING.equals(attribute.getContentType())) { |
31 | | - return new com.yoti.api.client.Attribute(attribute.getName(), |
32 | | - attribute.getValue().toString(DEFAULT_CHARSET), |
33 | | - extractMetadata(attribute, AnchorType.SOURCE), |
34 | | - extractMetadata(attribute, AnchorType.VERIFIER)); |
35 | | - } else if (ContentType.DATE.equals(attribute.getContentType())) { |
36 | | - return new com.yoti.api.client.Attribute(attribute.getName(), |
37 | | - DateAttributeValue.parseFrom(attribute.getValue().toByteArray()), |
38 | | - extractMetadata(attribute, AnchorType.SOURCE), |
39 | | - extractMetadata(attribute, AnchorType.VERIFIER)); |
40 | | - } else if (ContentType.JPEG.equals(attribute.getContentType())) { |
41 | | - return new com.yoti.api.client.Attribute(attribute.getName(), |
42 | | - new JpegAttributeValue(attribute.getValue().toByteArray()), |
43 | | - extractMetadata(attribute, AnchorType.SOURCE), |
44 | | - extractMetadata(attribute, AnchorType.VERIFIER)); |
45 | | - } else if (ContentType.PNG.equals(attribute.getContentType())) { |
46 | | - return new com.yoti.api.client.Attribute(attribute.getName(), |
47 | | - new PngAttributeValue(attribute.getValue().toByteArray()), |
48 | | - extractMetadata(attribute, AnchorType.SOURCE), |
49 | | - extractMetadata(attribute, AnchorType.VERIFIER)); |
50 | | - } else if (ContentType.JSON.equals(attribute.getContentType())) { |
51 | | - return new com.yoti.api.client.Attribute(attribute.getName(), |
52 | | - JSON_MAPPER.readValue(attribute.getValue().toString(DEFAULT_CHARSET), Map.class), |
53 | | - extractMetadata(attribute, AnchorType.SOURCE), |
54 | | - extractMetadata(attribute, AnchorType.VERIFIER)); |
| 27 | + public static Attribute convertAttribute(com.yoti.api.client.spi.remote.proto.AttrProto.Attribute attribute) throws ParseException, IOException { |
| 28 | + switch (attribute.getContentType()) { |
| 29 | + case STRING: |
| 30 | + return attributeWithMetadata(attribute, attribute.getValue().toString(DEFAULT_CHARSET)); |
| 31 | + case DATE: |
| 32 | + return attributeWithMetadata(attribute, DateAttributeValue.parseFrom(attribute.getValue().toByteArray())); |
| 33 | + case JPEG: |
| 34 | + return attributeWithMetadata(attribute, new JpegAttributeValue(attribute.getValue().toByteArray())); |
| 35 | + case PNG: |
| 36 | + return attributeWithMetadata(attribute, new PngAttributeValue(attribute.getValue().toByteArray())); |
| 37 | + case JSON: |
| 38 | + return attributeWithMetadata(attribute, JSON_MAPPER.readValue(attribute.getValue().toString(DEFAULT_CHARSET), Map.class)); |
| 39 | + default: |
| 40 | + LOG.error("Unknown type {} for attribute {}", attribute.getContentType(), attribute.getName()); |
| 41 | + return attributeWithMetadata(attribute, attribute.getValue().toString(DEFAULT_CHARSET)); |
55 | 42 | } |
| 43 | + } |
56 | 44 |
|
57 | | - LOG.error("Unknown type {} for attribute {}", attribute.getContentType(), attribute.getName()); |
58 | | - return new com.yoti.api.client.Attribute(attribute.getName(), |
59 | | - attribute.getValue().toString(DEFAULT_CHARSET), |
60 | | - extractMetadata(attribute, AnchorType.SOURCE), |
61 | | - extractMetadata(attribute, AnchorType.VERIFIER)); |
| 45 | + private static Attribute attributeWithMetadata(AttrProto.Attribute attribute, Object value) { |
| 46 | + return new Attribute(attribute.getName(), value, extractMetadata(attribute, AnchorType.SOURCE), extractMetadata(attribute, AnchorType.VERIFIER)); |
62 | 47 | } |
63 | 48 |
|
64 | 49 | private static Set<String> extractMetadata(AttrProto.Attribute attribute, AnchorType anchorType) { |
65 | 50 | Set<String> entries = new HashSet<String>(); |
66 | 51 | for (Anchor anchor : attribute.getAnchorsList()) { |
67 | 52 | AnchorVerifierSourceData anchorData = AnchorCertificateParser.getTypesFromAnchor(anchor); |
68 | | - if(anchorData.getType().equals(anchorType)) { |
| 53 | + if (anchorData.getType().equals(anchorType)) { |
69 | 54 | entries.addAll(anchorData.getEntries()); |
70 | 55 | } |
71 | 56 | } |
72 | 57 | return entries; |
73 | 58 | } |
74 | 59 |
|
75 | | - |
76 | 60 | } |
0 commit comments