Skip to content

Commit

Permalink
Work in progress, fixing the tests. Only new REST API implemented yet…
Browse files Browse the repository at this point in the history
…, no method signatures or custom Page paths.
  • Loading branch information
cmorgner committed Feb 28, 2024
1 parent 9d44a5e commit 7859368
Show file tree
Hide file tree
Showing 69 changed files with 1,355 additions and 956 deletions.
7 changes: 0 additions & 7 deletions structr-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@
<artifactId>graphql-java</artifactId>
<version>12.0</version>
</dependency>
<dependency>
<groupId>de.mpg.mpi-inf</groupId>
<artifactId>javatools</artifactId>
<version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.graalvm.sdk/graal-sdk -->
<dependency>
<groupId>org.graalvm.sdk</groupId>
Expand Down Expand Up @@ -219,14 +214,12 @@
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>${jaxb.version}</version>
<optional>true</optional>
</dependency>
<!-- JAXB RI, Jakarta XML Binding -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb.version}</version>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public MethodSignature(final String name) {

public abstract MethodCall createCall(final RESTCall parameters) throws FrameworkException;
public abstract MethodCall createCall(final Map<String, Object> parameters) throws FrameworkException;
public abstract MethodCall createCall(final Object[] arguments) throws FrameworkException;

public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -55,21 +58,21 @@ public boolean isStatic() {

@Override
public MethodCall createCall(final RESTCall parameters) throws FrameworkException {
return new ReflectiveMethodCall(null);

final Map<String, Object> converted = new LinkedHashMap<>();

// convert..

return createCall(converted);
}

@Override
public MethodCall createCall(final Map<String, Object> parameters) throws FrameworkException {

// this method is called from Java code so the parameters do not need to be converted
return new ReflectiveMethodCall(parameters);
}

@Override
public MethodCall createCall(final Object[] arguments) throws FrameworkException {
// this method is called from within the scripting engine so the parameters do not need to be converted
return new ReflectiveMethodCall(null);
}

private class ReflectiveMethodCall implements MethodCall {

private Map<String, Object> propertySet = null;
Expand All @@ -81,21 +84,35 @@ public ReflectiveMethodCall(final Map<String, Object> propertySet) {
@Override
public Object execute(final SecurityContext securityContext, final EvaluationHints hints) {

final Object[] args = { securityContext };
final List<Object> args = new LinkedList<>();

if (method.getParameterCount() > 0) {
args.add(securityContext);
}

if (method.getParameterCount() > 1) {
args.add(propertySet);
}

try {
return method.invoke(entity, args);
return method.invoke(entity, args.toArray());

} catch (IllegalArgumentException ex) {

ex.printStackTrace();

throw new RuntimeException(new FrameworkException(422, "Tried to call method " + method.getName() + " with invalid parameters. SchemaMethods expect their parameters to be passed as an object."));

} catch (IllegalAccessException ex) {

ex.printStackTrace();

logger.error("Unexpected exception while trying to get GraphObject member.", ex);

} catch (InvocationTargetException ex) {

ex.printStackTrace();

if (ex.getTargetException() instanceof FrameworkException) {

throw new RuntimeException(ex.getTargetException());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.structr.core.api;

import java.util.LinkedHashMap;
import java.util.Map;
import org.structr.common.SecurityContext;
import org.structr.common.error.FrameworkException;
Expand Down Expand Up @@ -49,20 +50,21 @@ public boolean isStatic() {

@Override
public MethodCall createCall(final RESTCall parameters) throws FrameworkException {
return new ScriptMethodCall(null);

final Map<String, Object> converted = new LinkedHashMap<>();

// convert..

return createCall(converted);
}

@Override
public MethodCall createCall(final Map<String, Object> parameters) throws FrameworkException {

// this method is called from Java code so the parameters do not need to be converted
return new ScriptMethodCall(parameters);
}

@Override
public MethodCall createCall(final Object[] arguments) throws FrameworkException {
return new ScriptMethodCall(null);
}

private class ScriptMethodCall implements MethodCall {

private Map<String, Object> propertySet = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,20 @@ private String getGenericMethodParameter(final List<Type> types, final Method me

return "<" + StringUtils.join(typeParameterNames, ", ") + "> ";
}

// ----- private static methods -----
public static String getCachedSourceCode(final String uuid) throws FrameworkException {

final SchemaMethod method = StructrApp.getInstance().get(SchemaMethod.class, uuid);
if (method != null) {

final String source = method.getProperty(SchemaMethod.source);
if (source != null) {

return "${" + source.trim() + "}";
}
}

return "${}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.structr.common.error.FrameworkException;

public abstract class PolyglotWrapper {

Expand Down Expand Up @@ -242,6 +243,59 @@ public static Object unwrap(final ActionContext actionContext, final Object obj)
return obj;
}

public static Map<String, Object> unwrapExecutableArguments(final ActionContext actionContext, final String methodName, final Value[] arguments) throws FrameworkException {

/*
int paramCount = method.getParameterCount();
if (paramCount == 0) {
return PolyglotWrapper.wrap(actionContext, method.invoke(null));
} else if (paramCount == 1) {
return PolyglotWrapper.wrap(actionContext, method.invoke(null, actionContext.getSecurityContext()));
} else if (paramCount == 2 && arguments.length == 0) {
return PolyglotWrapper.wrap(actionContext, method.invoke(null, actionContext.getSecurityContext(), new HashMap<String, Object>()));
} else if (arguments.length == 0) {
return PolyglotWrapper.wrap(actionContext, method.invoke(null, actionContext.getSecurityContext()));
} else {
return PolyglotWrapper.wrap(actionContext, method.invoke(null, ArrayUtils.add(Arrays.stream(arguments).map(arg -> PolyglotWrapper.unwrap(actionContext, arg)).toArray(), 0, actionContext.getSecurityContext())));
}
*/


final Map<String, Object> parameters = new LinkedHashMap<>();

if (arguments.length > 0) {

final Object value = PolyglotWrapper.unwrap(actionContext, arguments[0]);

if (arguments.length == 1 && value instanceof Map map) {

parameters.putAll(map);

} else {

throw new RuntimeException(
new FrameworkException(
422,
"Tried to call method \"" + methodName + "\" with invalid parameters. SchemaMethods expect their parameters to be passed as an object."
)
);

}
}

return parameters;
}

protected static List<Object> unwrapIterable(final ActionContext actionContext, final Iterable<Object> iterable) {

final List<Object> unwrappedList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ private static Context getAndUpdateContext(final String language, final ActionCo

} catch (Exception ex) {

ex.printStackTrace();

throw new FrameworkException(500, "Exception while trying to initialize new context for language: " + language + ". Cause: " + ex.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.structr.core.script.polyglot.wrappers;

import java.util.Map;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.graalvm.polyglot.proxy.ProxyObject;
Expand Down Expand Up @@ -110,7 +111,8 @@ public Object getMember(String key) {

try {

final MethodCall call = signature.createCall(arguments);
final Map<String, Object> parameters = PolyglotWrapper.unwrapExecutableArguments(actionContext, key, arguments);
final MethodCall call = signature.createCall(parameters);

return PolyglotWrapper.wrap(actionContext, call.execute(actionContext.getSecurityContext(), new EvaluationHints()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.structr.core.script.polyglot.wrappers;

import java.util.Map;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.graalvm.polyglot.proxy.ProxyObject;
Expand Down Expand Up @@ -54,7 +55,8 @@ public Object getMember(String key) {

try {

final MethodCall call = signature.createCall(arguments);
final Map<String, Object> parameters = PolyglotWrapper.unwrapExecutableArguments(actionContext, key, arguments);
final MethodCall call = signature.createCall(parameters);

return PolyglotWrapper.wrap(actionContext, call.execute(actionContext.getSecurityContext(), new EvaluationHints()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ public String getURL() {
}

public String getResourceSignature() {
// FIXME: fix resource signature
return getURL();

// remove leading slash from resource access grant
if (url.startsWith("/")) {
return url.substring(1);
}

return url;
}

public RestMethodResult doHead() throws FrameworkException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public abstract class RESTEndpoint {
private final Map<String, RESTParameter> parts = new LinkedHashMap<>();
private final String pathSeparator = "/";
private Pattern pattern = null;
private String view = null;

public RESTEndpoint(final RESTParameter... parameters) {
initialize(parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.structr.common.SecurityContext;
import org.structr.common.error.FrameworkException;
import org.structr.core.Services;
import org.structr.rest.resource.CollectionRelationshipsResource;
import org.structr.rest.resource.CypherQueryResource;
import org.structr.rest.resource.EntityResolverResource;
import org.structr.rest.resource.EnvResource;
Expand All @@ -36,7 +37,7 @@
import org.structr.rest.resource.MaintenanceResource;
import org.structr.rest.resource.MeResource;
import org.structr.rest.resource.PropertyResource;
import org.structr.rest.resource.RelationshipsResource;
import org.structr.rest.resource.InstanceRelationshipsResource;
import org.structr.rest.resource.RuntimeEventLogResource;
import org.structr.rest.resource.SchemaResource;
import org.structr.rest.resource.SchemaTypeResource;
Expand All @@ -60,11 +61,13 @@ public class RESTEndpoints {
static {

// initialize static API endpoints
RESTEndpoints.register(new CollectionRelationshipsResource());
RESTEndpoints.register(new CypherQueryResource());
RESTEndpoints.register(new EntityResolverResource());
RESTEndpoints.register(new EnvResource());
RESTEndpoints.register(new GlobalSchemaMethodsResource());
RESTEndpoints.register(new InstanceMethodResource());
RESTEndpoints.register(new InstanceRelationshipsResource());
RESTEndpoints.register(new LogResource());
RESTEndpoints.register(new LoginResource());
RESTEndpoints.register(new LogoutResource());
Expand All @@ -73,7 +76,6 @@ public class RESTEndpoints {
RESTEndpoints.register(new PropertyResource());
RESTEndpoints.register(new RegistrationResource());
RESTEndpoints.register(new ResetPasswordResource());
RESTEndpoints.register(new RelationshipsResource());
RESTEndpoints.register(new RuntimeEventLogResource());
RESTEndpoints.register(new SchemaResource());
RESTEndpoints.register(new SchemaTypeResource());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ public class StaticParameter implements RESTParameter {
private String part = null;

public StaticParameter(final String part) {

this.part = part;
}

@Override
public String key() {
return null;
return part;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
/**
* A resource that matches /{type}/{id}/{name} URLs.
*/
public abstract class AbstractTypeIdNameResource extends RESTEndpoint {
public abstract class AbstractTypeIdLowercaseNameResource extends RESTEndpoint {

private static final RESTParameter typeParameter = RESTParameter.forPattern("type", SchemaNode.schemaNodeNamePattern);
private static final RESTParameter uuidParameter = RESTParameter.forPattern("uuid", Settings.getValidUUIDRegexStringForURLParts());
private static final RESTParameter nameParameter = RESTParameter.forPattern("name", "[a-z_A-Z][a-z_A-Z0-9]*");
private static final RESTParameter nameParameter = RESTParameter.forPattern("name", "[a-z][a-z_A-Z0-9]*");

public AbstractTypeIdNameResource() {
public AbstractTypeIdLowercaseNameResource() {

super(
typeParameter,
Expand Down
Loading

0 comments on commit 7859368

Please sign in to comment.