Skip to content

Commit 512cdde

Browse files
authored
Increase file size to import and fix error message for wrong prefixes (#50)
1 parent 1be60a9 commit 512cdde

File tree

12 files changed

+176
-76
lines changed

12 files changed

+176
-76
lines changed

src/main/java/org/eclipse/esmf/ame/config/ApplicationConfig.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
import com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder;
3939

4040
/**
41-
* Configuration file to return the conversion service of all the characteristic class converters
41+
* Configuration class for setting up various application-level beans and configurations.
42+
* This class primarily sets up properties, file systems, model paths, and CORS mappings.
4243
*/
4344
@Configuration
4445
@ComponentScan( basePackageClasses = ResponseExceptionHandler.class )
@@ -49,13 +50,21 @@ public class ApplicationConfig implements WebMvcConfigurer {
4950
private final Environment environment;
5051
private FileSystem importFileSystem;
5152

53+
/**
54+
* Constructs an instance of ApplicationConfig with the provided settings and environment.
55+
*
56+
* @param applicationSettings The settings of the application.
57+
* @param environment The environment the application is running in.
58+
*/
5259
public ApplicationConfig( final ApplicationSettings applicationSettings, final Environment environment ) {
5360
this.applicationSettings = applicationSettings;
5461
this.environment = environment;
5562
}
5663

5764
/**
58-
* mapping to a registry returns nothing
65+
* Configures CORS mappings for the application, allowing specific HTTP methods.
66+
*
67+
* @param registry the CORS registry.
5968
*/
6069
@Override
6170
public void addCorsMappings( final CorsRegistry registry ) {
@@ -64,10 +73,9 @@ public void addCorsMappings( final CorsRegistry registry ) {
6473
}
6574

6675
/**
67-
* FUnction to return AspectModelValidator bean to be managed by spring
68-
* controller
76+
* Creates a bean of {@link AspectModelValidator} with JavaScript evaluations disabled.
6977
*
70-
* @return AspectModelValidator
78+
* @return a new instance of AspectModelValidator.
7179
*/
7280
@Bean
7381
public AspectModelValidator getAspectModelValidator() {
@@ -77,6 +85,11 @@ public AspectModelValidator getAspectModelValidator() {
7785
return new AspectModelValidator();
7886
}
7987

88+
/**
89+
* Creates and returns an in-memory file system for imports.
90+
*
91+
* @return a new or existing in-memory file system.
92+
*/
8093
@Bean
8194
public FileSystem importFileSystem() {
8295
if ( importFileSystem == null ) {
@@ -89,6 +102,11 @@ public FileSystem importFileSystem() {
89102
return importFileSystem;
90103
}
91104

105+
/**
106+
* Determines and returns the path for models based on the environment profile.
107+
*
108+
* @return the absolute path to the models.
109+
*/
92110
@Bean
93111
public String modelPath() {
94112
if ( environment.acceptsProfiles( Profiles.of( "test" ) ) ) {
@@ -98,6 +116,11 @@ public String modelPath() {
98116
return ProcessPath.MODELS.getPath().toString();
99117
}
100118

119+
/**
120+
* Creates a list of model resolver strategies with settings and file systems configured.
121+
*
122+
* @return a list containing an instance of LocalFolderResolverStrategy.
123+
*/
101124
@Bean
102125
public List<ModelResolverStrategy> modelStrategies() {
103126
return Collections.singletonList(

src/main/java/org/eclipse/esmf/ame/config/SecurityConfig.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,23 @@
1919
import org.springframework.security.web.SecurityFilterChain;
2020

2121
/**
22-
* Application Security configuration file to configure HTTPSecurity
22+
* Represents the application's security configuration.
23+
* This configuration is responsible for setting up the HTTP security rules,
24+
* such as request matchers and CSRF settings.
25+
*
26+
* <p>Typical usage includes disabling CSRF and permitting all requests to certain paths.</p>
2327
*/
2428
@Configuration
2529
public class SecurityConfig {
2630
/**
27-
* overridden Function to configure HTTPSecurity
31+
* Configures the HTTP security filter chain. This configuration disables CSRF,
32+
* permits all requests to paths matching "/**", and requires authentication for
33+
* all other requests.
34+
*
35+
* @param http an {@link HttpSecurity} instance to configure.
36+
* @return the configured {@link SecurityFilterChain}.
2837
*
29-
* @throws Exception On input error.
38+
* @throws Exception if there's an error configuring the {@link HttpSecurity}.
3039
*/
3140
@Bean
3241
public SecurityFilterChain filterChain( final HttpSecurity http ) throws Exception {

src/main/java/org/eclipse/esmf/ame/repository/strategy/LocalFolderResolverStrategy.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515

1616
import java.io.File;
1717
import java.io.IOException;
18-
import java.io.InputStream;
1918
import java.nio.channels.FileChannel;
20-
import java.nio.charset.Charset;
2119
import java.nio.charset.StandardCharsets;
2220
import java.nio.file.FileSystem;
2321
import java.nio.file.Files;
@@ -278,7 +276,8 @@ private List<AspectModelInformation> getListOfAspectModels( final List<String> f
278276
final String[] arg = transformToValidModelDirectory( path ).split( ":" );
279277
final String namespace = arg[0] + ":" + arg[1];
280278
final String fileName = arg[2];
281-
String aspectModel = LocalFolderResolverUtils.readString( importFileSystem.getPath( path ), StandardCharsets.UTF_8 );
279+
String aspectModel = LocalFolderResolverUtils.readString( importFileSystem.getPath( path ),
280+
StandardCharsets.UTF_8 );
282281
return new AspectModelInformation( namespace, fileName, aspectModel );
283282
} catch ( IOException e ) {
284283
throw new FileNotFoundException( "Cannot find in-memory file to create package information", e );

src/main/java/org/eclipse/esmf/ame/repository/strategy/utils/LocalFolderResolverUtils.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,15 @@
1313

1414
package org.eclipse.esmf.ame.repository.strategy.utils;
1515

16-
import java.io.File;
1716
import java.io.IOException;
1817
import java.io.InputStream;
1918
import java.nio.charset.Charset;
2019
import java.nio.file.Files;
2120
import java.nio.file.Path;
22-
import java.util.Objects;
2321

2422
import javax.annotation.Nonnull;
2523

26-
import org.apache.commons.io.FileUtils;
27-
import org.eclipse.esmf.ame.exceptions.FileNotFoundException;
28-
import org.eclipse.esmf.ame.exceptions.InvalidAspectModelException;
2924
import org.eclipse.esmf.ame.model.resolver.FolderStructure;
30-
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
3125
import org.slf4j.Logger;
3226
import org.slf4j.LoggerFactory;
3327

@@ -82,12 +76,13 @@ public static String buildFilePath( final String namespace, final String fileNam
8276
* @param path The path to the file to be read.
8377
* @param charset The character encoding to be used for decoding the file content.
8478
* @return The content of the file as a string decoded with the specified character encoding.
79+
*
8580
* @throws IOException If an I/O error occurs while reading the file.
8681
*/
87-
public static String readString( Path path, Charset charset) throws IOException {
88-
try ( InputStream inputStream = Files.newInputStream(path)) {
82+
public static String readString( Path path, Charset charset ) throws IOException {
83+
try ( InputStream inputStream = Files.newInputStream( path ) ) {
8984
byte[] bytes = inputStream.readAllBytes();
90-
return new String(bytes, charset);
85+
return new String( bytes, charset );
9186
}
9287
}
9388
}

src/main/java/org/eclipse/esmf/ame/resolver/strategy/InMemoryStrategy.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,54 @@
2727

2828
import org.apache.jena.rdf.model.Model;
2929
import org.apache.jena.riot.RiotException;
30+
import org.eclipse.esmf.ame.model.repository.AspectModelInformation;
3031
import org.eclipse.esmf.ame.repository.strategy.utils.LocalFolderResolverUtils;
3132
import org.eclipse.esmf.aspectmodel.resolver.AspectModelResolver;
3233
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
3334

3435
import io.vavr.control.Try;
36+
import lombok.Getter;
3537

38+
@Getter
3639
public class InMemoryStrategy extends ResolutionStrategy {
37-
public final FileSystem fileSystem;
40+
private final FileSystem fileSystem;
3841

39-
public InMemoryStrategy( final String aspectModel, final Path processingRootPath, final FileSystem fileSystem )
40-
throws RiotException {
41-
super( aspectModel, processingRootPath );
42+
public InMemoryStrategy( final AspectModelInformation aspectModelInformation, final Path processingRootPath,
43+
final FileSystem fileSystem ) throws RiotException {
44+
super( aspectModelInformation, processingRootPath );
4245
this.fileSystem = fileSystem;
4346
}
4447

4548
protected Try<Model> getModelFromFileSystem( final AspectModelUrn aspectModelUrn, final Path rootPath ) {
4649
try ( Stream<Path> pathStream = Files.walk( rootPath ) ) {
4750
final String filePath =
48-
aspectModelUrn.getNamespace() + File.separator + aspectModelUrn.getVersion() + File.separator
49-
+ aspectModelUrn.getName() + ".ttl";
51+
aspectModelUrn.getNamespace() + File.separator + aspectModelUrn.getVersion() + File.separator
52+
+ aspectModelUrn.getName() + ".ttl";
5053

5154
final Path file = fileSystem.getPath( filePath );
5255

5356
if ( Files.exists( file ) ) {
54-
return Try.of( () -> loadTurtleFromString( LocalFolderResolverUtils.readString( file, StandardCharsets.UTF_8 ) ) );
57+
return Try.of(
58+
() -> loadTurtleFromString( LocalFolderResolverUtils.readString( file, StandardCharsets.UTF_8 ) ) );
5559
}
5660

5761
LOG.warn( "Looking for {}, but no {}.ttl was found. Inspecting files in {}", aspectModelUrn.getName(),
58-
aspectModelUrn.getName(), filePath );
62+
aspectModelUrn.getName(), filePath );
5963

60-
Optional<Try<Model>> modelWithDefinition = pathStream
61-
.filter( Files::isRegularFile )
62-
.map( Path::toAbsolutePath )
63-
.map( Path::toString )
64-
.map( fileSystem::getPath )
65-
.map( aspectModelPath -> Try.of( () -> loadTurtleFromString( LocalFolderResolverUtils.readString( aspectModelPath, StandardCharsets.UTF_8 ) ) ) )
66-
.filter( tryModel -> tryModel.map(
67-
model -> AspectModelResolver.containsDefinition( model, aspectModelUrn ) )
68-
.getOrElse( false ) )
69-
.findFirst();
64+
Optional<Try<Model>> modelWithDefinition = pathStream.filter( Files::isRegularFile )
65+
.map( Path::toAbsolutePath ).map( Path::toString )
66+
.map( fileSystem::getPath )
67+
.map( aspectModelPath -> Try.of(
68+
() -> loadTurtleFromString(
69+
LocalFolderResolverUtils.readString(
70+
aspectModelPath,
71+
StandardCharsets.UTF_8 ) ) ) ).filter(
72+
tryModel -> tryModel.map(
73+
model -> AspectModelResolver.containsDefinition( model, aspectModelUrn ) )
74+
.getOrElse( false ) ).findFirst();
7075

7176
return modelWithDefinition.orElse( Try.failure( new FileNotFoundException(
72-
"No model file containing " + aspectModelUrn + " could be found in directory" ) ) );
77+
"No model file containing " + aspectModelUrn + " could be found in directory" ) ) );
7378
} catch ( IOException exception ) {
7479
return Try.failure( exception );
7580
}

src/main/java/org/eclipse/esmf/ame/resolver/strategy/ResolutionStrategy.java

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
import org.apache.jena.riot.RiotException;
3333
import org.apache.jena.vocabulary.RDF;
3434
import org.eclipse.esmf.ame.exceptions.FileReadException;
35+
import org.eclipse.esmf.ame.exceptions.InvalidAspectModelException;
3536
import org.eclipse.esmf.ame.exceptions.UrnNotFoundException;
37+
import org.eclipse.esmf.ame.model.repository.AspectModelInformation;
3638
import org.eclipse.esmf.aspectmodel.resolver.AbstractResolutionStrategy;
3739
import org.eclipse.esmf.aspectmodel.resolver.services.TurtleLoader;
3840
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
@@ -45,18 +47,54 @@
4547

4648
import io.vavr.NotImplementedError;
4749
import io.vavr.control.Try;
50+
import lombok.Getter;
4851

52+
/**
53+
* Represents a resolution strategy for handling aspect models.
54+
*
55+
* <p>This abstract class provides common methods and behaviors to resolve and load aspect models, and should be
56+
* extended to create specific resolution strategies.</p>
57+
*/
58+
@Getter
4959
public abstract class ResolutionStrategy extends AbstractResolutionStrategy {
5060
private static final Logger LOG = LoggerFactory.getLogger( ResolutionStrategy.class );
5161

52-
public final Path processingRootPath;
53-
public final Model aspectModel;
62+
private final Path processingRootPath;
63+
private final Model currentAspectModel;
64+
65+
private final String currentFileName;
5466

67+
/**
68+
* Constructs a new ResolutionStrategy with the given parameters.
69+
*
70+
* @param aspectModel The aspect model file content as string.
71+
* @param processingRootPath The root path where processing should occur.
72+
*/
5573
public ResolutionStrategy( final String aspectModel, final Path processingRootPath ) {
5674
this.processingRootPath = processingRootPath;
57-
this.aspectModel = loadTurtleFromString( aspectModel );
75+
this.currentFileName = "NEW LOADED FILE";
76+
this.currentAspectModel = loadTurtleFromString( aspectModel );
77+
}
78+
79+
/**
80+
* Constructs a new ResolutionStrategy with the given parameters.
81+
*
82+
* @param aspectModelInformation The information about the aspect model.
83+
* @param processingRootPath The root path where processing should occur.
84+
*/
85+
public ResolutionStrategy( final AspectModelInformation aspectModelInformation, final Path processingRootPath ) {
86+
this.processingRootPath = processingRootPath;
87+
this.currentFileName = aspectModelInformation.getFileName();
88+
this.currentAspectModel = loadTurtleFromString( aspectModelInformation.getAspectModel() );
5889
}
5990

91+
/**
92+
* Attempts to apply the given aspect model URN.
93+
* <p>The method will resolve the model from the filesystem or fallback to other strategies based on the URN.</p>
94+
*
95+
* @param aspectModelUrn The aspect model URN to be processed.
96+
* @return A try containing the resolved model, or a failure if the model cannot be resolved.
97+
*/
6098
@Override
6199
public Try<Model> apply( final AspectModelUrn aspectModelUrn ) {
62100
if ( aspectModelUrn == null ) {
@@ -75,19 +113,19 @@ private Try<Model> tryOnSuccess( final AspectModelUrn aspectModelUrn, final Try<
75113
return modelFromFileSystem;
76114
}
77115

78-
return Try.success( aspectModel );
116+
return Try.success( currentAspectModel );
79117
}
80118

81119
private Try<Model> tryOnFailure( final AspectModelUrn aspectModelUrn ) {
82-
final StmtIterator stmtIterator = aspectModel.listStatements(
120+
final StmtIterator stmtIterator = currentAspectModel.listStatements(
83121
ResourceFactory.createResource( aspectModelUrn.toString() ), null, (RDFNode) null );
84122

85123
if ( stmtIterator.hasNext() ) {
86-
return Try.success( aspectModel );
124+
return Try.success( currentAspectModel );
87125
}
88126

89-
return Try.failure( new UrnNotFoundException(
90-
String.format( "%s cannot be resolved correctly.", aspectModelUrn ), aspectModelUrn ) );
127+
return Try.failure( new UrnNotFoundException( String.format( "%s cannot be resolved correctly.", aspectModelUrn ),
128+
aspectModelUrn ) );
91129
}
92130

93131
protected abstract Try<Model> getModelFromFileSystem( AspectModelUrn aspectModelUrn, Path rootPath );
@@ -118,25 +156,45 @@ protected Try<Model> loadTurtleFromFile( final File aspectModel ) {
118156
}
119157
}
120158

159+
/**
160+
* Retrieves the aspect model URN for the current aspect model.
161+
*
162+
* @return The aspect model URN.
163+
*/
121164
public AspectModelUrn getAspectModelUrn() {
122-
return AspectModelUrn.fromUrn(
123-
getEsmfStatements( aspectModel ).orElseThrow(
124-
() -> new NotImplementedError( "AspectModelUrn cannot be found." ) ).next().getSubject().getURI() );
165+
return AspectModelUrn.fromUrn( getEsmfStatements( currentAspectModel ).orElseThrow(
166+
() -> new InvalidAspectModelException( String.format(
167+
"Unable to find a recognized version of SAMM (with the identifier 'urn:samm:org.eclipse.esmf.samm') in the file: %s.",
168+
this.currentFileName ) ) ).next().getSubject().getURI() );
125169
}
126170

171+
/**
172+
* Retrieves the ESMF statements for the given aspect model.
173+
*
174+
* @param aspectModel The aspect model to get statements from.
175+
* @return An optional containing the statement iterator if available, or empty otherwise.
176+
*/
127177
public static Optional<StmtIterator> getEsmfStatements( final Model aspectModel ) {
128178
final List<StmtIterator> stmtIterators = new ArrayList<>();
129-
130179
KnownVersion.getVersions()
131-
.forEach( version -> stmtIterators.addAll( getListOfAllSAMMElements( version )
132-
.stream()
133-
.filter( resource -> aspectModel.listStatements( null, RDF.type, resource ).hasNext() )
134-
.map( resource -> aspectModel.listStatements( null, RDF.type, resource ) )
135-
.toList() ) );
136-
180+
.forEach( version -> addAllStatementsFromVersion( aspectModel, stmtIterators, version ) );
137181
return stmtIterators.isEmpty() ? Optional.empty() : stmtIterators.stream().findFirst();
138182
}
139183

184+
private static void addAllStatementsFromVersion( final Model aspectModel, final List<StmtIterator> stmtIterators,
185+
final KnownVersion version ) {
186+
stmtIterators.addAll( getListOfAllSAMMElements( version ).stream().filter(
187+
resource -> aspectModel.listStatements( null, RDF.type, resource ).hasNext() )
188+
.map( resource -> aspectModel.listStatements( null,
189+
RDF.type, resource ) ).toList() );
190+
}
191+
192+
/**
193+
* Retrieves a list of all SAMM elements for the given known version.
194+
*
195+
* @param version The known version to get SAMM elements from.
196+
* @return A list of resources representing the SAMM elements.
197+
*/
140198
private static List<Resource> getListOfAllSAMMElements( final KnownVersion version ) {
141199
final SAMM samm = new SAMM( version );
142200
final SAMMC sammc = new SAMMC( version );

0 commit comments

Comments
 (0)