Skip to content

Commit

Permalink
♻️ Refactor few polyglot classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Sep 27, 2024
1 parent a737d7e commit 8b32a2e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,36 @@
*/
package org.restheart.polyglot;

import static org.restheart.polyglot.AbstractJSPlugin.addBindings;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import com.google.common.collect.Maps;
import com.mongodb.client.MongoClient;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.restheart.configuration.Configuration;
import org.restheart.exchange.Request;
import org.restheart.exchange.Response;
import org.restheart.plugins.InterceptPoint;
import org.restheart.plugins.Interceptor;
import org.restheart.plugins.PluginRecord;
import static org.restheart.polyglot.AbstractJSPlugin.addBindings;
import org.restheart.polyglot.interceptors.AbstractJSInterceptor;
import org.restheart.polyglot.interceptors.ByteArrayJSInterceptor;
import org.restheart.polyglot.interceptors.ByteArrayProxyJSInterceptor;
import org.restheart.polyglot.interceptors.CsvJSInterceptor;
import org.restheart.polyglot.interceptors.JsonJSInterceptor;
import org.restheart.polyglot.interceptors.MongoJSInterceptor;
import org.restheart.polyglot.interceptors.StringJSInterceptor;
import org.restheart.configuration.Configuration;
import org.restheart.exchange.Request;
import org.restheart.exchange.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.mongodb.client.MongoClient;

/**
*
* @author Andrea Di Cesare {@literal <[email protected]>}
Expand Down Expand Up @@ -118,26 +118,26 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
}

if (options.getMemberKeys().isEmpty()) {
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", " + packageHint);
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", " + PACKAGE_HINT);
}

if (!options.getMemberKeys().contains("name")) {
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", missing member 'options.name', " + packageHint);
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", missing member 'options.name', " + PACKAGE_HINT);
}

if (!options.getMember("name").isString()) {
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", wrong member 'options.name', " + packageHint);
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", wrong member 'options.name', " + PACKAGE_HINT);
}

var name = options.getMember("name").asString();

if (!options.getMemberKeys().contains("description")) {
throw new IllegalArgumentException(
"wrong js interceptor " + pluginPath.toAbsolutePath() + ", missing member 'options.description', " + packageHint);
"wrong js interceptor " + pluginPath.toAbsolutePath() + ", missing member 'options.description', " + PACKAGE_HINT);
}

if (!options.getMember("description").isString()) {
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", wrong member 'options.description', " + packageHint);
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", wrong member 'options.description', " + PACKAGE_HINT);
}

var description = options.getMember("description").asString();
Expand All @@ -164,14 +164,14 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
} else {
if (!options.getMember("interceptPoint").isString()) {
throw new IllegalArgumentException(
"wrong js interceptor " + pluginPath.toAbsolutePath() + ", wrong member 'options.interceptPoint', " + handleResolveHint);
"wrong js interceptor " + pluginPath.toAbsolutePath() + ", wrong member 'options.interceptPoint', " + HANDLE_RESOLVE_HINT);
} else {
var _interceptPoint = options.getMember("interceptPoint").asString();
try {
interceptPoint = InterceptPoint.valueOf(_interceptPoint);
} catch (Throwable t) {
throw new IllegalArgumentException(
"wrong js interceptor " + pluginPath.toAbsolutePath() + ", wrong member 'options.interceptPoint', " + handleResolveHint);
"wrong js interceptor " + pluginPath.toAbsolutePath() + ", wrong member 'options.interceptPoint', " + HANDLE_RESOLVE_HINT);
}
}
}
Expand All @@ -182,7 +182,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
pluginClass = "StringInterceptor";
} else if (!options.getMember("pluginClass").isString()) {
throw new IllegalArgumentException(
"wrong js interceptor " + pluginPath.toAbsolutePath() + ", wrong member 'options.pluginClass', " + handleResolveHint);
"wrong js interceptor " + pluginPath.toAbsolutePath() + ", wrong member 'options.pluginClass', " + HANDLE_RESOLVE_HINT);
} else {
pluginClass = options.getMember("pluginClass").asString();
}
Expand All @@ -201,7 +201,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
}

if (!handle.canExecute()) {
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", " + handleResolveHint);
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", " + HANDLE_RESOLVE_HINT);
}

// ******** evaluate and check resolve
Expand All @@ -218,7 +218,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
}

if (!resolve.canExecute()) {
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", " + handleResolveHint);
throw new IllegalArgumentException("wrong js interceptor " + pluginPath.toAbsolutePath() + ", " + HANDLE_RESOLVE_HINT);
}

AbstractJSInterceptor<? extends Request<?>, ? extends Response<?>> interceptor;
Expand All @@ -236,9 +236,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
}

switch (pluginClass) {
case "StringInterceptor":
case "org.restheart.plugins.StringInterceptor":
interceptor = new StringJSInterceptor(name,
case "StringInterceptor", "org.restheart.plugins.StringInterceptor" -> interceptor = new StringJSInterceptor(name,
pluginClass,
description,
interceptPoint,
Expand All @@ -247,10 +245,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
mclient,
config,
opts);
break;
case "BsonInterceptor":
case "org.restheart.plugins.BsonInterceptor":
interceptor = new StringJSInterceptor(name,
case "BsonInterceptor", "org.restheart.plugins.BsonInterceptor" -> interceptor = new StringJSInterceptor(name,
pluginClass,
description,
interceptPoint,
Expand All @@ -259,10 +254,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
mclient,
config,
opts);
break;
case "ByteArrayInterceptor":
case "org.restheart.plugins.ByteArrayInterceptor":
interceptor = new ByteArrayJSInterceptor(name,
case "ByteArrayInterceptor", "org.restheart.plugins.ByteArrayInterceptor" -> interceptor = new ByteArrayJSInterceptor(name,
pluginClass,
description,
interceptPoint,
Expand All @@ -271,10 +263,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
mclient,
config,
opts);
break;
case "ByteArrayProxyInterceptor":
case "org.restheart.plugins.ByteArrayProxyInterceptor":
interceptor = new ByteArrayProxyJSInterceptor(name,
case "ByteArrayProxyInterceptor", "org.restheart.plugins.ByteArrayProxyInterceptor" -> interceptor = new ByteArrayProxyJSInterceptor(name,
pluginClass,
description,
interceptPoint,
Expand All @@ -283,10 +272,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
mclient,
config,
opts);
break;
case "CsvInterceptor":
case "org.restheart.plugins.CsvInterceptor":
interceptor = new CsvJSInterceptor(name,
case "CsvInterceptor", "org.restheart.plugins.CsvInterceptor" -> interceptor = new CsvJSInterceptor(name,
pluginClass,
description,
interceptPoint,
Expand All @@ -295,10 +281,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
mclient,
config,
opts);
break;
case "JsonInterceptor":
case "org.restheart.plugins.JsonInterceptor":
interceptor = new JsonJSInterceptor(name,
case "JsonInterceptor", "org.restheart.plugins.JsonInterceptor" -> interceptor = new JsonJSInterceptor(name,
pluginClass,
description,
interceptPoint,
Expand All @@ -307,10 +290,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
mclient,
config,
opts);
break;
case "MongoInterceptor":
case "org.restheart.plugins.MongoInterceptor":
interceptor = new MongoJSInterceptor(name,
case "MongoInterceptor", "org.restheart.plugins.MongoInterceptor" -> interceptor = new MongoJSInterceptor(name,
pluginClass,
description,
interceptPoint,
Expand All @@ -319,12 +299,10 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
mclient,
config,
opts);
break;
default:
throw new IllegalArgumentException("wrong js interceptor, wrong member 'options.pluginClass', " + packageHint);
default -> throw new IllegalArgumentException("wrong js interceptor, wrong member 'options.pluginClass', " + PACKAGE_HINT);
}

return new PluginRecord<Interceptor<? extends Request<?>, ? extends Response<?>>>(interceptor.getName(),
return new PluginRecord<>(interceptor.getName(),
interceptor.getDescription(),
false,
true,
Expand All @@ -334,7 +312,7 @@ public JSInterceptorFactory(Optional<MongoClient> mclient, Configuration config)
}
}

private static final String handleResolveHint = """
private static final String HANDLE_RESOLVE_HINT = """
the interceptor js module must export the functions 'handle' and 'resolve', example:
export function handle(request, response) {
const BsonUtils = Java.type("org.restheart.utils.BsonUtils");
Expand All @@ -348,7 +326,7 @@ export function resolve(request) {
}
""";

private static final String packageHint = """
private static final String PACKAGE_HINT = """
the plugin module must export the object 'options', example:
export const options = {
name: "mongoCollInterceptor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
public class JavaScriptService extends AbstractJSPlugin implements StringService {
private static final Logger LOGGER = LoggerFactory.getLogger(JavaScriptService.class);

private static final String handleHint = """
private static final String HANDLE_HINT = """
the plugin module must export the function 'handle', example:
export function handle(request, response) {
LOGGER.debug('request {}', request.getContent());
Expand All @@ -60,7 +60,7 @@ export function handle(request, response) {
}
""";

private static final String packageHint = """
private static final String PACKAGE_HINT = """
the plugin module must export the object 'options', example:
export const options = {
name: "hello"
Expand Down Expand Up @@ -122,7 +122,7 @@ export function handle(request, response) {
} else if (t.getMessage() != null && t.getMessage().contains("Access to host class")) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ": " + t.getMessage());
} else {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ": " + t.getMessage() + ", " + packageHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ": " + t.getMessage() + ", " + PACKAGE_HINT);
}
}

Expand Down Expand Up @@ -210,64 +210,64 @@ static void checkOptions(Value options, Path pluginPath) {
// ******** evaluate and check options

if (options.getMemberKeys().isEmpty()) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + " , " + packageHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + " , " + PACKAGE_HINT);
}

if (!options.getMemberKeys().contains("name")) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", missing member 'options.name', " + packageHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", missing member 'options.name', " + PACKAGE_HINT);
}

if (!options.getMember("name").isString()) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.name', " + packageHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.name', " + PACKAGE_HINT);
}

if (!options.getMemberKeys().contains("description")) {
throw new IllegalArgumentException(
"wrong js service " + pluginPath.toAbsolutePath() + ", missing member 'options.description', " + packageHint);
"wrong js service " + pluginPath.toAbsolutePath() + ", missing member 'options.description', " + PACKAGE_HINT);
}

if (!options.getMember("description").isString()) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.description', " + packageHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.description', " + PACKAGE_HINT);
}

if (!options.getMemberKeys().contains("uri")) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", missing member 'options.uri', " + packageHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", missing member 'options.uri', " + PACKAGE_HINT);
}

if (!options.getMember("uri").isString()) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.uri', " + packageHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.uri', " + PACKAGE_HINT);
}

if (!options.getMember("uri").asString().startsWith("/")) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.uri', " + packageHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.uri', " + PACKAGE_HINT);
}

if (options.getMemberKeys().contains("secured") && !options.getMember("secured").isBoolean()) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.secured', " + packageHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.secured', " + PACKAGE_HINT);
}

if (options.getMemberKeys().contains("matchPolicy")) {
if (!options.getMember("matchPolicy").isString()) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.matchPolicy', " + packageHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.matchPolicy', " + PACKAGE_HINT);
} else {
var _matchPolicy = options.getMember("matchPolicy").asString();
try {
MATCH_POLICY.valueOf(_matchPolicy);
} catch (Throwable t) {
throw new IllegalArgumentException(
"wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.matchPolicy', " + packageHint);
"wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.matchPolicy', " + PACKAGE_HINT);
}
}
}

if (options.getMemberKeys().contains("modulesReplacements") && !options.getMember("modulesReplacements").isString()) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.modulesReplacements', " + packageHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", wrong member 'options.modulesReplacements', " + PACKAGE_HINT);
}
}

static void checkHandle(Value handle, Path pluginPath) {
if (!handle.canExecute()) {
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", " + handleHint);
throw new IllegalArgumentException("wrong js service " + pluginPath.toAbsolutePath() + ", " + HANDLE_HINT);
}
}
}
Loading

0 comments on commit 8b32a2e

Please sign in to comment.