• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.protocols.json;
17 
18 import java.io.IOException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import software.amazon.awssdk.annotations.SdkProtectedApi;
22 import software.amazon.awssdk.http.SdkHttpFullResponse;
23 import software.amazon.awssdk.protocols.jsoncore.JsonNode;
24 import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;
25 import software.amazon.awssdk.thirdparty.jackson.core.JsonFactory;
26 import software.amazon.awssdk.utils.IoUtils;
27 
28 /**
29  * Simple struct like class to hold both the raw json string content and it's parsed JsonNode
30  */
31 @SdkProtectedApi
32 //TODO Do we need this? It isn't well encapsulated because of storing non-copied arrays.
33 public class JsonContent {
34 
35     private static final Logger LOG = LoggerFactory.getLogger(JsonContent.class);
36 
37     private final byte[] rawContent;
38     private final JsonNode jsonNode;
39 
JsonContent(byte[] rawJsonContent, JsonNode jsonNode)40     JsonContent(byte[] rawJsonContent, JsonNode jsonNode) {
41         this.rawContent = rawJsonContent;
42         this.jsonNode = jsonNode;
43     }
44 
JsonContent(byte[] rawJsonContent, JsonFactory jsonFactory)45     private JsonContent(byte[] rawJsonContent, JsonFactory jsonFactory) {
46         this.rawContent = rawJsonContent;
47         this.jsonNode = parseJsonContent(rawJsonContent, jsonFactory);
48     }
49 
50     /**
51      * Static factory method to create a JsonContent object from the contents of the HttpResponse
52      * provided
53      */
createJsonContent(SdkHttpFullResponse httpResponse, JsonFactory jsonFactory)54     public static JsonContent createJsonContent(SdkHttpFullResponse httpResponse,
55                                                 JsonFactory jsonFactory) {
56 
57         byte[] rawJsonContent = httpResponse.content().map(c -> {
58             try {
59                 return IoUtils.toByteArray(c);
60             } catch (IOException e) {
61                 LOG.debug("Unable to read HTTP response content", e);
62             }
63             return null;
64         }).orElse(null);
65         return new JsonContent(rawJsonContent, jsonFactory);
66     }
67 
parseJsonContent(byte[] rawJsonContent, JsonFactory jsonFactory)68     private static JsonNode parseJsonContent(byte[] rawJsonContent, JsonFactory jsonFactory) {
69         if (rawJsonContent == null || rawJsonContent.length == 0) {
70             return JsonNode.emptyObjectNode();
71         }
72         try {
73             JsonNodeParser parser = JsonNodeParser.builder().jsonFactory(jsonFactory).build();
74             return parser.parse(rawJsonContent);
75         } catch (Exception e) {
76             LOG.debug("Unable to parse HTTP response content", e);
77             return JsonNode.emptyObjectNode();
78         }
79     }
80 
getRawContent()81     public byte[] getRawContent() {
82         return rawContent;
83     }
84 
getJsonNode()85     public JsonNode getJsonNode() {
86         return jsonNode;
87     }
88 }
89