Convert to using requireNonNull()
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / AbstractQueryParams.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.streams.listeners;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import java.io.StringReader;
14 import java.time.Instant;
15 import javax.xml.XMLConstants;
16 import javax.xml.parsers.DocumentBuilderFactory;
17 import javax.xml.parsers.ParserConfigurationException;
18 import javax.xml.xpath.XPath;
19 import javax.xml.xpath.XPathConstants;
20 import javax.xml.xpath.XPathFactory;
21 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
22 import org.w3c.dom.Document;
23 import org.xml.sax.InputSource;
24
25 /**
26  * Features of query parameters part of both notifications.
27  */
28 abstract class AbstractQueryParams extends AbstractNotificationsData {
29     // FIXME: BUG-7956: switch to using UntrustedXML
30     private static final DocumentBuilderFactory DBF;
31
32     static {
33         final DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
34         f.setCoalescing(true);
35         f.setExpandEntityReferences(false);
36         f.setIgnoringElementContentWhitespace(true);
37         f.setIgnoringComments(true);
38         f.setXIncludeAware(false);
39         try {
40             f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
41             f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
42             f.setFeature("http://xml.org/sax/features/external-general-entities", false);
43             f.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
44         } catch (final ParserConfigurationException e) {
45             throw new ExceptionInInitializerError(e);
46         }
47         DBF = f;
48     }
49
50     // FIXME: these should be final
51     private Instant start = null;
52     private Instant stop = null;
53     private String filter = null;
54     private boolean leafNodesOnly = false;
55
56     @VisibleForTesting
57     public final Instant getStart() {
58         return start;
59     }
60
61     /**
62      * Set query parameters for listener.
63      *
64      * @param start         Start-time of getting notification.
65      * @param stop          Stop-time of getting notification.
66      * @param filter        Indicates which subset of all possible events are of interest.
67      * @param leafNodesOnly If TRUE, notifications will contain changes of leaf nodes only.
68      */
69     @SuppressWarnings("checkstyle:hiddenField")
70     public void setQueryParams(final Instant start, final Instant stop, final String filter,
71             final boolean leafNodesOnly) {
72         this.start = requireNonNull(start);
73         this.stop = stop;
74         this.filter = filter;
75         this.leafNodesOnly = leafNodesOnly;
76     }
77
78     /**
79      * Check whether this query should only notify about leaf node changes.
80      *
81      * @return true if this query should only notify about leaf node changes
82      */
83     boolean getLeafNodesOnly() {
84         return leafNodesOnly;
85     }
86
87     @SuppressWarnings("checkstyle:IllegalCatch")
88     <T extends BaseListenerInterface> boolean checkStartStop(final Instant now, final T listener) {
89         if (this.stop != null) {
90             if (this.start.compareTo(now) < 0 && this.stop.compareTo(now) > 0) {
91                 return true;
92             }
93             if (this.stop.compareTo(now) < 0) {
94                 try {
95                     listener.close();
96                 } catch (final Exception e) {
97                     throw new RestconfDocumentedException("Problem with unregister listener." + e);
98                 }
99             }
100         } else if (this.start != null) {
101             if (this.start.compareTo(now) < 0) {
102                 this.start = null;
103                 return true;
104             }
105         } else {
106             return true;
107         }
108         return false;
109     }
110
111     /**
112      * Check if is filter used and then prepare and post data do client.
113      *
114      * @param xml XML data of notification.
115      */
116     @SuppressWarnings("checkstyle:IllegalCatch")
117     boolean checkFilter(final String xml) {
118         if (this.filter == null) {
119             return true;
120         }
121         try {
122             return parseFilterParam(xml);
123         } catch (final Exception e) {
124             throw new RestconfDocumentedException("Problem while parsing filter.", e);
125         }
126     }
127
128     /**
129      * Parse and evaluate filter statement by XML format.
130      *
131      * @return {@code true} or {@code false} depending on filter expression and data of notification.
132      * @throws Exception If operation fails.
133      */
134     private boolean parseFilterParam(final String xml) throws Exception {
135         final Document docOfXml = DBF.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
136         final XPath xPath = XPathFactory.newInstance().newXPath();
137         // FIXME: BUG-7956: xPath.setNamespaceContext(nsContext);
138         return (boolean) xPath.compile(this.filter).evaluate(docOfXml, XPathConstants.BOOLEAN);
139     }
140 }