From 09356646cc2e1871e3ec76fbc8f868e675614f02 Mon Sep 17 00:00:00 2001 From: vigorouscoding Date: Thu, 31 Oct 2024 16:43:22 +0100 Subject: [PATCH] Minor: Allow returning raw result for methods (if configured as such) even in simple browser page loads of REST resources --- .../src/main/java/org/structr/core/api/AbstractMethod.java | 1 + .../src/main/java/org/structr/core/api/ReflectiveMethod.java | 5 +++++ .../src/main/java/org/structr/core/api/ScriptMethod.java | 5 +++++ .../java/org/structr/rest/api/RESTMethodCallHandler.java | 5 +++++ .../java/org/structr/rest/servlet/AbstractDataServlet.java | 4 ++-- .../main/java/org/structr/rest/servlet/JsonRestServlet.java | 5 ++++- .../src/main/java/org/structr/flow/servlet/FlowServlet.java | 4 ++-- 7 files changed, 24 insertions(+), 5 deletions(-) diff --git a/structr-base/src/main/java/org/structr/core/api/AbstractMethod.java b/structr-base/src/main/java/org/structr/core/api/AbstractMethod.java index a354ee4dd9..175017199e 100644 --- a/structr-base/src/main/java/org/structr/core/api/AbstractMethod.java +++ b/structr-base/src/main/java/org/structr/core/api/AbstractMethod.java @@ -60,6 +60,7 @@ public AbstractMethod(final String name, final String summary, final String desc public abstract boolean isStatic(); public abstract boolean isPrivate(); + public abstract boolean returnRawResult(); public abstract Snippet getSnippet(); public abstract HttpVerb getHttpVerb(); public abstract Parameters getParameters(); diff --git a/structr-base/src/main/java/org/structr/core/api/ReflectiveMethod.java b/structr-base/src/main/java/org/structr/core/api/ReflectiveMethod.java index 9d0ef0c375..44e5ae0867 100644 --- a/structr-base/src/main/java/org/structr/core/api/ReflectiveMethod.java +++ b/structr-base/src/main/java/org/structr/core/api/ReflectiveMethod.java @@ -58,6 +58,11 @@ public boolean isPrivate() { return Modifier.isPrivate(method.getModifiers()); } + @Override + public boolean returnRawResult() { + return false; + } + @Override public boolean isStatic() { return Modifier.isStatic(method.getModifiers()); diff --git a/structr-base/src/main/java/org/structr/core/api/ScriptMethod.java b/structr-base/src/main/java/org/structr/core/api/ScriptMethod.java index 14138e42ae..d879b25f29 100644 --- a/structr-base/src/main/java/org/structr/core/api/ScriptMethod.java +++ b/structr-base/src/main/java/org/structr/core/api/ScriptMethod.java @@ -78,6 +78,11 @@ public boolean isPrivate() { return isPrivateMethod; } + @Override + public boolean returnRawResult() { + return returnRawResult; + } + @Override public boolean isStatic() { return isStaticMethod; diff --git a/structr-base/src/main/java/org/structr/rest/api/RESTMethodCallHandler.java b/structr-base/src/main/java/org/structr/rest/api/RESTMethodCallHandler.java index 788887f4c4..ca3541f0e8 100644 --- a/structr-base/src/main/java/org/structr/rest/api/RESTMethodCallHandler.java +++ b/structr-base/src/main/java/org/structr/rest/api/RESTMethodCallHandler.java @@ -60,4 +60,9 @@ protected RestMethodResult executeMethod(final SecurityContext securityContext, return new RestMethodResult(500, "Call to unlicensed function, see server log file for more details."); } } + + public boolean shouldReturnRawResult() { + + return this.method.returnRawResult(); + } } diff --git a/structr-base/src/main/java/org/structr/rest/servlet/AbstractDataServlet.java b/structr-base/src/main/java/org/structr/rest/servlet/AbstractDataServlet.java index bdce7b352b..80e1a80841 100644 --- a/structr-base/src/main/java/org/structr/rest/servlet/AbstractDataServlet.java +++ b/structr-base/src/main/java/org/structr/rest/servlet/AbstractDataServlet.java @@ -144,7 +144,7 @@ protected void commitResponse(final SecurityContext securityContext, final HttpS } } - protected void processResult(final SecurityContext securityContext, final HttpServletRequest request, final HttpServletResponse response, final ResultStream result, final String view, final int outputDepth, final boolean wrapSingleResultInArray) throws ServletException, IOException { + protected void processResult(final SecurityContext securityContext, final HttpServletRequest request, final HttpServletResponse response, final ResultStream result, final String view, final int outputDepth, final boolean wrapSingleResultInArray, final boolean returnRawResult) throws ServletException, IOException { final String serializeNullsSrc = request.getParameter(RequestKeywords.SerializeNulls.keyword()); final boolean serializeNulls = Services.parseBoolean(serializeNullsSrc, true); @@ -154,7 +154,7 @@ protected void processResult(final SecurityContext securityContext, final HttpSe final String accept = request.getHeader("Accept"); - if (accept != null && accept.contains("text/html")) { + if (returnRawResult == false && accept != null && accept.contains("text/html")) { writeHtml(securityContext, response, result, baseUrl, view, outputDepth, wrapSingleResultInArray, serializeNulls); diff --git a/structr-base/src/main/java/org/structr/rest/servlet/JsonRestServlet.java b/structr-base/src/main/java/org/structr/rest/servlet/JsonRestServlet.java index dd24fff925..838f029d54 100644 --- a/structr-base/src/main/java/org/structr/rest/servlet/JsonRestServlet.java +++ b/structr-base/src/main/java/org/structr/rest/servlet/JsonRestServlet.java @@ -57,6 +57,7 @@ import org.structr.core.graph.search.DefaultSortOrder; import org.structr.rest.api.RESTCallHandler; import org.structr.rest.api.RESTEndpoints; +import org.structr.rest.api.RESTMethodCallHandler; import org.structr.web.entity.User; /** @@ -813,7 +814,9 @@ protected void doGetOrHead(final HttpServletRequest request, final HttpServletRe final DecimalFormat decimalFormat = new DecimalFormat("0.000000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH)); result.setQueryTime(decimalFormat.format((queryTimeEnd - queryTimeStart) / 1000000000.0)); - processResult(securityContext, request, response, result, handler.getRequestedView(), depth, handler.isCollection()); + boolean returnRawResult = handler instanceof RESTMethodCallHandler && ((RESTMethodCallHandler) handler).shouldReturnRawResult(); + + processResult(securityContext, request, response, result, handler.getRequestedView(), depth, handler.isCollection(), returnRawResult); } } diff --git a/structr-modules/structr-flow-module/src/main/java/org/structr/flow/servlet/FlowServlet.java b/structr-modules/structr-flow-module/src/main/java/org/structr/flow/servlet/FlowServlet.java index 998d166da2..900997ecfe 100644 --- a/structr-modules/structr-flow-module/src/main/java/org/structr/flow/servlet/FlowServlet.java +++ b/structr-modules/structr-flow-module/src/main/java/org/structr/flow/servlet/FlowServlet.java @@ -105,7 +105,7 @@ protected void doGetOrHead(final HttpServletRequest request, final HttpServletRe DecimalFormat decimalFormat = new DecimalFormat("0.000000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH)); result.setQueryTime(decimalFormat.format((queryTimeEnd - queryTimeStart) / 1000000000.0)); - processResult(securityContext, request, response, result, PropertyView.Public, depth, false); + processResult(securityContext, request, response, result, PropertyView.Public, depth, false, false); } response.setStatus(HttpServletResponse.SC_OK); @@ -230,7 +230,7 @@ protected void doPost(final HttpServletRequest request, HttpServletResponse resp DecimalFormat decimalFormat = new DecimalFormat("0.000000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH)); result.setQueryTime(decimalFormat.format((queryTimeEnd - queryTimeStart) / 1000000000.0)); - processResult(securityContext, request, response, result, PropertyView.Public, depth, false); + processResult(securityContext, request, response, result, PropertyView.Public, depth, false, false); response.setStatus(HttpServletResponse.SC_OK);