Skip to content

Commit

Permalink
Merge pull request #281 from thc202/custom-engine-script
Browse files Browse the repository at this point in the history
Allow to invoke scripts with custom engines
  • Loading branch information
thc202 authored Jun 28, 2024
2 parents e435b72 + 897c5c4 commit c6f88ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/main/java/org/zaproxy/zest/core/v1/ZestActionInvoke.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ public String invoke(ZestResponse response, ZestRuntime runtime)
// Its a Zest script
engine = runtime.getScriptEngineFactory().getScriptEngine();
} else {
ScriptEngineManager manager = new ScriptEngineManager();
engine = manager.getEngineByExtension(ext);
if (engine == null) {
engine = manager.getEngineByName(ext);
}
engine = getScriptEngine(runtime, ext);
}
}

Expand Down Expand Up @@ -197,6 +193,20 @@ public String invoke(ZestResponse response, ZestRuntime runtime)
}
}

private static ScriptEngine getScriptEngine(ZestRuntime runtime, String ext) {
ScriptEngine engine = runtime.getScriptEngine(ext);
if (engine != null) {
return engine;
}

ScriptEngineManager manager = new ScriptEngineManager();
engine = manager.getEngineByExtension(ext);
if (engine != null) {
return engine;
}
return manager.getEngineByName(ext);
}

private Charset getCharsetImpl() {
if (charset == null || charset.isEmpty()) {
return Charset.defaultCharset();
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/zaproxy/zest/core/v1/ZestRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package org.zaproxy.zest.core.v1;

import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import org.openqa.selenium.WebDriver;

Expand Down Expand Up @@ -55,6 +56,19 @@ default void setGlobalVariable(String name, String value) {
// Nothing to do.
}

/**
* Gets a {@code ScriptEngine} for the given extension.
*
* <p>If no engine is returned it is used a default engine, if available.
*
* @param extension the extension of the script.
* @return the {@code ScriptEngine}, or {@code null}.
* @since 0.22.0
*/
default ScriptEngine getScriptEngine(String extension) {
return null;
}

/**
* Get the last response.
*
Expand Down

0 comments on commit c6f88ac

Please sign in to comment.