cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [2/2] cayenne git commit: Throw exception in case of wrong version or namespace in project XML
Date Mon, 25 Sep 2017 12:10:14 GMT
Throw exception in case of wrong version or namespace in project XML


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/f29c5ab1
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/f29c5ab1
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/f29c5ab1

Branch: refs/heads/master
Commit: f29c5ab1e853813e0f87dd27263da080c28f2783
Parents: 19958fd
Author: Nikita Timofeev <stariy95@gmail.com>
Authored: Mon Sep 25 14:48:29 2017 +0300
Committer: Nikita Timofeev <stariy95@gmail.com>
Committed: Mon Sep 25 14:48:29 2017 +0300

----------------------------------------------------------------------
 .../configuration/xml/DataChannelHandler.java   | 25 +------
 .../xml/NamespaceAwareNestedTagHandler.java     |  9 ++-
 .../configuration/xml/RootDataMapHandler.java   | 10 +--
 .../configuration/xml/VersionAwareHandler.java  | 70 ++++++++++++++++++++
 .../xml/XMLDataChannelDescriptorLoader.java     |  7 +-
 .../xml/XMLDataChannelDescriptorLoaderTest.java | 19 ++++++
 .../configuration/xml/XMLDataMapLoaderTest.java |  6 ++
 .../src/test/resources/custom-name-file.xml     |  2 +-
 .../configuration/xml/cayenne-testConfig4.xml   |  4 ++
 .../configuration/xml/cayenne-testConfig5.xml   |  4 ++
 .../configuration/xml/testConfigMap5.map.xml    |  7 ++
 11 files changed, 124 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/f29c5ab1/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/DataChannelHandler.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/DataChannelHandler.java
b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/DataChannelHandler.java
index 476bb9c..8b71bc7 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/DataChannelHandler.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/DataChannelHandler.java
@@ -19,19 +19,16 @@
 
 package org.apache.cayenne.configuration.xml;
 
-import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.configuration.DataChannelDescriptor;
-import org.apache.cayenne.util.LocalizedStringsHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
-import org.xml.sax.SAXException;
 
 /**
  * @since 4.1
  */
-final class DataChannelHandler extends NamespaceAwareNestedTagHandler {
+final class DataChannelHandler extends VersionAwareHandler {
 
     private static Logger logger = LoggerFactory.getLogger(XMLDataChannelDescriptorLoader.class);
 
@@ -41,31 +38,13 @@ final class DataChannelHandler extends NamespaceAwareNestedTagHandler
{
     DataChannelDescriptor descriptor;
 
     DataChannelHandler(XMLDataChannelDescriptorLoader xmlDataChannelDescriptorLoader, DataChannelDescriptor
dataChannelDescriptor, LoaderContext loaderContext) {
-        super(loaderContext);
+        super(loaderContext, DOMAIN_TAG);
         this.xmlDataChannelDescriptorLoader = xmlDataChannelDescriptorLoader;
         this.descriptor = dataChannelDescriptor;
         setTargetNamespace(DataChannelDescriptor.SCHEMA_XSD);
     }
 
     @Override
-    protected boolean processElement(String namespaceURI, String localName, Attributes attributes)
throws SAXException {
-        switch (localName) {
-            case DOMAIN_TAG:
-                validateVersion(attributes);
-                return true;
-        }
-        return false;
-    }
-
-    protected void validateVersion(Attributes attributes) {
-        String version = attributes.getValue("project-version");
-        if(!XMLDataChannelDescriptorLoader.CURRENT_PROJECT_VERSION.equals(version)) {
-            throw new CayenneRuntimeException("Unsupported project version: %s, please upgrade
project using Modeler v%s",
-                    version, LocalizedStringsHandler.getString("cayenne.version"));
-        }
-    }
-
-    @Override
     protected ContentHandler createChildTagHandler(String namespaceURI, String localName,
                                                    String name, Attributes attributes) {
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f29c5ab1/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/NamespaceAwareNestedTagHandler.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/NamespaceAwareNestedTagHandler.java
b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/NamespaceAwareNestedTagHandler.java
index ec332e5..6f2eca0 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/NamespaceAwareNestedTagHandler.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/NamespaceAwareNestedTagHandler.java
@@ -35,6 +35,8 @@ abstract public class NamespaceAwareNestedTagHandler extends SAXNestedTagHandler
 
     protected String targetNamespace;
 
+    protected boolean allowAllNamespaces;
+
     private StringBuilder charactersBuffer = new StringBuilder();
 
     public NamespaceAwareNestedTagHandler(LoaderContext loaderContext) {
@@ -66,7 +68,8 @@ abstract public class NamespaceAwareNestedTagHandler extends SAXNestedTagHandler
 
         ContentHandler childHandler = createChildTagHandler(namespaceURI, localName, qName,
attributes);
 
-        if(!namespaceURI.equals(targetNamespace) || !processElement(namespaceURI, localName,
attributes)) {
+        boolean validNamespace = allowAllNamespaces || namespaceURI.equals(targetNamespace);
+        if(!validNamespace || !processElement(namespaceURI, localName, attributes)) {
             // recursively pass element down into child handlers
             childHandler.startElement(namespaceURI, localName, qName, attributes);
         }
@@ -94,4 +97,8 @@ abstract public class NamespaceAwareNestedTagHandler extends SAXNestedTagHandler
     public void setTargetNamespace(String targetNamespace) {
         this.targetNamespace = targetNamespace;
     }
+
+    public void setAllowAllNamespaces(boolean allowAllNamespaces) {
+        this.allowAllNamespaces = allowAllNamespaces;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f29c5ab1/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/RootDataMapHandler.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/RootDataMapHandler.java
b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/RootDataMapHandler.java
index dba5022..f70a6ef 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/RootDataMapHandler.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/RootDataMapHandler.java
@@ -22,24 +22,18 @@ package org.apache.cayenne.configuration.xml;
 import org.apache.cayenne.map.DataMap;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
-import org.xml.sax.SAXException;
 
 /**
  * @since 4.1
  */
-public class RootDataMapHandler extends NamespaceAwareNestedTagHandler {
+public class RootDataMapHandler extends VersionAwareHandler {
 
     public RootDataMapHandler(LoaderContext loaderContext) {
-        super(loaderContext);
+        super(loaderContext, "data-map");
         setTargetNamespace(DataMap.SCHEMA_XSD);
     }
 
     @Override
-    protected boolean processElement(String namespaceURI, String localName, Attributes attributes)
throws SAXException {
-        return false;
-    }
-
-    @Override
     protected ContentHandler createChildTagHandler(String namespaceURI, String localName,
String qName, Attributes attributes) {
         if(targetNamespace.equals(namespaceURI) && "data-map".equals(localName))
{
             return new DataMapHandler(this);

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f29c5ab1/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/VersionAwareHandler.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/VersionAwareHandler.java
b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/VersionAwareHandler.java
new file mode 100644
index 0000000..2a0fb5c
--- /dev/null
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/VersionAwareHandler.java
@@ -0,0 +1,70 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+
+package org.apache.cayenne.configuration.xml;
+
+import java.util.Objects;
+
+import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.util.LocalizedStringsHandler;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+/**
+ * Handler that can validate root tag name, version and namespace.
+ *
+ * @since 4.1
+ */
+public abstract class VersionAwareHandler extends NamespaceAwareNestedTagHandler {
+
+    protected String rootTag;
+
+    public VersionAwareHandler(LoaderContext loaderContext, String rootTag) {
+        super(loaderContext);
+        setAllowAllNamespaces(true);
+        this.rootTag = Objects.requireNonNull(rootTag);
+    }
+
+    @Override
+    protected boolean processElement(String namespaceURI, String localName, Attributes attributes)
throws SAXException {
+        if(rootTag.equals(localName)) {
+            validateVersion(attributes);
+            validateNamespace(namespaceURI);
+        } else {
+            throw new CayenneRuntimeException("Illegal XML root tag: %s, expected: %s", localName,
rootTag);
+        }
+        return false;
+    }
+
+    protected void validateVersion(Attributes attributes) {
+        String version = attributes.getValue("project-version");
+        if(!XMLDataChannelDescriptorLoader.CURRENT_PROJECT_VERSION.equals(version)) {
+            throw new CayenneRuntimeException("Unsupported project version: %s, please upgrade
project using Modeler or " +
+                    "include cayenne-project-compatibility module v%s",
+                    version, LocalizedStringsHandler.getString("cayenne.version"));
+        }
+    }
+
+    protected void validateNamespace(String realNamespace) {
+        if(!targetNamespace.equals(realNamespace)) {
+            throw new CayenneRuntimeException("Unknown XML namespace %s, expected %s. Probably
xml was modified.",
+                    realNamespace, targetNamespace);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f29c5ab1/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoader.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoader.java
b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoader.java
index 2558b65..8444732 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoader.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoader.java
@@ -131,12 +131,7 @@ public class XMLDataChannelDescriptorLoader implements DataChannelDescriptorLoad
 		try(InputStream in = configurationURL.openStream()) {
 			XMLReader parser = xmlReaderProvider.get();
 			LoaderContext loaderContext = new LoaderContext(parser, handlerFactory);
-			loaderContext.addDataMapListener(new DataMapLoaderListener() {
-				@Override
-				public void onDataMapLoaded(DataMap dataMap) {
-					descriptor.getDataMaps().add(dataMap);
-				}
-			});
+			loaderContext.addDataMapListener(dataMap -> descriptor.getDataMaps().add(dataMap));
 
 			DataChannelHandler rootHandler = new DataChannelHandler(this, descriptor, loaderContext);
 			parser.setContentHandler(rootHandler);

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f29c5ab1/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoaderTest.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoaderTest.java
b/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoaderTest.java
index 8800df5..a0d5151 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoaderTest.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoaderTest.java
@@ -18,6 +18,7 @@
  ****************************************************************/
 package org.apache.cayenne.configuration.xml;
 
+import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.ConfigurationException;
 import org.apache.cayenne.configuration.ConfigurationNameMapper;
 import org.apache.cayenne.configuration.ConfigurationTree;
@@ -95,6 +96,24 @@ public class XMLDataChannelDescriptorLoaderTest {
         }
     }
 
+    @Test(expected = CayenneRuntimeException.class)
+    public void loadInvalidVersion() throws Exception {
+        XMLDataChannelDescriptorLoader loader = new XMLDataChannelDescriptorLoader();
+        injector.injectMembers(loader);
+
+        URL url = getClass().getResource("cayenne-testConfig4.xml");
+        loader.load(new URLResource(url));
+    }
+
+    @Test(expected = CayenneRuntimeException.class)
+    public void loadInvalidNamespace() throws Exception {
+        XMLDataChannelDescriptorLoader loader = new XMLDataChannelDescriptorLoader();
+        injector.injectMembers(loader);
+
+        URL url = getClass().getResource("cayenne-testConfig5.xml");
+        loader.load(new URLResource(url));
+    }
+
     @Test
     public void testLoadDataMap() {
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f29c5ab1/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/XMLDataMapLoaderTest.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/XMLDataMapLoaderTest.java
b/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/XMLDataMapLoaderTest.java
index d41b636..4692b66 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/XMLDataMapLoaderTest.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/XMLDataMapLoaderTest.java
@@ -68,6 +68,12 @@ public class XMLDataMapLoaderTest {
         loader.load(new URLResource(new URL("file:/no_such_file_for_map_xml")));
     }
 
+    @Test(expected = CayenneRuntimeException.class)
+    public void loadWrongVersionConfig() throws Exception {
+        URL url = getClass().getResource("testConfigMap5.map.xml");
+        loader.load(new URLResource(url));
+    }
+
     @Test
     public void loadEmptyConfig() throws Exception {
         URL url = getClass().getResource("testConfigMap2.map.xml");

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f29c5ab1/cayenne-server/src/test/resources/custom-name-file.xml
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/resources/custom-name-file.xml b/cayenne-server/src/test/resources/custom-name-file.xml
index 29b3610..6937ad0 100644
--- a/cayenne-server/src/test/resources/custom-name-file.xml
+++ b/cayenne-server/src/test/resources/custom-name-file.xml
@@ -17,6 +17,6 @@
 	specific language governing permissions and limitations
 	under the License.
 -->
-<domain project-version="10">
+<domain xmlns="http://cayenne.apache.org/schema/10/domain" project-version="10">
 	<map name="testmap"/>
 </domain>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f29c5ab1/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/cayenne-testConfig4.xml
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/cayenne-testConfig4.xml
b/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/cayenne-testConfig4.xml
new file mode 100644
index 0000000..c84608d
--- /dev/null
+++ b/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/cayenne-testConfig4.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<domain project-version="9">
+	
+</domain>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f29c5ab1/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/cayenne-testConfig5.xml
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/cayenne-testConfig5.xml
b/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/cayenne-testConfig5.xml
new file mode 100644
index 0000000..33287d7
--- /dev/null
+++ b/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/cayenne-testConfig5.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<domain project-version="10" xmlns="http://cayenne.apache.org/schema/10/domains">
+	
+</domain>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f29c5ab1/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/testConfigMap5.map.xml
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/testConfigMap5.map.xml
b/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/testConfigMap5.map.xml
new file mode 100644
index 0000000..6cb1333
--- /dev/null
+++ b/cayenne-server/src/test/resources/org/apache/cayenne/configuration/xml/testConfigMap5.map.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<data-map xmlns="http://cayenne.apache.org/schema/9/modelMap"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://cayenne.apache.org/schema/9/modelMap http://cayenne.apache.org/schema/9/modelMap.xsd"
+	project-version="9">
+
+</data-map>


Mime
View raw message