Skip to content

Commit

Permalink
Ensuring an unmarshalled InputStream is not closed
Browse files Browse the repository at this point in the history
Accepting an existing InputStream for consuming, should not assume that
the InputStream can be closed.
  • Loading branch information
runeflobakk committed Nov 2, 2023
1 parent c85ba76 commit a077a9b
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import no.digipost.signature.xsd.SignatureApiSchemas;
import no.digipost.xml.bind.MarshallingCustomization;
import no.digipost.xml.parsers.SaxParserProvider;
import no.digipost.xml.transform.sax.SaxInputSources;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
Expand Down Expand Up @@ -194,7 +194,7 @@ private <T> void doWithMarshaller(T object, ThrowingBiConsumer<? super T, ? supe
}

public <T> T unmarshal(InputStream inputStream, Class<T> type) {
Source xmlSource = saxParserProvider.createSource(new InputSource(inputStream));
Source xmlSource = saxParserProvider.createSource(SaxInputSources.fromInputStreamPreventClose(inputStream));
return unmarshal(unmarshaller -> unmarshaller.unmarshal(xmlSource), type);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) Posten Norge AS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package no.digipost.xml.transform.sax;

import org.xml.sax.InputSource;

import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.Charset;


class ClosePreventingInputSource extends InputSource {

private boolean initialized;

public ClosePreventingInputSource(InputStream inputStream, Charset encoding) {
super(new ClosePreventingInputStream(inputStream));
if (encoding != null) {
this.setEncoding(encoding.name());
}
this.initialized = true;
}

private static final class ClosePreventingInputStream extends FilterInputStream {
public ClosePreventingInputStream(InputStream in) {
super(in);
}
@Override
public void close() {
// prevent closing
}
}



@Override
public final Reader getCharacterStream() {
return null;
}

@Override
public final void setCharacterStream(Reader characterStream) {
throw new UnsupportedOperationException("Setting a character stream is not supported");
}

@Override
public final void setByteStream(InputStream byteStream) {
if (initialized) {
throw new UnsupportedOperationException("Setting a byte stream is not supported");
}
super.setByteStream(byteStream);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) Posten Norge AS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package no.digipost.xml.transform.sax;

import org.xml.sax.InputSource;

import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;

import static java.util.Objects.requireNonNull;

public final class SaxInputSources {

/**
* Create a SAX {@link InputSource} wrapping the given existing InputStream
* which will not be closed by anything consuming the InputSource.
* The wrapped InputStream, which will be returned by {@link InputSource#getByteStream()},
* will be ensured cannot be closed, even if attempted. The responsibility to
* properly manage an existing InputStream (i.e. closing)
* is outside the InputSource, and it cannot assume that the wrapped stream
* can be closed after done consuming it.
*/
public static InputSource fromInputStreamPreventClose(InputStream inputStream) {
return fromInputStreamPreventClose(inputStream, null);
}

/**
* Create a SAX {@link InputSource} wrapping the given existing InputStream
* which will not be closed by anything consuming the InputSource.
*
* @see #fromInputStreamPreventClose(InputStream)
*/
public static InputSource fromInputStreamPreventClose(InputStream inputStream, Charset encoding) {
return new ClosePreventingInputSource(inputStream, encoding);
}


public static InputSource fromClasspath(String resourceName) {
return fromClasspath(resourceName, null);
}

public static InputSource fromClasspath(String resourceName, Charset encoding) {
return fromClasspath(resourceName, encoding, UrlInputSource.class.getClassLoader());
}

public static InputSource fromClasspath(String resourceName, Charset encoding, ClassLoader classLoader) {
URL location = classLoader.getResource(resourceName.startsWith("/") ? resourceName.substring(1) : resourceName);
requireNonNull(location, resourceName + " not found on classpath");
return new UrlInputSource(location, encoding);
}

private SaxInputSources() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,18 @@

import static java.util.Objects.requireNonNull;

public class UrlInputSource extends InputSource {
class UrlInputSource extends InputSource {

public static UrlInputSource fromClasspath(String resourceName, Charset encoding) {
return fromClasspath(resourceName, encoding, UrlInputSource.class.getClassLoader());
}

public static UrlInputSource fromClasspath(String resourceName, Charset encoding, ClassLoader classLoader) {
URL location = classLoader.getResource(resourceName.startsWith("/") ? resourceName.substring(1) : resourceName);
requireNonNull(location, resourceName + " not found on classpath");
return new UrlInputSource(location, encoding);
}

private URL location;
private final URL location;
private boolean initialized;

public UrlInputSource(URL location, Charset encoding) {
this.location = location;
this.setEncoding(encoding.name());
this.location = requireNonNull(location, "location URL");
this.setSystemId(location.toString());
if (encoding != null) {
this.setEncoding(encoding.name());
}
this.initialized = true;
}

@Override
Expand All @@ -58,13 +52,20 @@ public final InputStream getByteStream() {
}

@Override
public final String getEncoding() {
return super.getEncoding();
public final Reader getCharacterStream() {
return null;
}

@Override
public final Reader getCharacterStream() {
return null;
public final void setCharacterStream(Reader characterStream) {
throw new UnsupportedOperationException("Setting a character stream is not supported");
}

@Override
public final void setByteStream(InputStream byteStream) {
if (initialized) {
throw new UnsupportedOperationException("Setting a byte stream is not supported");
}
super.setByteStream(byteStream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package no.digipost.xml.validation;

import no.digipost.xml.parsers.SaxParserProvider;
import no.digipost.xml.transform.sax.UrlInputSource;
import no.digipost.xml.transform.sax.SaxInputSources;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
Expand All @@ -25,8 +25,6 @@

import java.util.Collection;

import static java.nio.charset.StandardCharsets.UTF_8;

public final class SchemaHelper {

public static Schema createW3cXmlSchema(Collection<String> schemaResourceNames) {
Expand All @@ -35,7 +33,7 @@ public static Schema createW3cXmlSchema(Collection<String> schemaResourceNames)
}
try {
Source[] schemaSources = schemaResourceNames.stream()
.map(resourceName -> UrlInputSource.fromClasspath(resourceName, UTF_8))
.map(SaxInputSources::fromClasspath)
.map(SaxParserProvider.createSecuredProvider()::createSource)
.toArray(Source[]::new);

Expand Down

0 comments on commit a077a9b

Please sign in to comment.