2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.rest.impl;
10 import com.google.common.base.Optional;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.lang.annotation.Annotation;
14 import java.lang.reflect.Type;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.WebApplicationException;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.ext.MessageBodyReader;
23 import javax.ws.rs.ext.Provider;
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.parsers.ParserConfigurationException;
27 import org.opendaylight.controller.sal.rest.api.Draft02;
28 import org.opendaylight.controller.sal.rest.api.RestconfService;
29 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
30 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
31 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
32 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
33 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlUtils;
36 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
37 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Element;
47 @Consumes({ Draft02.MediaTypes.DATA + RestconfService.XML, Draft02.MediaTypes.OPERATION + RestconfService.XML,
48 MediaType.APPLICATION_XML, MediaType.TEXT_XML })
49 public class XmlNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
51 private final static Logger LOG = LoggerFactory.getLogger(XmlNormalizedNodeBodyReader.class);
52 private static final DocumentBuilderFactory BUILDERFACTORY;
55 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
57 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
58 factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
59 factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
60 factory.setXIncludeAware(false);
61 factory.setExpandEntityReferences(false);
62 } catch (final ParserConfigurationException e) {
63 throw new ExceptionInInitializerError(e);
65 factory.setNamespaceAware(true);
66 factory.setCoalescing(true);
67 factory.setIgnoringElementContentWhitespace(true);
68 factory.setIgnoringComments(true);
69 BUILDERFACTORY = factory;
73 public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
74 final MediaType mediaType) {
79 public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
80 final Annotation[] annotations, final MediaType mediaType,
81 final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
82 WebApplicationException {
84 final Optional<InstanceIdentifierContext> path = getIdentifierWithSchema();
86 final DocumentBuilder dBuilder;
88 dBuilder = BUILDERFACTORY.newDocumentBuilder();
89 } catch (final ParserConfigurationException e) {
90 throw new RuntimeException("Failed to parse XML document", e);
92 final Document doc = dBuilder.parse(entityStream);
94 final NormalizedNode<?, ?> result = parse(path.get(),doc);
95 return new NormalizedNodeContext(path.get(),result);
96 } catch (final Exception e) {
97 LOG.debug("Error parsing json input", e);
99 throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
100 ErrorTag.MALFORMED_MESSAGE);
104 private static NormalizedNode<?,?> parse(final InstanceIdentifierContext pathContext,final Document doc) {
106 final List<Element> elements = Collections.singletonList(doc.getDocumentElement());
107 DataSchemaNode schemaNode = pathContext.getSchemaNode();
109 final String docRootElm = doc.getDocumentElement().getLocalName();
110 final String schemaNodeName = pathContext.getSchemaNode().getQName().getLocalName();
112 // TODO : do we want to really follow netconf-restconf specification ?
113 if (!schemaNodeName.equalsIgnoreCase(docRootElm)) {
114 final Collection<DataSchemaNode> children = ((DataNodeContainer) schemaNode).getChildNodes();
115 for (final DataSchemaNode child : children) {
116 if (child.getQName().getLocalName().equalsIgnoreCase(docRootElm)) {
123 // FIXME the factory instance should be cached if the schema context is the same
124 final DomToNormalizedNodeParserFactory parserFactory =
125 DomToNormalizedNodeParserFactory.getInstance(XmlUtils.DEFAULT_XML_CODEC_PROVIDER, pathContext.getSchemaContext());
127 if(schemaNode instanceof ContainerSchemaNode) {
128 return parserFactory.getContainerNodeParser().parse(Collections.singletonList(doc.getDocumentElement()), (ContainerSchemaNode) schemaNode);
129 } else if(schemaNode instanceof ListSchemaNode) {
130 final ListSchemaNode casted = (ListSchemaNode) schemaNode;
131 return parserFactory.getMapEntryNodeParser().parse(elements, casted);