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.


Creating a data service

Now, let's start creating the data service from scratch:

  1. Log in to the management console of ESB profile of WSO2 EI using the following URL on your browser:  "https://localhost:9443/carbon/".
  2. Click Create under the Data Service menu to open the Create Data Service window.
  3. Enter the following data service name.

    Data Service NameWebResource
  4. Leave the default values for the other fields.
  5. 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.

  1. Click Add New Datasource and enter the following details:

    Datasource IDEnter WebHarvestDataSource.
    Datasource TypeThis 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>
  2. Save the datasource. 
  3. 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.

  1. Click Add New Query and enter the following details:

    Query IDEnter webquery as the query ID.
    DatasourceSelect the datasource for which you are going to write a query. Select the web datasource that you created previously.
    Scrapper VariableEnter 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 is weatherInfo (< var-def name = 'AppInfo' >).
  2. 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. 
    1. Start by giving the following details:

      Output TypeSpecify 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 elementSpecify 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 NameSpecify the XML element that should group each individual result.  Enter App in this field.
    2. Click Add New Output Mapping to start creating the output mapping for the Title field.
    3. Click Add to save the output mapping. 
    4. Now, add another output mapping for the Description column.

    5. You will now have the following output mappings listed for the webquery query:

  3. 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.

  1. Click Add New Operation and enter the following information.

    Operation NameGetResourceOp
    Query IDWebQuery
  2. 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.

  1. Click Add New Resource and enter the following information.

    Resource PathAppInfo
    Resource MethodGET
    Query IDwebquery
  2. 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.

  1. Go to the Deployed Services screen.
  2. Click the Try this service link for the WebResource data service. The TryIt Tool will open with the data service.
  3. Select the GetResource operation and click Send.
  4. 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.