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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 397
b99d7e355d4b
child 637
9c07ef4934dd
child 721
06807f9a6835
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

alanb@368 1 /*
mkos@397 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
alanb@368 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
alanb@368 4 *
alanb@368 5 * This code is free software; you can redistribute it and/or modify it
alanb@368 6 * under the terms of the GNU General Public License version 2 only, as
alanb@368 7 * published by the Free Software Foundation. Oracle designates this
alanb@368 8 * particular file as subject to the "Classpath" exception as provided
alanb@368 9 * by Oracle in the LICENSE file that accompanied this code.
alanb@368 10 *
alanb@368 11 * This code is distributed in the hope that it will be useful, but WITHOUT
alanb@368 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
alanb@368 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
alanb@368 14 * version 2 for more details (a copy is included in the LICENSE file that
alanb@368 15 * accompanied this code).
alanb@368 16 *
alanb@368 17 * You should have received a copy of the GNU General Public License version
alanb@368 18 * 2 along with this work; if not, write to the Free Software Foundation,
alanb@368 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
alanb@368 20 *
alanb@368 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
alanb@368 22 * or visit www.oracle.com if you need additional information or have any
alanb@368 23 * questions.
alanb@368 24 */
alanb@368 25
alanb@368 26 package com.sun.xml.internal.bind.v2.util;
alanb@368 27
alanb@368 28 import com.sun.xml.internal.bind.Util;
alanb@368 29 import com.sun.xml.internal.bind.v2.Messages;
alanb@368 30 import java.util.logging.Level;
alanb@368 31 import java.util.logging.Logger;
alanb@368 32 import javax.xml.XMLConstants;
alanb@368 33 import javax.xml.parsers.DocumentBuilderFactory;
alanb@368 34 import javax.xml.parsers.ParserConfigurationException;
alanb@368 35 import javax.xml.parsers.SAXParserFactory;
alanb@368 36 import javax.xml.transform.TransformerConfigurationException;
alanb@368 37 import javax.xml.transform.TransformerFactory;
alanb@368 38 import javax.xml.validation.SchemaFactory;
alanb@368 39 import javax.xml.xpath.XPathFactory;
alanb@368 40 import javax.xml.xpath.XPathFactoryConfigurationException;
mkos@397 41
mkos@397 42 import org.xml.sax.SAXException;
alanb@368 43 import org.xml.sax.SAXNotRecognizedException;
alanb@368 44 import org.xml.sax.SAXNotSupportedException;
alanb@368 45
mkos@408 46 import static com.sun.xml.internal.bind.Util.getSystemProperty;
mkos@408 47
alanb@368 48 /**
alanb@368 49 * Provides helper methods for creating properly configured XML parser
alanb@368 50 * factory instances with namespace support turned on and configured for
alanb@368 51 * security.
alanb@368 52 * @author snajper
alanb@368 53 */
alanb@368 54 public class XmlFactory {
alanb@368 55
mkos@397 56 // not in older JDK, so must be duplicated here, otherwise javax.xml.XMLConstants should be used
mkos@397 57 public static final String ACCESS_EXTERNAL_SCHEMA = "http://javax.xml.XMLConstants/property/accessExternalSchema";
mkos@408 58 public static final String ACCESS_EXTERNAL_DTD = "http://javax.xml.XMLConstants/property/accessExternalDTD";
mkos@397 59
alanb@368 60 private static final Logger LOGGER = Logger.getLogger(XmlFactory.class.getName());
alanb@368 61
alanb@368 62 /**
alanb@368 63 * If true XML security features when parsing XML documents will be disabled.
alanb@368 64 * The default value is false.
alanb@368 65 *
alanb@368 66 * Boolean
alanb@368 67 * @since 2.2.6
alanb@368 68 */
alanb@368 69 private static final String DISABLE_XML_SECURITY = "com.sun.xml.internal.bind.disableXmlSecurity";
alanb@368 70
mkos@408 71 public static final boolean XML_SECURITY_DISABLED = Boolean.parseBoolean(getSystemProperty(DISABLE_XML_SECURITY));
alanb@368 72
mkos@408 73 private static boolean isXMLSecurityDisabled(boolean runtimeSetting) {
mkos@408 74 return XML_SECURITY_DISABLED || runtimeSetting;
alanb@368 75 }
alanb@368 76
alanb@368 77 /**
alanb@368 78 * Returns properly configured (e.g. security features) schema factory
alanb@368 79 * - namespaceAware == true
alanb@368 80 * - securityProcessing == is set based on security processing property, default is true
alanb@368 81 */
alanb@368 82 public static SchemaFactory createSchemaFactory(final String language, boolean disableSecureProcessing) throws IllegalStateException {
alanb@368 83 try {
alanb@368 84 SchemaFactory factory = SchemaFactory.newInstance(language);
alanb@368 85 if (LOGGER.isLoggable(Level.FINE)) {
alanb@368 86 LOGGER.log(Level.FINE, "SchemaFactory instance: {0}", factory);
alanb@368 87 }
mkos@408 88 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !isXMLSecurityDisabled(disableSecureProcessing));
alanb@368 89 return factory;
alanb@368 90 } catch (SAXNotRecognizedException ex) {
alanb@368 91 LOGGER.log(Level.SEVERE, null, ex);
alanb@368 92 throw new IllegalStateException(ex);
alanb@368 93 } catch (SAXNotSupportedException ex) {
alanb@368 94 LOGGER.log(Level.SEVERE, null, ex);
alanb@368 95 throw new IllegalStateException(ex);
alanb@368 96 } catch (AbstractMethodError er) {
alanb@368 97 LOGGER.log(Level.SEVERE, null, er);
alanb@368 98 throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
alanb@368 99 }
alanb@368 100 }
alanb@368 101
alanb@368 102 /**
alanb@368 103 * Returns properly configured (e.g. security features) parser factory
alanb@368 104 * - namespaceAware == true
alanb@368 105 * - securityProcessing == is set based on security processing property, default is true
alanb@368 106 */
alanb@368 107 public static SAXParserFactory createParserFactory(boolean disableSecureProcessing) throws IllegalStateException {
alanb@368 108 try {
alanb@368 109 SAXParserFactory factory = SAXParserFactory.newInstance();
alanb@368 110 if (LOGGER.isLoggable(Level.FINE)) {
alanb@368 111 LOGGER.log(Level.FINE, "SAXParserFactory instance: {0}", factory);
alanb@368 112 }
alanb@368 113 factory.setNamespaceAware(true);
mkos@408 114 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !isXMLSecurityDisabled(disableSecureProcessing));
alanb@368 115 return factory;
alanb@368 116 } catch (ParserConfigurationException ex) {
alanb@368 117 LOGGER.log(Level.SEVERE, null, ex);
alanb@368 118 throw new IllegalStateException( ex);
alanb@368 119 } catch (SAXNotRecognizedException ex) {
alanb@368 120 LOGGER.log(Level.SEVERE, null, ex);
alanb@368 121 throw new IllegalStateException( ex);
alanb@368 122 } catch (SAXNotSupportedException ex) {
alanb@368 123 LOGGER.log(Level.SEVERE, null, ex);
alanb@368 124 throw new IllegalStateException( ex);
alanb@368 125 } catch (AbstractMethodError er) {
alanb@368 126 LOGGER.log(Level.SEVERE, null, er);
alanb@368 127 throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
alanb@368 128 }
alanb@368 129 }
alanb@368 130
alanb@368 131 /**
alanb@368 132 * Returns properly configured (e.g. security features) factory
alanb@368 133 * - securityProcessing == is set based on security processing property, default is true
alanb@368 134 */
alanb@368 135 public static XPathFactory createXPathFactory(boolean disableSecureProcessing) throws IllegalStateException {
alanb@368 136 try {
alanb@368 137 XPathFactory factory = XPathFactory.newInstance();
alanb@368 138 if (LOGGER.isLoggable(Level.FINE)) {
alanb@368 139 LOGGER.log(Level.FINE, "XPathFactory instance: {0}", factory);
alanb@368 140 }
mkos@408 141 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !isXMLSecurityDisabled(disableSecureProcessing));
alanb@368 142 return factory;
alanb@368 143 } catch (XPathFactoryConfigurationException ex) {
alanb@368 144 LOGGER.log(Level.SEVERE, null, ex);
alanb@368 145 throw new IllegalStateException( ex);
alanb@368 146 } catch (AbstractMethodError er) {
alanb@368 147 LOGGER.log(Level.SEVERE, null, er);
alanb@368 148 throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
alanb@368 149 }
alanb@368 150 }
alanb@368 151
alanb@368 152 /**
alanb@368 153 * Returns properly configured (e.g. security features) factory
alanb@368 154 * - securityProcessing == is set based on security processing property, default is true
alanb@368 155 */
alanb@368 156 public static TransformerFactory createTransformerFactory(boolean disableSecureProcessing) throws IllegalStateException {
alanb@368 157 try {
alanb@368 158 TransformerFactory factory = TransformerFactory.newInstance();
alanb@368 159 if (LOGGER.isLoggable(Level.FINE)) {
alanb@368 160 LOGGER.log(Level.FINE, "TransformerFactory instance: {0}", factory);
alanb@368 161 }
mkos@408 162 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !isXMLSecurityDisabled(disableSecureProcessing));
alanb@368 163 return factory;
alanb@368 164 } catch (TransformerConfigurationException ex) {
alanb@368 165 LOGGER.log(Level.SEVERE, null, ex);
alanb@368 166 throw new IllegalStateException( ex);
alanb@368 167 } catch (AbstractMethodError er) {
alanb@368 168 LOGGER.log(Level.SEVERE, null, er);
alanb@368 169 throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
alanb@368 170 }
alanb@368 171 }
alanb@368 172
alanb@368 173 /**
alanb@368 174 * Returns properly configured (e.g. security features) factory
alanb@368 175 * - namespaceAware == true
alanb@368 176 * - securityProcessing == is set based on security processing property, default is true
alanb@368 177 */
alanb@368 178 public static DocumentBuilderFactory createDocumentBuilderFactory(boolean disableSecureProcessing) throws IllegalStateException {
alanb@368 179 try {
alanb@368 180 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
alanb@368 181 if (LOGGER.isLoggable(Level.FINE)) {
alanb@368 182 LOGGER.log(Level.FINE, "DocumentBuilderFactory instance: {0}", factory);
alanb@368 183 }
alanb@368 184 factory.setNamespaceAware(true);
mkos@408 185 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !isXMLSecurityDisabled(disableSecureProcessing));
alanb@368 186 return factory;
alanb@368 187 } catch (ParserConfigurationException ex) {
alanb@368 188 LOGGER.log(Level.SEVERE, null, ex);
alanb@368 189 throw new IllegalStateException( ex);
alanb@368 190 } catch (AbstractMethodError er) {
alanb@368 191 LOGGER.log(Level.SEVERE, null, er);
alanb@368 192 throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
alanb@368 193 }
alanb@368 194 }
alanb@368 195
mkos@408 196 public static SchemaFactory allowExternalAccess(SchemaFactory sf, String value, boolean disableSecureProcessing) {
mkos@397 197
mkos@408 198 // if xml security (feature secure processing) disabled, nothing to do, no restrictions applied
mkos@408 199 if (isXMLSecurityDisabled(disableSecureProcessing)) {
mkos@408 200 if (LOGGER.isLoggable(Level.FINE)) {
mkos@408 201 LOGGER.log(Level.FINE, Messages.JAXP_XML_SECURITY_DISABLED.format());
mkos@408 202 }
mkos@408 203 return sf;
mkos@408 204 }
mkos@408 205
mkos@408 206 if (System.getProperty("javax.xml.accessExternalSchema") != null) {
mkos@408 207 if (LOGGER.isLoggable(Level.FINE)) {
mkos@408 208 LOGGER.log(Level.FINE, Messages.JAXP_EXTERNAL_ACCESS_CONFIGURED.format());
mkos@408 209 }
mkos@397 210 return sf;
mkos@397 211 }
mkos@397 212
mkos@397 213 try {
mkos@408 214 sf.setProperty(ACCESS_EXTERNAL_SCHEMA, value);
mkos@408 215 if (LOGGER.isLoggable(Level.FINE)) {
mkos@408 216 LOGGER.log(Level.FINE, Messages.JAXP_SUPPORTED_PROPERTY.format(ACCESS_EXTERNAL_SCHEMA));
mkos@408 217 }
mkos@397 218 } catch (SAXException ignored) {
mkos@397 219 // nothing to do; support depends on version JDK or SAX implementation
mkos@408 220 if (LOGGER.isLoggable(Level.CONFIG)) {
mkos@408 221 LOGGER.log(Level.CONFIG, Messages.JAXP_UNSUPPORTED_PROPERTY.format(ACCESS_EXTERNAL_SCHEMA), ignored);
mkos@408 222 }
mkos@408 223 }
mkos@408 224 return sf;
mkos@408 225 }
mkos@408 226
mkos@408 227 public static SchemaFactory allowExternalDTDAccess(SchemaFactory sf, String value, boolean disableSecureProcessing) {
mkos@408 228
mkos@408 229 // if xml security (feature secure processing) disabled, nothing to do, no restrictions applied
mkos@408 230 if (isXMLSecurityDisabled(disableSecureProcessing)) {
mkos@408 231 if (LOGGER.isLoggable(Level.FINE)) {
mkos@408 232 LOGGER.log(Level.FINE, Messages.JAXP_XML_SECURITY_DISABLED.format());
mkos@408 233 }
mkos@408 234 return sf;
mkos@408 235 }
mkos@408 236
mkos@408 237 if (System.getProperty("javax.xml.accessExternalDTD") != null) {
mkos@408 238 if (LOGGER.isLoggable(Level.FINE)) {
mkos@408 239 LOGGER.log(Level.FINE, Messages.JAXP_EXTERNAL_ACCESS_CONFIGURED.format());
mkos@408 240 }
mkos@408 241 return sf;
mkos@408 242 }
mkos@408 243
mkos@408 244 try {
mkos@408 245 sf.setProperty(ACCESS_EXTERNAL_DTD, value);
mkos@408 246 if (LOGGER.isLoggable(Level.FINE)) {
mkos@408 247 LOGGER.log(Level.FINE, Messages.JAXP_SUPPORTED_PROPERTY.format(ACCESS_EXTERNAL_DTD));
mkos@408 248 }
mkos@408 249 } catch (SAXException ignored) {
mkos@408 250 // nothing to do; support depends on version JDK or SAX implementation
mkos@408 251 if (LOGGER.isLoggable(Level.CONFIG)) {
mkos@408 252 LOGGER.log(Level.CONFIG, Messages.JAXP_UNSUPPORTED_PROPERTY.format(ACCESS_EXTERNAL_DTD), ignored);
mkos@408 253 }
mkos@397 254 }
mkos@397 255 return sf;
mkos@397 256 }
mkos@397 257
alanb@368 258 }

mercurial