Skip to content

Commit

Permalink
Modifies doOptions in JsonRestServlet to return more accruate Allow h…
Browse files Browse the repository at this point in the history
…eader values.
  • Loading branch information
cmorgner committed Feb 28, 2024
1 parent 2d52f00 commit 71b0574
Show file tree
Hide file tree
Showing 75 changed files with 1,277 additions and 950 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
package org.structr.common.error;

import com.google.gson.*;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* Exception to be thrown when an unlicensed scripting function is encountered.
*/
public class AssertException extends RuntimeException implements JsonException {

private int statusCode = 422;
private final Map<String, String> headers = new LinkedHashMap<>();
private int statusCode = 422;

public AssertException(final String message, final int statusCode) {

Expand All @@ -39,6 +42,11 @@ public int getStatus() {
return statusCode;
}

@Override
public Map<String, String> headers() {
return headers;
}

@Override
public JsonElement toJSON() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.servlet.http.HttpServletResponse;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

Expand All @@ -35,10 +36,11 @@
*/
public class FrameworkException extends Exception implements JsonException {

private ErrorBuffer errorBuffer = new ErrorBuffer();
private Map<String, String> data = null;
private String message = null;
private int status = HttpServletResponse.SC_OK;
private final Map<String, String> headers = new LinkedHashMap<>();
private ErrorBuffer errorBuffer = new ErrorBuffer();
private Map<String, String> data = null;
private String message = null;
private int status = HttpServletResponse.SC_OK;

public FrameworkException(final int status, final String message) {
this(status, message, (ErrorToken)null);
Expand Down Expand Up @@ -101,6 +103,11 @@ public String toString() {

}

@Override
public Map<String, String> headers() {
return headers;
}

@Override
public JsonElement toJSON() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
package org.structr.common.error;

import com.google.gson.JsonElement;
import java.util.Map;

/**
* Common base class for FrameworkException and AssertException to be able
* to handle them with the same code.
*/
public interface JsonException {

Map<String, String> headers();
JsonElement toJSON();
int getStatus();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.structr.common.error.SemanticErrorToken;
import org.structr.core.GraphObject;
import org.structr.core.api.Arguments.Argument;
import org.structr.core.entity.SchemaMethod.HttpVerb;
import org.structr.schema.action.EvaluationHints;
import org.structr.schema.parser.DatePropertyParser;

Expand All @@ -48,8 +49,7 @@ public AbstractMethod(final String name, final String summary, final String desc

public abstract boolean isStatic();
public abstract boolean isPrivate();
public abstract boolean useGET();
public abstract boolean usePOST();
public abstract HttpVerb getHttpVerb();
public abstract Parameters getParameters();
public abstract String getFullMethodName();
public abstract Object execute(final SecurityContext securityContext, final GraphObject entity, final Arguments arguments, final EvaluationHints hints) throws FrameworkException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.structr.common.error.AssertException;
import org.structr.common.error.FrameworkException;
import org.structr.core.GraphObject;
import org.structr.core.entity.SchemaMethod.HttpVerb;
import org.structr.schema.action.EvaluationHints;

/**
Expand Down Expand Up @@ -62,13 +63,8 @@ public boolean isStatic() {
}

@Override
public boolean useGET() {
return false;
}

@Override
public boolean usePOST() {
return true;
public HttpVerb getHttpVerb() {
return HttpVerb.POST;
}

@Override
Expand All @@ -78,7 +74,7 @@ public Parameters getParameters() {

@Override
public String getFullMethodName() {
return "method " + method.getDeclaringClass().getSimpleName() + "." + method.getName();
return "method " + method.getDeclaringClass().getSimpleName() + "." + method.getName() + "‛";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.structr.core.GraphObject;
import org.structr.core.entity.AbstractSchemaNode;
import org.structr.core.entity.SchemaMethod;
import org.structr.core.entity.SchemaMethod.HttpVerb;
import org.structr.schema.action.Actions;
import org.structr.schema.action.EvaluationHints;

Expand Down Expand Up @@ -60,13 +61,8 @@ public boolean isStatic() {
}

@Override
public boolean useGET() {
return method.usesGet();
}

@Override
public boolean usePOST() {
return !useGET();
public HttpVerb getHttpVerb() {
return method.getHttpVerb();
}

@Override
Expand All @@ -80,11 +76,11 @@ public String getFullMethodName() {
final AbstractSchemaNode declaringClass = method.getProperty(SchemaMethod.schemaNode);
if (declaringClass == null) {

return "user-defined function " + method.getName();
return "user-defined function " + method.getName() + "‛";

} else {

return "method " + declaringClass.getName() + "." + method.getName();
return "method " + declaringClass.getName() + "." + method.getName() + "‛";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public <T extends GraphObject> T get(final Class<T> type, final String uuid) thr

} else {

throw new IllegalStateException("Invalid type " + type + ", cannot be used in query.");
throw new IllegalStateException("Invalid type " + type + ", cannot be used in query");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public final void removeProperty(final PropertyKey key) throws FrameworkExceptio
user = currentUser.getProperty(AbstractNode.id);
}

throw new FrameworkException(403, "Modification of node " + this.getProperty(AbstractNode.id) + " with type " + this.getProperty(AbstractNode.type) + " by user " + user + " not permitted.");
throw new FrameworkException(403, "Modification of node " + this.getProperty(AbstractNode.id) + " with type " + this.getProperty(AbstractNode.type) + " by user " + user + " not permitted");
}

if (this.dbNode != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public class SchemaMethod extends SchemaReloadingNode implements Favoritable {

public static final String schemaMethodNamePattern = "[a-zA-Z_][a-zA-Z0-9_]*";

public enum HttpVerb {
GET, PUT, POST, PATCH, DELETE
}

public static final Property<Iterable<SchemaMethodParameter>> parameters = new EndNodes<>("parameters", SchemaMethodParameters.class);
public static final Property<AbstractSchemaNode> schemaNode = new StartNode<>("schemaNode", SchemaNodeMethod.class, new PropertySetNotion(AbstractNode.id, AbstractNode.name, SchemaNode.isBuiltinType));
public static final Property<String> signature = new StringProperty("signature").indexed();
Expand All @@ -68,7 +72,7 @@ public class SchemaMethod extends SchemaReloadingNode implements Favoritable {
public static final Property<String> description = new StringProperty("description");
public static final Property<Boolean> isStatic = new BooleanProperty("isStatic");
public static final Property<Boolean> isPrivate = new BooleanProperty("isPrivate");
public static final Property<Boolean> usesGet = new BooleanProperty("usesGet");
public static final Property<HttpVerb> httpVerb = new EnumProperty<>("httpVerb", HttpVerb.class).defaultValue(HttpVerb.POST);
// Note: if you add properties here, make sure to add the in Deployment3Test.java#test33SchemaMethods as well!

// property which is only used to mark a schema method as "will be deleted"
Expand All @@ -79,19 +83,19 @@ public class SchemaMethod extends SchemaReloadingNode implements Favoritable {
));

public static final View defaultView = new View(SchemaMethod.class, PropertyView.Public,
name, schemaNode, source, returnType, exceptions, callSuper, overridesExisting, doExport, codeType, isPartOfBuiltInSchema, tags, summary, description, isStatic, isPrivate, usesGet
name, schemaNode, source, returnType, exceptions, callSuper, overridesExisting, doExport, codeType, isPartOfBuiltInSchema, tags, summary, description, isStatic, isPrivate, httpVerb
);

public static final View uiView = new View(SchemaMethod.class, PropertyView.Ui,
name, schemaNode, source, returnType, exceptions, callSuper, overridesExisting, doExport, codeType, isPartOfBuiltInSchema, tags, summary, description, isStatic, includeInOpenAPI, openAPIReturnType, isPrivate, usesGet
name, schemaNode, source, returnType, exceptions, callSuper, overridesExisting, doExport, codeType, isPartOfBuiltInSchema, tags, summary, description, isStatic, includeInOpenAPI, openAPIReturnType, isPrivate, httpVerb
);

public static final View schemaView = new View(SchemaNode.class, "schema",
id, type, schemaNode, name, source, returnType, exceptions, callSuper, overridesExisting, doExport, codeType, isPartOfBuiltInSchema, tags, summary, description, isStatic, includeInOpenAPI, openAPIReturnType, isPrivate, usesGet
id, type, schemaNode, name, source, returnType, exceptions, callSuper, overridesExisting, doExport, codeType, isPartOfBuiltInSchema, tags, summary, description, isStatic, includeInOpenAPI, openAPIReturnType, isPrivate, httpVerb
);

public static final View exportView = new View(SchemaMethod.class, "export",
id, type, schemaNode, name, source, returnType, exceptions, callSuper, overridesExisting, doExport, codeType, isPartOfBuiltInSchema, tags, summary, description, isStatic, includeInOpenAPI, openAPIReturnType, isPrivate, usesGet
id, type, schemaNode, name, source, returnType, exceptions, callSuper, overridesExisting, doExport, codeType, isPartOfBuiltInSchema, tags, summary, description, isStatic, includeInOpenAPI, openAPIReturnType, isPrivate, httpVerb
);

public Iterable<SchemaMethodParameter> getParameters() {
Expand Down Expand Up @@ -152,8 +156,8 @@ public boolean isPrivateMethod() {
return getProperty(isPrivate);
}

public boolean usesGet() {
return getProperty(usesGet);
public HttpVerb getHttpVerb() {
return getProperty(httpVerb);
}

public boolean isJava() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Boolean getProperty(final SecurityContext securityContext, final GraphObj

@Override
public Object setProperty(final SecurityContext securityContext, final GraphObject obj, final Boolean value) throws FrameworkException {
throw new FrameworkException(422, "Unable to change value of a constant property", new ReadOnlyPropertyToken(obj.getType(), jsonName()));
throw new FrameworkException(422, "Unable to change value of constant property ‛" + jsonName() + "‛", new ReadOnlyPropertyToken(obj.getType(), jsonName()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@
public class RESTCall extends LinkedHashMap<String, String> {

private final List<String> pathParameters = new LinkedList<>();
private Class userType = null;
private String viewName = null;
private String url = null;
private boolean isDefaultView = false;

public RESTCall(final String url, final String viewName, final boolean isDefaultView) {
public RESTCall(final String url, final String viewName, final boolean isDefaultView, final Class userType) {

this.isDefaultView = isDefaultView;
this.viewName = viewName;
this.userType = userType;
this.url = url;
}

Expand All @@ -60,4 +62,8 @@ public void addPathParameters(final String[] parts) {
public List<String> getPathParameters() {
return pathParameters;
}

public Class getUserType() {
return userType;
}
}
Loading

0 comments on commit 71b0574

Please sign in to comment.