Tuesday, November 27, 2012

XML validation against schema

We could use simple online tools to validate xml files against a single schema. But if we have more than one schema files, which are sub schemas of the base schema,  needed to validate a single xml file, we could use some other  tools which are availble freely.
I used libxml which works better. But for windows-64 bit platform, binary distribution is not available. We need to compile the source files. For linux, compilation would be easy.

Run xmllint from the "bin" folder

# xmllint --noout --schema <BASE_XSD_FILE>  <XML_FILE_TO_BE_VALIDATED>

If there are any errors it will be shown in the command prompt.

Monday, November 26, 2012

Configuring wso2esb to pass messages through proxy server

Organizations may expose the services over a proxy server for several purposes. In such a case, when user configures ESB, he has to provide proxy server configurations.
In axis2 configuration , at the transport sender configuration two properties has to be provided.
  • http.proxyHost : Proxy server's IP
  • http.proxyPort : Prosy server's port
eg :
 <transportSender name="http" class="org.apache.synapse.transport.nhttp.HttpCoreNIOSender">
        <parameter name="non-blocking" locked="false">true&lt;/parameter>
        <parameter name="http.proxyHost" locked="false">192.168.0.26  </parameter>
        <parameter name="http.proxyPort" locked="false">3128</parameter>
 </transportSender>
And a property (POST_TO_URI) has to be set in the synapse configuration to make ESB's out going URL a complete URL.

eg:
<inSequence>
    <property name="POST_TO_URI" value="true" scope="axis2"/>
    <send>
        <endpoint>
            <address uri="http://192.168.0.26:9000/services/SimpleStockQuoteService"/>
        </endpoint>
    </send>
</inSequence>
Depends on the proxy server's behaviour we may need to set some additional properties.
  • DISABLE_CHUNKING : If the proxy server doesn't support HTTP chunking. 
<property name="DISABLE_CHUNKING" value="true" scope="axis2"/>
  • FORCE_HTTP_1.0 : If proxy server supports only HTTP/1.0 messages.
<property name="FORCE_HTTP_1.0" value="true" scope="axis2"/>
 These properties can be applied to WSO2API Manager as well, since WSO2ESB is used as the gateway for APIManager.

Saturday, November 24, 2012

Extracting CDATA section using XSLT

In a XML message, we pass some data which we might not want to be parsed by xml parsers.
Characters like "<>" are illegal in XML elements.  To save such characters we use CDATA section in our xml message.
For instance in the following xml message, we use CDATA block to pass <metadata> to other end without parsing "<,>" signs.
eg:


    <ser:getArtifactContentResponse    xmlns:ser="http://services.generic.governance.carbon.wso2.org">
    <ser:return>
         <![CDATA[
   <metadata xmlns="http://www.wso2.org/governance/metadata">
    <overview>       
        <name>scannapp&lt;/name>
        <developerId>developer702&lt;/developerId>
        <stateId>2&lt;/stateId>
        <serverURL>http://abc.com&lt;/serverURL>
        <id>cspapp1103&lt;/id>
        <description>scann doc&lt;/description>
        <hostingTypeId>1&lt;/hostingTypeId>       
    </overview>
</metadata>
  ]]>
  </ser:return>
</ser:getArtifactContentResponse>

 
To extract the CDATA section,( here it contains xml message) we could use simple xslt script.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" xmlns:ns="http://services.generic.governance.carbon.wso2.org">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" />
    <xsl:template match="/">
        <xsl:value-of select="//ns:getArtifactContentResponse/ns:return/text()" disable-output-escaping="yes"/>
    </xsl:template>
</xsl:stylesheet>
The output would be ;

   <metadata xmlns="http://www.wso2.org/governance/metadata">
    <overview>       
        <name>scannapp</name>
        <developerId>developer702</developerId>
        <stateId>2</stateId>
        <serverURL>http://abc.com</serverURL>
        <id>cspapp1103</id>
        <description>scann doc</description>
        <hostingTypeId>1</hostingTypeId>       
    </overview>
</metadata>

Thursday, November 22, 2012

Overcomming character encoding issue in windows for JAVA applications

I faced invalid UTF-8/character encoding issues multiple times recently when compiling/running aplictaions in java. We could enforce java VM to use the encoding pattern as UTF-8. To do that, set the following environment property.

variable name :   JAVA_TOOL_OPTIONS
variable value :   -Dfile.encoding=UTF8