This topic leads you through the creation of a simple application that sends a SOAP message to the Query API Service and processes the response.
The application you create takes an XML file containing a SOAP message as input, sends the message to the Query API service, and outputs the results to standard output (the console). This application, or an application like it, is necessary to process the SOAP messages we write in the remainder of this tutorial.
The code in this topic requires Java SE 5 or greater and JAX-WS 2.0. That stated, the concepts should be familiar to any developer proficient in working with SOAP. You should have no problem implementing this application in the language of your choice.
The Query API is located on your Analytics server at http://analytics_server:port_number/analytics/QueryService. The default port_number is 11944.
Element | Detail |
---|---|
WSDL Location | http://analytics_server:port_number/analytics/QueryService?WSDL |
Namespace | http://www.bea.com/analytics/AnalyticsQueryService |
Service Name | AnalyticsQueryService |
Port Name | AnalyticsQueryServicePort |
This Java code establishes objects necessary for a SOAP connection to the Query API service on the Analytics Server named analytics:
import java.net.URL; import javax.xml.namespace.QName; import javax.xml.soap.SOAPMessage; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; class QueryAPIExample { public static void main(String[] args) { try { QName serviceName = new QName( "http://www.bea.com/analytics/AnalyticsQueryService", "AnalyticsQueryService"); URL serviceURL = new URL( "http://analytics:11944/analytics/QueryService?wsdl"); Service service = Service.create(serviceURL, serviceName); QName portName = new QName( "http://www.bea.com/analytics/AnalyticsQueryService", "AnalyticsQueryServicePort"); Dispatch<SOAPMessage> dispatch = service.createDispatch( portName, SOAPMessage.class, Service.Mode.MESSAGE); } catch (Exception e) { e.printStackTrace(); } } }
In the Java example, the SOAP message is loaded from query.xml and then sent using the Dispatch object. The code now looks like:
import java.io.FileInputStream; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPMessage; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; class QueryAPIExample { public static void main(String[] args) { try { QName serviceName = new QName( "http://www.bea.com/analytics/AnalyticsQueryService", "AnalyticsQueryService"); URL serviceURL = new URL( "http://analytics:11944/analytics/QueryService?wsdl"); Service service = Service.create(serviceURL, serviceName); QName portName = new QName( "http://www.bea.com/analytics/AnalyticsQueryService", "AnalyticsQueryServicePort"); Dispatch<SOAPMessage> dispatch = service.createDispatch( portName, SOAPMessage.class, Service.Mode.MESSAGE); SOAPMessage request = MessageFactory.newInstance().createMessage(null, new FileInputStream("query.xml")); // Send the request and get the response SOAPMessage response = dispatch.invoke(request); } catch (Exception e) { e.printStackTrace(); } } }
You are interested in the elements contained in the <return> element of the SOAP response, which are described in the following table:
Element | Description |
---|---|
<results> | We see one <results> element for each row of data our request generates. Each <results> element contains one or more <values> elements. |
<values> | In each <results> element, there is one <values> element for each type of data we request. |
<columns> | The <columns> element describes the type or types of data we request. The sequence of the <values> elements in each <results> element corresponds directly to the sequence of the <columns> elements. |
The following is an example of a response from the Query API service:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Header/> <S:Body> <ns2:executeResultSetQueryResponse xmlns:ns2="http://www.bea.com/analytics/AnalyticsQueryService"> <return> <results> <values xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Report </values> <count>0</count> </results> <columns>portlet.name</columns> </return> </ns2:executeResultSetQueryResponse> </S:Body> </S:Envelope>
In this response the result of the query has one column, described as portlet.name (the name property of the portlet dimension of the event). The query has generated one row of data, and the name of the portlet returned is Report.
In the following code, the Java example is updated to process a response with any number of <results> and <columns>, and output the data to standard output.
import java.io.FileInputStream; import java.net.URL; import java.util.Iterator; import javax.xml.namespace.QName; import javax.xml.soap.MessageFactory; import javax.xml.soap.Name; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPFactory; import javax.xml.soap.SOAPMessage; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; class QueryAPIExample { public static void main(String[] args) { try { QName serviceName = new QName( "http://www.bea.com/analytics/AnalyticsQueryService", "AnalyticsQueryService"); URL serviceURL = new URL( "http://analytics:11944/analytics/QueryService?wsdl"); Service service = Service.create(serviceURL, serviceName); QName portName = new QName( "http://www.bea.com/analytics/AnalyticsQueryService", "AnalyticsQueryServicePort"); Dispatch<SOAPMessage> dispatch = service.createDispatch( portName, SOAPMessage.class, Service.Mode.MESSAGE); SOAPMessage request = MessageFactory.newInstance().createMessage(null, new FileInputStream("query.xml")); // Send the request and get the response SOAPMessage response = dispatch.invoke(request); // Process the request and print the result SOAPBody resBody = response.getSOAPBody(); SOAPFactory soapFactory = SOAPFactory.newInstance(); Name name; name = soapFactory.createName( "executeResultSetQueryResponse", "ns2", "http://www.bea.com/analytics/AnalyticsQueryService"); SOAPElement resResultSet = (SOAPElement)resBody.getChildElements(name).next(); name = soapFactory.createName("return"); SOAPElement resReturn = (SOAPElement)resResultSet.getChildElements(name).next(); name = soapFactory.createName("results"); Iterator results = resReturn.getChildElements(name); System.out.println("Analytics Query API Results:"); System.out.println("----------------------------\n\n"); name = soapFactory.createName("values"); SOAPElement value; while(results.hasNext()) { Iterator values = ((SOAPElement)results.next()).getChildElements(name); while (values.hasNext()) { value = (SOAPElement)values.next(); System.out.print(value.getValue() + "\t\t"); } System.out.print("\n"); } } catch (Exception e) { e.printStackTrace(); } } }