Fri, 24 Oct 2014 15:02:28 +0200
8054367: More references for endpoints
Summary: fix also reviewed by Iaroslav.Savytskyi@oracle.com, Alexander.Fomin@oracle.com
Reviewed-by: mullan, skoivu
ohair@286 | 1 | /* |
mkos@515 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.ws.util.xml; |
ohair@286 | 27 | |
ohair@286 | 28 | import com.sun.istack.internal.Nullable; |
ohair@286 | 29 | import com.sun.org.apache.xml.internal.resolver.Catalog; |
ohair@286 | 30 | import com.sun.org.apache.xml.internal.resolver.CatalogManager; |
ohair@286 | 31 | import com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver; |
ohair@286 | 32 | import com.sun.xml.internal.ws.server.ServerRtException; |
ohair@286 | 33 | import com.sun.xml.internal.ws.util.ByteArrayBuffer; |
ohair@286 | 34 | import org.w3c.dom.Attr; |
ohair@286 | 35 | import org.w3c.dom.Element; |
ohair@286 | 36 | import org.w3c.dom.EntityReference; |
ohair@286 | 37 | import org.w3c.dom.Node; |
ohair@286 | 38 | import org.w3c.dom.NodeList; |
ohair@286 | 39 | import org.w3c.dom.Text; |
alanb@368 | 40 | import org.xml.sax.*; |
ohair@286 | 41 | |
alanb@368 | 42 | import javax.xml.XMLConstants; |
ohair@286 | 43 | import javax.xml.namespace.QName; |
alanb@368 | 44 | import javax.xml.parsers.DocumentBuilderFactory; |
ohair@286 | 45 | import javax.xml.parsers.ParserConfigurationException; |
ohair@286 | 46 | import javax.xml.parsers.SAXParserFactory; |
alanb@368 | 47 | import javax.xml.stream.XMLInputFactory; |
ohair@286 | 48 | import javax.xml.transform.Result; |
ohair@286 | 49 | import javax.xml.transform.Source; |
ohair@286 | 50 | import javax.xml.transform.Transformer; |
ohair@286 | 51 | import javax.xml.transform.TransformerConfigurationException; |
ohair@286 | 52 | import javax.xml.transform.TransformerException; |
ohair@286 | 53 | import javax.xml.transform.TransformerFactory; |
ohair@286 | 54 | import javax.xml.transform.sax.SAXTransformerFactory; |
ohair@286 | 55 | import javax.xml.transform.sax.TransformerHandler; |
ohair@286 | 56 | import javax.xml.transform.stream.StreamSource; |
mkos@397 | 57 | import javax.xml.validation.SchemaFactory; |
ohair@286 | 58 | import javax.xml.ws.WebServiceException; |
alanb@368 | 59 | import javax.xml.xpath.XPathFactory; |
alanb@368 | 60 | import javax.xml.xpath.XPathFactoryConfigurationException; |
ohair@286 | 61 | import java.io.IOException; |
ohair@286 | 62 | import java.io.InputStream; |
ohair@286 | 63 | import java.io.OutputStreamWriter; |
ohair@286 | 64 | import java.io.Writer; |
ohair@286 | 65 | import java.net.URL; |
mkos@721 | 66 | import java.security.AccessController; |
mkos@721 | 67 | import java.security.PrivilegedAction; |
ohair@286 | 68 | import java.util.ArrayList; |
ohair@286 | 69 | import java.util.Enumeration; |
ohair@286 | 70 | import java.util.Iterator; |
ohair@286 | 71 | import java.util.List; |
ohair@286 | 72 | import java.util.StringTokenizer; |
alanb@368 | 73 | import java.util.logging.Level; |
alanb@368 | 74 | import java.util.logging.Logger; |
ohair@286 | 75 | |
ohair@286 | 76 | /** |
ohair@286 | 77 | * @author WS Development Team |
ohair@286 | 78 | */ |
ohair@286 | 79 | public class XmlUtil { |
mkos@397 | 80 | |
mkos@397 | 81 | // not in older JDK, so must be duplicated here, otherwise javax.xml.XMLConstants should be used |
mkos@397 | 82 | private static final String ACCESS_EXTERNAL_SCHEMA = "http://javax.xml.XMLConstants/property/accessExternalSchema"; |
mkos@397 | 83 | |
ohair@286 | 84 | private final static String LEXICAL_HANDLER_PROPERTY = |
ohair@286 | 85 | "http://xml.org/sax/properties/lexical-handler"; |
ohair@286 | 86 | |
alanb@368 | 87 | private static final Logger LOGGER = Logger.getLogger(XmlUtil.class.getName()); |
alanb@368 | 88 | |
mkos@721 | 89 | private static final String DISABLE_XML_SECURITY = "com.sun.xml.internal.ws.disableXmlSecurity"; |
alanb@368 | 90 | |
mkos@721 | 91 | private static boolean XML_SECURITY_DISABLED = AccessController.doPrivileged( |
mkos@721 | 92 | new PrivilegedAction<Boolean>() { |
mkos@721 | 93 | @Override |
mkos@721 | 94 | public Boolean run() { |
mkos@721 | 95 | return Boolean.getBoolean(DISABLE_XML_SECURITY); |
mkos@721 | 96 | } |
mkos@721 | 97 | } |
mkos@721 | 98 | ); |
alanb@368 | 99 | |
ohair@286 | 100 | public static String getPrefix(String s) { |
ohair@286 | 101 | int i = s.indexOf(':'); |
ohair@286 | 102 | if (i == -1) |
ohair@286 | 103 | return null; |
ohair@286 | 104 | return s.substring(0, i); |
ohair@286 | 105 | } |
ohair@286 | 106 | |
ohair@286 | 107 | public static String getLocalPart(String s) { |
ohair@286 | 108 | int i = s.indexOf(':'); |
ohair@286 | 109 | if (i == -1) |
ohair@286 | 110 | return s; |
ohair@286 | 111 | return s.substring(i + 1); |
ohair@286 | 112 | } |
ohair@286 | 113 | |
ohair@286 | 114 | |
ohair@286 | 115 | |
ohair@286 | 116 | public static String getAttributeOrNull(Element e, String name) { |
ohair@286 | 117 | Attr a = e.getAttributeNode(name); |
ohair@286 | 118 | if (a == null) |
ohair@286 | 119 | return null; |
ohair@286 | 120 | return a.getValue(); |
ohair@286 | 121 | } |
ohair@286 | 122 | |
ohair@286 | 123 | public static String getAttributeNSOrNull( |
ohair@286 | 124 | Element e, |
ohair@286 | 125 | String name, |
ohair@286 | 126 | String nsURI) { |
ohair@286 | 127 | Attr a = e.getAttributeNodeNS(nsURI, name); |
ohair@286 | 128 | if (a == null) |
ohair@286 | 129 | return null; |
ohair@286 | 130 | return a.getValue(); |
ohair@286 | 131 | } |
ohair@286 | 132 | |
ohair@286 | 133 | public static String getAttributeNSOrNull( |
ohair@286 | 134 | Element e, |
ohair@286 | 135 | QName name) { |
ohair@286 | 136 | Attr a = e.getAttributeNodeNS(name.getNamespaceURI(), name.getLocalPart()); |
ohair@286 | 137 | if (a == null) |
ohair@286 | 138 | return null; |
ohair@286 | 139 | return a.getValue(); |
ohair@286 | 140 | } |
ohair@286 | 141 | |
ohair@286 | 142 | /* public static boolean matchesTagNS(Element e, String tag, String nsURI) { |
ohair@286 | 143 | try { |
ohair@286 | 144 | return e.getLocalName().equals(tag) |
ohair@286 | 145 | && e.getNamespaceURI().equals(nsURI); |
ohair@286 | 146 | } catch (NullPointerException npe) { |
ohair@286 | 147 | |
ohair@286 | 148 | // localname not null since parsing would fail before here |
ohair@286 | 149 | throw new WSDLParseException( |
ohair@286 | 150 | "null.namespace.found", |
ohair@286 | 151 | e.getLocalName()); |
ohair@286 | 152 | } |
ohair@286 | 153 | } |
ohair@286 | 154 | |
ohair@286 | 155 | public static boolean matchesTagNS( |
ohair@286 | 156 | Element e, |
ohair@286 | 157 | javax.xml.namespace.QName name) { |
ohair@286 | 158 | try { |
ohair@286 | 159 | return e.getLocalName().equals(name.getLocalPart()) |
ohair@286 | 160 | && e.getNamespaceURI().equals(name.getNamespaceURI()); |
ohair@286 | 161 | } catch (NullPointerException npe) { |
ohair@286 | 162 | |
ohair@286 | 163 | // localname not null since parsing would fail before here |
ohair@286 | 164 | throw new WSDLParseException( |
ohair@286 | 165 | "null.namespace.found", |
ohair@286 | 166 | e.getLocalName()); |
ohair@286 | 167 | } |
ohair@286 | 168 | }*/ |
ohair@286 | 169 | |
ohair@286 | 170 | public static Iterator getAllChildren(Element element) { |
ohair@286 | 171 | return new NodeListIterator(element.getChildNodes()); |
ohair@286 | 172 | } |
ohair@286 | 173 | |
ohair@286 | 174 | public static Iterator getAllAttributes(Element element) { |
ohair@286 | 175 | return new NamedNodeMapIterator(element.getAttributes()); |
ohair@286 | 176 | } |
ohair@286 | 177 | |
ohair@286 | 178 | public static List<String> parseTokenList(String tokenList) { |
ohair@286 | 179 | List<String> result = new ArrayList<String>(); |
ohair@286 | 180 | StringTokenizer tokenizer = new StringTokenizer(tokenList, " "); |
ohair@286 | 181 | while (tokenizer.hasMoreTokens()) { |
ohair@286 | 182 | result.add(tokenizer.nextToken()); |
ohair@286 | 183 | } |
ohair@286 | 184 | return result; |
ohair@286 | 185 | } |
ohair@286 | 186 | |
ohair@286 | 187 | public static String getTextForNode(Node node) { |
alanb@368 | 188 | StringBuilder sb = new StringBuilder(); |
ohair@286 | 189 | |
ohair@286 | 190 | NodeList children = node.getChildNodes(); |
ohair@286 | 191 | if (children.getLength() == 0) |
ohair@286 | 192 | return null; |
ohair@286 | 193 | |
ohair@286 | 194 | for (int i = 0; i < children.getLength(); ++i) { |
ohair@286 | 195 | Node n = children.item(i); |
ohair@286 | 196 | |
ohair@286 | 197 | if (n instanceof Text) |
ohair@286 | 198 | sb.append(n.getNodeValue()); |
ohair@286 | 199 | else if (n instanceof EntityReference) { |
ohair@286 | 200 | String s = getTextForNode(n); |
ohair@286 | 201 | if (s == null) |
ohair@286 | 202 | return null; |
ohair@286 | 203 | else |
ohair@286 | 204 | sb.append(s); |
ohair@286 | 205 | } else |
ohair@286 | 206 | return null; |
ohair@286 | 207 | } |
ohair@286 | 208 | |
ohair@286 | 209 | return sb.toString(); |
ohair@286 | 210 | } |
ohair@286 | 211 | |
ohair@286 | 212 | public static InputStream getUTF8Stream(String s) { |
ohair@286 | 213 | try { |
ohair@286 | 214 | ByteArrayBuffer bab = new ByteArrayBuffer(); |
ohair@286 | 215 | Writer w = new OutputStreamWriter(bab, "utf-8"); |
ohair@286 | 216 | w.write(s); |
ohair@286 | 217 | w.close(); |
ohair@286 | 218 | return bab.newInputStream(); |
ohair@286 | 219 | } catch (IOException e) { |
ohair@286 | 220 | throw new RuntimeException("should not happen"); |
ohair@286 | 221 | } |
ohair@286 | 222 | } |
ohair@286 | 223 | |
mkos@515 | 224 | static final ContextClassloaderLocal<TransformerFactory> transformerFactory = new ContextClassloaderLocal<TransformerFactory>() { |
mkos@515 | 225 | @Override |
mkos@515 | 226 | protected TransformerFactory initialValue() throws Exception { |
mkos@515 | 227 | return TransformerFactory.newInstance(); |
mkos@515 | 228 | } |
mkos@515 | 229 | }; |
ohair@286 | 230 | |
mkos@515 | 231 | static final ContextClassloaderLocal<SAXParserFactory> saxParserFactory = new ContextClassloaderLocal<SAXParserFactory>() { |
mkos@515 | 232 | @Override |
mkos@515 | 233 | protected SAXParserFactory initialValue() throws Exception { |
mkos@515 | 234 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
mkos@515 | 235 | factory.setNamespaceAware(true); |
mkos@515 | 236 | return factory; |
mkos@515 | 237 | } |
mkos@515 | 238 | }; |
ohair@286 | 239 | |
ohair@286 | 240 | /** |
ohair@286 | 241 | * Creates a new identity transformer. |
ohair@286 | 242 | */ |
ohair@286 | 243 | public static Transformer newTransformer() { |
ohair@286 | 244 | try { |
mkos@515 | 245 | return transformerFactory.get().newTransformer(); |
ohair@286 | 246 | } catch (TransformerConfigurationException tex) { |
ohair@286 | 247 | throw new IllegalStateException("Unable to create a JAXP transformer"); |
ohair@286 | 248 | } |
ohair@286 | 249 | } |
ohair@286 | 250 | |
ohair@286 | 251 | /** |
ohair@286 | 252 | * Performs identity transformation. |
ohair@286 | 253 | */ |
ohair@286 | 254 | public static <T extends Result> |
ohair@286 | 255 | T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException { |
ohair@286 | 256 | if (src instanceof StreamSource) { |
ohair@286 | 257 | // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing |
ohair@286 | 258 | // is not turned on by default |
ohair@286 | 259 | StreamSource ssrc = (StreamSource) src; |
mkos@515 | 260 | TransformerHandler th = ((SAXTransformerFactory) transformerFactory.get()).newTransformerHandler(); |
ohair@286 | 261 | th.setResult(result); |
mkos@515 | 262 | XMLReader reader = saxParserFactory.get().newSAXParser().getXMLReader(); |
ohair@286 | 263 | reader.setContentHandler(th); |
ohair@286 | 264 | reader.setProperty(LEXICAL_HANDLER_PROPERTY, th); |
ohair@286 | 265 | reader.parse(toInputSource(ssrc)); |
ohair@286 | 266 | } else { |
ohair@286 | 267 | newTransformer().transform(src, result); |
ohair@286 | 268 | } |
ohair@286 | 269 | return result; |
ohair@286 | 270 | } |
ohair@286 | 271 | |
ohair@286 | 272 | private static InputSource toInputSource(StreamSource src) { |
ohair@286 | 273 | InputSource is = new InputSource(); |
ohair@286 | 274 | is.setByteStream(src.getInputStream()); |
ohair@286 | 275 | is.setCharacterStream(src.getReader()); |
ohair@286 | 276 | is.setPublicId(src.getPublicId()); |
ohair@286 | 277 | is.setSystemId(src.getSystemId()); |
ohair@286 | 278 | return is; |
ohair@286 | 279 | } |
ohair@286 | 280 | |
ohair@286 | 281 | /* |
ohair@286 | 282 | * Gets an EntityResolver using XML catalog |
ohair@286 | 283 | */ |
ohair@286 | 284 | public static EntityResolver createEntityResolver(@Nullable URL catalogUrl) { |
ohair@286 | 285 | // set up a manager |
ohair@286 | 286 | CatalogManager manager = new CatalogManager(); |
ohair@286 | 287 | manager.setIgnoreMissingProperties(true); |
ohair@286 | 288 | // Using static catalog may result in to sharing of the catalog by multiple apps running in a container |
ohair@286 | 289 | manager.setUseStaticCatalog(false); |
ohair@286 | 290 | Catalog catalog = manager.getCatalog(); |
ohair@286 | 291 | try { |
ohair@286 | 292 | if (catalogUrl != null) { |
ohair@286 | 293 | catalog.parseCatalog(catalogUrl); |
ohair@286 | 294 | } |
ohair@286 | 295 | } catch (IOException e) { |
ohair@286 | 296 | throw new ServerRtException("server.rt.err",e); |
ohair@286 | 297 | } |
ohair@286 | 298 | return workaroundCatalogResolver(catalog); |
ohair@286 | 299 | } |
ohair@286 | 300 | |
ohair@286 | 301 | /** |
ohair@286 | 302 | * Gets a default EntityResolver for catalog at META-INF/jaxws-catalog.xml |
ohair@286 | 303 | */ |
ohair@286 | 304 | public static EntityResolver createDefaultCatalogResolver() { |
ohair@286 | 305 | |
ohair@286 | 306 | // set up a manager |
ohair@286 | 307 | CatalogManager manager = new CatalogManager(); |
ohair@286 | 308 | manager.setIgnoreMissingProperties(true); |
ohair@286 | 309 | // Using static catalog may result in to sharing of the catalog by multiple apps running in a container |
ohair@286 | 310 | manager.setUseStaticCatalog(false); |
ohair@286 | 311 | // parse the catalog |
ohair@286 | 312 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
ohair@286 | 313 | Enumeration<URL> catalogEnum; |
ohair@286 | 314 | Catalog catalog = manager.getCatalog(); |
ohair@286 | 315 | try { |
ohair@286 | 316 | if (cl == null) { |
ohair@286 | 317 | catalogEnum = ClassLoader.getSystemResources("META-INF/jax-ws-catalog.xml"); |
ohair@286 | 318 | } else { |
ohair@286 | 319 | catalogEnum = cl.getResources("META-INF/jax-ws-catalog.xml"); |
ohair@286 | 320 | } |
ohair@286 | 321 | |
ohair@286 | 322 | while(catalogEnum.hasMoreElements()) { |
ohair@286 | 323 | URL url = catalogEnum.nextElement(); |
ohair@286 | 324 | catalog.parseCatalog(url); |
ohair@286 | 325 | } |
ohair@286 | 326 | } catch (IOException e) { |
ohair@286 | 327 | throw new WebServiceException(e); |
ohair@286 | 328 | } |
ohair@286 | 329 | |
ohair@286 | 330 | return workaroundCatalogResolver(catalog); |
ohair@286 | 331 | } |
ohair@286 | 332 | |
ohair@286 | 333 | /** |
ohair@286 | 334 | * Default CatalogResolver implementation is broken as it depends on CatalogManager.getCatalog() which will always create a new one when |
ohair@286 | 335 | * useStaticCatalog is false. |
ohair@286 | 336 | * This returns a CatalogResolver that uses the catalog passed as parameter. |
ohair@286 | 337 | * @param catalog |
ohair@286 | 338 | * @return CatalogResolver |
ohair@286 | 339 | */ |
ohair@286 | 340 | private static CatalogResolver workaroundCatalogResolver(final Catalog catalog) { |
ohair@286 | 341 | // set up a manager |
ohair@286 | 342 | CatalogManager manager = new CatalogManager() { |
ohair@286 | 343 | @Override |
ohair@286 | 344 | public Catalog getCatalog() { |
ohair@286 | 345 | return catalog; |
ohair@286 | 346 | } |
ohair@286 | 347 | }; |
ohair@286 | 348 | manager.setIgnoreMissingProperties(true); |
ohair@286 | 349 | // Using static catalog may result in to sharing of the catalog by multiple apps running in a container |
ohair@286 | 350 | manager.setUseStaticCatalog(false); |
ohair@286 | 351 | |
ohair@286 | 352 | return new CatalogResolver(manager); |
ohair@286 | 353 | } |
ohair@286 | 354 | |
ohair@286 | 355 | /** |
ohair@286 | 356 | * {@link ErrorHandler} that always treat the error as fatal. |
ohair@286 | 357 | */ |
ohair@286 | 358 | public static final ErrorHandler DRACONIAN_ERROR_HANDLER = new ErrorHandler() { |
alanb@368 | 359 | @Override |
ohair@286 | 360 | public void warning(SAXParseException exception) { |
ohair@286 | 361 | } |
ohair@286 | 362 | |
alanb@368 | 363 | @Override |
ohair@286 | 364 | public void error(SAXParseException exception) throws SAXException { |
ohair@286 | 365 | throw exception; |
ohair@286 | 366 | } |
ohair@286 | 367 | |
alanb@368 | 368 | @Override |
ohair@286 | 369 | public void fatalError(SAXParseException exception) throws SAXException { |
ohair@286 | 370 | throw exception; |
ohair@286 | 371 | } |
ohair@286 | 372 | }; |
alanb@368 | 373 | |
alanb@368 | 374 | public static DocumentBuilderFactory newDocumentBuilderFactory() { |
alanb@368 | 375 | return newDocumentBuilderFactory(true); |
alanb@368 | 376 | } |
alanb@368 | 377 | |
alanb@368 | 378 | public static DocumentBuilderFactory newDocumentBuilderFactory(boolean secureXmlProcessing) { |
alanb@368 | 379 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
alanb@368 | 380 | try { |
mkos@408 | 381 | factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isXMLSecurityDisabled(secureXmlProcessing)); |
alanb@368 | 382 | } catch (ParserConfigurationException e) { |
mkos@408 | 383 | LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[] { factory.getClass().getName() } ); |
alanb@368 | 384 | } |
alanb@368 | 385 | return factory; |
alanb@368 | 386 | } |
alanb@368 | 387 | |
alanb@368 | 388 | public static TransformerFactory newTransformerFactory(boolean secureXmlProcessingEnabled) { |
alanb@368 | 389 | TransformerFactory factory = TransformerFactory.newInstance(); |
alanb@368 | 390 | try { |
mkos@408 | 391 | factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isXMLSecurityDisabled(secureXmlProcessingEnabled)); |
alanb@368 | 392 | } catch (TransformerConfigurationException e) { |
mkos@408 | 393 | LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[]{factory.getClass().getName()}); |
alanb@368 | 394 | } |
alanb@368 | 395 | return factory; |
alanb@368 | 396 | } |
alanb@368 | 397 | |
alanb@368 | 398 | public static TransformerFactory newTransformerFactory() { |
alanb@368 | 399 | return newTransformerFactory(true); |
alanb@368 | 400 | } |
alanb@368 | 401 | |
alanb@368 | 402 | public static SAXParserFactory newSAXParserFactory(boolean secureXmlProcessingEnabled) { |
alanb@368 | 403 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
alanb@368 | 404 | try { |
mkos@408 | 405 | factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isXMLSecurityDisabled(secureXmlProcessingEnabled)); |
alanb@368 | 406 | } catch (Exception e) { |
mkos@408 | 407 | LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[]{factory.getClass().getName()}); |
alanb@368 | 408 | } |
alanb@368 | 409 | return factory; |
alanb@368 | 410 | } |
alanb@368 | 411 | |
alanb@368 | 412 | public static XPathFactory newXPathFactory(boolean secureXmlProcessingEnabled) { |
alanb@368 | 413 | XPathFactory factory = XPathFactory.newInstance(); |
alanb@368 | 414 | try { |
mkos@408 | 415 | factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isXMLSecurityDisabled(secureXmlProcessingEnabled)); |
alanb@368 | 416 | } catch (XPathFactoryConfigurationException e) { |
mkos@408 | 417 | LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[] { factory.getClass().getName() } ); |
alanb@368 | 418 | } |
alanb@368 | 419 | return factory; |
alanb@368 | 420 | } |
alanb@368 | 421 | |
alanb@368 | 422 | public static XMLInputFactory newXMLInputFactory(boolean secureXmlProcessingEnabled) { |
alanb@368 | 423 | XMLInputFactory factory = XMLInputFactory.newInstance(); |
mkos@408 | 424 | if (isXMLSecurityDisabled(secureXmlProcessingEnabled)) { |
alanb@368 | 425 | // TODO-Miran: are those apppropriate defaults? |
alanb@368 | 426 | factory.setProperty(XMLInputFactory.SUPPORT_DTD, false); |
alanb@368 | 427 | factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); |
alanb@368 | 428 | } |
alanb@368 | 429 | return factory; |
alanb@368 | 430 | } |
alanb@368 | 431 | |
mkos@408 | 432 | private static boolean isXMLSecurityDisabled(boolean runtimeDisabled) { |
mkos@408 | 433 | return XML_SECURITY_DISABLED || runtimeDisabled; |
alanb@368 | 434 | } |
alanb@368 | 435 | |
mkos@408 | 436 | public static SchemaFactory allowExternalAccess(SchemaFactory sf, String value, boolean disableSecureProcessing) { |
mkos@397 | 437 | |
mkos@408 | 438 | // if xml security (feature secure processing) disabled, nothing to do, no restrictions applied |
mkos@408 | 439 | if (isXMLSecurityDisabled(disableSecureProcessing)) { |
mkos@408 | 440 | if (LOGGER.isLoggable(Level.FINE)) { |
mkos@408 | 441 | LOGGER.log(Level.FINE, "Xml Security disabled, no JAXP xsd external access configuration necessary."); |
mkos@408 | 442 | } |
mkos@408 | 443 | return sf; |
mkos@408 | 444 | } |
mkos@408 | 445 | |
mkos@408 | 446 | if (System.getProperty("javax.xml.accessExternalSchema") != null) { |
mkos@408 | 447 | if (LOGGER.isLoggable(Level.FINE)) { |
mkos@408 | 448 | LOGGER.log(Level.FINE, "Detected explicitly JAXP configuration, no JAXP xsd external access configuration necessary."); |
mkos@408 | 449 | } |
mkos@397 | 450 | return sf; |
mkos@397 | 451 | } |
mkos@397 | 452 | |
mkos@397 | 453 | try { |
mkos@408 | 454 | sf.setProperty(ACCESS_EXTERNAL_SCHEMA, value); |
mkos@408 | 455 | if (LOGGER.isLoggable(Level.FINE)) { |
mkos@408 | 456 | LOGGER.log(Level.FINE, "Property \"{0}\" is supported and has been successfully set by used JAXP implementation.", new Object[]{ACCESS_EXTERNAL_SCHEMA}); |
mkos@408 | 457 | } |
mkos@397 | 458 | } catch (SAXException ignored) { |
mkos@408 | 459 | // nothing to do; support depends on version JDK or SAX implementation |
mkos@408 | 460 | if (LOGGER.isLoggable(Level.CONFIG)) { |
mkos@408 | 461 | LOGGER.log(Level.CONFIG, "Property \"{0}\" is not supported by used JAXP implementation.", new Object[]{ACCESS_EXTERNAL_SCHEMA}); |
mkos@408 | 462 | } |
mkos@397 | 463 | } |
mkos@397 | 464 | return sf; |
mkos@397 | 465 | } |
mkos@408 | 466 | |
ohair@286 | 467 | } |