Skip to content

Commit

Permalink
#26227 adding the ability to print a template or payload return
Browse files Browse the repository at this point in the history
  • Loading branch information
jdotcms committed Sep 25, 2023
1 parent 0daa688 commit ac49e5e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
17 changes: 14 additions & 3 deletions dotCMS/src/main/java/com/dotcms/rendering/js/JsResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ private Response processRequest(final HttpServletRequest request, final HttpServ
.setPageMode(PageMode.get(request))
.build();

// todo: here should be use graalvm

final JavascriptReader javascriptReader = JavascriptReaderFactory.getJavascriptReader(UtilMethods.isSet(folderName));

final Map<String, Object> contextParams = CollectionsUtils.map(
Expand Down Expand Up @@ -579,8 +579,8 @@ private Response evalJavascript(final HttpServletRequest request, final HttpServ
}

return UtilMethods.isSet(contentType)
? Response.ok(result.toString()).type(contentType).build()
: Response.ok(result.toString()).type(MediaType.TEXT_PLAIN_TYPE).build();
? Response.ok(resultToString(result)).type(contentType).build()
: Response.ok(resultToString(result)).type(MediaType.TEXT_PLAIN_TYPE).build();

}
} catch(MethodInvocationException e) {
Expand All @@ -600,6 +600,17 @@ private Response evalJavascript(final HttpServletRequest request, final HttpServ
return Response.ok(dotJSON.getMap()).build();
}

private String resultToString(final Object result) {
if (result instanceof Map) {

final Map map = Map.class.cast(result);
if (map.containsKey("output")) {
return map.get("output").toString();
}
}
return result.toString();
}

private Map<String, Object> getBodyMapFromMultipart(final FormDataMultiPart multipart) throws IOException, JSONException {

return this.multiPartUtils.getBodyMapFromMultipart(multipart);
Expand Down
15 changes: 11 additions & 4 deletions dotCMS/src/main/java/com/dotcms/rendering/js/JsScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.dotcms.util.CollectionsUtils;
import com.dotmarketing.util.Logger;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;

Expand All @@ -19,18 +20,24 @@
* @author jsanca
*/
public class JsScriptEngine implements ScriptEngine {

private static final String ENGINE_JS = "js";
@Override
public Object eval(final HttpServletRequest request,
final HttpServletResponse response,
final Reader scriptReader,
final Map<String, Object> contextParams) {

final DotJSON dotJSON = (DotJSON)contextParams.getOrDefault("dotJSON", new DotJSON());
try (Context context = Context.create()) {
try (Context context = Context.newBuilder(ENGINE_JS)
//.allowHostAccess(HostAccess.ALL) // todo: ask if we want all access to the classpath
//allows access to all Java classes
//.allowHostClassLookup(className -> true)
.build()) {

final Object fileName = contextParams.getOrDefault("dot:jsfilename", "sample.js");
final Source source = Source.newBuilder("js", scriptReader, fileName.toString()).build();
final Value bindings = context.getBindings("js");
final Source source = Source.newBuilder(ENGINE_JS, scriptReader, fileName.toString()).build();
final Value bindings = context.getBindings(ENGINE_JS);
contextParams.entrySet().forEach(entry -> bindings.putMember(entry.getKey(), entry.getValue()));
this.addTools(request, response, bindings, contextParams);

Expand All @@ -39,7 +46,7 @@ public Object eval(final HttpServletRequest request,
bindings.putMember("response", response);
Value eval = context.eval(source);
if (eval.canExecute()) {
eval = eval.execute();
eval = contextParams.containsKey("dot:arguments")?eval.execute((Object[])contextParams.get("dot:arguments")): eval.execute();
}
return CollectionsUtils.map("output", eval.asString(), "dotJSON", dotJSON);
} catch (final IOException e) {
Expand Down

0 comments on commit ac49e5e

Please sign in to comment.