diff --git a/ng-appserver/src/main/java/ng/appserver/NGApplication.java b/ng-appserver/src/main/java/ng/appserver/NGApplication.java index 49b216a8..80578eb9 100644 --- a/ng-appserver/src/main/java/ng/appserver/NGApplication.java +++ b/ng-appserver/src/main/java/ng/appserver/NGApplication.java @@ -24,6 +24,7 @@ import ng.appserver.NGProperties.PropertiesSourceArguments; import ng.appserver.NGProperties.PropertiesSourceResource; import ng.appserver.directactions.NGDirectActionRequestHandler; +import ng.appserver.resources.NGResource; import ng.appserver.resources.NGResourceManager; import ng.appserver.resources.NGResourceManagerDynamic; import ng.appserver.resources.StandardNamespace; @@ -442,16 +443,17 @@ private NGResponse noHandlerResponse( final NGRequest request ) { return new NGResponse( "No resource name specified", 400 ); } - final Optional resourceBytes = resourceManager().bytesForPublicResourceNamed( resourcePath ); + // FIXME: We've not decided what to do about namespacing and public resources // Hugi 2024-06-26 + final Optional resource = resourceManager().obtainPublicResource( resourcePath ); // FIXME: Shouldn't we allow the user to customize the response for a non-existent resource? // Hugi 2024-02-05 - if( resourceBytes.isEmpty() ) { + if( resource.isEmpty() ) { final NGResponse errorResponse = new NGResponse( "public resource '" + resourcePath + "' does not exist", 404 ); errorResponse.setHeader( "content-type", "text/html" ); return errorResponse; } - return NGResourceRequestHandler.responseForResource( resourceBytes, resourcePath ); + return NGResourceRequestHandler.responseForResource( Optional.of( resource.get().bytes() ), resourcePath ); } /** diff --git a/ng-appserver/src/main/java/ng/appserver/NGProperties.java b/ng-appserver/src/main/java/ng/appserver/NGProperties.java index 78540666..fa1e74c8 100644 --- a/ng-appserver/src/main/java/ng/appserver/NGProperties.java +++ b/ng-appserver/src/main/java/ng/appserver/NGProperties.java @@ -1,6 +1,5 @@ package ng.appserver; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.UncheckedIOException; import java.util.ArrayList; @@ -16,6 +15,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ng.appserver.resources.NGResource; + /** * Handles properties loading * @@ -115,7 +116,7 @@ public String resourcePath() { @Override public Map readAll() { - final Optional propertyBytes = NGApplication.application().resourceManager().bytesForAppResourceNamed( _namespace, _resourcePath ); + final Optional propertyBytes = NGApplication.application().resourceManager().obtainAppResource( _namespace, _resourcePath ); if( !propertyBytes.isPresent() ) { logger.warn( "No default properties file found {}::{}", _namespace, _resourcePath ); @@ -124,7 +125,7 @@ public Map readAll() { try { final Properties p = new Properties(); - p.load( new ByteArrayInputStream( propertyBytes.get() ) ); + p.load( propertyBytes.get().inputStream() ); // FIXME: WE should probably close this stream. Look into once resource loading is done // Hugi 2024-06-25 return (Map)p; } catch( IOException e ) { diff --git a/ng-appserver/src/main/java/ng/appserver/NGResourceRequestHandler.java b/ng-appserver/src/main/java/ng/appserver/NGResourceRequestHandler.java index d737e03f..9f05af5c 100644 --- a/ng-appserver/src/main/java/ng/appserver/NGResourceRequestHandler.java +++ b/ng-appserver/src/main/java/ng/appserver/NGResourceRequestHandler.java @@ -36,6 +36,8 @@ public NGResponse handleRequest( final NGRequest request ) { return responseForResource( resourceBytes, resourcePath ); } + // FIXME: Replace (or at least accompany) with an NGResource-invoking method // Hugi 2024-06-26 + @Deprecated public static NGResponse responseForResource( Optional resourceBytes, final String resourcePath ) { // FIXME: Shouldn't we allow the user to customize the response for a non-existent resource? // Hugi 2021-12-06 diff --git a/ng-appserver/src/main/java/ng/appserver/resources/NGResourceLoader.java b/ng-appserver/src/main/java/ng/appserver/resources/NGResourceLoader.java index b117416b..e3e6d93a 100644 --- a/ng-appserver/src/main/java/ng/appserver/resources/NGResourceLoader.java +++ b/ng-appserver/src/main/java/ng/appserver/resources/NGResourceLoader.java @@ -37,20 +37,6 @@ public void addResourceSource( final String namespace, final ResourceType resour .add( resourceSource ); } - /** - * @return The named resource if it exists, an empty optional if not found - */ - @Deprecated - public Optional bytesForResource( final String namespace, final ResourceType resourceType, String resourcePath ) { - Optional resource = obtainResource( namespace, resourceType, resourcePath ); - - if( !resource.isPresent() ) { - return Optional.empty(); - } - - return Optional.of( resource.get().bytes() ); - } - /** * @return The named resource if it exists, an empty optional if not found * diff --git a/ng-appserver/src/main/java/ng/appserver/resources/NGResourceManager.java b/ng-appserver/src/main/java/ng/appserver/resources/NGResourceManager.java index 78a66c30..23243678 100644 --- a/ng-appserver/src/main/java/ng/appserver/resources/NGResourceManager.java +++ b/ng-appserver/src/main/java/ng/appserver/resources/NGResourceManager.java @@ -28,7 +28,7 @@ public class NGResourceManager { /** * Cache storing resources in-memory by namespace -> resource type -> resource path */ - private final Map>>> resourceCache = new ConcurrentHashMap<>(); + private final Map>>> resourceCache = new ConcurrentHashMap<>(); /** * Specifies if we want to use the resources cache. @@ -63,8 +63,8 @@ private NGResourceLoader resourceLoader() { /** * @return The specified app resource */ - public Optional bytesForAppResourceNamed( final String namespace, final String resourcePath ) { - return bytesForResource( namespace, StandardResourceType.App, resourcePath ); + public Optional obtainAppResource( final String namespace, final String resourcePath ) { + return obtainResource( namespace, StandardResourceType.App, resourcePath ); } /** @@ -78,8 +78,8 @@ public Optional bytesForAppResourceNamed( final String resourcePath ) { /** * @return The specified webserver resource */ - public Optional bytesForWebserverResourceNamed( final String namespace, final String resourcePath ) { - return bytesForResource( namespace, StandardResourceType.WebServer, resourcePath ); + public Optional obtainWebserverResource( final String namespace, final String resourcePath ) { + return obtainResource( namespace, StandardResourceType.WebServer, resourcePath ); } /** @@ -93,8 +93,8 @@ public Optional bytesForWebserverResourceNamed( final String resourcePat /** * @return The specified component template resource */ - public Optional bytesForComponentTemplateResourceNamed( final String namespace, final String resourcePath ) { - return bytesForResource( namespace, StandardResourceType.ComponentTemplate, resourcePath ); + public Optional obtainComponentTemplateResource( final String namespace, final String resourcePath ) { + return obtainResource( namespace, StandardResourceType.ComponentTemplate, resourcePath ); } /** @@ -108,22 +108,22 @@ public Optional bytesForComponentTemplateResourceNamed( final String res /** * @return The specified public resource */ - public Optional bytesForPublicResourceNamed( final String namespace, final String resourcePath ) { - return bytesForResource( namespace, StandardResourceType.Public, resourcePath ); + public Optional obtainPublicResource( final String namespace, final String resourcePath ) { + return obtainResource( namespace, StandardResourceType.Public, resourcePath ); } /** * @return The specified public resource resource by searching in all namespaces */ @Deprecated - public Optional bytesForPublicResourceNamed( final String resourcePath ) { - return bytesForResourceSearchingAllNamespaces( StandardResourceType.Public, resourcePath ); + public Optional obtainPublicResource( final String resourcePath ) { + return obtainResourceSearchingAllNamespaces( StandardResourceType.Public, resourcePath ); } /** * @return The bytes for the named resource, looking for a cached copy first if caching is enabled (i.e. if we're in production mode) */ - private Optional bytesForResource( final String namespace, final ResourceType resourceType, final String resourcePath ) { + private Optional obtainResource( final String namespace, final ResourceType resourceType, final String resourcePath ) { Objects.requireNonNull( namespace ); Objects.requireNonNull( resourcePath ); Objects.requireNonNull( resourceType ); @@ -134,10 +134,10 @@ private Optional bytesForResource( final String namespace, final Resourc return resourceCache .computeIfAbsent( namespace, _unused -> new ConcurrentHashMap<>() ) .computeIfAbsent( resourceType, _unused -> new ConcurrentHashMap<>() ) - .computeIfAbsent( resourcePath, _unused -> resourceLoader().bytesForResource( namespace, resourceType, resourcePath ) ); + .computeIfAbsent( resourcePath, _unused -> resourceLoader().obtainResource( namespace, resourceType, resourcePath ) ); } - return resourceLoader().bytesForResource( namespace, resourceType, resourcePath ); + return resourceLoader().obtainResource( namespace, resourceType, resourcePath ); } /** @@ -145,11 +145,25 @@ private Optional bytesForResource( final String namespace, final Resourc */ @Deprecated private Optional bytesForResourceSearchingAllNamespaces( final ResourceType resourceType, final String resourcePath ) { + final Optional resource = obtainResourceSearchingAllNamespaces( resourceType, resourcePath ); + + if( !resource.isEmpty() ) { + return Optional.of( resource.get().bytes() ); + } + + return Optional.empty(); + } + + /** + * @return the specified resource by searching all namespaces + */ + @Deprecated + private Optional obtainResourceSearchingAllNamespaces( final ResourceType resourceType, final String resourcePath ) { for( String namespace : resourceLoader().namespaces() ) { - final Optional resourceBytes = bytesForResource( namespace, resourceType, resourcePath ); + final Optional resource = obtainResource( namespace, resourceType, resourcePath ); - if( !resourceBytes.isEmpty() ) { - return resourceBytes; + if( !resource.isEmpty() ) { + return resource; } }