-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.extism.sdk; | ||
|
||
import com.sun.jna.Pointer; | ||
|
||
/** | ||
* CancelHandle is used to cancel a running Plugin | ||
*/ | ||
public class CancelHandle { | ||
private Pointer handle; | ||
|
||
public CancelHandle(Pointer handle) { | ||
this.handle = handle; | ||
} | ||
|
||
/** | ||
* Cancel execution of the Plugin associated with the CancelHandle | ||
*/ | ||
boolean cancel() { | ||
return LibExtism.INSTANCE.extism_plugin_cancel(this.handle); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.extism.sdk; | ||
|
||
/** | ||
* Thrown when an exceptional condition has occurred. | ||
*/ | ||
public class ExtismException extends RuntimeException { | ||
|
||
public ExtismException() { | ||
} | ||
|
||
public ExtismException(String message) { | ||
super(message); | ||
} | ||
|
||
public ExtismException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.extism.sdk; | ||
|
||
import java.util.Optional; | ||
|
||
public interface ExtismFunction<T extends HostUserData> { | ||
void invoke( | ||
ExtismCurrentPlugin plugin, | ||
LibExtism.ExtismVal[] params, | ||
LibExtism.ExtismVal[] returns, | ||
Optional<T> data | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.extism.sdk; | ||
|
||
import com.sun.jna.PointerType; | ||
|
||
public class HostUserData extends PointerType { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.extism.sdk.manifest; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import org.extism.sdk.wasm.WasmSource; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Manifest { | ||
|
||
@SerializedName("wasm") | ||
private final List<WasmSource> sources; | ||
|
||
@SerializedName("memory") | ||
private final MemoryOptions memoryOptions; | ||
|
||
// FIXME remove this and related stuff if not supported in java-sdk | ||
@SerializedName("allowed_hosts") | ||
private final List<String> allowedHosts; | ||
|
||
@SerializedName("allowed_paths") | ||
private final Map<String, String> allowedPaths; | ||
|
||
@SerializedName("config") | ||
private final Map<String, String> config; | ||
|
||
public Manifest() { | ||
this(new ArrayList<>(), null, null, null, null); | ||
} | ||
|
||
public Manifest(WasmSource source) { | ||
this(List.of(source)); | ||
} | ||
|
||
public Manifest(List<WasmSource> sources) { | ||
this(sources, null, null, null, null); | ||
} | ||
|
||
public Manifest(List<WasmSource> sources, MemoryOptions memoryOptions) { | ||
this(sources, memoryOptions, null, null, null); | ||
} | ||
|
||
public Manifest(List<WasmSource> sources, MemoryOptions memoryOptions, Map<String, String> config) { | ||
this(sources, memoryOptions, config, null, null); | ||
} | ||
|
||
public Manifest(List<WasmSource> sources, MemoryOptions memoryOptions, Map<String, String> config, List<String> allowedHosts) { | ||
this(sources, memoryOptions, config, allowedHosts, null); | ||
} | ||
|
||
public Manifest(List<WasmSource> sources, MemoryOptions memoryOptions, Map<String, String> config, List<String> allowedHosts, Map<String, String> allowedPaths) { | ||
this.sources = sources; | ||
this.memoryOptions = memoryOptions; | ||
this.config = config; | ||
this.allowedHosts = allowedHosts; | ||
this.allowedPaths = allowedPaths; | ||
} | ||
|
||
public void addSource(WasmSource source) { | ||
this.sources.add(source); | ||
} | ||
|
||
public List<WasmSource> getSources() { | ||
return Collections.unmodifiableList(sources); | ||
} | ||
|
||
public MemoryOptions getMemoryOptions() { | ||
return memoryOptions; | ||
} | ||
|
||
public Map<String, String> getConfig() { | ||
if (config == null || config.isEmpty()) { | ||
return Collections.emptyMap(); | ||
} | ||
return Collections.unmodifiableMap(config); | ||
} | ||
|
||
public List<String> getAllowedHosts() { | ||
if (allowedHosts == null || allowedHosts.isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
return Collections.unmodifiableList(allowedHosts); | ||
} | ||
|
||
public Map<String, String> getAllowedPaths() { | ||
if (allowedPaths == null || allowedPaths.isEmpty()) { | ||
return Collections.emptyMap(); | ||
} | ||
return Collections.unmodifiableMap(allowedPaths); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.extism.sdk.manifest; | ||
|
||
import java.util.Map; | ||
|
||
// FIXME remove this and related stuff if not supported in java-sdk | ||
public class ManifestHttpRequest { | ||
|
||
private final String url; | ||
private final Map<String, String> header; | ||
private final String method; | ||
|
||
public ManifestHttpRequest(String url, Map<String, String> header, String method) { | ||
this.url = url; | ||
this.header = header; | ||
this.method = method; | ||
} | ||
|
||
public String url() { | ||
return url; | ||
} | ||
|
||
public Map<String, String> header() { | ||
return header; | ||
} | ||
|
||
public String method() { | ||
return method; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.extism.sdk.manifest; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* Configures memory for the Wasm runtime. | ||
* Memory is described in units of pages (64KB) and represent contiguous chunks of addressable memory. | ||
* | ||
* @param max Max number of pages. | ||
*/ | ||
public class MemoryOptions { | ||
@SerializedName("max") | ||
private final Integer max; | ||
|
||
public MemoryOptions(Integer max) { | ||
this.max = max; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.extism.sdk.support; | ||
|
||
import java.security.MessageDigest; | ||
|
||
public class Hashing { | ||
|
||
public static String sha256HexDigest(byte[] input) { | ||
try { | ||
var messageDigest = MessageDigest.getInstance("SHA-256"); | ||
var messageDigestBytes = messageDigest.digest(input); | ||
|
||
var hexString = new StringBuilder(); | ||
for (var b : messageDigestBytes) { | ||
hexString.append(String.format("%02x", b)); | ||
} | ||
return hexString.toString(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.extism.sdk.support; | ||
|
||
import com.google.gson.*; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
import org.extism.sdk.manifest.Manifest; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
public class JsonSerde { | ||
|
||
private static final Gson GSON; | ||
|
||
static { | ||
GSON = new GsonBuilder() // | ||
.disableHtmlEscaping() // | ||
// needed to convert the byte[] to a base64 encoded String | ||
.registerTypeHierarchyAdapter(byte[].class, new ByteArrayAdapter()) // | ||
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) // | ||
.setPrettyPrinting() // | ||
.create(); | ||
} | ||
|
||
public static String toJson(Manifest manifest) { | ||
return GSON.toJson(manifest); | ||
} | ||
|
||
private static class ByteArrayAdapter extends TypeAdapter<byte[]> { | ||
|
||
@Override | ||
public void write(JsonWriter out, byte[] byteValue) throws IOException { | ||
out.value(new String(Base64.getEncoder().encode(byteValue))); | ||
} | ||
|
||
@Override | ||
public byte[] read(JsonReader in) { | ||
try { | ||
if (in.peek() == JsonToken.NULL) { | ||
in.nextNull(); | ||
return new byte[]{}; | ||
} | ||
String byteValue = in.nextString(); | ||
if (byteValue != null) { | ||
return Base64.getDecoder().decode(byteValue); | ||
} | ||
return new byte[]{}; | ||
} catch (Exception e) { | ||
throw new JsonParseException(e); | ||
} | ||
} | ||
} | ||
} |