Sunday, February 19, 2017

Customize the response header "Server: WSO2-PassThrough-HTTP" in WSO2

In some scenarios you want to customize the Server header which is going out from the ESB. By default inside ESB, it is setting  the Server header to WSO2-PassThrough-HTTP [1].

[1]

[2017-02-17 13:18:14,920] DEBUG - wire << "HTTP/1.1 200 OK[\r][\n]"
[2017-02-17 13:18:14,920] DEBUG - wire << "RevokedAccessToken: wV4dUQ4HGHiNbsb2zIFKmMftLqEa[\r][\n]"
[2017-02-17 13:18:14,920] DEBUG - wire << "AuthorizedUser: admin[\r][\n]"
[2017-02-17 13:18:14,920] DEBUG - wire << "Content-Type: text/html[\r][\n]"
[2017-02-17 13:18:14,920] DEBUG - wire << "Pragma: no-cache[\r][\n]"
[2017-02-17 13:18:14,920] DEBUG - wire << "Cache-Control: no-store[\r][\n]"
[2017-02-17 13:18:14,920] DEBUG - wire << "Date: Fri, 17 Feb 2017 07:48:14 GMT[\r][\n]"
[2017-02-17 13:18:14,921] DEBUG - wire << "Server: WSO2-PassThrough-HTTP[\r][\n]"
[2017-02-17 13:18:14,921] DEBUG - wire << "Transfer-Encoding: chunked[\r][\n]"
[2017-02-17 13:18:14,921] DEBUG - wire << "Connection: Keep-Alive[\r][\n]"
[2017-02-17 13:18:14,921] DEBUG - wire << "[\r][\n]"
[2017-02-17 13:18:14,923] DEBUG - wire << "0[\r][\n]"
[2017-02-17 13:18:14,923] DEBUG - wire << "[\r][\n]"

Lets assume that need to customize the server header from "WSO2-PassThrough-HTTP" to "MyServerHeader" as bellow which coming with the response.

Server: MyServerHeader

Then you need to add the http.origin-server to passthru-http.properties file located in ESB_HOME/repository/conf/ directory with customized value as bellow.


http.origin-server=MyServerHeader

Once you restart the server, the response will be changed as bellow with the changed server header.


[2017-02-17 13:34:14,867] DEBUG - wire << "HTTP/1.1 200 OK[\r][\n]"
[2017-02-17 13:34:14,867] DEBUG - wire << "Access-Control-Allow-Headers: authorization,Access-Control-Allow-Origin,Content-Type[\r][\n]"
[2017-02-17 13:34:14,867] DEBUG - wire << "Via: 1.1 vegur[\r][\n]"
[2017-02-17 13:34:14,867] DEBUG - wire << "Content-Type: text/plain; charset=utf-8[\r][\n]"
[2017-02-17 13:34:14,867] DEBUG - wire << "Date: Fri, 17 Feb 2017 08:04:14 GMT[\r][\n]"
[2017-02-17 13:34:14,867] DEBUG - wire << "Server: MyServerHeader[\r][\n]"
[2017-02-17 13:34:14,867] DEBUG - wire << "Transfer-Encoding: chunked[\r][\n]"
[2017-02-17 13:34:14,868] DEBUG - wire << "Connection: Keep-Alive[\r][\n]"
[2017-02-17 13:34:14,868] DEBUG - wire << "[\r][\n]"
[2017-02-17 13:34:14,869] DEBUG - wire << "a[\r][\n]"
[2017-02-17 13:34:14,870] DEBUG - wire << "responseee[\r][\n]"
[2017-02-17 13:34:14,870] DEBUG - wire << "0[\r][\n]"
[2017-02-17 13:34:14,870] DEBUG - wire << "[\r][\n]"

Thanks.

Sending SOH character at the beginning of a message in wso2

In here we are going append the SOH (Start Of Heading) character to our incoming message to WSO2 ESB, and send the payload with the SOH character to the Backend.

For that you need to write a class mediator which appends the SOH and payload, then send to the back end. In this example we are going to the send the payload with SOH to IBMMQ server (which we consider as our BE).

Step 1

Create the class mediator as following


package com.home;

import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

import javax.jms.*;

public class SOHCustomMediator extends AbstractMediator {

    public boolean mediate(MessageContext messageContext) {
        try {

            String sohCharactor = (String) messageContext.getProperty("regProperty");
            String messageBody = (String) messageContext.getProperty("messagebody");

            //======================================================

            MQQueueConnectionFactory queueConnectionFactory = new MQQueueConnectionFactory();
            //Configure the connection properties
            queueConnectionFactory.setHostName ("localhost");
            queueConnectionFactory.setPort (1414);
            queueConnectionFactory.setQueueManager ("ESBQManager");
            queueConnectionFactory.setChannel ("mychannel");
            queueConnectionFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
            //  Started the queue connection
            QueueConnection queueConnection = queueConnectionFactory.createQueueConnection ("mqm", "1qaz2wsx@");
            queueConnection.start();
            // Set JMS configuration for queue and session
            Queue queue = new MQQueue("LocalQueue1");
            QueueSession session = queueConnection.createQueueSession (false, Session.AUTO_ACKNOWLEDGE);
            QueueSender sender = session.createSender (queue);
            //Con-cat the message and the SOH
            TextMessage message = session.createTextMessage(sohCharactor + messageBody);
            message.setStringProperty("MyCustomProperty", "DUMMY_PROPERTY_VALUE");
            System.err.println("Sending message:" + message.getText());
            sender.send (message);
            System.out.println ("sent message: " + message);
            //close the session and connection
            session.close();
            queueConnection.close();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return true;
    }
}

Step 2

Store the SOH.txt in the ESB registry (gov:trunk/SOH.txt)

Step 3

Add below jar files to <ESB_HOME>/repository/components/lib folder. Because those are needed for the class mediator to send the message to the IBM MQ.


  • com.ibm.dhbcore.jar
  • com.ibm.mq.jar
  • com.ibm.mqjms.jar
  • com.ibm.mq.soap.jar
  • com.ibm.msg.client.osgi.wmq_7.0.1.3.jar

Step 4

Here is the sample proxy which is setting the SOH character and message payload to properties and then call the class mediator.


<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="SOHProxy"
       startOnLoad="true"
       statistics="disable"
       trace="disable">
   <target>
      <inSequence>
         <log level="full"/>
         <property expression="json-eval($)" name="messagebody"/>
         <property expression="get-property('registry', 'gov:trunk/SOH.txt')"
                   name="regProperty"/>
         <class name="com.home.SOHCustomMediator"/>
      </inSequence>
   </target>
   <description/>
</proxy>


That's all you need to do.

Thank You.


Blogger Widgets