Skip to content

Commit

Permalink
add missing java sources
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Jan 30, 2024
1 parent 5715c08 commit 52e2338
Show file tree
Hide file tree
Showing 19 changed files with 462 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/extism.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions java/src/main/java/org/extism/sdk/CancelHandle.java
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);
}
}
18 changes: 18 additions & 0 deletions java/src/main/java/org/extism/sdk/ExtismException.java
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);
}
}
12 changes: 12 additions & 0 deletions java/src/main/java/org/extism/sdk/ExtismFunction.java
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
);
}
7 changes: 7 additions & 0 deletions java/src/main/java/org/extism/sdk/HostUserData.java
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 {

}
93 changes: 93 additions & 0 deletions java/src/main/java/org/extism/sdk/manifest/Manifest.java
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;
}
}
18 changes: 18 additions & 0 deletions java/src/main/java/org/extism/sdk/manifest/MemoryOptions.java
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;
}
}
21 changes: 21 additions & 0 deletions java/src/main/java/org/extism/sdk/support/Hashing.java
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);
}
}
}
56 changes: 56 additions & 0 deletions java/src/main/java/org/extism/sdk/support/JsonSerde.java
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);
}
}
}
}
Loading

0 comments on commit 52e2338

Please sign in to comment.