1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.xml.secure;
19
20 import java.io.StringReader;
21
22 import org.w3c.dom.bootstrap.DOMImplementationRegistry;
23 import org.w3c.dom.ls.DOMImplementationLS;
24 import org.w3c.dom.ls.LSException;
25 import org.w3c.dom.ls.LSInput;
26 import org.w3c.dom.ls.LSResourceResolver;
27
28 /**
29 * {@link LSResourceResolver} floor: consults an optional caller-supplied resolver and ignores (resolves to empty) whatever the caller does not resolve.
30 * <p>
31 * The schema-compile counterpart of {@link FallbackIgnoreEntityResolver2}. The secure {@link javax.xml.validation.SchemaFactory},
32 * {@link javax.xml.validation.Validator} and {@link javax.xml.validation.ValidatorHandler} wrappers install one of these and route a caller-set resolver
33 * through {@link #setDelegate} rather than letting it replace the floor. A caller opts a specific resource in by returning a non-{@code null} {@link LSInput};
34 * anything left unresolved resolves to an empty {@link LSInput}, so the external resource is neither fetched nor leaked.
35 * </p>
36 */
37 final class FallbackIgnoreLSResourceResolver implements LSResourceResolver {
38
39 /**
40 * DOM Level 3 Load/Save implementation used to build the empty input for unresolved lookups.
41 */
42 private static final DOMImplementationLS DOM_LS;
43
44 static {
45 DOMImplementationLS domLs;
46 try {
47 domLs = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS");
48 } catch (final ReflectiveOperationException e) {
49 // The registry loads through the context class loader, which may not see the JDK's implementation (for example an OSGi bundle class loader).
50 final Thread thread = Thread.currentThread();
51 final ClassLoader contextClassLoader = thread.getContextClassLoader();
52 thread.setContextClassLoader(ClassLoader.getSystemClassLoader());
53 try {
54 domLs = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS");
55 } catch (final ReflectiveOperationException retry) {
56 retry.addSuppressed(e);
57 throw new ExceptionInInitializerError(retry);
58 } finally {
59 thread.setContextClassLoader(contextClassLoader);
60 }
61 }
62 DOM_LS = domLs;
63 }
64
65 private LSResourceResolver delegate;
66
67 /**
68 * Constructs a new resolver that consults the given delegate and ignores whatever it does not resolve.
69 *
70 * @param delegate optional caller-supplied resolver to consult first; may be {@code null}.
71 */
72 FallbackIgnoreLSResourceResolver(final LSResourceResolver delegate) {
73 this.delegate = delegate;
74 }
75
76 /**
77 * Gets the delegate provided by the constructor or set by {@link #setDelegate}, or {@code null}.
78 *
79 * @return The delegate provided by the constructor or set by {@link #setDelegate}, or {@code null}.
80 */
81 LSResourceResolver getDelegate() {
82 return delegate;
83 }
84
85 @Override
86 public LSInput resolveResource(final String type, final String namespaceURI, final String publicId, final String systemId, final String baseURI) {
87 final LSInput resolved = delegate != null ? delegate.resolveResource(type, namespaceURI, publicId, systemId, baseURI) : null;
88 if (resolved != null) {
89 return resolved;
90 }
91 if (SecureException.throwOnUnresolved()) {
92 // The interface declares no checked exception; LSException is the DOM Load/Save runtime failure type.
93 throw new LSException(LSException.PARSE_ERR, SecureException.forbidden(type, namespaceURI, publicId, systemId, baseURI));
94 }
95 // A character stream, not setStringData(""): the JDK's DOMEntityResolverWrapper discards empty string data, leaving a source with no content and a
96 // null system ID that Xerces then fails to absolutize. The echoed identifiers give Xerces a valid base URI; the content still comes from this
97 // empty stream, so nothing is fetched.
98 final LSInput empty = DOM_LS.createLSInput();
99 empty.setCharacterStream(new StringReader(""));
100 empty.setPublicId(publicId);
101 empty.setSystemId(systemId);
102 empty.setBaseURI(baseURI);
103 return empty;
104 }
105
106 /**
107 * Sets the delegate to consult first, replacing any previous delegate. A {@code null} value removes the delegate and leaves a pure ignore-all floor.
108 *
109 * @param delegate The delegate to consult first, or {@code null} for a pure ignore-all floor.
110 */
111 void setDelegate(final LSResourceResolver delegate) {
112 this.delegate = delegate;
113 }
114 }