13
$\begingroup$

Does anyone knows how can I implement a REST Web Service with webMathematica?

The SOAP service is well documented here and in the webMathematica User Guide.

I saw that is possible to create a REST service in the User Guide (page 10) and in the webMathematica page: REST in webMathematica Marketing Page

But I didn't find any documentation about it.

Some idea on how to do that, or reference to documentation about it?

See this question in Wolfram Community site

$\endgroup$
4
  • 1
    $\begingroup$ There is webMathematica/WEB-INF/Applications/WebServicesServer/Rest.m, but I have no idea how it works. There seems to be a "REST-like" interface over at WA, and WA is somehow based on webMathematica, so it seems possible. Maybe WA people are reading this and have pity with good old webMathematica users? $\endgroup$ Dec 14, 2014 at 20:41
  • $\begingroup$ @RolfMertig interesting. In the same directory there is a Documentation folder with a nb about Web Service, but no REST implementation either. $\endgroup$
    – Murta
    Dec 15, 2014 at 19:24
  • 1
    $\begingroup$ @Murta If you mean to provide a REST-like server service that will be accessed by client, then that example is in the documentation (the workbench one). The AJAX example is a REST-like example. The page directive to set media type, they give an example of a HTTP GET with variables in the URL. $\endgroup$
    – Hans
    Dec 16, 2014 at 0:25
  • $\begingroup$ Tks @Hans. I'm new into this area, and your comment was the kick that I needed to start to understand It. $\endgroup$
    – Murta
    Dec 27, 2014 at 15:41

2 Answers 2

7
$\begingroup$

After @Hans comment, get much more clear how to do it in webMathematica. It could't be simpler.

IMO, the best way to see how it works, after install webMathematica, is going into: tomcatDirectory\webapps\webMathematica\Examples\AJAX. Let's see for example the ReturnDate.jsp file:

<msp:evaluate>
If[ $$fullDate === "true",
    DateString[],
    DateString@{"Hour", ":","Minute",":","Second"}
]
</msp:evaluate>

Now if you call the address:

http:\\localhost:8080\webMathematica\Examples\AJAX\ReturnDate.jsp?fullDate=true

we get:

enter image description here

So, our service is created.

See how simple is to set the variables using $$ syntax, as we see in $$fullDate

$\endgroup$
4
$\begingroup$

If you have a look at the "webMathematica/Examples/PlotScript/PlotScript.jsp" example :

    <%@ taglib uri="http://www.wolfram.com/msp" prefix="msp" %>
<html>
<head>
<script>
function plot(f) {
    win = window.open( "PlotScript1.jsp?fun=" + URLescape(f.fun.value) + "&x1=" +
            URLescape(f.x1.value), "plot", "toolbar=none,resizeable=yes,width=450,height=350");
}
</script>
</head>

<body>
<form action="Plot" method="post">
<input type="text" name="fun"   size="24" value="<msp:evaluate>MSPValue[ $$fun,  "Sin[x]^2"]</msp:evaluate>"/> <br/> 
<input type="text" name="x1"   size="24" value="<msp:evaluate>MSPValue[ $$x1,  "10"]</msp:evaluate>"/>
<button type="submit" onClick="plot(this.form)"/>
</form>

</body>
</html>

The link generated is http://localhost:8080/webMathematica/Examples/PlotScript/PlotScript.jsp?fun=Sin[x]^2&x1=10

This is something similar to REST API. The link passes the values of "fun" and "x1" to PlotScript1.jsp. If you look at the code of PlotScript1.jsp then its nothing but plotting based on the POSTed values.

    <%@ taglib uri="http://www.wolfram.com/msp" prefix="msp" %>

<html>

<head>
<title>Plotting with JavaScript</title>
</head>

<body>

<msp:evaluate> 
    MSPBlock[ {$$fun, $$x1},
        MSPShow[ Plot[$$fun, {x,0,$$x1},ImageSize->400]]] 
</msp:evaluate> 

<form action="Plot" method="post">
<input type="button" value="Close" onClick="window.close()"/>
</form>

</body>
</html>

Offcourse now you could use the same pattern and create a more complicated code in PlotScript1.jsp.

I use nodejs extensively to write REST API's and I can do the same within WebMathematica JSP page. Someone explained using mathematica function to connect and query MongoDB (Check here). I could just use the variables defined within my JSP to query the database, pass the obtained relevant JSON objects to a mathematica code (maybe another JSP) that generates some result (Like plot etc) and lastly use MSPShow[] to render the result.

The tricker part is the web based authentication just by using mathematica (Not the HTTP Basic or digest auth methods but form based). Would appreciate any good design patterns.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.