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();
55 factory.setNamespaceAware(true);
56 factory.setCoalescing(true);
57 factory.setIgnoringElementContentWhitespace(true);
58 factory.setIgnoringComments(true);
59 BUILDERFACTORY = factory;
63 public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
64 final MediaType mediaType) {
69 public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
70 final Annotation[] annotations, final MediaType mediaType,
71 final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
72 WebApplicationException {
74 Optional<InstanceIdentifierContext> path = getIdentifierWithSchema();
76 final DocumentBuilder dBuilder;
78 dBuilder = BUILDERFACTORY.newDocumentBuilder();
79 } catch (ParserConfigurationException e) {
80 throw new RuntimeException("Failed to parse XML document", e);
82 Document doc = dBuilder.parse(entityStream);
84 NormalizedNode<?, ?> result = parse(path.get(),doc);
85 return new NormalizedNodeContext(path.get(),result);
86 } catch (Exception e) {
87 LOG.debug("Error parsing json input", e);
89 throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
90 ErrorTag.MALFORMED_MESSAGE);
94 private static NormalizedNode<?,?> parse(InstanceIdentifierContext pathContext,Document doc) {
95 List<Element> elements = Collections.singletonList(doc.getDocumentElement());
96 DataSchemaNode schemaNode = pathContext.getSchemaNode();
97 if(schemaNode instanceof ContainerSchemaNode) {
98 return DOM_PARSER_FACTORY.getContainerNodeParser().parse(Collections.singletonList(doc.getDocumentElement()), (ContainerSchemaNode) schemaNode);
99 } else if(schemaNode instanceof ListSchemaNode) {
100 ListSchemaNode casted = (ListSchemaNode) schemaNode;
101 return DOM_PARSER_FACTORY.getMapEntryNodeParser().parse(elements, casted);