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.Collections;
16 import java.util.List;
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.ext.MessageBodyReader;
22 import javax.ws.rs.ext.Provider;
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
26 import org.opendaylight.controller.sal.rest.api.Draft02;
27 import org.opendaylight.controller.sal.rest.api.RestconfService;
28 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
29 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
30 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
31 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
32 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlUtils;
35 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
36 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Element;
45 @Consumes({ Draft02.MediaTypes.DATA + RestconfService.XML, Draft02.MediaTypes.OPERATION + RestconfService.XML,
46 MediaType.APPLICATION_XML, MediaType.TEXT_XML })
47 public class XmlNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
49 private final static Logger LOG = LoggerFactory.getLogger(XmlNormalizedNodeBodyReader.class);
50 private final static DomToNormalizedNodeParserFactory DOM_PARSER_FACTORY = DomToNormalizedNodeParserFactory.getInstance(XmlUtils.DEFAULT_XML_CODEC_PROVIDER);
51 private static final DocumentBuilderFactory BUILDERFACTORY;
54 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
56 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
57 factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
58 factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
59 factory.setXIncludeAware(false);
60 factory.setExpandEntityReferences(false);
61 } catch (ParserConfigurationException e) {
62 throw new ExceptionInInitializerError(e);
64 factory.setNamespaceAware(true);
65 factory.setCoalescing(true);
66 factory.setIgnoringElementContentWhitespace(true);
67 factory.setIgnoringComments(true);
68 BUILDERFACTORY = factory;
72 public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
73 final MediaType mediaType) {
78 public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
79 final Annotation[] annotations, final MediaType mediaType,
80 final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
81 WebApplicationException {
83 Optional<InstanceIdentifierContext> path = getIdentifierWithSchema();
85 final DocumentBuilder dBuilder;
87 dBuilder = BUILDERFACTORY.newDocumentBuilder();
88 } catch (ParserConfigurationException e) {
89 throw new RuntimeException("Failed to parse XML document", e);
91 Document doc = dBuilder.parse(entityStream);
93 NormalizedNode<?, ?> result = parse(path.get(),doc);
94 return new NormalizedNodeContext(path.get(),result);
95 } catch (Exception e) {
96 LOG.debug("Error parsing json input", e);
98 throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
99 ErrorTag.MALFORMED_MESSAGE);
103 private static NormalizedNode<?,?> parse(InstanceIdentifierContext pathContext,Document doc) {
104 List<Element> elements = Collections.singletonList(doc.getDocumentElement());
105 DataSchemaNode schemaNode = pathContext.getSchemaNode();
106 if(schemaNode instanceof ContainerSchemaNode) {
107 return DOM_PARSER_FACTORY.getContainerNodeParser().parse(Collections.singletonList(doc.getDocumentElement()), (ContainerSchemaNode) schemaNode);
108 } else if(schemaNode instanceof ListSchemaNode) {
109 ListSchemaNode casted = (ListSchemaNode) schemaNode;
110 return DOM_PARSER_FACTORY.getMapEntryNodeParser().parse(elements, casted);