Remove "/" sign in AbstractRestconfStreamRegistry
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / api / XmlResourceBody.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.server.api;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import javax.xml.stream.XMLStreamException;
13 import javax.xml.transform.dom.DOMSource;
14 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
15 import org.opendaylight.yangtools.util.xml.UntrustedXML;
16 import org.opendaylight.yangtools.yang.common.ErrorTag;
17 import org.opendaylight.yangtools.yang.common.ErrorType;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.xml.sax.SAXException;
24
25 /**
26  * An XML-encoded {@link ResourceBody}.
27  */
28 public final class XmlResourceBody extends ResourceBody {
29     private static final Logger LOG = LoggerFactory.getLogger(XmlResourceBody.class);
30
31     public XmlResourceBody(final InputStream inputStream) {
32         super(inputStream);
33     }
34
35     @Override
36     void streamTo(final DatabindPath.Data path, final PathArgument name, final InputStream inputStream,
37             final NormalizedNodeStreamWriter writer) throws IOException {
38         try (var xmlParser = XmlParserStream.create(writer, path.databind().xmlCodecs(), path.inference())) {
39             final var doc = UntrustedXML.newDocumentBuilder().parse(inputStream);
40             final var docRoot = doc.getDocumentElement();
41             final var docRootName = docRoot.getLocalName();
42             final var docRootNs = docRoot.getNamespaceURI();
43             final var qname = name.getNodeType();
44             final var pathName = qname.getLocalName();
45             final var pathNs = qname.getNamespace().toString();
46             if (!docRootName.equals(pathName) || !docRootNs.equals(pathNs)) {
47                 throw new RestconfDocumentedException("Incorrect message root element (" + docRootNs + ")" + docRootName
48                     + ", should be (" + pathNs + ")" + pathName, ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
49             }
50
51             xmlParser.traverse(new DOMSource(docRoot));
52         } catch (SAXException | XMLStreamException e) {
53             LOG.debug("Error parsing XML input", e);
54             throwIfYangError(e);
55             throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
56                     ErrorTag.MALFORMED_MESSAGE, e);
57         }
58     }
59 }