From 5fbb87b1ae1c17791ab8bbe8bffed406015cd8d3 Mon Sep 17 00:00:00 2001 From: jclausen Date: Thu, 11 Apr 2024 18:24:01 -0500 Subject: [PATCH] Add no-resolution interception for File component requests to allow web module to contribute upload actions --- .../bifs/global/system/CreateObject.java | 5 ++-- .../boxlang/runtime/components/io/File.java | 26 +++++++++++++++---- .../boxlang/runtime/events/BoxEvent.java | 3 ++- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main/java/ortus/boxlang/runtime/bifs/global/system/CreateObject.java b/src/main/java/ortus/boxlang/runtime/bifs/global/system/CreateObject.java index 79a96b348..4aa15cae5 100644 --- a/src/main/java/ortus/boxlang/runtime/bifs/global/system/CreateObject.java +++ b/src/main/java/ortus/boxlang/runtime/bifs/global/system/CreateObject.java @@ -22,6 +22,7 @@ import ortus.boxlang.runtime.bifs.BIF; import ortus.boxlang.runtime.bifs.BoxBIF; import ortus.boxlang.runtime.context.IBoxContext; +import ortus.boxlang.runtime.events.BoxEvent; import ortus.boxlang.runtime.loader.ClassLocator; import ortus.boxlang.runtime.scopes.ArgumentsScope; import ortus.boxlang.runtime.scopes.Key; @@ -52,7 +53,7 @@ public CreateObject() { * @param arguments Argument scope for the BIF. * * @argument.type The type of object to create - * + * * @argument.className A classname for a component/class request or the java class to create * */ @@ -73,7 +74,7 @@ public Object _invoke( IBoxContext context, ArgumentsScope arguments ) { put( Key.arguments, arguments ); } }; - interceptorService.announce( "onCreateObjectRequest", new Struct( interceptorArgs ) ); + interceptorService.announce( BoxEvent.ON_CREATEOBJECT_REQUEST, new Struct( interceptorArgs ) ); if ( interceptorArgs.get( Key.response ) != null ) { return interceptorArgs.get( Key.response ); } else { diff --git a/src/main/java/ortus/boxlang/runtime/components/io/File.java b/src/main/java/ortus/boxlang/runtime/components/io/File.java index 67b5cf3fb..e316a5192 100644 --- a/src/main/java/ortus/boxlang/runtime/components/io/File.java +++ b/src/main/java/ortus/boxlang/runtime/components/io/File.java @@ -27,8 +27,10 @@ import ortus.boxlang.runtime.components.Component; import ortus.boxlang.runtime.context.IBoxContext; import ortus.boxlang.runtime.dynamic.ExpressionInterpreter; +import ortus.boxlang.runtime.events.BoxEvent; import ortus.boxlang.runtime.scopes.Key; import ortus.boxlang.runtime.types.IStruct; +import ortus.boxlang.runtime.types.Struct; import ortus.boxlang.runtime.types.exceptions.BoxRuntimeException; import ortus.boxlang.runtime.validation.Validator; @@ -183,12 +185,26 @@ public BodyResult _invoke( IBoxContext context, IStruct attributes, ComponentBod attributes.getAsString( Key.variable ), actionsMap.get( Key.readBinary ).invoke( context, attributes, false, fileReadBinaryKey ) ); - } else if ( action.equals( Key.upload ) ) { - throw new BoxRuntimeException( "The file action [upload] is not yet implemented in the core runtime" ); - } else if ( action.equals( Key.uploadAll ) ) { - throw new BoxRuntimeException( "The file action [uploadAll] is not yet implemented in the core runtime" ); } else { - throw new BoxRuntimeException( "unimplemeted action: " + action ); + // Announce an interception so that modules can contribute to object creation requests + HashMap interceptorArgs = new HashMap() { + + { + put( Key.response, null ); + put( Key.context, context ); + put( Key.arguments, attributes ); + } + }; + interceptorService.announce( BoxEvent.ON_FILECOMPONENT_INVOKE, new Struct( interceptorArgs ) ); + if ( interceptorArgs.get( Key.response ) != null ) { + ExpressionInterpreter.setVariable( + context, + attributes.getAsString( Key.variable ), + interceptorArgs.get( Key.response ) + ); + } else { + throw new BoxRuntimeException( "The file action [" + action.getName() + "] is not currently supported" ); + } } return DEFAULT_RETURN; diff --git a/src/main/java/ortus/boxlang/runtime/events/BoxEvent.java b/src/main/java/ortus/boxlang/runtime/events/BoxEvent.java index 04ddf6b8d..d95e6f5aa 100644 --- a/src/main/java/ortus/boxlang/runtime/events/BoxEvent.java +++ b/src/main/java/ortus/boxlang/runtime/events/BoxEvent.java @@ -42,11 +42,12 @@ public enum BoxEvent { */ ON_BIF_INSTANCE( "onBIFInstance" ), ON_COMPONENT_INSTANCE( "onComponentInstance" ), + ON_FILECOMPONENT_INVOKE( "onFileComponentInvoke" ), + ON_CREATEOBJECT_REQUEST( "onCreateObjectRequest" ), /** * Dynamic Object Events */ - ON_CREATEOBJECT_REQUEST( "onCreateObjectRequest" ), AFTER_DYNAMIC_OBJECT_CREATION( "afterDynamicObjectCreation" ), /**