Skip to content

Commit

Permalink
Add no-resolution interception for File component requests to allow w…
Browse files Browse the repository at this point in the history
…eb module to contribute upload actions
  • Loading branch information
jclausen committed Apr 11, 2024
1 parent 56f35ca commit 5fbb87b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
*/
Expand All @@ -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 {
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/ortus/boxlang/runtime/components/io/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Key, Object> interceptorArgs = new HashMap<Key, Object>() {

{
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;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ortus/boxlang/runtime/events/BoxEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" ),

/**
Expand Down

0 comments on commit 5fbb87b

Please sign in to comment.