REST Service
Below is a beanshell script which provides a REST Service for generating a PNG of a text string - by way of comparison, this script produces exactly the same result
as the previous REST Service trailmap which was implemented in DPML. This beanshell script uses a formal Java style
with strong type declarations, though it is still evaluated at runtime.
This script first obtains the param argument from the request which invoked it. If the argument does not exist an exception will be thrown. The
exception catch block serves an HTML service description.
If the argument does exist it is sourced as an IXAspect (ie an XML DOM). The XAspect provides a ReadOnly XDA object which is used to obtain the text and fontsize
parameters necessary for the active:org.ten60.util.image.text2PNG accessor to generate the PNG image. The parameters are
converted to XML literals which are placed as arguments on a sub-request to the text2PNG accessor. The sub-request is configured to return
a BinaryStreamAspect. The request is issued and the text2PNG accessor generates a PNG bitmap which is returned in a representation. Finally this
representation is set as the response of the script.
You can execute this script here
.
import org.ten60.netkernel.layer1.nkf.*;
import org.ten60.netkernel.layer1.representation.*;
import com.ten60.netkernel.urii.aspect.*;
import org.ten60.netkernel.xml.xda.*;
import org.ten60.netkernel.xml.representation.*;
import com.ten60.netkernel.urii.*;
/**
* Strict Java Implementation
*/
void main()
{
INKFResponse resp=null;
try
{ //Get param argument as IXAspect
IXAspect argsxa=context.sourceAspect("this:param:param", IXAspect.class);
IXDAReadOnly args=argsxa.getXDA();
//Process arguments
String text=args.getText("/nvp/text",true);
fontsize="";
try
{ fontsize=args.getText("/nvp/fontsize",true);
}
catch(Exception e)
{ fontsize="12";
}
//Create XML Literals
IURAspect opd=new StringAspect("<text>"+text+"</text>");
IURAspect opt=new StringAspect("<text2PNG><fontsize>"+fontsize+"</fontsize></text2PNG>");
//Create and issue request to text2PNG Accessor
req=context.createSubRequest();
req.setURI("active:org.ten60.util.image.text2PNG");
req.addArgument("operand",opd);
req.addArgument("operator",opt);
req.setAspectClass(IAspectBinaryStream.class);
IURRepresentation result=context.issueSubRequest(req);
//Build a response from the aspect
resp=context.createResponseFrom(result);
resp.setMimeType("image/png");
}
catch(Exception e)
{ //Exception - so no parameters, serve HTML service description
IURRepresentation result=context.source("ffcpl:/trailmaps/scripting/description.xml");
resp=context.createResponseFrom(result);
resp.setMimeType("text/html");
}
//Issue response
context.setResponse(resp);
}