camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [2/2] git commit: CAMEL-6933 Support Xerces global features configuration
Date Tue, 05 Nov 2013 09:30:26 GMT
CAMEL-6933 Support Xerces global features configuration

Conflicts:
	camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java


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

Branch: refs/heads/camel-2.11.x
Commit: 8c3eae2e046320c1f82372f1b617c18da3b7de5c
Parents: f50a618
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 17:29:53 2013 +0800

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


http://git-wip-us.apache.org/repos/asf/camel/blob/8c3eae2e/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 ac5c2fc..b596eef 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
@@ -28,6 +28,7 @@ import java.io.StringReader;
 import java.io.StringWriter;
 import java.lang.reflect.Constructor;
 import java.nio.ByteBuffer;
+import java.util.Map;
 import java.util.Properties;
 
 import javax.xml.parsers.DocumentBuilder;
@@ -62,9 +63,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}
  *
@@ -77,6 +82,7 @@ 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");
 
     /*
@@ -85,6 +91,8 @@ public class XmlConverter {
      */
     private static final Class<?> DOM_TO_SAX_CLASS;
 
+    private static final Logger LOG = LoggerFactory.getLogger(XPathBuilder.class);
+    
     private DocumentBuilderFactory documentBuilderFactory;
     private TransformerFactory transformerFactory;
 
@@ -905,11 +913,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/8c3eae2e/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