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

Fri, 23 Aug 2013 09:57:21 +0100

author
mkos
date
Fri, 23 Aug 2013 09:57:21 +0100
changeset 397
b99d7e355d4b
parent 368
0989ad8c0860
child 408
b0610cd08440
permissions
-rw-r--r--

8022885: Update JAX-WS RI integration to 2.2.9-b14140
8013016: Rebase 8009009 against the latest jdk8/jaxws
Reviewed-by: alanb, chegar

     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  */
    26 package com.sun.xml.internal.bind.v2.util;
    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;
    42 import org.xml.sax.SAXException;
    43 import org.xml.sax.SAXNotRecognizedException;
    44 import org.xml.sax.SAXNotSupportedException;
    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 {
    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";
    57     private static final Logger LOGGER = Logger.getLogger(XmlFactory.class.getName());
    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";
    68     public static final boolean DISABLE_SECURE_PROCESSING =
    69             Boolean.parseBoolean(Util.getSystemProperty(DISABLE_XML_SECURITY));
    71     private static boolean xmlFeatureValue(boolean runtimeSetting) {
    72         return !(DISABLE_SECURE_PROCESSING || runtimeSetting);
    73     }
    75     /**
    76      * Returns properly configured (e.g. security features) schema factory
    77      * - namespaceAware == true
    78      * - securityProcessing == is set based on security processing property, default is true
    79      */
    80     public static SchemaFactory createSchemaFactory(final String language, boolean disableSecureProcessing) throws IllegalStateException {
    81         try {
    82             SchemaFactory factory = SchemaFactory.newInstance(language);
    83             if (LOGGER.isLoggable(Level.FINE)) {
    84                 LOGGER.log(Level.FINE, "SchemaFactory instance: {0}", factory);
    85             }
    86             factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, xmlFeatureValue(disableSecureProcessing));
    87             return factory;
    88         } catch (SAXNotRecognizedException ex) {
    89             LOGGER.log(Level.SEVERE, null, ex);
    90             throw new IllegalStateException(ex);
    91         } catch (SAXNotSupportedException ex) {
    92             LOGGER.log(Level.SEVERE, null, ex);
    93             throw new IllegalStateException(ex);
    94         } catch (AbstractMethodError er) {
    95             LOGGER.log(Level.SEVERE, null, er);
    96             throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
    97         }
    98     }
   100     /**
   101      * Returns properly configured (e.g. security features) parser factory
   102      * - namespaceAware == true
   103      * - securityProcessing == is set based on security processing property, default is true
   104      */
   105     public static SAXParserFactory createParserFactory(boolean disableSecureProcessing) throws IllegalStateException {
   106         try {
   107             SAXParserFactory factory = SAXParserFactory.newInstance();
   108             if (LOGGER.isLoggable(Level.FINE)) {
   109                 LOGGER.log(Level.FINE, "SAXParserFactory instance: {0}", factory);
   110             }
   111             factory.setNamespaceAware(true);
   112             factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, xmlFeatureValue(disableSecureProcessing));
   113             return factory;
   114         } catch (ParserConfigurationException ex) {
   115             LOGGER.log(Level.SEVERE, null, ex);
   116             throw new IllegalStateException( ex);
   117         } catch (SAXNotRecognizedException ex) {
   118             LOGGER.log(Level.SEVERE, null, ex);
   119             throw new IllegalStateException( ex);
   120         } catch (SAXNotSupportedException ex) {
   121             LOGGER.log(Level.SEVERE, null, ex);
   122             throw new IllegalStateException( ex);
   123         } catch (AbstractMethodError er) {
   124             LOGGER.log(Level.SEVERE, null, er);
   125             throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
   126         }
   127     }
   129     /**
   130      * Returns properly configured (e.g. security features) factory
   131      * - securityProcessing == is set based on security processing property, default is true
   132      */
   133     public static XPathFactory createXPathFactory(boolean disableSecureProcessing) throws IllegalStateException {
   134         try {
   135             XPathFactory factory = XPathFactory.newInstance();
   136             if (LOGGER.isLoggable(Level.FINE)) {
   137                 LOGGER.log(Level.FINE, "XPathFactory instance: {0}", factory);
   138             }
   139             factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, xmlFeatureValue(disableSecureProcessing));
   140             return factory;
   141         } catch (XPathFactoryConfigurationException ex) {
   142             LOGGER.log(Level.SEVERE, null, ex);
   143             throw new IllegalStateException( ex);
   144         } catch (AbstractMethodError er) {
   145             LOGGER.log(Level.SEVERE, null, er);
   146             throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
   147         }
   148     }
   150     /**
   151      * Returns properly configured (e.g. security features) factory
   152      * - securityProcessing == is set based on security processing property, default is true
   153      */
   154     public static TransformerFactory createTransformerFactory(boolean disableSecureProcessing) throws IllegalStateException {
   155         try {
   156             TransformerFactory factory = TransformerFactory.newInstance();
   157             if (LOGGER.isLoggable(Level.FINE)) {
   158                 LOGGER.log(Level.FINE, "TransformerFactory instance: {0}", factory);
   159             }
   160             factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, xmlFeatureValue(disableSecureProcessing));
   161             return factory;
   162         } catch (TransformerConfigurationException ex) {
   163             LOGGER.log(Level.SEVERE, null, ex);
   164             throw new IllegalStateException( ex);
   165         } catch (AbstractMethodError er) {
   166             LOGGER.log(Level.SEVERE, null, er);
   167             throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
   168         }
   169     }
   171     /**
   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     }
   194     public static SchemaFactory allowFileAccess(SchemaFactory sf, boolean disableSecureProcessing) {
   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         }
   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     }
   212 }

mercurial