de30d31ac92e132dffa91d611132eac409893ad9
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / databind / JsonResourceBody.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040.databind;
9
10 import com.google.gson.JsonIOException;
11 import com.google.gson.JsonParseException;
12 import com.google.gson.stream.JsonReader;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.InputStreamReader;
16 import java.nio.charset.StandardCharsets;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.server.api.DataPutPath;
20 import org.opendaylight.yangtools.yang.common.ErrorTag;
21 import org.opendaylight.yangtools.yang.common.ErrorType;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
24 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * A JSON-encoded {@link ResourceBody}.
30  */
31 public final class JsonResourceBody extends ResourceBody {
32     private static final Logger LOG = LoggerFactory.getLogger(JsonResourceBody.class);
33
34     public JsonResourceBody(final InputStream inputStream) {
35         super(inputStream);
36     }
37
38     @Override
39     void streamTo(final DataPutPath path, final PathArgument name, final InputStream inputStream,
40             final NormalizedNodeStreamWriter writer) throws IOException {
41         try (var jsonParser = newParser(path, writer)) {
42             try (var reader = new JsonReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
43                 jsonParser.parse(reader);
44             } catch (JsonParseException e) {
45                 LOG.debug("Error parsing JSON input", e);
46                 throw newRDE(e);
47             }
48         }
49     }
50
51     private static @NonNull RestconfDocumentedException newRDE(final JsonParseException cause) {
52         final var root = cause instanceof JsonIOException jsonIO && jsonIO.getCause() instanceof IOException io ? io
53             : cause;
54         return new RestconfDocumentedException("Error parsing input: " + root.getMessage(), ErrorType.PROTOCOL,
55             ErrorTag.MALFORMED_MESSAGE, cause);
56     }
57
58     private static JsonParserStream newParser(final DataPutPath path, final NormalizedNodeStreamWriter writer) {
59         final var codecs = path.databind().jsonCodecs();
60         final var inference = path.inference();
61         if (inference.isEmpty()) {
62             return JsonParserStream.create(writer, codecs);
63         }
64
65         final var stack = inference.toSchemaInferenceStack();
66         stack.exit();
67         return JsonParserStream.create(writer, codecs, stack.toInference());
68     }
69 }