This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Exposing a Web Resource as a Data Service
This tutorial will guide you on how to expose a web resource as a data service using the ESB profile of WSO2 Enterprise Integrator (WSO2 EI). You can create a data service that can fetch selected data from a web resource and display the data in XML, JSON or RDF form.
Follow the instructions below. Also, see the samples in Data Integration Samples.
Start the Create New Data Service wizard
Now, let's start creating the data service from scratch:
Log in to the management console using the following URL on your browser: "https://localhost:9443/carbon/".
Click Create under the Data Service menu to open the Create Data Service window.
Enter WebResource as the data service name as shown below.
Click Next to go to the Datasources screen.
Add a Web Resource as the datasource
You can add a web resource as the datasource by following the steps given below.
- Click Add New Datasource to open the following screen:
- Enter the following values:
- In the Datasource ID field, enter WebHarvestDataSource as the datasource Id.
- In the Datasource Type field, specifies the type of datasource for which you will create the data service. Select Web Datasource from the list. You will now get the following screen:
Select Inline Web Harvest Config and enter the configuration as shown below:
<config> <var-def name='AppInfo'> <xslt> <xml> <html-to-xml> <http method='get' url='https://play.google.com/store/apps'/> </html-to-xml> </xml> <stylesheet> <![CDATA[ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/"> <AppInfo> <xsl:for-each select="//*[@class='details']"> <App> <Title><xsl:value-of select="a[@class='title']"/></Title> <Description><xsl:value-of select="div[@class='description']"/></Description> </App> </xsl:for-each> </AppInfo> </xsl:template> </xsl:stylesheet> ]]> </stylesheet> </xslt> </var-def> </config>
- Save the datasource.
- Click Next to go to the Queries screen.
Define a query for the datasource
Now let's start writing a query for getting data from the web resource. The query will specify the data that should be fetched by this query, and the format that should be used to display the data.
- Click Add New Query to open the Add New Query screen.
- Enter the following values:
- Query ID: In this field, enter webquery as the query ID.
- Datasource: In this field, select the datasource for which you are going to write a query. Select the web datasource that you created previously.
- Scrapper Variable: When you add a query to a Web datasource, you must enter a Scraper Variable. This scraper variable must be the same as the output name in the web datasource configuration, which returns the output from the configuration. In this example, the
var-def
name in the configuration isAppInfo
(<
var-def
name
=
'AppInfo'
>
).
- Define Output Mapping: Now, let's specify how the data fetched from the datasource should be displayed in the output. We will create output mappings for the following data in the web resource: Title and Description.
- In the Output type field, specify the format in which the query results should be presented. You can select XML, JSON or RDF. We will use XML for this tutorial.
- In the Grouped by element field, specify a grouping for all the output mappings. This will be the XML element that will group the query result. Enter AppInfo in this field.
- In the Row Name field, specify the XML element that should group each individual result. Enter App in this field.
- Click Add New Output Mapping to start creating the output mapping for the Title field. Enter values as shown below:
- Click Add to save the output mapping.
Now, add another output mapping for the Description column.
You will now have the following output mappings listed for the webquery query:
Once you have created all the output mappings, click Main Configuration to return to the Edit Query screen.
Find out more about defining Output Mappings.
- Click Save, to save the query.
- Click Next to go to the Operations screen.
Defining and operation to invoke the query
You need to define an operation, to expose data as a SOAP service.
- Click Add New Operation to open the Add New Operation screen.
- In the Operation Name field, enter GetResource.
- In the Query ID field, select WebQuery.
- Save the operation.
Finish creating the data service
In this tutorial, we are only defining a SOAP service to expose the data. Therefore, you can click Finish to complete the data service creation process, after defining the operation. You will now be taken to the Deployed Services screen, which shows all the data services deployed on the server.
If you click Next after defining the operation, you will be taken to the Add Resources screen, which allows you to expose the data as a REST resource. If you want to create a REST resource, see Exposing Data as a REST Resource for information.
Invoking your data service
You can try the data service you created by using the TryIt tool that is in your product by default.
- Go to the Deployed Services screen.
- Click the Try this service link for the WebResource data service. The TryIt Tool will open with the data service.
- Select the GetResource operation and click Send.
- The response will be published.