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.
Creating a data service
Now, let's start creating the data service from scratch:
Download the product installer from here, and run the installer.
Let's call the installation location of your product the <EI_HOME> directory.If you installed the product using the installer, this is located in a place specific to your OS as shown below:
OS Home directory Mac OS /Library/WSO2/EnterpriseIntegrator/6.5.0
Windows C:\Program Files\WSO2\EnterpriseIntegrator\6.5.0\
Ubuntu /usr/lib/wso2/EnterpriseIntegrator/6.5.0
CentOS /usr/lib64/EnterpriseIntegrator/6.5.0
Start the ESB profile:
- Log in to the management console of ESB profile of WSO2 EI 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 the following data service name.
Data Service Name WebResource - Leave the default values for the other fields.
- Click Next to go to the Datasources screen.
Connecting to the datasource
You can add a web resource as the datasource by following the steps given below.
Click Add New Datasource and enter the following details:
Datasource ID Enter WebHarvestDataSource. Datasource Type This specifies the type of datasource for which you will create the data service. Select Web Datasource from the list. Inline Web Harvest Config 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.
Creating a query to GET data
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 and enter the following details:
Query ID Enter webquery as the query ID. Datasource Select the datasource for which you are going to write a query. Select the web datasource that you created previously. Scrapper Variable Enter AppInfo. 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 isweatherInfo
(<
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.
Start by giving the following details:
Output Type 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. Grouped by element 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. Row Name 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.
- 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:
- Click Next to go to the Operations screen.
Creating a SOAP operation to invoke the query
To invoke the query, you need to define an operation.
Click Add New Operation and enter the following information.
Operation Name GetResourceOp Query ID WebQuery - Save the operation.
You can now invoke the data service query using SOAP.
Creating a REST resource to invoke the query
Now, let's create REST resources to invoke the query created above. Alternatively, you can create SOAP operations to invoke the queries. See the previous section for instructions.
Click Add New Resource and enter the following information.
Resource Path AppInfo Resource Method GET Query ID webquery - Save the resource.
You can now invoke the data service query using REST.
Finish creating the data service
Once you have defined the operation, click Finish to complete the data service creation process. You will now be taken to the Deployed Services screen, which shows all the data services deployed on the server.
Invoking your data service using SOAP
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.
Invoking your data service using REST
You can send an HTTP GET request to invoke the data service using cURL as shown below.
curl -X GET http://localhost:8280/services/WebResource.HTTPEndpoint/AppInfo
This will return the response in XML.