Wednesday, November 30, 2016

Configure ESB 5.0.0 to send requests through Basic Authenticated HTTP Proxy ( Squid )

This article explains how to send requests through HTTP proxy which is secured by basic authentication.

Configuration at HTTP Proxy ( Squid )


Add the below section in /etc/squid3/squid.conf

auth_param basic program /usr/lib/squid3/basic_ncsa_auth /etc/squid3/passwords
auth_param basic children 5
auth_param basic realm Squid proxy-caching web server
auth_param basic credentialsttl 2 hours
auth_param basic casesensitive off

acl ncsa_users proxy_auth REQUIRED
http_access allow ncsa_users

http_port 3128


Also make sure you have enabled the ports. For testing have enabled all ports in my configuration. Based on your requirement only enable the known ports for security reasons.

acl SSL_ports port 1-65535
acl Safe_ports port 1-65535

Setting up the user for HTTP Proxy


sudo htpasswd -c /etc/squid3/passwords ajanthandocusign 

Configuration at WSO2 ESB 5.0.0



Here the back end endpoint I'm calling is HTTPS, due to that the axis2.xml configurations for proxyProfiles need to be added under the PassThroughHttpSSLSender section. Here you can see the targerHosts set as “ demo.docusign.net”. This is the docusign API HTTPS host name and by providing this only the requests to “ demo.docusign.net” will be routed through HTTP proxy and others will the connected directly.

<transportSender name="https" class="org.apache.synapse.transport.passthru.PassThroughHttpSSLSender">
        <parameter name="non-blocking" locked="false">true</parameter> 
        <parameter name="keystore" locked="false">
            <KeyStore>
                <Location>repository/resources/security/wso2carbon.jks</Location>
                <Type>JKS</Type>
                <Password>wso2carbon</Password>
                <KeyPassword>wso2carbon</KeyPassword>
            </KeyStore>
        </parameter>
        <parameter name="truststore" locked="false">
            <TrustStore>
                <Location>repository/resources/security/client-truststore.jks</Location>
                <Type>JKS</Type>
                <Password>wso2carbon</Password>
            </TrustStore>
        </parameter>
       <parameter name="HostnameVerifier">AllowAll</parameter>
       <parameter name="proxyProfiles">
       <profile>
    <targetHosts>demo.docusign.net</targetHosts>
    <proxyHost>localhost</proxyHost>
    <proxyPort>3128</proxyPort>
    <proxyUserName>ajanthandocusign</proxyUserName>
    <proxyPassword>ajanthandocusign</proxyPassword>
       </profile>      
 </parameter>


            <!--supports Strict|AllowAll|DefaultAndLocalhost or the default if none specified -->
    </transportSender>

Sample API


<api xmlns="http://ws.apache.org/ns/synapse" name="DocusignAPI" context="/testDocSign">
   <resource methods="POST" uri-template="/">
      <inSequence>
         <log level="custom">
            <property name="STATUS" value="----------------DocusignAPI Invoked-------------"/>
         </log>
         <property name="POST_TO_URI" value="true" scope="axis2"/>
         <docusign.init>
            <apiUrl>https://demo.docusign.net</apiUrl>
            <apiVersion>v2</apiVersion>
            <integratorKey>4cd4663d-b437-49c7-b0a6-4df9b6663520</integratorKey>
            <userName>ajanthane@wso2.com</userName>
            <password>EasternProvince1</password>
            <blocking>false</blocking>
         </docusign.init>
         <call>
            <endpoint>
               <http method="GET" uri-template="https://demo.docusign.net/restapi/v2/accounts/2062967/templates"/>
            </endpoint>
         </call>
         <respond/>
      </inSequence>
   </resource>
</api>

Result Verification


By enabling the DEBUG logs at log4j.properties for

log4j.category.org.apache.synapse.transport=DEBUG
log4j.logger.org.apache.synapse.transport.http.wire=DEBUG

We can verify it by the below wso2carbon.log

[2016-11-30 18:16:21,117] DEBUG - TransportUtils createSOAPEnvelope using Builder (class org.apache.synapse.commons.builders.XFormURLEncodedBuilder) selected from type (application/x-www-form-urlencoded)
[2016-11-30 18:16:21,118]  INFO - LogMediator STATUS = ----------------DocusignAPI Invoked-------------
[2016-11-30 18:16:21,118] DEBUG - RelayUtils Content Type is application/x-www-form-urlencoded
[2016-11-30 18:16:21,123] DEBUG - RelayUtils Content Type is application/x-www-form-urlencoded
[2016-11-30 18:16:21,128]  INFO - TimeoutHandler This engine will expire all callbacks after GLOBAL_TIMEOUT: 120 seconds, irrespective of the timeout action, after the specified or optional timeout
[2016-11-30 18:16:21,132] DEBUG - TargetConnections Trying to get a connection {tls}->http://localhost:3128->https://demo.docusign.net:443
[2016-11-30 18:16:21,133] DEBUG - HostConnections Creating new connection pool: {tls}->http://localhost:3128->https://demo.docusign.net:443
[2016-11-30 18:16:21,135] DEBUG - ConnectCallback Connected to remote address: localhost/127.0.0.1:3128 from local address: /127.0.0.1:36335
[2016-11-30 18:16:21,138] DEBUG - TargetHandler http-outgoing-1: Connected (org.apache.synapse.transport.passthru.connections.HostConnections@6ecc458a)
[2016-11-30 18:16:21,139] DEBUG - HostConnections New connection {tls}->http://localhost:3128->https://demo.docusign.net:443 is added to the free list
[2016-11-30 18:16:21,140] DEBUG - DeliveryAgent Submitting new request to the connection: http-outgoing-1
[2016-11-30 18:16:21,143] DEBUG - LoggingNHttpClientConnection http-outgoing-1: Produce output
[2016-11-30 18:16:21,143] DEBUG - TargetHandler http-outgoing-1: Request ready
[2016-11-30 18:16:21,145] DEBUG - TargetHandler http-outgoing-1: Sending CONNECT request to http://localhost:3128
[2016-11-30 18:16:21,145] DEBUG - LoggingNHttpClientConnection http-outgoing-1: CONNECT demo.docusign.net:443 HTTP/1.1
[2016-11-30 18:16:21,145] DEBUG - headers http-outgoing-1 >> CONNECT demo.docusign.net:443 HTTP/1.1
[2016-11-30 18:16:21,146] DEBUG - headers http-outgoing-1 >> Host: demo.docusign.net:443
[2016-11-30 18:16:21,147] DEBUG - headers http-outgoing-1 >> Proxy-Connection: Keep-Alive
[2016-11-30 18:16:21,147] DEBUG - headers http-outgoing-1 >> Proxy-Authorization: Basic YWphbnRoYW5kb2N1c2lnbjphamFudGhhbmRvY3VzaWdu
[2016-11-30 18:16:21,147] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "CONNECT demo.docusign.net:443 HTTP/1.1[\r][\n]"
[2016-11-30 18:16:21,147] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Host: demo.docusign.net:443[\r][\n]"
[2016-11-30 18:16:21,147] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Proxy-Connection: Keep-Alive[\r][\n]"
[2016-11-30 18:16:21,147] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Proxy-Authorization: Basic YWphbnRoYW5kb2N1c2lnbjphamFudGhhbmRvY3VzaWdu[\r][\n]"
[2016-11-30 18:16:21,148] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "[\r][\n]"
[2016-11-30 18:16:21,672] DEBUG - LoggingNHttpClientConnection http-outgoing-1: Consume input
[2016-11-30 18:16:21,672] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "HTTP/1.1 200 Connection established[\r][\n]"
[2016-11-30 18:16:21,672] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "[\r][\n]"
[2016-11-30 18:16:21,672] DEBUG - headers http-outgoing-1 << HTTP/1.1 200 Connection established
[2016-11-30 18:16:21,673] DEBUG - TargetHandler http-outgoing-1: HTTP/1.1 200 Connection established
[2016-11-30 18:16:21,676] DEBUG - TargetHandler http-outgoing-1: Tunnel established
[2016-11-30 18:16:22,623] DEBUG - LoggingNHttpClientConnection http-outgoing-1: Produce output
[2016-11-30 18:16:22,624] DEBUG - TargetHandler http-outgoing-1: Request ready
[2016-11-30 18:16:22,626] DEBUG - LoggingNHttpClientConnection http-outgoing-1: GET https://demo.docusign.net/restapi/v2/accounts/2062967/templates HTTP/1.1
[2016-11-30 18:16:22,626] DEBUG - headers http-outgoing-1 >> GET https://demo.docusign.net/restapi/v2/accounts/2062967/templates HTTP/1.1
[2016-11-30 18:16:22,627] DEBUG - headers http-outgoing-1 >> Accept-Language: en-US,en;q=0.8
[2016-11-30 18:16:22,627] DEBUG - headers http-outgoing-1 >> Accept-Encoding: gzip, deflate
[2016-11-30 18:16:22,627] DEBUG - headers http-outgoing-1 >> X-DocuSign-Authentication: {"Username":"ajanthane@wso2.com","Password":"EasternProvince1","IntegratorKey":"4cd4663d-b437-49c7-b0a6-4df9b6663520"}
[2016-11-30 18:16:22,628] DEBUG - headers http-outgoing-1 >> Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
[2016-11-30 18:16:22,628] DEBUG - headers http-outgoing-1 >> Postman-Token: 50297aed-16db-083f-5ecb-df76b3675402
[2016-11-30 18:16:22,628] DEBUG - headers http-outgoing-1 >> Content-Type: application/x-www-form-urlencoded
[2016-11-30 18:16:22,629] DEBUG - headers http-outgoing-1 >> Accept: */*
[2016-11-30 18:16:22,629] DEBUG - headers http-outgoing-1 >> Cache-Control: no-cache
[2016-11-30 18:16:22,629] DEBUG - headers http-outgoing-1 >> Host: demo.docusign.net
[2016-11-30 18:16:22,630] DEBUG - headers http-outgoing-1 >> Connection: Keep-Alive
[2016-11-30 18:16:22,630] DEBUG - headers http-outgoing-1 >> User-Agent: Synapse-PT-HttpComponents-NIO
[2016-11-30 18:16:22,631] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "GET https://demo.docusign.net/restapi/v2/accounts/2062967/templates HTTP/1.1[\r][\n]"
[2016-11-30 18:16:22,632] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Accept-Language: en-US,en;q=0.8[\r][\n]"
[2016-11-30 18:16:22,632] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Accept-Encoding: gzip, deflate[\r][\n]"
[2016-11-30 18:16:22,633] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "X-DocuSign-Authentication: {"Username":"ajanthane@wso2.com","Password":"EasternProvince1","IntegratorKey":"4cd4663d-b437-49c7-b0a6-4df9b6663520"}[\r][\n]"
[2016-11-30 18:16:22,633] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop[\r][\n]"
[2016-11-30 18:16:22,633] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Postman-Token: 50297aed-16db-083f-5ecb-df76b3675402[\r][\n]"
[2016-11-30 18:16:22,634] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Content-Type: application/x-www-form-urlencoded[\r][\n]"
[2016-11-30 18:16:22,634] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Accept: */*[\r][\n]"
[2016-11-30 18:16:22,634] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Cache-Control: no-cache[\r][\n]"
[2016-11-30 18:16:22,635] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Host: demo.docusign.net[\r][\n]"
[2016-11-30 18:16:22,635] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "Connection: Keep-Alive[\r][\n]"
[2016-11-30 18:16:22,636] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "User-Agent: Synapse-PT-HttpComponents-NIO[\r][\n]"
[2016-11-30 18:16:22,636] DEBUG - wire HTTPS-Sender I/O dispatcher-1 << "[\r][\n]"
[2016-11-30 18:16:23,056] DEBUG - LoggingNHttpClientConnection http-outgoing-1: Consume input
[2016-11-30 18:16:23,057] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "HTTP/1.1 200 OK[\r][\n]"
[2016-11-30 18:16:23,057] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "Cache-Control: no-cache[\r][\n]"
[2016-11-30 18:16:23,057] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "Content-Length: 1856[\r][\n]"
[2016-11-30 18:16:23,058] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "Content-Type: application/json; charset=utf-8[\r][\n]"
[2016-11-30 18:16:23,058] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "X-RateLimit-Reset: 1480510800[\r][\n]"
[2016-11-30 18:16:23,058] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "X-RateLimit-Limit: 1000[\r][\n]"
[2016-11-30 18:16:23,058] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "X-RateLimit-Remaining: 998[\r][\n]"
[2016-11-30 18:16:23,058] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "Date: Wed, 30 Nov 2016 12:46:20 GMT[\r][\n]"
[2016-11-30 18:16:23,058] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "Strict-Transport-Security: max-age=31536000; includeSubDomains[\r][\n]"
[2016-11-30 18:16:23,058] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "[\r][\n]"
[2016-11-30 18:16:23,058] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "{[\r][\n]"
[2016-11-30 18:16:23,058] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "  "envelopeTemplates": [[\r][\n]"
[2016-11-30 18:16:23,059] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "    {[\r][\n]"
[2016-11-30 18:16:23,059] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "templateId": "e67870f4-737d-4300-b6fe-3b842eed5924",[\r][\n]"
[2016-11-30 18:16:23,059] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "name": "SampleAjan1",[\r][\n]"
[2016-11-30 18:16:23,059] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "shared": "true",[\r][\n]"
[2016-11-30 18:16:23,059] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "password": "",[\r][\n]"
[2016-11-30 18:16:23,059] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "description": "Sample",[\r][\n]"
[2016-11-30 18:16:23,059] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "lastModified": "2016-11-29T08:49:32.6230000Z",[\r][\n]"
[2016-11-30 18:16:23,059] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "pageCount": 1,[\r][\n]"
[2016-11-30 18:16:23,059] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "uri": "/templates/e67870f4-737d-4300-b6fe-3b842eed5924",[\r][\n]"
[2016-11-30 18:16:23,060] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "folderName": "Templates",[\r][\n]"
[2016-11-30 18:16:23,060] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "folderId": "b32049c4-4a18-4d09-a647-1c4d383bf3bd",[\r][\n]"
[2016-11-30 18:16:23,060] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "folderUri": "/folders/b32049c4-4a18-4d09-a647-1c4d383bf3bd",[\r][\n]"
[2016-11-30 18:16:23,060] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "owner": {[\r][\n]"
[2016-11-30 18:16:23,060] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "        "userName": "Ajanthan E",[\r][\n]"
[2016-11-30 18:16:23,060] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "        "email": "ajanthane@wso2.com",[\r][\n]"
[2016-11-30 18:16:23,060] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "        "userId": "4ef8cdca-77c1-41b8-9a11-23461baa9c3f"[\r][\n]"
[2016-11-30 18:16:23,060] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      },[\r][\n]"
[2016-11-30 18:16:23,060] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "emailSubject": "Please DocuSign: Buddha-Quotes-4.jpg",[\r][\n]"
[2016-11-30 18:16:23,061] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "emailBlurb": "",[\r][\n]"
[2016-11-30 18:16:23,061] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "signingLocation": "Online",[\r][\n]"
[2016-11-30 18:16:23,061] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "authoritativeCopy": "false",[\r][\n]"
[2016-11-30 18:16:23,061] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "enableWetSign": "true",[\r][\n]"
[2016-11-30 18:16:23,061] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "allowMarkup": "false",[\r][\n]"
[2016-11-30 18:16:23,061] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "allowReassign": "true"[\r][\n]"
[2016-11-30 18:16:23,061] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "    },[\r][\n]"
[2016-11-30 18:16:23,061] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "    {[\r][\n]"
[2016-11-30 18:16:23,061] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "templateId": "3f75ab09-2e0d-4493-846b-f1d7c7bdbfbd",[\r][\n]"
[2016-11-30 18:16:23,062] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "name": "Untitled 11/29/2016",[\r][\n]"
[2016-11-30 18:16:23,062] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "shared": "true",[\r][\n]"
[2016-11-30 18:16:23,062] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "password": "",[\r][\n]"
[2016-11-30 18:16:23,062] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "description": "",[\r][\n]"
[2016-11-30 18:16:23,062] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "lastModified": "2016-11-29T08:16:12.5070000Z",[\r][\n]"
[2016-11-30 18:16:23,062] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "uri": "/templates/3f75ab09-2e0d-4493-846b-f1d7c7bdbfbd",[\r][\n]"
[2016-11-30 18:16:23,062] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "folderName": "Templates",[\r][\n]"
[2016-11-30 18:16:23,062] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "folderId": "b32049c4-4a18-4d09-a647-1c4d383bf3bd",[\r][\n]"
[2016-11-30 18:16:23,062] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "folderUri": "/folders/b32049c4-4a18-4d09-a647-1c4d383bf3bd",[\r][\n]"
[2016-11-30 18:16:23,062] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "owner": {[\r][\n]"
[2016-11-30 18:16:23,062] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "        "userName": "Ajanthan E",[\r][\n]"
[2016-11-30 18:16:23,063] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "        "email": "ajanthane@wso2.com",[\r][\n]"
[2016-11-30 18:16:23,063] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "        "userId": "4ef8cdca-77c1-41b8-9a11-23461baa9c3f"[\r][\n]"
[2016-11-30 18:16:23,063] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      },[\r][\n]"
[2016-11-30 18:16:23,063] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "emailSubject": "",[\r][\n]"
[2016-11-30 18:16:23,063] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "emailBlurb": "",[\r][\n]"
[2016-11-30 18:16:23,063] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "signingLocation": "Online",[\r][\n]"
[2016-11-30 18:16:23,063] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "authoritativeCopy": "false",[\r][\n]"
[2016-11-30 18:16:23,063] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "enableWetSign": "true",[\r][\n]"
[2016-11-30 18:16:23,063] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "      "allowMarkup": "false"[\r][\n]"
[2016-11-30 18:16:23,063] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "    }[\r][\n]"
[2016-11-30 18:16:23,064] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "  ],[\r][\n]"
[2016-11-30 18:16:23,064] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "  "resultSetSize": "2",[\r][\n]"
[2016-11-30 18:16:23,064] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "  "startPosition": "0",[\r][\n]"
[2016-11-30 18:16:23,064] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "  "endPosition": "1",[\r][\n]"
[2016-11-30 18:16:23,064] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "  "totalSetSize": "2"[\r][\n]"
[2016-11-30 18:16:23,064] DEBUG - wire HTTPS-Sender I/O dispatcher-1 >> "}"
[2016-11-30 18:16:23,064] DEBUG - headers http-outgoing-1 << HTTP/1.1 200 OK
[2016-11-30 18:16:23,064] DEBUG - headers http-outgoing-1 << Cache-Control: no-cache
[2016-11-30 18:16:23,064] DEBUG - headers http-outgoing-1 << Content-Length: 1856
[2016-11-30 18:16:23,065] DEBUG - headers http-outgoing-1 << Content-Type: application/json; charset=utf-8
[2016-11-30 18:16:23,065] DEBUG - headers http-outgoing-1 << X-RateLimit-Reset: 1480510800
[2016-11-30 18:16:23,065] DEBUG - headers http-outgoing-1 << X-RateLimit-Limit: 1000
[2016-11-30 18:16:23,065] DEBUG - headers http-outgoing-1 << X-RateLimit-Remaining: 998
[2016-11-30 18:16:23,065] DEBUG - headers http-outgoing-1 << Date: Wed, 30 Nov 2016 12:46:20 GMT
[2016-11-30 18:16:23,065] DEBUG - headers http-outgoing-1 << Strict-Transport-Security: max-age=31536000; includeSubDomains
[2016-11-30 18:16:23,065] DEBUG - TargetHandler http-outgoing-1: HTTP/1.1 200 OK
[2016-11-30 18:16:23,068] DEBUG - TargetHandler http-outgoing-1: Input ready
[2016-11-30 18:16:23,069] DEBUG - TargetHandler http-outgoing-1: Content decoder [content length: 1856; pos: 1856; completed: true]
[2016-11-30 18:16:23,071] DEBUG - LoggingNHttpServerConnection http-incoming-1: Produce output
[2016-11-30 18:16:23,071] DEBUG - SourceHandler http-incoming-1: Response ready
[2016-11-30 18:16:23,071] DEBUG - LoggingNHttpServerConnection http-incoming-1: HTTP/1.1 200 OK
[2016-11-30 18:16:23,071] DEBUG - headers http-incoming-1 << HTTP/1.1 200 OK
[2016-11-30 18:16:23,072] DEBUG - headers http-incoming-1 << Strict-Transport-Security: max-age=31536000; includeSubDomains
[2016-11-30 18:16:23,073] DEBUG - headers http-incoming-1 << X-RateLimit-Remaining: 998
[2016-11-30 18:16:23,073] DEBUG - headers http-incoming-1 << X-RateLimit-Limit: 1000
[2016-11-30 18:16:23,073] DEBUG - headers http-incoming-1 << X-RateLimit-Reset: 1480510800
[2016-11-30 18:16:23,073] DEBUG - headers http-incoming-1 << Content-Type: application/json; charset=utf-8
[2016-11-30 18:16:23,073] DEBUG - headers http-incoming-1 << Cache-Control: no-cache
[2016-11-30 18:16:23,073] DEBUG - headers http-incoming-1 << Date: Wed, 30 Nov 2016 12:46:23 GMT
[2016-11-30 18:16:23,073] DEBUG - headers http-incoming-1 << Transfer-Encoding: chunked
[2016-11-30 18:16:23,073] DEBUG - headers http-incoming-1 << Connection: keep-alive
[2016-11-30 18:16:23,073] DEBUG - SourceHandler http-incoming-1: Output ready
[2016-11-30 18:16:23,073] DEBUG - SourceHandler http-incoming-1: Content encoder [chunk-coded; completed: true]
[2016-11-30 18:16:23,074] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "HTTP/1.1 200 OK[\r][\n]"
[2016-11-30 18:16:23,074] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "Strict-Transport-Security: max-age=31536000; includeSubDomains[\r][\n]"
[2016-11-30 18:16:23,074] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "X-RateLimit-Remaining: 998[\r][\n]"
[2016-11-30 18:16:23,074] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "X-RateLimit-Limit: 1000[\r][\n]"
[2016-11-30 18:16:23,074] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "X-RateLimit-Reset: 1480510800[\r][\n]"
[2016-11-30 18:16:23,074] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "Content-Type: application/json; charset=utf-8[\r][\n]"
[2016-11-30 18:16:23,075] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "Cache-Control: no-cache[\r][\n]"
[2016-11-30 18:16:23,075] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "Date: Wed, 30 Nov 2016 12:46:23 GMT[\r][\n]"
[2016-11-30 18:16:23,075] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "Transfer-Encoding: chunked[\r][\n]"
[2016-11-30 18:16:23,075] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "Connection: keep-alive[\r][\n]"
[2016-11-30 18:16:23,075] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "[\r][\n]"
[2016-11-30 18:16:23,075] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "740[\r][\n]"
[2016-11-30 18:16:23,076] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "{[\r][\n]"
[2016-11-30 18:16:23,076] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "  "envelopeTemplates": [[\r][\n]"
[2016-11-30 18:16:23,076] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "    {[\r][\n]"
[2016-11-30 18:16:23,076] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "templateId": "e67870f4-737d-4300-b6fe-3b842eed5924",[\r][\n]"
[2016-11-30 18:16:23,076] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "name": "SampleAjan1",[\r][\n]"
[2016-11-30 18:16:23,076] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "shared": "true",[\r][\n]"
[2016-11-30 18:16:23,077] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "password": "",[\r][\n]"
[2016-11-30 18:16:23,077] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "description": "Sample",[\r][\n]"
[2016-11-30 18:16:23,077] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "lastModified": "2016-11-29T08:49:32.6230000Z",[\r][\n]"
[2016-11-30 18:16:23,077] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "pageCount": 1,[\r][\n]"
[2016-11-30 18:16:23,077] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "uri": "/templates/e67870f4-737d-4300-b6fe-3b842eed5924",[\r][\n]"
[2016-11-30 18:16:23,077] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "folderName": "Templates",[\r][\n]"
[2016-11-30 18:16:23,077] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "folderId": "b32049c4-4a18-4d09-a647-1c4d383bf3bd",[\r][\n]"
[2016-11-30 18:16:23,078] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "folderUri": "/folders/b32049c4-4a18-4d09-a647-1c4d383bf3bd",[\r][\n]"
[2016-11-30 18:16:23,078] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "owner": {[\r][\n]"
[2016-11-30 18:16:23,078] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "        "userName": "Ajanthan E",[\r][\n]"
[2016-11-30 18:16:23,078] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "        "email": "ajanthane@wso2.com",[\r][\n]"
[2016-11-30 18:16:23,078] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "        "userId": "4ef8cdca-77c1-41b8-9a11-23461baa9c3f"[\r][\n]"
[2016-11-30 18:16:23,078] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      },[\r][\n]"
[2016-11-30 18:16:23,079] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "emailSubject": "Please DocuSign: Buddha-Quotes-4.jpg",[\r][\n]"
[2016-11-30 18:16:23,079] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "emailBlurb": "",[\r][\n]"
[2016-11-30 18:16:23,079] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "signingLocation": "Online",[\r][\n]"
[2016-11-30 18:16:23,079] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "authoritativeCopy": "false",[\r][\n]"
[2016-11-30 18:16:23,079] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "enableWetSign": "true",[\r][\n]"
[2016-11-30 18:16:23,079] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "allowMarkup": "false",[\r][\n]"
[2016-11-30 18:16:23,079] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "allowReassign": "true"[\r][\n]"
[2016-11-30 18:16:23,080] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "    },[\r][\n]"
[2016-11-30 18:16:23,080] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "    {[\r][\n]"
[2016-11-30 18:16:23,080] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "templateId": "3f75ab09-2e0d-4493-846b-f1d7c7bdbfbd",[\r][\n]"
[2016-11-30 18:16:23,080] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "name": "Untitled 11/29/2016",[\r][\n]"
[2016-11-30 18:16:23,080] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "shared": "true",[\r][\n]"
[2016-11-30 18:16:23,080] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "password": "",[\r][\n]"
[2016-11-30 18:16:23,080] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "description": "",[\r][\n]"
[2016-11-30 18:16:23,081] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "lastModified": "2016-11-29T08:16:12.5070000Z",[\r][\n]"
[2016-11-30 18:16:23,081] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "uri": "/templates/3f75ab09-2e0d-4493-846b-f1d7c7bdbfbd",[\r][\n]"
[2016-11-30 18:16:23,081] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "folderName": "Templates",[\r][\n]"
[2016-11-30 18:16:23,081] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "folderId": "b32049c4-4a18-4d09-a647-1c4d383bf3bd",[\r][\n]"
[2016-11-30 18:16:23,081] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "folderUri": "/folders/b32049c4-4a18-4d09-a647-1c4d383bf3bd",[\r][\n]"
[2016-11-30 18:16:23,081] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "owner": {[\r][\n]"
[2016-11-30 18:16:23,081] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "        "userName": "Ajanthan E",[\r][\n]"
[2016-11-30 18:16:23,082] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "        "email": "ajanthane@wso2.com",[\r][\n]"
[2016-11-30 18:16:23,082] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "        "userId": "4ef8cdca-77c1-41b8-9a11-23461baa9c3f"[\r][\n]"
[2016-11-30 18:16:23,082] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      },[\r][\n]"
[2016-11-30 18:16:23,082] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "emailSubject": "",[\r][\n]"
[2016-11-30 18:16:23,082] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "emailBlurb": "",[\r][\n]"
[2016-11-30 18:16:23,082] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "signingLocation": "Online",[\r][\n]"
[2016-11-30 18:16:23,083] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "authoritativeCopy": "false",[\r][\n]"
[2016-11-30 18:16:23,083] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "enableWetSign": "true",[\r][\n]"
[2016-11-30 18:16:23,083] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "      "allowMarkup": "false"[\r][\n]"
[2016-11-30 18:16:23,083] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "    }[\r][\n]"
[2016-11-30 18:16:23,083] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "  ],[\r][\n]"
[2016-11-30 18:16:23,083] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "  "resultSetSize": "2",[\r][\n]"
[2016-11-30 18:16:23,083] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "  "startPosition": "0",[\r][\n]"
[2016-11-30 18:16:23,084] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "  "endPosition": "1",[\r][\n]"
[2016-11-30 18:16:23,084] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "  "totalSetSize": "2"[\r][\n]"
[2016-11-30 18:16:23,085] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "}[\r][\n]"
[2016-11-30 18:16:23,085] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "0[\r][\n]"
[2016-11-30 18:16:23,085] DEBUG - wire HTTP-Listener I/O dispatcher-1 << "[\r][\n]"


Thursday, November 24, 2016

How to overwrite the Character Set Encoding in WSO2 ESB 5.0.0

Use the below property to change the character set encoding. Here the scope must be axis2.
<property name="CHARACTER_SET_ENCODING" value="UTF-8" scope="axis2" type="STRING"/>
Sample API used for this kind of scenario is as below:
<api xmlns="http://ws.apache.org/ns/synapse" name="character-encode" context="/test">
   <resource methods="POST">
      <inSequence>
         <log level="custom">
            <property name="STATUS:" value="-------------IN INVOKED-------------------------"/>
         </log>         
         <send>
            <endpoint>
               <http method="POST" uri-template="http://localhost:8089/test/get"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <log level="custom">
            <property name="STATUS:" value="-------------OUT INVOKED-------------------------"/>
         </log>
         <property name="CHARACTER_SET_ENCODING" value="UTF-8" scope="axis2" type="STRING"/>
         <property name="messageType" value="application/json" scope="axis2" type="STRING"/>
         <send/>
      </outSequence>
   </resource>
</api>

Wednesday, November 2, 2016

Monitoring the Database Query Execution in WSO2 ESB 4.9.0


This article explains about how to monitor and log the Database query operations in WSO2 ESB 4.9.0.
  • Download the log4jdbc4-1.2.jar from [1] and copy into ESB_HOME/repository/components/lib also you should have the ojdbc6-11.2.0.2.0.jar.
  • Modify the master-datasources file as below:

<datasource>
            <name>WSO2_CARBON_DB</name>
            <description>The datasource used for registry and user manager</description>
            <jndiConfig>
                <name>jdbc/WSO2CarbonDB</name>
            </jndiConfig>
            <definition type="RDBMS">
                <configuration>
                    <url>jdbc:log4jdbc:oracle:thin:@172.22.217.37:1521/xe</url>
                    <username>esb_test</username>
                    <password>esb_test</password>
                    <driverClassName>net.sf.log4jdbc.DriverSpy</driverClassName>
                    <maxActive>50</maxActive>
                    <maxWait>30000</maxWait>
                    <testOnBorrow>true</testOnBorrow>
                    <validationQuery>SELECT 1 FROM DUAL</validationQuery>
                    <validationInterval>60000</validationInterval>
                </configuration>
            </definition>
</datasource>

  • Add the following to log4j.properties

log4j.logger.jdbc.sqlonly=INFO
log4j.logger.jdbc.sqltiming=INFO
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=ERROR
log4j.logger.jdbc.connection=DEBUG

  • Restart the server and observe the wso2carbon.log. 

[2016-11-02 19:05:38,817]  INFO - sqlonly DELETE FROM UM_ROLE_PERMISSION WHERE UM_ROLE_NAME='cg_unpublisher' AND UM_PERMISSION_ID = (SELECT 
UM_ID FROM UM_PERMISSION WHERE UM_RESOURCE_ID = '/permission/admin/manage/un-publish' AND UM_ACTION 
= 'ui.execute' AND UM_TENANT_ID=-1234) AND UM_TENANT_ID=-1234 AND UM_DOMAIN_ID=(SELECT UM_DOMAIN_ID 
FROM UM_DOMAIN WHERE UM_TENANT_ID=-1234 AND UM_DOMAIN_NAME='INTERNAL') 

[2016-11-02 19:05:38,818]  INFO - sqltiming DELETE FROM UM_ROLE_PERMISSION WHERE UM_ROLE_NAME='cg_unpublisher' AND UM_PERMISSION_ID = (SELECT 
UM_ID FROM UM_PERMISSION WHERE UM_RESOURCE_ID = '/permission/admin/manage/un-publish' AND UM_ACTION 
= 'ui.execute' AND UM_TENANT_ID=-1234) AND UM_TENANT_ID=-1234 AND UM_DOMAIN_ID=(SELECT UM_DOMAIN_ID 
FROM UM_DOMAIN WHERE UM_TENANT_ID=-1234 AND UM_DOMAIN_NAME='INTERNAL') 
 {executed in 0 msec}
[2016-11-02 19:05:38,818]  INFO - sqlonly INSERT INTO UM_ROLE_PERMISSION (UM_PERMISSION_ID, UM_ROLE_NAME, UM_IS_ALLOWED, UM_TENANT_ID, 
UM_DOMAIN_ID) VALUES (13, 'cg_unpublisher', 1, -1234, (SELECT UM_DOMAIN_ID FROM UM_DOMAIN WHERE 
UM_TENANT_ID=-1234 AND UM_DOMAIN_NAME='INTERNAL')) 

[2016-11-02 19:05:38,819]  INFO - sqltiming INSERT INTO UM_ROLE_PERMISSION (UM_PERMISSION_ID, UM_ROLE_NAME, UM_IS_ALLOWED, UM_TENANT_ID, 
UM_DOMAIN_ID) VALUES (13, 'cg_unpublisher', 1, -1234, (SELECT UM_DOMAIN_ID FROM UM_DOMAIN WHERE 
UM_TENANT_ID=-1234 AND UM_DOMAIN_NAME='INTERNAL')) 
 {executed in 1 msec}
[2016-11-02 19:05:38,824]  INFO - sqlonly SELECT UM_ID FROM UM_HYBRID_ROLE WHERE UM_ROLE_NAME ='cg_publisher' AND UM_TENANT_ID=-1234 

[2016-11-02 19:05:38,825]  INFO - sqltiming SELECT UM_ID FROM UM_HYBRID_ROLE WHERE UM_ROLE_NAME ='cg_publisher' AND UM_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,825]  INFO - sqlonly SELECT UM_ID FROM UM_HYBRID_ROLE WHERE UM_ROLE_NAME ='cg_unpublisher' AND UM_TENANT_ID=-1234 

[2016-11-02 19:05:38,826]  INFO - sqltiming SELECT UM_ID FROM UM_HYBRID_ROLE WHERE UM_ROLE_NAME ='cg_unpublisher' AND UM_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,827]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/ProvisioningAdminService.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,827]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/ProvisioningAdminService.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,828]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,828]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,829]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/SynapseApplicationAdmin.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,830]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/SynapseApplicationAdmin.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,830]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,831]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,833]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/MediationSecurityAdminService.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,834]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/MediationSecurityAdminService.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,834]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,836]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 2 msec}
[2016-11-02 19:05:38,837]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/CarbonAppUploader.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,838]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/CarbonAppUploader.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,838]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,839]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,839]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/OperationAdmin.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,840]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/OperationAdmin.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,840]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,840]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,841]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/GAppTenantRegistrationService.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,842]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/GAppTenantRegistrationService.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,842]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,842]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,843]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/EventPublishService.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,843]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/EventPublishService.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,844]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,844]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,845]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/SequenceAdminService.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,845]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/SequenceAdminService.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,845]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,846]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,846]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/wso2carbon-sts.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,847]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/wso2carbon-sts.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,847]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,847]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,848]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/MediationLibraryAdminService.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,849]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/MediationLibraryAdminService.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,849]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,849]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,850]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/StatisticsAdmin.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,851]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/StatisticsAdmin.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,851]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,851]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,852]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/LoggedUserInfoAdmin.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,853]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/LoggedUserInfoAdmin.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,853]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,853]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,854]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/MediationStatisticsAdmin.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,854]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/MediationStatisticsAdmin.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,854]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,855]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,856]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/TopicManagerAdminService.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,856]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/TopicManagerAdminService.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,856]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,857]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,857]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/MessageProcessorAdminService.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,858]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/MessageProcessorAdminService.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 1 msec}
[2016-11-02 19:05:38,858]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,858]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,859]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/ApplicationAdmin.flag' 
AND REG_TENANT_ID=-1234 

[2016-11-02 19:05:38,859]  INFO - sqltiming SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags/ApplicationAdmin.flag' 
AND REG_TENANT_ID=-1234 
 {executed in 0 msec}
[2016-11-02 19:05:38,860]  INFO - sqlonly SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE='/_system/config/repository/components/org.wso2.carbon.cloud.cg/flags' 
AND REG_TENANT_ID=-1234 



[1] https://code.google.com/archive/p/log4jdbc/downloads

Implementing a Custom Message Builder in WSO2 ESB 4.9.0

Message Builder's in ESB


In WSO2 ESB message builders are used to convert the message into SOAP. Whenever a message came into ESB, the receiving transport will select a message builder from axis2.xml based on the content type of the message and convert it to SOAP and it will be used throughout the mediation.

There are predefined message builders inside ESB and you can refer [1] for more details on it.

In some circumstances, we need to change the logic inside the predefined message builders to achieve some custom tasks while the message get build. Here, for example, consider that there is a message with invalid namespace configuration or malformed xml message comes into the ESB and at that time while building at axiom level it will throw exception and the message will not propagate
to synapse level. For some validation purpose or guranteed delivery we may need the message to be
get into synapse level, even when there is a exception occur at message builder.

In these cases we need to create a custom message builder to capture the exception and handling it and passing a custom payload with the original message to the synapse level. Below is the sample custom message builder, which will capture the exception while message building and send a custom payload back to synapse level. In this sample used the Builder interface to implement the custom message builder and as the expected message content type is application/xml used the same logic of the ApplicationXMLBuilder and handled the exception. Based on your need you can modify which contentType you want to handle and use the Builder interface to implement your custom use case.


When using this if you want to use this for a specific proxy service only in a JMS listener you need to first add this to axis2.xml

<messageBuilder contentType="application/test"
 class="com.custom.messagebuilder.CustomApplicationXMLMessageBuilder"/>

Then add the content type parameter in the proxy as below:

<parameter name="transport.jms.ContentType">application/test</parameter>

The Java Sample Message Builder


package com.custom.messagebuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.io.StringWriter;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.OMNodeEx;
import org.apache.axiom.om.impl.builder.StAXBuilder;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axiom.om.util.StAXParserConfiguration;
import org.apache.axiom.om.util.StAXUtils;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.builder.Builder;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.Constants;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/* This is a custom message builder to escape the build error's at axis2 level due to invalid messages and forward a custom error message with the actual message as value for <originalMessage>
 * tag to the synapse level. 
 * */

public class CustomApplicationXMLMessageBuilder implements Builder {

 private static final Log log = LogFactory
   .getLog(CustomApplicationXMLMessageBuilder.class);

 private final XMLInputFactory inputFactory = XMLInputFactory.newInstance();

 public CustomApplicationXMLMessageBuilder() {
  inputFactory.setProperty("javax.xml.stream.supportDTD", Boolean.FALSE);
  inputFactory.setProperty(
    "javax.xml.stream.isReplacingEntityReferences", Boolean.FALSE);
  inputFactory.setProperty(
    "javax.xml.stream.isSupportingExternalEntities", Boolean.FALSE);
 }

 public OMElement processDocument(InputStream inputStream,
   String contentType, MessageContext messageContext) throws AxisFault {

  if (log.isInfoEnabled()) {
   log.info("[CustomApplicationXMLMessageBuilder]" + "- Invoked");
   log.info("[CustomApplicationXMLMessageBuilder]"
     + "- Content Type Received: " + contentType);
  }
  // Cloning the input stream to get the original message into exception.
  byte[] byteArray = null;
  try {
   byteArray = IOUtils.toByteArray(inputStream);
  } catch (IOException e2) {
   e2.printStackTrace();
  }

  InputStream input1 = new ByteArrayInputStream(byteArray);
  InputStream input2 = new ByteArrayInputStream(byteArray);

  SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
  SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope();

  if (input1 != null) {
   if (log.isInfoEnabled()) {
    log.info("[CustomApplicationXMLMessageBuilder] Inputstream not null.");
   }
   try {
    PushbackInputStream pushbackInputStream = new PushbackInputStream(
      input1);
    int b;
    if ((b = pushbackInputStream.read()) > 0) {
     pushbackInputStream.unread(b);
     javax.xml.stream.XMLStreamReader xmlReader;

     if ("true"
       .equals(messageContext
         .getProperty(Constants.Configuration.APPLICATION_XML_BUILDER_ALLOW_DTD))
       || ((messageContext
         .getParameter(Constants.Configuration.APPLICATION_XML_BUILDER_ALLOW_DTD) != null) && ("true"
         .equals(messageContext
           .getParameter(
             Constants.Configuration.APPLICATION_XML_BUILDER_ALLOW_DTD)
           .getValue())))) {
      xmlReader = inputFactory
        .createXMLStreamReader(
          pushbackInputStream,
          (String) messageContext
            .getProperty(Constants.Configuration.CHARACTER_SET_ENCODING));
     } else {
      xmlReader = StAXUtils
        .createXMLStreamReader(
          StAXParserConfiguration.SOAP,
          pushbackInputStream,
          (String) messageContext
            .getProperty(Constants.Configuration.CHARACTER_SET_ENCODING));
     }
     // Handling the exception thrown, when a invalid message received to StAXOMBuilder
     try {
      
      StAXBuilder builder = new StAXOMBuilder(xmlReader);
      OMNodeEx documentElement = (OMNodeEx) builder
        .getDocumentElement();
      documentElement.setParent(null);
      SOAPBody body = soapEnvelope.getBody();
      body.addChild(documentElement);
      
     } catch (Exception e) {
      if (log.isWarnEnabled()) {
       log.warn("[CustomApplicationXMLMessageBuilder] Exception thrown due to Invalid Message and creating a custom message to forward to synapse level.");
      }
      
      // Reading the original message by copying the input stream to writer
      StringWriter writer = new StringWriter();
      String originalPayload;
      IOUtils.copy(input2, writer);
      originalPayload = writer.toString();
      
      if (log.isDebugEnabled()) {
       log.debug("[CustomApplicationXMLMessageBuilder] Original Payload Received at Input Stream: "
         + originalPayload);
      }
      
      //Appending the custom payload to the soap body
      SOAPBody body = soapEnvelope.getBody();
      OMElement documentElementInvalidMessage = null;
      OMElement documentElementOriginalPayload = null;
      documentElementInvalidMessage = AXIOMUtil.stringToOM("<messageType>" + "INVALID_MESSAGE" + "</messageType>");
      documentElementOriginalPayload = AXIOMUtil.stringToOM("<originalMessage>"+ "<![CDATA[" + originalPayload + "]]>" +"</originalMessage>");           
      body.addChild(documentElementInvalidMessage);
      body.addChild(documentElementOriginalPayload); 
      
     }
    }
   } catch (XMLStreamException e) {
    if (log.isWarnEnabled()) {
     log.warn("[CustomApplicationXMLMessageBuilder]"
       + "- Entered XML Stream Exception.");
    }
    throw AxisFault.makeFault(e);
   } catch (IOException e) {
    if (log.isWarnEnabled()) {
     log.warn("[CustomApplicationXMLMessageBuilder]"
       + "- Entered IO Exception.");
    }
    throw AxisFault.makeFault(e);
   }
  }
  if (log.isInfoEnabled()) {
   log.info("[CustomApplicationXMLMessageBuilder]"
     + "- Returned SOAP Envelope: " + soapEnvelope);
  }
  return soapEnvelope;
 }
}


The pom.xml


<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>
 <groupid>CustomApplicationXMLMessageBuilder</groupid>
 <artifactid>CustomApplicationXMLMessageBuilder</artifactid>
 <version>0.0.1</version>
 <name>CustomApplicationXMLMessageBuilder</name>
 <dependencies>
  <dependency>
   <groupid>org.apache.axis2.wso2</groupid>
   <artifactid>axis2</artifactid>
   <version>1.6.1.wso2v14</version>
  </dependency>
  <dependency>
   <groupid>org.apache.ws.commons.axiom.wso2</groupid>
   <artifactid>axiom</artifactid>
   <version>1.2.11.wso2v6</version>
  </dependency>
  <dependency>
   <groupid>commons-logging</groupid>
   <artifactid>commons-logging</artifactid>
   <version>1.1.1</version>
  </dependency>
  <dependency>
   <groupid>org.apache.commons</groupid>
   <artifactid>commons-io</artifactid>
   <version>1.3.2</version>
  </dependency>
 </dependencies>
 <repositories>
  <repository>
   <releases>
    <enabled>true</enabled>
    <updatepolicy>daily</updatepolicy>
    <checksumpolicy>ignore</checksumpolicy>
   </releases>
   <id>wso2-nexus</id>
   <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
  </repository>
 </repositories>
 <pluginrepositories>
  <pluginrepository>
   <releases>
    <enabled>true</enabled>
    <updatepolicy>daily</updatepolicy>
    <checksumpolicy>ignore</checksumpolicy>
   </releases>
   <id>wso2-nexus</id>
   <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
  </pluginrepository>
 </pluginrepositories>
 <build>
  <plugins>
   <plugin>
    <artifactid>maven-eclipse-plugin</artifactid>
    <version>2.9</version>
    <configuration>
     <buildcommands>
      <buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
     </buildcommands>
     <projectnatures>
      <projectnature>org.wso2.developerstudio.eclipse.artifact.mediator.project.nature</projectnature>
      <projectnature>org.eclipse.jdt.core.javanature</projectnature>
     </projectnatures>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>






Tuesday, November 1, 2016

Creating a Mock Service to return HTTP Status Code with Custom Status Code Description using Tomcat / Servlet

In some cases, we may need to setup a mock service to return HTTP status codes with custom messages. You can follow the below to achieve this.


The Sample Servlet


package com.custom;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CustomStatus
 */
public class CustomStatus extends HttpServlet {
 private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CustomStatus() {
        super();
        // TODO Auto-generated constructor stub
    }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.setContentType("text/plain");
  response.setHeader("FROM", "AJANTH");
  if(request.getParameter("statusToReturn").equals("4001")){ 
   
   response.setStatus(400, "Invalid Request Parameters");
   
  }else if(request.getParameter("statusToReturn").equals("4002")){ 
   
   response.setStatus(400, "Invalid Request for Bad Gateway");   
  } 
  
  response.getWriter().write("Test From BackEND");
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }

}


Configuration Need to do at Tomcat


In TOMCAT_HOME/conf/catalina.properties add the below lines:

org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER = true

Now when you send request through SOAP UI, you will see the below, where the header appended with our custom message.