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 /** DOM Level 3 Load/Save implementation used to build the empty input for unresolved lookups. */
40 private static final DOMImplementationLS DOM_LS;
41
42 static {
43 try {
44 DOM_LS = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS");
45 } catch (final ReflectiveOperationException e) {
46 throw new ExceptionInInitializerError(e);
47 }
48 }
49
50 private LSResourceResolver delegate;
51
52 /**
53 * Constructs a new resolver that consults the given delegate and ignores whatever it does not resolve.
54 *
55 * @param delegate optional caller-supplied resolver to consult first; may be {@code null}.
56 */
57 FallbackIgnoreLSResourceResolver(final LSResourceResolver delegate) {
58 this.delegate = delegate;
59 }
60
61 /**
62 * Gets the delegate provided by the constructor or set by {@link #setDelegate}, may be {@code null}.
63 *
64 * @return The delegate provided by the constructor or set by {@link #setDelegate}, may be {@code null}.
65 */
66 LSResourceResolver getDelegate() {
67 return delegate;
68 }
69
70 @Override
71 public LSInput resolveResource(final String type, final String namespaceURI, final String publicId, final String systemId, final String baseURI) {
72 final LSInput resolved = delegate != null ? delegate.resolveResource(type, namespaceURI, publicId, systemId, baseURI) : null;
73 if (resolved != null) {
74 return resolved;
75 }
76 if (SecureException.throwOnUnresolved()) {
77 // The interface declares no checked exception; LSException is the DOM Load/Save runtime failure type.
78 throw new LSException(LSException.PARSE_ERR, SecureException.forbidden(type, namespaceURI, publicId, systemId, baseURI));
79 }
80 // A character stream, not setStringData(""): the JDK's DOMEntityResolverWrapper discards empty string data, leaving a source with no content and a
81 // null system id that Xerces then fails to absolutize. The echoed identifiers give Xerces a valid base URI; the content still comes from this
82 // empty stream, so nothing is fetched.
83 final LSInput empty = DOM_LS.createLSInput();
84 empty.setCharacterStream(new StringReader(""));
85 empty.setPublicId(publicId);
86 empty.setSystemId(systemId);
87 empty.setBaseURI(baseURI);
88 return empty;
89 }
90
91 /**
92 * Sets the delegate to consult first, replacing any previous delegate. A {@code null} value removes the delegate and leaves a pure ignore-all floor.
93 *
94 * @param delegate The delegate to consult first, or {@code null} for a pure ignore-all floor.
95 */
96 void setDelegate(final LSResourceResolver delegate) {
97 this.delegate = delegate;
98 }
99 }