|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.imds;
|
17 | 17 |
|
18 |
| -import java.io.IOException; |
| 18 | +import java.io.UncheckedIOException; |
19 | 19 | import java.util.Arrays;
|
20 | 20 | import java.util.List;
|
21 |
| -import org.slf4j.Logger; |
22 |
| -import org.slf4j.LoggerFactory; |
23 | 21 | import software.amazon.awssdk.annotations.SdkPublicApi;
|
24 | 22 | import software.amazon.awssdk.core.document.Document;
|
25 | 23 | import software.amazon.awssdk.imds.internal.unmarshall.document.DocumentUnmarshaller;
|
26 | 24 | import software.amazon.awssdk.protocols.jsoncore.JsonNode;
|
27 | 25 | import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;
|
| 26 | +import software.amazon.awssdk.thirdparty.jackson.core.JsonParseException; |
| 27 | +import software.amazon.awssdk.utils.ToString; |
28 | 28 | import software.amazon.awssdk.utils.Validate;
|
29 | 29 |
|
30 | 30 | /**
|
31 | 31 | * The class is used for Response Handling and Parsing the metadata fetched by the get call in the {@link Ec2Metadata} interface.
|
32 | 32 | * The class provides convenience methods to the users to parse the metadata as a String, List and Document.
|
33 | 33 | */
|
34 | 34 | @SdkPublicApi
|
35 |
| -public class MetadataResponse { |
36 |
| - |
37 |
| - private static final Logger log = LoggerFactory.getLogger(MetadataResponse.class); |
| 35 | +public final class MetadataResponse { |
38 | 36 |
|
39 | 37 | private static final JsonNodeParser JSON_NODE_PARSER = JsonNode.parserBuilder().removeErrorLocations(true).build();
|
40 | 38 |
|
41 | 39 | private final String body;
|
42 | 40 |
|
43 |
| - public MetadataResponse(String body) { |
| 41 | + private MetadataResponse(String body) { |
44 | 42 | this.body = Validate.notNull(body, "Metadata is null");
|
45 | 43 | }
|
46 | 44 |
|
47 | 45 | /**
|
48 |
| - * Returns the Metadata Response body as a String. This method can be used for parsing the retrieved |
49 |
| - * singular metadata from IMDS. |
50 |
| - * |
| 46 | + * Create a {@link MetadataResponse} with the given body as it's content. |
| 47 | + * @param body the content of the response |
| 48 | + * @return a {@link MetadataResponse} with the given body as it's content. |
| 49 | + */ |
| 50 | + public static MetadataResponse create(String body) { |
| 51 | + return new MetadataResponse(body); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
51 | 55 | * @return String Representation of the Metadata Response Body.
|
52 |
| - * |
53 |
| - * <p> |
54 |
| - * Example: |
55 |
| - * <pre> |
56 |
| - * {@code |
57 |
| - * |
58 |
| - * Ec2Metadata ec2Metadata = Ec2Metadata.create(); |
59 |
| - * MetadataResponse metadataResponse = client.get("/latest/meta-data/ami-id"); |
60 |
| - * String response = metadataResponse.asString(); |
61 |
| - * } |
62 |
| - * </pre> |
63 | 56 | */
|
64 | 57 | public String asString() {
|
65 | 58 | return body;
|
66 | 59 | }
|
67 | 60 |
|
68 | 61 | /**
|
69 |
| - * Parses the response String into a list of Strings split by delimiter ("\n"). This method can be used for parsing the |
70 |
| - * list-type metadata from IMDS. |
71 |
| - * |
72 |
| - * @return List Representation of the Metadata Response Body. |
73 |
| - * |
74 |
| - * <p> |
75 |
| - * Example: |
76 |
| - * <pre> |
77 |
| - * {@code |
78 |
| - * |
79 |
| - * Ec2Metadata ec2Metadata = Ec2Metadata.create(); |
80 |
| - * MetadataResponse metadataResponse = client.get("/latest/meta-data/ancestor-ami-ids"); |
81 |
| - * List<String>response = metadataResponse.asList(); |
82 |
| - * } |
83 |
| - * </pre> |
| 62 | + * Splits the Metadata response body on new line character and returns it as a list. |
| 63 | + * @return The Metadata response split on each line. |
84 | 64 | */
|
85 | 65 | public List<String> asList() {
|
86 | 66 | return Arrays.asList(body.split("\n"));
|
87 | 67 | }
|
88 | 68 |
|
89 | 69 | /**
|
90 |
| - * Parses the response String into {@link Document} type. This method can be used for |
| 70 | + * Parses the response String into a {@link Document} type. This method can be used for |
91 | 71 | * parsing the metadata in a String Json Format.
|
92 |
| - * |
93 |
| - * @return Document Representation of the Metadata Response Body. |
94 |
| - * @throws IOException in case parsing does not happen correctly. |
95 |
| - * |
96 |
| - * <p> |
97 |
| - * Example: |
98 |
| - * <pre> |
99 |
| - * {@code |
100 |
| - * |
101 |
| - * Ec2Metadata ec2Metadata = Ec2Metadata.create(); |
102 |
| - * MetadataResponse metadataResponse = client.get("/latest/dynamic/instance-identity/document"); |
103 |
| - * Document document = metadataResponse.asDocument(); |
104 |
| - * } |
105 |
| - * </pre> |
| 72 | + * @return Document Representation, as json, of the Metadata Response Body. |
| 73 | + * @throws UncheckedIOException (wrapping a {@link JsonParseException} if the Response body is not of JSON format. |
106 | 74 | */
|
107 |
| - |
108 |
| - public Document asDocument() throws IOException { |
109 |
| - |
| 75 | + public Document asDocument() { |
110 | 76 | JsonNode node = JSON_NODE_PARSER.parse(body);
|
111 | 77 | return node.visit(new DocumentUnmarshaller());
|
112 | 78 | }
|
113 | 79 |
|
| 80 | + @Override |
| 81 | + public boolean equals(Object o) { |
| 82 | + if (this == o) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + if (o == null || getClass() != o.getClass()) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + MetadataResponse that = (MetadataResponse) o; |
| 89 | + return body.equals(that.body); |
| 90 | + } |
114 | 91 |
|
| 92 | + @Override |
| 93 | + public int hashCode() { |
| 94 | + return body.hashCode(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String toString() { |
| 99 | + return ToString.builder("MetadataResponse") |
| 100 | + .add("body", body) |
| 101 | + .build(); |
| 102 | + } |
115 | 103 | }
|
0 commit comments