Welcome!

Websphere Authors: Sandi Mappic, Jnan Dash, Bob Gourley, Kevin Benedict, RealWire News Distribution

Related Topics: Java, XML, Websphere, AJAX & REA

Java: Article

WebSphere Datapower Configuration for Database

Construct Database Query from Requests URI

The purpose of this article is to describe the steps involve to configure Datapower service for the Database communication. The service will parse the incoming parameters from the request, construct a SQL query string and returns the data from the Database, based on the constructed query.

Database
For the demonstration, the following table "DataPowerTest" is created in MS SQL Server. The following columns and data were added as well.

Datapower Configurations

1. In the Datapower, create SQL Data Source with the following values

Input

Values

Name

GallagherDirectoryDB

Database Type

Microsoft SQL Server

Connection User Name

<username>

Connection Password

<password>

Data Source ID

GallagherDirectory <database name>

Data Source Host

<database hostname>

Data Source Port

1433

2.    Create the GallagherDirectoryQuery.xslt file and type the following. Upload the file on the Datapower

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:dp="http://www.datapower.com/extensions"

xmlns:dpconfig="http://www.datapower.com/param/config"

extension-element-prefixes="dp"

exclude-result-prefixes="dp dpconfig">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">

<xsl:variable name="incomingURI">

<xsl:value-of select="dp:variable('var://service/URI')"/>

</xsl:variable>

<xsl:variable name="squote">'</xsl:variable>

<xsl:variable name="tempVar">

<xsl:value-of select="substring-after($incomingURI,'?')"/>

</xsl:variable>

<xsl:call-template name="parseAtt">

<xsl:with-param name="tempVar" select="$tempVar" />

<xsl:with-param name="andVar" select="''" />

</xsl:call-template>

</xsl:template>

<xsl:template name="parseAtt">

<xsl:param name="tempVar" />

<xsl:param name="andVar" />

<xsl:variable name="squote">'</xsl:variable>

<xsl:variable name="att1Var">

<xsl:if test="contains($tempVar,'&amp;')">

<xsl:value-of select="substring-before($tempVar,'&amp;')" />

</xsl:if>

<xsl:if test="not(contains($tempVar,'&amp;'))">

<xsl:value-of select="$tempVar"/>

</xsl:if>

</xsl:variable>

<xsl:variable name="att2Var">

<xsl:value-of select="substring-after($tempVar,'&amp;')" />

</xsl:variable>

<xsl:variable name="parameter1Var">

<xsl:if test="contains($att1Var,'=')">

<xsl:value-of select="substring-before($att1Var,'=')"/>

</xsl:if>

</xsl:variable>

<xsl:variable name="value1Var">

<xsl:if test="contains($att1Var,'=')">

<xsl:value-of select="substring-after($att1Var,'=')"/>

</xsl:if>

</xsl:variable>

<xsl:variable name="dbVar" select="concat($andVar, $parameter1Var,' like ',$squote, '%',$value1Var,'%',$squote)"/>

<xsl:if test="not($att2Var='')">

<xsl:call-template name="parseAtt">

<xsl:with-param name="tempVar" select="substring-after($tempVar,'&amp;')" />

<xsl:with-param name="andVar" select="concat($dbVar, ' and ')" />

</xsl:call-template>

</xsl:if>

<xsl:if test="$att2Var=''">

<xsl:variable name="query">

select * from DataPowerTest Where

<xsl:value-of select="$dbVar"/>

</xsl:variable>

<xsl:variable name="result" select="dp:sql-execute('GallagherDirectoryDB',$query)" />

<xsl:copy-of select="$result" />

</xsl:if>

</xsl:template>

</xsl:stylesheet>

3. Create a new Multi-Protocol Gateway with the following values

Input

Values

Name

RESTDemoMPGW

Type

dynamic-backends

Response Type

Non-XML

Request Type

SOAP-XML

4. Create a HTTP Front side handler with the following values

Input

Values

Name

RESTDemoFSH

Local IP Address

<setup on one of the Ethernet Interface>

Port

80

Allow Methods and Version

GET (and all the other default values)

5. At the Multi-Protocol Gateway screen, click on the "Advance" tab menu and click on "Process Messages Whose Body Is Empty"

6. Click on Apply.

7. On RESTDemoMPGW, create a new Policy and named it "RESTDEmoPolicy"

8. Create a new "Client to Server" rule and configure Match action icon for "* " for URL (match all)

9. Assign a "Transform" action to the rule and assign the "GallagherDirectoryQuery.xslt" file.

10. Drag the "Advanced" action to the rule and select "SET Variable". Set the following variable

Variable

Values

/service/mpgw/skip-backside

1

11. Click "Apply Policy" and then "Close Window". Click on "Apply" at the Multi-Protocol Gateway screen.

12. Save configurations.

Testing

1. From the SOAPUI, send the following request to the MPGW

http://<datapowerhostname>:<port>/people/?Id=2&Name=T

2. The successful query with the condition "Id=2" and "Name=T*" will send the data back from database.

3. From the SOAPUI, send another following request to the MPGW

http://<datapowerhostname>:<port>/people/?Id=1&Name=K&Division=C

4. The successful query with the condition "Id=1", "Name=K*" and "Division=C*" will send the data back from database.

More Stories By Asim Saddal

Asim Saddal works in the Middleware (WebSphere Application Server, WebSphere Datapower, WebSphere Process Server, WebSphere VE) practice of IBM Software Services for WebSphere.