camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/2] git commit: CAMEL-6933 Support Xerces global features configuration
Date Tue, 05 Nov 2013 08:50:35 GMT
Updated Branches:
  refs/heads/master 5761250c7 -> 2183730cf


CAMEL-6933 Support Xerces global features configuration


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

Branch: refs/heads/master
Commit: 3f10f610cd4421e403bdd00ffca1245688e7ce5c
Parents: 5761250
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Tue Nov 5 15:41:27 2013 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Tue Nov 5 15:44:58 2013 +0800

----------------------------------------------------------------------
 .../camel/converter/jaxp/XmlConverter.java      | 26 +++++++++
 .../camel/builder/xml/XPathFeatureTest.java     | 57 ++++++++++++++++++++
 2 files changed, 83 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3f10f610/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java b/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
index 994a724..79e71b9 100644
--- a/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
+++ b/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
@@ -27,6 +27,7 @@ import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.nio.ByteBuffer;
+import java.util.Map;
 import java.util.Properties;
 
 import javax.xml.parsers.DocumentBuilder;
@@ -60,9 +61,13 @@ import org.apache.camel.BytesSource;
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
 import org.apache.camel.StringSource;
+import org.apache.camel.builder.xml.XPathBuilder;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * A helper class to transform to and from various JAXB types such as {@link Source} and
{@link Document}
  *
@@ -75,8 +80,11 @@ public class XmlConverter {
     public static final String DEFAULT_CHARSET_PROPERTY = "org.apache.camel.default.charset";
     
     public static final String OUTPUT_PROPERTIES_PREFIX = "org.apache.camel.xmlconverter.output.";
+    public static final String DOCUMENT_BUILDER_FACTORY_FEATURE = "org.apache.camel.xmlconverter.documentBuilderFactory.feature";
     public static String defaultCharset = ObjectHelper.getSystemProperty(Exchange.DEFAULT_CHARSET_PROPERTY,
"UTF-8");
 
+    private static final Logger LOG = LoggerFactory.getLogger(XPathBuilder.class);
+    
     private DocumentBuilderFactory documentBuilderFactory;
     private TransformerFactory transformerFactory;
 
@@ -886,12 +894,30 @@ public class XmlConverter {
 
     // Helper methods
     //-------------------------------------------------------------------------
+    
+    protected void setupFeatures(DocumentBuilderFactory factory) {
+        Properties properties = System.getProperties();
+        for (Map.Entry<Object, Object> prop : properties.entrySet()) {
+            String key = (String) prop.getKey();
+            if (key.startsWith(XmlConverter.DOCUMENT_BUILDER_FACTORY_FEATURE)) {
+                String uri = ObjectHelper.after(key, ":");
+                Boolean value = Boolean.valueOf((String)prop.getValue());
+                try {
+                    factory.setFeature(uri, value);
+                } catch (ParserConfigurationException e) {
+                    LOG.warn("DocumentBuilderFactory doesn't support the feature {0} with
value {1}, due to {2}.", new Object[]{uri, value, e});
+                }
+            }
+        }
+    }
 
     public DocumentBuilderFactory createDocumentBuilderFactory() {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         factory.setNamespaceAware(true);
         factory.setIgnoringElementContentWhitespace(true);
         factory.setIgnoringComments(true);
+        // setup the feature from the system property
+        setupFeatures(factory);
         return factory;
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/3f10f610/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFeatureTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFeatureTest.java b/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFeatureTest.java
new file mode 100644
index 0000000..2a3f947
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/builder/xml/XPathFeatureTest.java
@@ -0,0 +1,57 @@
+/**
+ * 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.camel.builder.xml;
+
+
+import javax.xml.parsers.SAXParserFactory;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.converter.jaxp.XmlConverter;
+
+import static org.apache.camel.builder.xml.XPathBuilder.xpath;
+
+public class XPathFeatureTest extends ContextTestSupport {
+    public static final String DOM_BUILER_FACTORY_FEATRUE = XmlConverter.DOCUMENT_BUILDER_FACTORY_FEATURE;
+    
+    public static final String XML_DATA = " <!DOCTYPE foo [ " 
+        + " <!ELEMENT foo ANY > <!ENTITY xxe SYSTEM \"file:///bin/test.sh\" >]>
<test> &xxe; </test>";
+                                              
+    
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    public void testXPathResult() throws Exception {
+        // Set this feature will disable the external general entities
+        System.setProperty(DOM_BUILER_FACTORY_FEATRUE + ":" 
+            + "http://xml.org/sax/features/external-general-entities", "false");
+       
+        String result = (String)xpath("/").stringResult().evaluate(createExchange(XML_DATA));
+        assertEquals("Get a wrong result", "  ", result);
+        System.clearProperty(DOM_BUILER_FACTORY_FEATRUE + ":" 
+            + "http://xml.org/sax/features/external-general-entities");
+    }
+    
+    protected Exchange createExchange(Object xml) {
+        Exchange exchange = createExchangeWithBody(context, xml);
+        return exchange;
+    }
+
+
+}


Mime
View raw message