src/share/jaxws_classes/com/sun/xml/internal/bind/v2/util/XmlFactory.java

Print this page


   1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.v2.util;
  27 
  28 import com.sun.xml.internal.bind.Util;
  29 import com.sun.xml.internal.bind.v2.Messages;
  30 import java.util.logging.Level;
  31 import java.util.logging.Logger;
  32 import javax.xml.XMLConstants;
  33 import javax.xml.parsers.DocumentBuilderFactory;
  34 import javax.xml.parsers.ParserConfigurationException;
  35 import javax.xml.parsers.SAXParserFactory;
  36 import javax.xml.transform.TransformerConfigurationException;
  37 import javax.xml.transform.TransformerFactory;
  38 import javax.xml.validation.SchemaFactory;
  39 import javax.xml.xpath.XPathFactory;
  40 import javax.xml.xpath.XPathFactoryConfigurationException;


  41 import org.xml.sax.SAXNotRecognizedException;
  42 import org.xml.sax.SAXNotSupportedException;
  43 
  44 /**
  45  * Provides helper methods for creating properly configured XML parser
  46  * factory instances with namespace support turned on and configured for
  47  * security.
  48  * @author snajper
  49  */
  50 public class XmlFactory {
  51 



  52     private static final Logger LOGGER = Logger.getLogger(XmlFactory.class.getName());
  53 
  54     /**
  55      * If true XML security features when parsing XML documents will be disabled.
  56      * The default value is false.
  57      *
  58      * Boolean
  59      * @since 2.2.6
  60      */
  61     private static final String DISABLE_XML_SECURITY  = "com.sun.xml.internal.bind.disableXmlSecurity";
  62 
  63     public static final boolean DISABLE_SECURE_PROCESSING =
  64             Boolean.parseBoolean(Util.getSystemProperty(DISABLE_XML_SECURITY));
  65 
  66     private static boolean xmlFeatureValue(boolean runtimeSetting) {
  67         return !(DISABLE_SECURE_PROCESSING || runtimeSetting);
  68     }
  69 
  70     /**
  71      * Returns properly configured (e.g. security features) schema factory


 167      * Returns properly configured (e.g. security features) factory
 168      * - namespaceAware == true
 169      * - securityProcessing == is set based on security processing property, default is true
 170      */
 171     public static DocumentBuilderFactory createDocumentBuilderFactory(boolean disableSecureProcessing) throws IllegalStateException {
 172         try {
 173             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 174             if (LOGGER.isLoggable(Level.FINE)) {
 175                 LOGGER.log(Level.FINE, "DocumentBuilderFactory instance: {0}", factory);
 176             }
 177             factory.setNamespaceAware(true);
 178             factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, xmlFeatureValue(disableSecureProcessing));
 179             return factory;
 180         } catch (ParserConfigurationException ex) {
 181             LOGGER.log(Level.SEVERE, null, ex);
 182             throw new IllegalStateException( ex);
 183         } catch (AbstractMethodError er) {
 184             LOGGER.log(Level.SEVERE, null, er);
 185             throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
 186         }


















 187     }
 188 
 189 }
   1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.v2.util;
  27 
  28 import com.sun.xml.internal.bind.Util;
  29 import com.sun.xml.internal.bind.v2.Messages;
  30 import java.util.logging.Level;
  31 import java.util.logging.Logger;
  32 import javax.xml.XMLConstants;
  33 import javax.xml.parsers.DocumentBuilderFactory;
  34 import javax.xml.parsers.ParserConfigurationException;
  35 import javax.xml.parsers.SAXParserFactory;
  36 import javax.xml.transform.TransformerConfigurationException;
  37 import javax.xml.transform.TransformerFactory;
  38 import javax.xml.validation.SchemaFactory;
  39 import javax.xml.xpath.XPathFactory;
  40 import javax.xml.xpath.XPathFactoryConfigurationException;
  41 
  42 import org.xml.sax.SAXException;
  43 import org.xml.sax.SAXNotRecognizedException;
  44 import org.xml.sax.SAXNotSupportedException;
  45 
  46 /**
  47  * Provides helper methods for creating properly configured XML parser
  48  * factory instances with namespace support turned on and configured for
  49  * security.
  50  * @author snajper
  51  */
  52 public class XmlFactory {
  53 
  54     // not in older JDK, so must be duplicated here, otherwise javax.xml.XMLConstants should be used
  55     public static final String ACCESS_EXTERNAL_SCHEMA = "http://javax.xml.XMLConstants/property/accessExternalSchema";
  56 
  57     private static final Logger LOGGER = Logger.getLogger(XmlFactory.class.getName());
  58 
  59     /**
  60      * If true XML security features when parsing XML documents will be disabled.
  61      * The default value is false.
  62      *
  63      * Boolean
  64      * @since 2.2.6
  65      */
  66     private static final String DISABLE_XML_SECURITY  = "com.sun.xml.internal.bind.disableXmlSecurity";
  67 
  68     public static final boolean DISABLE_SECURE_PROCESSING =
  69             Boolean.parseBoolean(Util.getSystemProperty(DISABLE_XML_SECURITY));
  70 
  71     private static boolean xmlFeatureValue(boolean runtimeSetting) {
  72         return !(DISABLE_SECURE_PROCESSING || runtimeSetting);
  73     }
  74 
  75     /**
  76      * Returns properly configured (e.g. security features) schema factory


 172      * Returns properly configured (e.g. security features) factory
 173      * - namespaceAware == true
 174      * - securityProcessing == is set based on security processing property, default is true
 175      */
 176     public static DocumentBuilderFactory createDocumentBuilderFactory(boolean disableSecureProcessing) throws IllegalStateException {
 177         try {
 178             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 179             if (LOGGER.isLoggable(Level.FINE)) {
 180                 LOGGER.log(Level.FINE, "DocumentBuilderFactory instance: {0}", factory);
 181             }
 182             factory.setNamespaceAware(true);
 183             factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, xmlFeatureValue(disableSecureProcessing));
 184             return factory;
 185         } catch (ParserConfigurationException ex) {
 186             LOGGER.log(Level.SEVERE, null, ex);
 187             throw new IllegalStateException( ex);
 188         } catch (AbstractMethodError er) {
 189             LOGGER.log(Level.SEVERE, null, er);
 190             throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
 191         }
 192     }
 193 
 194     public static SchemaFactory allowFileAccess(SchemaFactory sf, boolean disableSecureProcessing) {
 195 
 196         // if feature secure processing enabled, nothing to do, file is allowed,
 197         // or user is able to control access by standard JAXP mechanisms
 198         if (disableSecureProcessing) {
 199             return sf;
 200         }
 201 
 202         try {
 203             sf.setProperty(ACCESS_EXTERNAL_SCHEMA, "file");
 204             LOGGER.log(Level.FINE, Messages.JAXP_SUPPORTED_PROPERTY.format(ACCESS_EXTERNAL_SCHEMA));
 205         } catch (SAXException ignored) {
 206             // nothing to do; support depends on version JDK or SAX implementation
 207             LOGGER.log(Level.CONFIG, Messages.JAXP_UNSUPPORTED_PROPERTY.format(ACCESS_EXTERNAL_SCHEMA), ignored);
 208         }
 209         return sf;
 210     }
 211 
 212 }