Friday, October 28, 2016

Smooks Data Mapping with WSO2 ESB

This post will contain detailed instructions on how to do a XML to XML transformation using WSO2 ESB 4.9.0. WSO2 Developer Studio 3.8.0 is used to create the project.  

Create Maven Multi Module Project

Create Maven Multi Module Project with following details. This is the parent project for all the other projects.
  • Group Id - com.wso2.training.SmooksDataMapper 
  • ArtifactID - SmooksDataMapperLab 



Create Registry Resource Project

  • To create a Registry Resources Project, go to File -> New -> Projects -> WSO2 -> Repository and select Registry Resources Project.
    • Name : SmooksRegistryProject
    • Group ID :com.wso2.training.SmooksRegistryProject
  • Create a new Registry Resource for Smooks Configuration. Select From Existing Template option and select the template as Smooks Configuration.
    • ResourceName : SmooksConfigXMLToXML
    • Template : Smooks Configuration
    • Registry : Conf
    • Registry Path :/system/config/smooks 

  • Now you will see smooks-configuration file has been created and it will be open in the embedded JBoss Smooks editor.

Create a Composite Application Project
  • Create CAR project
    • Name : SmooksDataMapperCAR
    • Group ID : com.wso2.training.SmooksDataMapperCAR
    • Tick specify Parent from Wokspace
  • Add following two files there under a folder.
model-input.xml

<order id='444'>
    <header>
           <customer number="555">Amila</customer>
    </header>
    <order-items>
       <order-item id='1'>
           <product>1</product>
           <quantity>2</quantity>
           <price>400</price>
           </order-item>
    </order-items>
</order>


model-output.xml

<salesorder>
 <details>
       <orderid></orderid>
       <customer>
           <id></id>
           <name></name>
       </customer>
    </details>
    <itemList>
       <item>
           <id></id>
           <productId></productId>
           <quantity></quantity>
           <price></price>
       </item>
    </itemList>
</salesorder>

 Define Smooks Configuration
  • Go to Smooks Configuration
    • Set Input type as xml
    • Add the input model with data(model-input.xml) by browsing the workspace
         That will automatically create a input model in input model view

  • Then click on the add button on Input Task and then click on apply template

  • set Message Type as XML and click on Next
  • Set “Create Template From:” as Sample XML & point the mapping model(model-output.xml) from workspace and  then click on finish.
 
  • This will dynamically generate the graphical model as follows.

  • Configure Mapping

You can do the mapping by just connecting relevant elements or attributes by clicking on one element on the input model and connecting it to the corresponding element in the output model as shown below. Ensure to do the mapping from top to bottom or else the configuration will fail at run time. So do the mapping exactly as shown below.


 Run Smooks Configuration
  • Add libraries from smooks framework. To add these libraries select the Registry Resources project and go to Project -> Properties.
  • Select Java Build Path and go to Libraries tab.
  • Click on Add Library button and you will see Add Library page which has WSO2 Classpath Libraries entry at the bottom.
  • Select WSO2 Classpath Libraries and click Next.
  • Now you will see a multi-tab page which includes most of the third party libraries that will be needed for your SOA development. To add smooks related libraries, go to Smooks tab.

  • Select all the libraries and click Finish.
  • Now you will see, all the smooks related libraries are added to project classpath.
  • Now you can run smooks configuration file by right click on the file (SmooksConfigXMLToXML.xml) and select Run As -> Smooks Run Configuration 
  • If your mapping was successful it will create the output as below.
 
 Create ESB Proxy Service
  •  Create a new ESB config Project
    • Name : SmooksTransESBProject
    • Group Id : com.wso2.training.SmooksTransESBProject
    • Select Specify Parent from Workspace
  • Create  new proxy service within that ESB config project
    • Proxy Service Name : SmooksTransProxy
    • Proxy Service Type : Custom Proxy 
  • Add mediators to the inSequence of the proxy as follows.
    • Log Meidator - Log Level : Full
    • Smooks Mediator -  Configuration Key : SmooksConfigXMLToXML.xml (Select by browsing the workspace)
    • Log Mediator - Log Level : Full    
    • Add respond Mediator
       
Deploying the CAR file
  • Go to the pom.xml of the Composite Application Project (SmooksDataMapperCAR)
  • Select SmooksRegistryProject and SmooksTransESBProject as the dependencies.
  • Right clieck on CAR project -> Export Composite Application and give a Export Destination
  • Then go to ESB management console and upload the CAR file.
Test the Service

Test using try it tool and give the input as follows.

<order id='764'>
    <header>
           <customer number="444">James</customer>
    </header>
    <order-items>
       <order-item id='7'>
           <product>6</product>
           <quantity>8</quantity>
           <price>543</price>
       </order-item>
    </order-items>
</order>


References : http://wso2.com/library/tutorials/2011/06/perform-data-mapping-smooks-editor-wso2-carbon-studio/

Friday, October 21, 2016

How to Limit the WSO2 Service Log File Size

Limit the file size of wso2carbon.log

By default wso2carbon.log file is rotated on daily basis. But if we want to rotate the log files based on file size we can do as follows.

  • Change the log4j.appender.CARBON_LOGFILE appender in the /repository/conf/log4j.properties file as follows. 

log4j.appender.CARBON_LOGFILE=org.apache.log4j.RollingFileAppender

  • Add following two properties under the RollingFileAppender. You can change the MaxFileSize and MaxBackupIndex as Required.

log4j.appender.CARBON_LOGFILE.MaxFileSize=10MB 
log4j.appender.CARBON_LOGFILE.MaxBackupIndex=20


MaxFileSize = Maximum allowed file size (in bytes) before rolling over. Suffixes "KB", "MB" and "GB" are allowed.
MaxBackupIndex  = Maximum number of backup files to keep
  • Now the configuration looks like below

# CARBON_LOGFILE is set to be a DailyRollingFileAppender using a PatternLayout.
#log4j.appender.CARBON_LOGFILE=org.wso2.carbon.utils.logging.appenders.CarbonDailyRollingFileAppender
log4j.appender.CARBON_LOGFILE=org.apache.log4j.RollingFileAppender
# Log file will be overridden by the configuration setting in the DB
# This path should be relative to WSO2 Carbon Home
log4j.appender.CARBON_LOGFILE.File=${carbon.home}/repository/logs/${instance.log}/wso2carbon${instance.log}.log
log4j.appender.CARBON_LOGFILE.Append=true
log4j.appender.CARBON_LOGFILE.layout=org.wso2.carbon.utils.logging.TenantAwarePatternLayout
# ConversionPattern will be overridden by the configuration setting in the DB
log4j.appender.CARBON_LOGFILE.layout.ConversionPattern=TID: [%T] [%S] [%d] %P%5p {%c} - %x %m {%c}%n
log4j.appender.CARBON_LOGFILE.layout.TenantPattern=%U%@%D [%T] [%S]
log4j.appender.CARBON_LOGFILE.threshold=DEBUG
log4j.appender.CARBON_LOGFILE.MaxFileSize=10MB
log4j.appender.CARBON_LOGFILE.MaxBackupIndex=20

If the size of the log file is exceeding the value defined in the MaxFileSize property, then the content is copied to a backup file and the logs are continued to be added to a new empty log file.For example if MaxBackupIndex is two you will see 3 files maximum. The wso2carbon.log.2 log file contains the oldest logs and wso2carbon.log contains the latest logs.


wso2carbon.log.2
wso2carbon.log.1
wso2carbon.log

Disable Http Access Logs

HTTP Requests/Responses are logged in the access log(s) and are helpful to monitor your application's usage activities, such as the persons who access it, how many hits it receives, what the errors are etc. This information is useful for troubleshooting. As the runtime of WSO2 products are based on Apache Tomcat, you can use the AccessLogValve variable in Tomcat to configure HTTP access logs in WSO2 products.
The /repository/conf/tomcat/catalina-server.xml contains the valve related to the access logs as below. If you want to disable access logs comment out the below configuration.


<Valve className="org.apache.catalina.valves.AccessLogValve" directory="${carbon.home}/repository/logs"
 prefix="http_access_" suffix=".log"
pattern="combined"/>

Rotate Http Access Logs

We can achieve this by using "fileDateFormat". It allows a customized timestamp in the access log file name. The file is rotated whenever the formatted timestamp changes. The default value is yyyy-MM-dd. We can change it to rotate every hour by adding fileDateFormat to catalina-server.xml as follows. Tomcat itself does not do any housekeeping on the old files, so it is possible to have a cron job set up on the system to archive older files into a backup directory and/or delete them.

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="${carbon.home}/repository/logs"
prefix="http_access_" suffix=".log"
pattern="combined" fileDateFormat="yyyy-MM-dd.HH"/>

Thursday, October 20, 2016

Change the Log File Location of WSO2 ESB

Lets say we need to change log file location to "/home/services/wso2logs" instead of default /repository/logs folder. 
  • Change following file locations in /repository/conf/log4j.properties as follows.
log4j.appender.SERVICE_APPENDER.File=/home/services/wso2logs/${instance.log}/wso2-esb-service${instance.log}.log
log4j.appender.TRACE_APPENDER.File=/home/services/wso2logs/${instance.log}/wso2-esb-trace${instance.log}.log
log4j.appender.CARBON_LOGFILE.File=/home/services/wso2logs/${instance.log}/wso2carbon${instance.log}.log
log4j.appender.ERROR_LOGFILE.File=/home/services/wso2logs/${instance.log}/wso2-esb-errors.log
log4j.appender.AUDIT_LOGFILE.File=/home/services/wso2logs/audit.log
log4j.appender.ATOMIKOS.File = /home/services/wso2logs/tm.out

  • Change below value in  /repository/conf/tomcat/catalina-server.xml as follows to change http_access_management_console.log file location.
Valve className="org.apache.catalina.valves.AccessLogValve" directory="/home/services/wso2logs/"

  • We still have patches.log file and empty wso2carbon.log and wso2carbon-trace-messages.log in the default location. Since patch application process is done even before the carbon server is started those log configuration file is located within org.wso2.carbon.server-.jar file. So as a workaround to move these log files we can open the log4j.properties file within /lib/org.wso2.carbon.server-.jar and change the below properties as follows.
log4j.appender.CARBON_LOGFILE.File/home/services/wso2logs/${instance.log}/wso2carbon${instance.log}.log
log4j.appender.CARBON_TRACE_LOGFILE.File=/home/services/wso2logs/${instance.log}/wso2carbon-trace-messages${instance.log}.log
log4j.appender.CARBON_PATCHES_LOGFILE.File=/home/services/wso2logs/${instance.log}/patches.log

  • Make sure write permission is there for the new location and restart the service. Now all the log files are created in the new location. 

WSO2 ESB Log Files Explained

WSO2 ESB is generating several log files during its operation. This post will describe the usage of these logs along with some example logs. I am running the ESB 4.9.0 with the below proxy service for generate these sample logs. By default all these log files will be created at the location /repository/logs/ folder.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="TestProxy"
       transports="http,https"
       statistics="disable"
       trace="enable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log level="custom">
            <property name="TEST" value="RECV"/>
         </log>
         <loopback/>
      </inSequence>
      <outSequence>
         <payloadFactory media-type="xml">
            <format>
               <test xmlns="">5</test>
            </format>
            <args/>
         </payloadFactory>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>
         

WSO2 Carbon log

This is the main log file of the service and named as wso2carbon.log. It contains all the server level and service level logs. The default log level is INFO and it can be changed by changing below property in log4j.properties file to desired level.

log4j.logger.org.wso2.carbon=INFO

Trace log

It offers you a way to monitor a mediation execution and named as wso2-esb-trace.log. Trace logs will be enabled only if tracing is enabled on particular proxy or a sequence. If we enable tracing for the above proxy by using the management console we will get below.


13:54:03,490 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Proxy Service TestProxy received a new message from : 10.6.19.64
13:54:03,491 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Message To: /services/TestProxy.TestProxyHttpEndpoint/mediate
13:54:03,491 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER SOAPAction: null
13:54:03,491 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER WSA-Action: null
13:54:03,492 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Setting default fault-sequence for proxy
13:54:03,493 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Using the anonymous in-sequence of the proxy service for mediation
13:54:03,493 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Start : Sequence <anonymous>
13:54:03,494 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Sequence <SequenceMediator> :: mediate()
13:54:03,494 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Mediation started from mediator position : 0
13:54:03,494 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Start : Log mediator
13:54:03,496 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER TEST = RECV
13:54:03,496 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER End : Log mediator
13:54:03,496 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Start : Loopback Mediator
13:54:03,496 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Setting default fault-sequence for proxy
13:54:03,496 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Start : Sequence <anonymous>
13:54:03,496 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Sequence <SequenceMediator> :: mediate()
13:54:03,496 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Mediation started from mediator position : 0
13:54:03,496 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Building message. Sequence <SequenceMediator> is content aware
13:54:03,518 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Start : Send mediator
13:54:03,519 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER Sending response message using implicit message properties..
Sending To: null
SOAPAction: null
13:54:03,529 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER End : Send mediator
13:54:03,529 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER End : Sequence <anonymous>
13:54:03,529 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER End : Loopback Mediator
13:54:03,529 [-] [PassThroughMessageProcessor-1]  INFO TRACE_LOGGER End : Sequence <anonymous>
13:54:45,755 [-] [http-nio-9443-exec-18]  INFO TRACE_LOGGER Building Axis service for Proxy service : TestProxy
13:54:45,755 [-] [http-nio-9443-exec-18]  INFO TRACE_LOGGER Loading the WSDL :  <Inlined>
13:54:45,755 [-] [http-nio-9443-exec-18]  INFO TRACE_LOGGER Did not find a WSDL. Assuming a POX or Legacy service
13:54:45,755 [-] [http-nio-9443-exec-18]  INFO TRACE_LOGGER Exposing transports : [http, https]
13:54:45,757 [-] [http-nio-9443-exec-18]  INFO TRACE_LOGGER Adding service TestProxy to the Axis2 configuration
13:54:45,770 [-] [http-nio-9443-exec-18]  INFO TRACE_LOGGER Successfully created the Axis2 service for Proxy service : TestProxy

Patches log

All patch related logs are recorded in the Patches.log file. 

[2016-10-19 13:38:25,951]  INFO {org.wso2.carbon.server.util.PatchUtils} -  Checking for patch changes ...
[2016-10-19 13:38:25,953]  INFO {org.wso2.carbon.server.util.PatchUtils} -  New patch available - patch0049
[2016-10-19 13:38:25,957]  INFO {org.wso2.carbon.server.extensions.PatchInstaller} -  Patch changes detected 
[2016-10-19 13:38:31,949]  INFO {org.wso2.carbon.server.util.PatchUtils} -  Backed up plugins to patch0000
[2016-10-19 13:38:31,949]  INFO {org.wso2.carbon.server.util.PatchUtils} -  Applying patches ...
[2016-10-19 13:38:31,951]  INFO {org.wso2.carbon.server.util.PatchUtils} -  Applying - patch0049
[2016-10-19 13:38:31,966]  INFO {org.wso2.carbon.server.util.PatchUtils} -  Patched org.wso2.carbon.inbound.endpoint_4.4.10.jar(MD5:6f76773b9d10b200e8b7786a2787fc96)
[2016-10-19 13:38:31,996]  INFO {org.wso2.carbon.server.util.PatchUtils} -  Patched synapse-nhttp-transport_2.1.3-wso2v11.jar(MD5:907846d87999fbbc4ba8ac0381e97822)
[2016-10-19 13:38:31,997]  INFO {org.wso2.carbon.server.util.PatchUtils} -  Patch verification started
[2016-10-19 13:38:32,019]  INFO {org.wso2.carbon.server.util.PatchUtils} -  Patch verification successfully completed

Audit log

Audit logs provide information related to the users who has tried to access the server and do changes. It is recommended to enable audit logs in the production environment and it is named as audit.log.


[2016-10-19 13:44:43,522]  INFO -  'admin@carbon.super [-1234]' logged in at [2016-10-19 13:44:43,520-0500] 
[2016-10-19 13:45:14,190]  INFO -  'admin@carbon.super [-1234]' logged out at [2016-10-19 13:45:14,0188] 
[2016-10-19 13:46:14,976]  WARN -  Failed Administrator login attempt 'testuser[-1234]' at [2016-10-19 13:46:14,971-0500] 
[2016-10-19 13:47:49,084]  INFO -  'admin@carbon.super [-1234]' logged in at [2016-10-19 13:47:49,081-0500] 
[2016-10-19 13:48:29,501]  INFO -  Initiator : null@carbon.super | Action : Change Password by User | Target : null@carbon.super | Data : {  } | Result : Failed  

Error log

All Errors and warnings at the server can be found in this log file named as wso2-esb-errors.log


2016-10-19 13:38:36,504 [-] [Start Level Event Dispatcher]  WARN ValidationResultPrinter Carbon is configured to use the default keystore (wso2carbon.jks). To maximize security when deploying to a production environment, configure a new keystore with a unique password in the production server profile.
2016-10-19 13:38:54,768 [-] [localhost-startStop-1]  WARN DefaultSchemaGenerator We don't support method overloading. Ignoring [validateAudienceRestriction]
2016-10-19 13:46:14,971 [-] [http-nio-9443-exec-32]  WARN CarbonAuthenticationUtil Failed Administrator login attempt 'tet[-1234]' at [2016-10-19 13:46:14,971-0500]
2016-10-19 13:48:29,515 [-] [http-nio-9443-exec-16] ERROR UserAdminClient Error while updating password. Wrong old credential provided 
org.wso2.carbon.user.mgt.stub.UserAdminUserAdminException: UserAdminUserAdminException
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
 at java.lang.Class.newInstance(Class.java:442)

ESB Service log

Informaton related to all service invocations information about called services etc and all logs that have been made with the "Log" Mediator in Synapse Proxies or Sequences can be found here.  By default INFO log level is used the file is named as wso2-esb-service.log

2016-10-19 14:50:09,209 [-] [localhost-startStop-1]  INFO TestProxy Building Axis service for Proxy service : TestProxy
2016-10-19 14:50:09,212 [-] [localhost-startStop-1]  INFO TestProxy Adding service TestProxy to the Axis2 configuration
2016-10-19 14:50:09,226 [-] [localhost-startStop-1]  INFO TestProxy Successfully created the Axis2 service for Proxy service : TestProxy
2016-10-19 14:55:06,491 [-] [PassThroughMessageProcessor-1]  INFO TestProxy TEST = RECV

Access Logs

Service and REST API invocation access log

tracks when a service or REST API is invoked. By default, the service/API invocation access logs are disabled for performance reasons. Can be enable by using below. The log file is created with the name as http_access_YYYY_MM_DD.log.

log4j.logger.org.apache.synapse.transport.http.access=INFO

If you invoke the service using below url we can see the logs as follows.



- fe80:0:0:0:d84a:683c:68a5:209%11 - - [19/Oct/2016:12:59:03 -0600] "GET /services/TestProxy.TestProxyHttpEndpoint/mediate HTTP/1.1" - - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"
- fe80:0:0:0:d84a:683c:68a5:209%11  - [19/Oct/2016:12:59:03 -0600] "- - " 200 - "-" "-" 

Management Console Access Log

This tracks usage of the Management Console. By default it is created as http_access_management_console_YYYY_MM_DD.log file. It is rotated on a daily basis. To change the configurations of this log need to change below value in /repository/conf/tomcat/catalina-server.xml

<Valve className="org.apache.catalina.valves.AccessLogValve">


0:0:0:0:0:0:0:1 - - [19/Oct/2016:13:29:14 -0500] "GET /carbon/admin/js/jquery.ui.tabs.min.js HTTP/1.1" 200 3594 "https://localhost:9443/carbon/admin/login.jsp" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"
0:0:0:0:0:0:0:1 - - [19/Oct/2016:13:29:14 -0500] "GET /carbon/admin/js/cookies.js HTTP/1.1" 200 1136 "https://localhost:9443/carbon/admin/login.jsp" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"
0:0:0:0:0:0:0:1 - - [19/Oct/2016:13:29:14 -0500] "GET /carbon/admin/js/jquery.ui.core.min.js HTTP/1.1" 200 1996 "https://localhost:9443/carbon/admin/login.jsp" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"

Trace Messages log

By default trace message logs are not enabled in the ESB. You can enable it for any required component by changing the log4j.properties file. For example trace for org.apache.synapse.transport.http.headers can be enabled by adding below. The logs will be created at wso2carbon-trace-messages.log file.

log4j.appender.CARBON_TRACE_LOGFILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CARBON_TRACE_LOGFILE.File=${carbon.home}/repository/logs/${instance.log}/wso2carbon-trace-messages${instance.log}.log
log4j.appender.CARBON_TRACE_LOGFILE.Append=true
log4j.appender.CARBON_TRACE_LOGFILE.layout=org.wso2.carbon.utils.logging.TenantAwarePatternLayout
log4j.appender.CARBON_TRACE_LOGFILE.layout.ConversionPattern=[%d] %P%5p {%c} - %x %m %n
log4j.appender.CARBON_TRACE_LOGFILE.layout.TenantPattern=%U%@%D [%T] [%S]
log4j.appender.CARBON_TRACE_LOGFILE.threshold=TRACE
log4j.logger.org.apache.synapse.transport.http.headers=TRACE,CARBON_TRACE_LOGFILE

The generated log file contains below.

[2016-10-19 14:43:47,286] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 >> POST /services/TestProxy.TestProxyHttpEndpoint/mediate HTTP/1.1 
[2016-10-19 14:43:47,288] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 >> Content-Type: application/xml; charset=UTF-8 
[2016-10-19 14:43:47,289] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 >> Cookie: region3_registry_menu=visible; region1_configure_menu=none; region4_monitor_menu=none; region5_tools_menu=none; MSG14769029100080.6387579241161663=true; menuPanel=visible; menuPanelType=main; requestedURI="../../carbon/service-mgt/index.jsp?region=region1&item=services_list_menu"; current-breadcrumb=manage_menu%2Cservices_menu%2Cservices_list_menu%23; JSESSIONID=9AEF469AF1960BA436D77E2B6F9BADEC 
[2016-10-19 14:43:47,297] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 >> User-Agent: Axis2 
[2016-10-19 14:43:47,305] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 >> Host: OMISLABL9:8280 
[2016-10-19 14:43:47,309] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 >> Transfer-Encoding: chunked 
[2016-10-19 14:43:47,423] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 << HTTP/1.1 200 OK 
[2016-10-19 14:43:47,424] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 << Cookie: region3_registry_menu=visible; region1_configure_menu=none; region4_monitor_menu=none; region5_tools_menu=none; MSG14769029100080.6387579241161663=true; menuPanel=visible; menuPanelType=main; requestedURI="../../carbon/service-mgt/index.jsp?region=region1&item=services_list_menu"; current-breadcrumb=manage_menu%2Cservices_menu%2Cservices_list_menu%23; JSESSIONID=9AEF469AF1960BA436D77E2B6F9BADEC 
[2016-10-19 14:43:47,427] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 << Host: OMISLABL9:8280 
[2016-10-19 14:43:47,434] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 << Content-Type: application/xml; charset=UTF-8; charset=UTF-8 
[2016-10-19 14:43:47,440] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 << Date: Wed, 19 Oct 2016 19:43:47 GMT 
[2016-10-19 14:43:47,446] DEBUG {org.apache.synapse.transport.http.headers} -  http-incoming-1 << Transfer-Encoding: chunked 

Transaction logs

A transaction is a set of operations that executed as a single unit.WSO2 carbon platform has integrated the "Atomikos" transaction manager which is a implementation of Java Transaction API (JTA). Some products like WSO2DSS, WSO2ESB shipped this transaction manager by default. Then infomation related to Atomikos are logged in tm.out file.

INFO Start Level Event Dispatcher com.atomikos.logging.LoggerFactory - Using Slf4J for logging.
WARN Start Level Event Dispatcher com.atomikos.icatch.config.UserTransactionServiceImp - Using init file: /C:/Users/apathira/Desktop/WEST/Runtime/LOGGIN~1/WSO2ES~1.0/WSO2ES~1.0/bin/../lib/transactions.properties
INFO Start Level Event Dispatcher com.atomikos.persistence.imp.FileLogStream - Starting read of logfile C:\Users\apathira\Desktop\WEST\Runtime\LOGGIN~1\WSO2ES~1.0\WSO2ES~1.0\repository\data\tmlog12.log
INFO Start Level Event Dispatcher com.atomikos.persistence.imp.FileLogStream - Done read of logfile
INFO Start Level Event Dispatcher com.atomikos.persistence.imp.FileLogStream - Logfile closed: C:\Users\apathira\Desktop\WEST\Runtime\LOGGIN~1\WSO2ES~1.0\WSO2ES~1.0\repository\data\tmlog12.log
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING core version: 3.8.0
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.console_file_name = tm.out
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.console_file_count = 1
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.automatic_resource_registration = true
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.client_demarcation = false
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.threaded_2pc = false
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.serial_jta_transactions = true
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.log_base_dir = repository/data
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.console_log_level = INFO
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.max_actives = 50
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.checkpoint_interval = 500
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.enable_logging = true
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.output_dir = repository/logs
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.log_base_name = tmlog
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.console_file_limit = 1073741824
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.max_timeout = 8000000
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.tm_unique_name = 10.6.19.64.tm
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING java.naming.factory.initial = com.sun.jndi.rmi.registry.RegistryContextFactory
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING java.naming.provider.url = rmi://localhost:1099
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.service = com.atomikos.icatch.standalone.UserTransactionServiceFactory
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.force_shutdown_on_vm_exit = true
INFO Start Level Event Dispatcher com.atomikos.icatch.config.imp.AbstractUserTransactionService - USING com.atomikos.icatch.default_jta_timeout = 5000000


Monday, October 17, 2016

How to use Class Mediator in WSO2 ESB

The Class Mediator creates an instance of a custom-specified class and sets it as a mediator. Use the Class mediator for user-specific, custom developments only when there is no built-in mediator that already provides the required functionality.

Creating a Class Mediator

There are two ways you can create a class mediator:
  •     Implement the mediator interface
  •     Extend AbstractMediator class

We can use WSO2 Developer Studio to Create class mediator easily.

Create a New Mediator Project By selecting File-> New Project -> WSO2 -> Extensions -> Project Types -> Mediator Project






It will create the class mediator by extending the AbstractMediator. Then we need to implement the mediate method which is invoked when the mediator is executed at the mediation flow.  The return statement of the mediate method decides whether the mediation flow should continue further or not.

MessageContext is the representation for a message within the ESB message flow. In the mediate method we get access to the message context. From the message context we can access message payload, message headers, properties, ESB configurations, etc. We can also set parameters to the mediator.

Sample Class mediator implementation is as follows.

package org.wso2.test;

import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;

import org.apache.synapse.MessageContext; 
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class SampleClassMediator extends AbstractMediator { 

    public String classProperty;
    
    public boolean mediate(MessageContext context) { 
        //Mediator Property
        System.out.println("------Mediator Property-------");
        System.out.println("Using Property :  " + classProperty);
        
        //Accessing Message Properties
        System.out.println("------Message Properties-------");
        String prop = (String) context.getProperty("StringProp");
        System.out.println("Using msg Prop : "+ prop);
        
        //Accessing Axis2 Properties
        System.out.println("------Axis2 Properties-------");
        Map<String,Object> mapAxis2Properties = ((Axis2MessageContext)context).getAxis2MessageContext().getProperties();
        for (Map.Entry entry : mapAxis2Properties.entrySet()) {
            System.out.println("AXIS:"+entry.getKey() + ", " + entry.getValue());
        }
        
        //Accessing Transport Headers
        System.out.println("------Transport Headers-------");
        Map<String,Object> mapTransportProperties = (Map<String, Object>) ((Axis2MessageContext)context).getAxis2MessageContext().getProperty("TRANSPORT_HEADERS");
        for (Map.Entry entry : mapTransportProperties.entrySet()) {
            System.out.println("TRANS:" +entry.getKey() + ", " + entry.getValue());
        }
        
        //Accessing Syanpse Configuration properties
        System.out.println("------Synapse Config Properties-------");
        Properties p = ((Axis2MessageContext)context).getEnvironment().getSynapseConfiguration().getProperties();
        Enumeration keys = p.keys();
        while(keys.hasMoreElements()){
            String key = (String)keys.nextElement();
            String value = (String)p.get(key);
            System.out.println("PRP: "+ key + " | " + value);
        }
        
        //Accessing Soap envelop
        System.out.println("------Soap envelop-------");
        System.out.println("MSG : " + context.getEnvelope());
        return true; 
    }
    
    public String getClassProperty() {
        return classProperty;
    }
    
    public void setClassProperty(String propValue) {
        this.classProperty = propValue;
    }
}



After implementing the method export the project as .jar file and copy to /repository/components/lib folder and restart the ESB.

Creating the Proxy service

You can use the implemented class mediator within a proxy service as follows.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="TestClassMedProxy"
       transports="http,https"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log level="custom">
            <property name="BEGIN" value="Begin Sequence"/>
         </log>
         <property name="StringProp"
                   value="&#34;TestString&#34;"
                   scope="default"
                   type="STRING"/>
         <class name="org.wso2.test.SampleClassMediator">
            <property name="classProperty" value="5"/>
         </class>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>
                                


Then send a request to the proxy service and you will see following output in the consloe as we are printing to the console.

[2016-10-18 07:03:27,414]  INFO - LogMediator BEGIN = Begin Sequence
------Mediator Property-------
Using Property :  5
------Message Properties-------
Using msg Prop : "TestString"
------Axis2 Properties-------
AXIS:addressing.validateAction, false
AXIS:local_WSO2_WSAS, org.wso2.carbon.core.init.CarbonServerManager@182ab504
AXIS:CARBON_TASK_JOB_METADATA_SERVICE, org.wso2.carbon.task.JobMetaDataProviderServiceHandler@4d72eb52
AXIS:file.resource.map, {}
AXIS:CARBON_TASK_MANAGEMENT_SERVICE, org.wso2.carbon.task.TaskManagementServiceHandler@fdccc49
AXIS:mediation.event.broker, org.wso2.carbon.event.core.internal.CarbonEventBroker@51c8cdb1
AXIS:CARBON_TASK_MANAGER, org.wso2.carbon.task.TaskManager@1c87c019
AXIS:wso2tracer.msg.seq.buff, org.wso2.carbon.utils.logging.CircularBuffer@467590a0
AXIS:ContainerManaged, true
AXIS:WORK_DIR, /home/anupama/Anupama/Workspace/WSO2Products/ESB/wso2esb-4.9.0/tmp/work
AXIS:PASS_THROUGH_TRANSPORT_WORKER_POOL, org.apache.axis2.transport.base.threads.NativeWorkerPool@2fa2b2a3
AXIS:CARBON_TASK_REPOSITORY, org.apache.synapse.task.TaskDescriptionRepository@389047a3
AXIS:local_current.server.status, RUNNING
AXIS:tenant.config.contexts, {}
AXIS:CONFIGURATION_MANAGER, org.wso2.carbon.mediation.initializer.configurations.ConfigurationManager@1a35cfa0
AXIS:service.epr.map, {}
AXIS:primaryBundleContext, org.eclipse.osgi.framework.internal.core.BundleContextImpl@2506f4
AXIS:GETRequestProcessorMap, {info=org.wso2.carbon.core.transports.util.InfoProcessor@70afacd6, wsdl=org.wso2.carbon.core.transports.util.Wsdl11Processor@4a223fca, wsdl2=org.wso2.carbon.core.transports.util.Wsdl20Processor@7035d472, xsd=org.wso2.carbon.core.transports.util.XsdProcessor@44c95d1b, tryit=org.wso2.carbon.tryit.TryitRequestProcessor@490d9048, wadltryit=org.wso2.carbon.tryit.WADLTryItRequestProcessor@120a3acc, stub=org.wso2.carbon.wsdl2form.StubRequestProcessor@4c42eabb, wsdl2form=org.wso2.carbon.wsdl2form.WSDL2FormRequestProcessor@42ed57ad, wadl2form=org.wso2.carbon.tryit.WADL2FormRequestProcessor@67c70068}
------Transport Headers-------
TRANS:Content-Type, application/soap+xml; charset=UTF-8; action="urn:mediate"
TRANS:Cookie, menuPanel=visible; menuPanelType=main; region1_manage_menu=visible; JSESSIONID=4B3870641F979576800AB05FAEB9360D; requestedURI=../../carbon/admin/index.jsp; region1_configure_menu=none; region3_registry_menu=none; region4_monitor_menu=none; region5_tools_menu=none; current-breadcrumb=manage_menu%2Cservices_menu%2Cservices_list_menu%23
TRANS:Host, anupama:8280
TRANS:Transfer-Encoding, chunked
TRANS:User-Agent, Axis2
------Synapse Config Properties-------
PRP: synapse.xpath.func.extensions | org.wso2.carbon.mediation.security.vault.xpath.SecureVaultLookupXPathFunctionProvider
PRP: statistics.clean.interval | 1000
PRP: synapse.commons.json.preserve.namespace | false
PRP: resolve.root | ./.
PRP: synapse.home | .
PRP: __separateRegDef | true
PRP: synapse.global_timeout_interval | 120000
PRP: synapse.temp_data.chunk.size | 3072
PRP: statistics.clean.enable | true
PRP: synapse.observers | org.wso2.carbon.mediation.dependency.mgt.DependencyTracker
PRP: synapse.sal.endpoints.sesssion.timeout.default | 600000
PRP: synapse.carbon.ext.tenant.info | org.wso2.carbon.mediation.initializer.handler.CarbonTenantInfoConfigurator
------Soap envelop-------
MSG : <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body><test>Test Data</test></soapenv:Body></soapenv:Envelope>