BIRT Scripted Data Source - Dataset with REST Call Fails using XMLHttpRequest

I'm trying to use a REST call instead of XML files.  I get an error using XMLHttpRequest to retrieve my REST API call to my database server; here is my code which gets data back in responseText as two elements .count and .result[].

If I try this same code in an HTML page it correctly returns my results.  I've adapted the 'fetch()' to BIRT syntax to transfer my data to local Data Set values for display in my BIRT Report.  I am using a Scripted Data Source, with a Data Set.  The only defined Script functions are in the Data Set for open() and fetch() as seen below.  I found another thread that suggested using the importPackage( ) seen in my code.  Same error with or without it.

The error generated when I try to run it is:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

org.eclipse.birt.report.engine.api.EngineException: There are errors evaluating script


"importPackage(javax.xml.parsers);
var xhr = new XMLHttpRequest();
var xhrtoken="concealedtoprotecttheinnocent";
var xhrurl = "http://****.****.****.****:YYYY/irm/REST/v1.14/pathway-types";
var countIterator = 0;

.

.

.

"
Fail to execute script in function __bm_OPEN(). Source:



" + importPackage(javax.xml.parsers);

.

.

.

"

A BIRT exception occurred. See next exception for more information.
ReferenceError: "XMLHttpRequest" is not defined. (/report/data-sets/script-data-set[@id="6"]/method[@name="open"]#3).

   at org.eclipse.birt.report.engine.script.internal.DtEScriptExecutor.handleJS(DtEScriptExecutor.java:99)
   at org.eclipse.birt.report.engine.script.internal.DataSetScriptExecutor.handleJS(DataSetScriptExecutor.java:256)
   at org.eclipse.birt.report.engine.script.internal.ScriptDataSetScriptExecutor.handleOpen(ScriptDataSetScriptExecutor.java:98)
   at org.eclipse.birt.data.engine.impl.ScriptDataSetRuntime.open(ScriptDataSetRuntime.java:80)
   at org.eclipse.birt.data.engine.impl.PreparedScriptDSQuery$ScriptDSQueryExecutor$CustomDataSet.open(PreparedScriptDSQuery.java:247)
   at org.eclipse.birt.data.engine.impl.PreparedScriptDSQuery$ScriptDSQueryExecutor.executeOdiQuery(PreparedScriptDSQuery.java:223)
   at org.eclipse.birt.data.engine.impl.QueryExecutor.execute(QueryExecutor.java:1251)
   at org.eclipse.birt.data.engine.impl.ServiceForQueryResults.executeQuery(ServiceForQueryResults.java:233)
   at org.eclipse.birt.data.engine.impl.QueryResults.getResultIterator(QueryResults.java:178)
   at org.eclipse.birt.report.engine.data.dte.QueryResultSet.(QueryResultSet.java:98)

   at org.eclipse.birt.report.engine.data.dte.DteDataEngine.doExecuteQuery(DteDataEngine.java:168)

   at org.eclipse.birt.report.engine.data.dte.DataGenerationEngine.doExecuteQuery(DataGenerationEngine.java:83)

   at org.eclipse.birt.report.engine.data.dte.AbstractDataEngine.execute(AbstractDataEngine.java:286)

   at org.eclipse.birt.report.engine.executor.ExecutionContext.executeQuery(ExecutionContext.java:1947)

   at org.eclipse.birt.report.engine.executor.QueryItemExecutor.executeQuery(QueryItemExecutor.java:80)

   at org.eclipse.birt.report.engine.executor.ListItemExecutor.execute(ListItemExecutor.java:66)

   at org.eclipse.birt.report.engine.internal.executor.wrap.WrappedReportItemExecutor.execute(WrappedReportItemExecutor.java:46)

   at org.eclipse.birt.report.engine.internal.executor.emitter.ReportItemEmitterExecutor.execute(ReportItemEmitterExecutor.java:46)

   at org.eclipse.birt.report.engine.internal.executor.dup.SuppressDuplicateItemExecutor.execute(SuppressDuplicateItemExecutor.java:43)

   at org.eclipse.birt.report.engine.internal.executor.wrap.WrappedReportItemExecutor.execute(WrappedReportItemExecutor.java:46)

   at org.eclipse.birt.report.engine.internal.executor.l18n.LocalizedReportItemExecutor.execute(LocalizedReportItemExecutor.java:34)

   at org.eclipse.birt.report.engine.layout.html.HTMLBlockStackingLM.layoutNodes(HTMLBlockStackingLM.java:65)

   at org.eclipse.birt.report.engine.layout.html.HTMLPageLM.layout(HTMLPageLM.java:92)

   at org.eclipse.birt.report.engine.layout.html.HTMLReportLayoutEngine.layout(HTMLReportLayoutEngine.java:100)

   at org.eclipse.birt.report.engine.presentation.ReportDocumentBuilder.build(ReportDocumentBuilder.java:249)

   at org.eclipse.birt.report.engine.api.impl.RunTask.doRun(RunTask.java:273)

   at org.eclipse.birt.report.engine.api.impl.RunTask.run(RunTask.java:89)

   at org.eclipse.birt.report.service.ReportEngineService.runReport(ReportEngineService.java:1325)

   at org.eclipse.birt.report.service.BirtViewerReportService.runReport(BirtViewerReportService.java:159)

   at org.eclipse.birt.report.service.actionhandler.BirtRunReportActionHandler.__execute(BirtRunReportActionHandler.java:81)

   at org.eclipse.birt.report.service.actionhandler.BirtGetPageActionHandler.__checkDocumentExists(BirtGetPageActionHandler.java:58)

   at org.eclipse.birt.report.service.actionhandler.AbstractGetPageActionHandler.prepareParameters(AbstractGetPageActionHandler.java:118)

   at org.eclipse.birt.report.service.actionhandler.AbstractGetPageActionHandler.__execute(AbstractGetPageActionHandler.java:103)

   at org.eclipse.birt.report.service.actionhandler.AbstractBaseActionHandler.execute(AbstractBaseActionHandler.java:90)

   at org.eclipse.birt.report.soapengine.processor.AbstractBaseDocumentProcessor.__executeAction(AbstractBaseDocumentProcessor.java:47)

   at org.eclipse.birt.report.soapengine.processor.AbstractBaseComponentProcessor.executeAction(AbstractBaseComponentProcessor.java:143)

   at org.eclipse.birt.report.soapengine.processor.BirtDocumentProcessor.handleGetPage(BirtDocumentProcessor.java:87)

   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

   at java.lang.reflect.Method.invoke(Unknown Source)

   at org.eclipse.birt.report.soapengine.processor.AbstractBaseComponentProcessor.process(AbstractBaseComponentProcessor.java:112)

   at org.eclipse.birt.report.soapengine.endpoint.BirtSoapBindingImpl.getUpdatedObjects(BirtSoapBindingImpl.java:66)

   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

   at java.lang.reflect.Method.invoke(Unknown Source)

   at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397)

   at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186)

   at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)

   at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)

   at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)

   at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)

   at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)

   at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)

   at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)

   at org.eclipse.birt.report.servlet.BirtSoapMessageDispatcherServlet.doPost(BirtSoapMessageDispatcherServlet.java:265)

   at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)

   at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)

   at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)

   at org.eclipse.birt.report.servlet.BirtSoapMessageDispatcherServlet.service(BirtSoapMessageDispatcherServlet.java:122)

   at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:845)

   at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1689)

   at org.eclipse.birt.report.filter.ViewerFilter.doFilter(ViewerFilter.java:68)

   at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1668)

   at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581)

   at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)

   at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)

   at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)

   at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1174)

   at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511)

   at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)

   at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1106)

   at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)

   at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)

   at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:119)

   at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)

   at org.eclipse.jetty.server.Server.handle(Server.java:524)

   at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:319)

   at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:253)

   at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273)

   at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)

   at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)

   at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303)

   at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148)

   at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136)

   at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671)

   at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589)

   at java.lang.Thread.run(Unknown Source)



Here is my code, do you have any suggestions?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Dataset - open()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

importPackage(javax.xml.parsers);

 

var xhr = new XMLHttpRequest();

var xhrtoken="specialstringtoauthenticatemyconnection";


var countIterator = 0;

var countMax = 0;

 

function myFunction(respFromReq) {

respText = JSON.parse(this.responseText); // assign to global the data returned.

countMax = respText.count; // respText has two elements; .count and .result[]

}

 

xhr.onreadystatechange = function() {// function to call when the XHR request completes

   if (this.readyState == 4 && this.status == 200) 

   {

myFunction(xhr); // call MyFunction if we have data.

   }

   else if (this.readyState == 4)

    respText = "FAILED status = " + this.status;

};

 

xhr.open("GET", xhrurl, true);

xhr.setRequestHeader("Irm-AccessKey", xhrtoken);

xhr.setRequestHeader("Accept", "application/json");

xhr.setRequestHeader("Content-Type", "application/json");

xhr.send();

 

Dataset - fetch()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if (countIterator < respText.count)

{

// My understanding is 'fetch()' is called each time a record is requested.

// In 'open()' we defined a variable countIterator which holds the current index into

// the result[] array containing the data.  if the count isn't exhausted

// we transfer the data to local BIRT variables and increment the counter.

row["__type"] = respText.result[countIterator].__type;

row["_12884901926_description"] = respText.result[countIterator]._12884901926_description;

row["_12884901926_endAOpeningSize"] = respText.result[countIterator]._12884901926_endAOpeningSize;

row["_12884901926_endBOpeningSize"] = respText.result[countIterator]._12884901926_endBOpeningSize;

row["_12884901926_endOpeningType"] = respText.result[countIterator]._12884901926_endOpeningType;

row["_12884901926_material"] = respText.result[countIterator]._12884901926_material;

countIterator++;

return true;

}

else return false; 

Comments

Sign In or Register to comment.