You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by Claus Ibsen <cl...@gmail.com> on 2013/11/06 10:44:49 UTC

Re: [1/2] git commit: CAMEL-6933 Support Xerces global features configuration

Hi

It would be nice if we could log one time at INFO level all the xml
features that was configured, so end users can see that.
You could put all those into a List and then log that once. So the
logging don't get too verbose.



On Tue, Nov 5, 2013 at 9:50 AM,  <ni...@apache.org> wrote:
> 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 <wi...@gmail.com>
> Authored: Tue Nov 5 15:41:27 2013 +0800
> Committer: Willem Jiang <wi...@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;
> +    }
> +
> +
> +}
>



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: [1/2] git commit: CAMEL-6933 Support Xerces global features configuration

Posted by Willem jiang <wi...@gmail.com>.
Good suggestion.   
I will updated the code with these log information.

--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem





On Wednesday, November 6, 2013 at 5:44 PM, Claus Ibsen wrote:

> Hi
>  
> It would be nice if we could log one time at INFO level all the xml
> features that was configured, so end users can see that.
> You could put all those into a List and then log that once. So the
> logging don't get too verbose.
>  
>  
>  
> On Tue, Nov 5, 2013 at 9:50 AM, <ningjiang@apache.org (mailto:ningjiang@apache.org)> wrote:
> > 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 (mailto:willem.jiang@gmail.com)>
> > Authored: Tue Nov 5 15:41:27 2013 +0800
> > Committer: Willem Jiang <willem.jiang@gmail.com (mailto: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 (http://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;
> > + }
> > +
> > +
> > +}
>  
>  
>  
>  
>  
> --  
> Claus Ibsen
> -----------------
> Red Hat, Inc.
> Email: cibsen@redhat.com (mailto:cibsen@redhat.com)
> Twitter: davsclaus
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen