Skip to content

Commit aa29771

Browse files
authored
Merge pull request #690 from bci-oss/689-check-for-invalid-encoding
Check for invalid encoding when loading Aspect Model files
2 parents f64fd9e + a00906d commit aa29771

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/resolver/AspectModelFileLoader.java

+20-5
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,25 @@
1515

1616
import static org.apache.commons.lang3.StringUtils.isBlank;
1717

18-
import java.io.BufferedReader;
1918
import java.io.ByteArrayInputStream;
2019
import java.io.File;
2120
import java.io.FileInputStream;
2221
import java.io.FileNotFoundException;
2322
import java.io.IOException;
2423
import java.io.InputStream;
25-
import java.io.InputStreamReader;
2624
import java.net.MalformedURLException;
2725
import java.net.URI;
2826
import java.net.URISyntaxException;
2927
import java.net.URL;
28+
import java.nio.ByteBuffer;
29+
import java.nio.CharBuffer;
30+
import java.nio.charset.CharsetDecoder;
31+
import java.nio.charset.MalformedInputException;
3032
import java.nio.charset.StandardCharsets;
33+
import java.nio.charset.UnmappableCharacterException;
3134
import java.nio.file.Paths;
3235
import java.util.List;
3336
import java.util.Optional;
34-
import java.util.stream.Collectors;
3537

3638
import org.eclipse.esmf.aspectmodel.AspectModelFile;
3739
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
@@ -53,6 +55,11 @@ public static RawAspectModelFile load( final File file ) {
5355
try {
5456
final RawAspectModelFile fromString = load( content( new FileInputStream( file ) ) );
5557
return new RawAspectModelFile( fromString.sourceModel(), fromString.headerComment(), Optional.of( file.toURI() ) );
58+
} catch ( final ModelResolutionException exception ) {
59+
if ( exception.getMessage().startsWith( "Encountered invalid encoding" ) ) {
60+
throw new ModelResolutionException( "Encountered invalid encoding in input file " + file, exception.getCause() );
61+
}
62+
throw exception;
5663
} catch ( final FileNotFoundException exception ) {
5764
throw new ModelResolutionException( "File not found: " + file, exception );
5865
}
@@ -70,8 +77,16 @@ public static RawAspectModelFile load( final String rdfTurtle ) {
7077
}
7178

7279
private static String content( final InputStream inputStream ) {
73-
return new BufferedReader( new InputStreamReader( inputStream, StandardCharsets.UTF_8 ) ).lines()
74-
.collect( Collectors.joining( "\n" ) );
80+
try {
81+
final byte[] bytes = inputStream.readAllBytes();
82+
final CharsetDecoder charsetDecoder = StandardCharsets.UTF_8.newDecoder();
83+
final CharBuffer decodedCharBuffer = charsetDecoder.decode( ByteBuffer.wrap( bytes ) );
84+
return new String( bytes, StandardCharsets.UTF_8 );
85+
} catch ( final MalformedInputException | UnmappableCharacterException exception ) {
86+
throw new ModelResolutionException( "Encountered invalid encoding in input" );
87+
} catch ( final IOException exception ) {
88+
throw new RuntimeException( exception );
89+
}
7590
}
7691

7792
private static List<String> headerComment( final String content ) {

core/esmf-aspect-meta-model-java/src/test/java/org/eclipse/esmf/aspectmodel/loader/AspectModelLoaderTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.eclipse.esmf.aspectmodel.AspectModelFile;
3333
import org.eclipse.esmf.aspectmodel.resolver.FileSystemStrategy;
3434
import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategy;
35+
import org.eclipse.esmf.aspectmodel.resolver.exceptions.ModelResolutionException;
3536
import org.eclipse.esmf.metamodel.AbstractEntity;
3637
import org.eclipse.esmf.metamodel.AspectModel;
3738
import org.eclipse.esmf.metamodel.ComplexType;
@@ -56,6 +57,13 @@ void loadAspectModelWithoutCharacteristicDatatype() {
5657
.hasMessage( "No datatype is defined on the Characteristic instance 'Characteristic1: '." );
5758
}
5859

60+
@Test
61+
void testFileWithInvalidEncoding() {
62+
assertThatThrownBy( () -> TestResources.load( InvalidTestAspect.INVALID_ENCODING ) )
63+
.isInstanceOf( ModelResolutionException.class )
64+
.hasMessageContaining( "Encountered invalid encoding" );
65+
}
66+
5967
@Test
6068
void testOfAbstractEntityCyclomaticCreation() {
6169
final Map<String, ComplexType> entities =

core/esmf-aspect-model-validator/src/test/java/org/eclipse/esmf/aspectmodel/validation/services/AspectModelValidatorTest.java

-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515

1616
import static org.assertj.core.api.Assertions.assertThat;
1717

18-
import java.io.File;
1918
import java.util.ArrayList;
2019
import java.util.Arrays;
2120
import java.util.List;
2221
import java.util.function.Supplier;
2322

24-
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
2523
import org.eclipse.esmf.aspectmodel.resolver.modelfile.MetaModelFile;
2624
import org.eclipse.esmf.aspectmodel.shacl.fix.Fix;
2725
import org.eclipse.esmf.aspectmodel.shacl.violation.DatatypeViolation;

core/esmf-test-aspect-models/src/main/java/org/eclipse/esmf/test/InvalidTestAspect.java

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public enum InvalidTestAspect implements TestModel {
2323
ASPECT_WITH_SAMM_NAMESPACE_FOR_CUSTOM_UNIT,
2424
ASPECT_WITH_RECURSIVE_PROPERTY,
2525
INVALID_SYNTAX,
26+
INVALID_ENCODING,
2627
MISSING_ASPECT_DECLARATION,
2728
INVALID_EXAMPLE_VALUE_DATATYPE,
2829
INVALID_PREFERRED_NAME_DATATYPE,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
2+
#
3+
# See the AUTHORS file(s) distributed with this work for additional
4+
# information regarding authorship.
5+
#
6+
# This Source Code Form is subject to the terms of the Mozilla Public
7+
# License, v. 2.0. If a copy of the MPL was not distributed with this
8+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
9+
#
10+
# SPDX-License-Identifier: MPL-2.0
11+
12+
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
13+
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
14+
15+
:InvalidSyntax a samm:Aspect ;
16+
samm:description "The following umlaut is ISO-8859 encoded: Ä"@en ;
17+
samm:properties () ;
18+
samm:operations () .

0 commit comments

Comments
 (0)