mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-26 01:05:38 +00:00
#40 Fix NPE in Result.getReportDocument for malformed xml input
Compute correct Hashcode, when Input is malformed
This commit is contained in:
parent
5f18236b68
commit
8224690b6a
4 changed files with 55 additions and 17 deletions
|
|
@ -1,5 +1,8 @@
|
||||||
package de.kosit.validationtool.impl.input;
|
package de.kosit.validationtool.impl.input;
|
||||||
|
|
||||||
|
import static de.kosit.validationtool.impl.input.StreamHelper.drain;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
@ -25,11 +28,20 @@ public abstract class AbstractInput implements Input, LazyReadInput {
|
||||||
@Override
|
@Override
|
||||||
public byte[] getHashCode() {
|
public byte[] getHashCode() {
|
||||||
if (this.hashCode == null) {
|
if (this.hashCode == null) {
|
||||||
throw new IllegalStateException("Hashcode is not computed yet");
|
log.warn("Extra calculating hashcode. This is in-efficient in most cases");
|
||||||
|
computeHashcode();
|
||||||
}
|
}
|
||||||
return this.hashCode;
|
return this.hashCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void computeHashcode() {
|
||||||
|
try {
|
||||||
|
drain(this);
|
||||||
|
} catch (final IOException e) {
|
||||||
|
log.error("Error extra computing hashcode", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected InputStream wrap(final InputStream stream) {
|
protected InputStream wrap(final InputStream stream) {
|
||||||
InputStream result = stream;
|
InputStream result = stream;
|
||||||
if (!isHashcodeComputed()) {
|
if (!isHashcodeComputed()) {
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ public class SourceInput extends AbstractInput {
|
||||||
return wrap();
|
return wrap();
|
||||||
}
|
}
|
||||||
if (isConsumed()) {
|
if (isConsumed()) {
|
||||||
throw new IllegalStateException("A StreamSource can only read once");
|
throw new IllegalStateException("A SourceInput can only read once");
|
||||||
}
|
}
|
||||||
return this.source;
|
return this.source;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,12 @@ import java.security.DigestInputStream;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
import javax.xml.transform.stream.StreamSource;
|
||||||
|
|
||||||
import org.apache.commons.io.input.CountingInputStream;
|
import org.apache.commons.io.input.CountingInputStream;
|
||||||
|
|
||||||
|
import de.kosit.validationtool.api.Input;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper for stream handling.
|
* Helper for stream handling.
|
||||||
*
|
*
|
||||||
|
|
@ -56,6 +60,10 @@ public class StreamHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final int EOF = -1;
|
||||||
|
|
||||||
|
private static final int DEFAULT_BUFFER_SIZE = 4096;
|
||||||
|
|
||||||
private StreamHelper() {
|
private StreamHelper() {
|
||||||
// hide
|
// hide
|
||||||
}
|
}
|
||||||
|
|
@ -92,4 +100,36 @@ public class StreamHelper {
|
||||||
public static InputStream wrapDigesting(final LazyReadInput input, final InputStream stream, final String digestAlgorithm) {
|
public static InputStream wrapDigesting(final LazyReadInput input, final InputStream stream, final String digestAlgorithm) {
|
||||||
return new DigestingInputStream(input, stream, createDigest(digestAlgorithm));
|
return new DigestingInputStream(input, stream, createDigest(digestAlgorithm));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drains the {@link Input} without further processing. This is useful to computing hashcode etc.
|
||||||
|
*
|
||||||
|
* @param input the input
|
||||||
|
* @return the input drained once
|
||||||
|
* @throws IOException on I/O errors
|
||||||
|
*/
|
||||||
|
public static Input drain(final Input input) throws IOException {
|
||||||
|
final StreamSource s = (StreamSource) input.getSource();
|
||||||
|
try ( final InputStream stream = s.getInputStream() ) {
|
||||||
|
drain(stream);
|
||||||
|
}
|
||||||
|
return input;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drains the {@link InputStream} without further processing. This is useful to computing hashcode etc.
|
||||||
|
*
|
||||||
|
* @param input the input
|
||||||
|
* @throws IOException on I/O errors
|
||||||
|
*/
|
||||||
|
public static void drain(final InputStream input) throws IOException {
|
||||||
|
final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
||||||
|
|
||||||
|
int n;
|
||||||
|
while (EOF != (n = input.read(buffer))) {
|
||||||
|
// nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
package de.kosit.validationtool.api;
|
package de.kosit.validationtool.api;
|
||||||
|
|
||||||
|
import static de.kosit.validationtool.impl.input.StreamHelper.drain;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
|
@ -51,9 +52,7 @@ public class InputFactoryTest {
|
||||||
|
|
||||||
private static URL NOT_EXISTING;
|
private static URL NOT_EXISTING;
|
||||||
|
|
||||||
private static final int EOF = -1;
|
|
||||||
|
|
||||||
private static final int DEFAULT_BUFFER_SIZE = 4096;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
|
|
@ -83,20 +82,7 @@ public class InputFactoryTest {
|
||||||
assertThat(s1).isNotEqualTo(s3);
|
assertThat(s1).isNotEqualTo(s3);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Input drain(final Input input) throws IOException {
|
|
||||||
final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
|
||||||
final StreamSource s = (StreamSource) input.getSource();
|
|
||||||
try ( final InputStream stream = s.getInputStream() ) {
|
|
||||||
|
|
||||||
int n;
|
|
||||||
while (EOF != (n = stream.read(buffer))) {
|
|
||||||
// nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return input;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrongAlgorithm() {
|
public void testWrongAlgorithm() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue