Friday, July 29, 2016

Secure vault support in WSO2 ESB inbound parameters



In ESB 5.0.0, wso2:vault-lookup('alias') expression can be used in all inbound endpoint parameters. even in registry resource text, valut-lookup() expression can be used and set resource as the parameter value.

Parameter value  need to be encrypted and store with a alias and retrieve the actual value in your ESB  inbound configuration as below.

Before adding alias, You need to run the cipher tool in ESB. Go to bin directory and issue
"sh ciphertool.sh -Dconfigure"
Then go to the management console and click on "Manage Password" and create a alias my.username and my.password. here you can store the encrypted value for given values.

Add a registry resource(conf:/userInfo/password) which contains  {wso2:vault-lookup('my.password')} as the text contains.

Then you can use the alias in your inbound parameters with lookup fuction.
 Ex: wso2:vault-lookup('my.password')


<inboundEndpoint xmlns="http://ws.apache.org/ns/synapse"  
          name="activemq"  
          sequence="seq1"  
          onError="fault"  
          protocol="jms"  
          suspend="false">  
   <parameters>  
    <parameter name="interval">10</parameter>  
    <parameter name="sequential">true</parameter>  
    <parameter name="coordination">true</parameter>  
    <parameter name="java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>  
    <parameter name="java.naming.provider.url">tcp://localhost:61616</parameter>  
    <parameter name="transport.jms.ConnectionFactoryJNDIName">QueueConnectionFactory</parameter>  
    <parameter name="transport.jms.ConnectionFactoryType">queue</parameter>  
    <parameter name="transport.jms.Destination">order</parameter>  
    <parameter name="transport.jms.SessionTransacted">false</parameter>  
    <parameter name="transport.jms.SessionAcknowledgement">AUTO_ACKNOWLEDGE</parameter>  
    <parameter name="transport.jms.CacheLevel">3</parameter>  
    <parameter name="transport.jms.UserName">{wso2:vault-lookup('my.username')}</parameter>  
    <parameter name="transport.jms.Password" key="conf:/userInfo/password"/>  
    <parameter name="transport.jms.SubscriptionDurable">false</parameter>  
    <parameter name="transport.jms.SharedSubscription">false</parameter>  
   </parameters>  
 </inboundEndpoint>  


Monday, July 25, 2016

foreach sample configuration in WSO2 ESB

foreach mediator helps you to iterate through a element array. For a example, if you need to iterate xml array and concatenate them together, bellow configuration read all the id and concatenate them.


 <foreach xmlns:ns1="http://ws.wso2.org/dataservice"  
          id="Idforeach"  
          expression="//ns1:Entries/ns1:Entry/ns1:id">  
       <sequence>  
        <property name="ID"  
             expression="fn:concat($ctx:ID,//ns1:id, ',')"  
             type="STRING"/>  
       </sequence>  
     </foreach>  
     <log level="custom">  
       <property name="IDs" expression="$ctx:ID"/>  
     </log>  
Ex: ESB will log "IDs = 105,106," for below request payload

 <Entries xmlns="http://ws.wso2.org/dataservice">  
 <Entry><id>105</id></Entry>  
 <Entry><id>106</id></Entry>  
 </Entries>



How to secure a queue with username and password in ActiveMQ


How to secure a queue with username and password in ActiveMQ

By Default, Queue in a ActiveMQ server can be access without username and password. Adding below configuration in to activemq.xml will make sure that queue is secure with username and password. Then you need to provide a username and password when you create a connection.



 <broker  
   xmlns="http://activemq.apache.org/schema/core"   
 brokerName="localhost" dataDirectory="${activemq.data}">  
 ...........     
 <plugins>  
     <simpleAuthenticationPlugin>  
       <users>  
         <authenticationUser username="nuwan"   
                   password="mypassword"   
                   groups="admins,publishers,consumers" />  
       </users>  
     </simpleAuthenticationPlugin>  
   </plugins>   
 ................   
  </broker>  



Wednesday, March 11, 2015

Getting Date String from unix time stamp in HIVE Query

if You try to get the Date String from hive query as below, You will get an invalid date.

SELECT timestamp, from_unixtime(timestamp, 'yyyy-MM-dd HH:mm:ss') FROM TableName

1426109700733 > 47161-08-14 00:32:13
 
 from_unixtime expect the time in seconds and you have to use below query to get the date correct.

SELECT timestamp, from_unixtime(CAST(timestamp/1000 as BIGINT), 'yyyy-MM-dd HH:mm:ss') FROM TableName

1426109700733 > 2015-03-11 14:35:00

Thursday, August 7, 2014

How to send a HTTP POST or PUT request with empty content message body into a backend server using WSO2 ESB

How to send a HTTP POST or PUT request with empty content message body into a backend server using WSO2 ESB

By the design, ESB expects a message body to send the request to backend server when the http method is POST or PUT. But you can overwrite this behavior by using the property FORCE_POST_PUT_NOBODY. Then you can post or put the request without message body or with the message body if exist to the backend server.

Bellow is the sample proxy service which capable for sending empty messages

<proxy xmlns="http://ws.apache.org/ns/synapse" name="httpendpoint" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">  
   <target>  
    <inSequence>  
      <log level="full" />  
      <property name="FORCE_POST_PUT_NOBODY" value="true" scope="axis2" type="BOOLEAN" />  
    </inSequence>  
    <outSequence>  
      <send />  
    </outSequence>  
    <endpoint>  
      <http uri-template="http://localhost:8080/echoempty/echoemptyrequest" />  
    </endpoint>  
   </target>  
   <description />  
 </proxy>  

         
Then you can send the http messages as bellow to the proxy service.

POST /services/httpendpoint HTTP/1.1
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 0
Host: 127.0.0.1:8282
User-Agent: Apache-HttpClient/4.2.3 (java 1.5) 

                     

Monday, July 7, 2014

How to Send a String Parameter to an Axis2 Web Service

If you have written a web service operation which accept a String parameter, You will not be able to pass a string value directly. The below operation is required a string parameter. Then you need to wrap the text inside the CDATA tag. Other wise it will not read the parameter value.

     public void echo(String request)  {
        System.out.println(request);
    }

If you want to send a simple string , You need to wrap it with CDATA. The the output will be >> "Text Content"

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wso2="http://wso2.org">
   <soapenv:Header />
   <soapenv:Body>
      <wso2:echo>
         <!--Optional:-->
         <wso2:request><![CDATA[Text Content]]></wso2:request>
      </wso2:echo>
   </soapenv:Body>
</soapenv:Envelope>
 
If you want to pass a xml content as a string then you have to use CDATA tag as bellow.
Then the output will be >> "<foo>Test</foo>"
 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wso2="http://wso2.org">
   <soapenv:Header />
   <soapenv:Body>
      <wso2:echo>
         <!--Optional:-->
         <wso2:request><![CDATA[<foo>Test</foo>]]></wso2:request>
      </wso2:echo>
   </soapenv:Body>
</soapenv:Envelope> 

You need CDATA tag always to send the String value to a web service operation if they have String type parameter

Friday, May 16, 2014

How to append a query parameter to an API published from WSO2 API Manager

How to append a query parameter to an API published from WSO2 API Manager

There are some situations where you need to send some url query parameters with the request when publishing an API through the API Manager. But You do not need client to send those parameters with the request URL. In that case how do you append those parameters to the endpoint url. In this blog post see how to append a query parametr to endpoint url.

First you need to create an API and published the API. Please refer how to create an API to create an API in WSO2 API Manager.

Then the requirement is once you invoke the API with some query parameter, It will call the actual service endpoint with some other query parameter.

You can append query parameters to the endpoint url from API Manager. synapse property 'REST_URL_POSTFIX' can be used to manage the query parameter. Once you create an api, what you have to do is that, you need to update the synapse configuration of the api created with the relevant property. the property REST_URL_POSTFIX will give you the query string coming into the api from the client. Then you can change the query string as you need.

<property name="REST_URL_POSTFIX" expression="fn:concat(get-property('axis2','REST_URL_POSTFIX'), '&amp;exchange=xyz')" scope="axis2" type="STRING"/>

Once You create an API, You can add parameter by following bellow steps.
1) Login to AM(https://ipaddress:port/carbon/) and Click on main > Source View
Then you can see the synapse configuration

2) Find the api configuration you have created by the api name.

3) Then update the insequence of the api with bellow property.
<property name="REST_URL_POSTFIX" expression="fn:concat(get-property('axis2','REST_URL_POSTFIX'), '&amp;exchange=xyz')" scope="axis2" type="STRING"/>

4) Click on update button.

Then your api looks as bellow. 

 <api xmlns="http://ws.apache.org/ns/synapse" name="admin--testapi" context="/testapi" version="1.0.0" version-type="url">  
   <resource methods="GET" url-mapping="/*">  
     <inSequence>  
       <property name="POST_TO_URI" value="true" scope="axis2"/>  
       <property name="REST_URL_POSTFIX" expression="fn:concat(get-property('axis2','REST_URL_POSTFIX'), '&amp;exchange=xyz')" scope="axis2" type="STRING"/>  
       <filter source="$ctx:AM_KEY_TYPE" regex="PRODUCTION">  
         <then>  
           <send>  
             <endpoint name="admin--testapi_APIEndpoint_0">  
               <address uri="http://localhost:8080/stockquote/getQuote">  
                 <timeout>  
                   <duration>30000</duration>  
                   <responseAction>fault</responseAction>  
                 </timeout>  
                 <suspendOnFailure>  
                   <errorCodes>-1</errorCodes>  
                   <initialDuration>0</initialDuration>  
                   <progressionFactor>1.0</progressionFactor>  
                   <maximumDuration>0</maximumDuration>  
                 </suspendOnFailure>  
                 <markForSuspension>  
                   <errorCodes>-1</errorCodes>  
                 </markForSuspension>  
               </address>  
             </endpoint>  
           </send>  
         </then>  
         <else>  
           <sequence key="_sandbox_key_error_"/>  
         </else>  
       </filter>  
     </inSequence>  
     <outSequence>  
       <send/>  
     </outSequence>  
   </resource>  
   <handlers>  
     <handler class="org.wso2.carbon.apimgt.gateway.handlers.security.APIAuthenticationHandler"/>  
     <handler class="org.wso2.carbon.apimgt.gateway.handlers.throttling.APIThrottleHandler">  
       <property name="id" value="A"/>  
       <property name="policyKey" value="gov:/apimgt/applicationdata/tiers.xml"/>  
     </handler>  
     <handler class="org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageHandler"/>  
     <handler class="org.wso2.carbon.apimgt.usage.publisher.APIMgtGoogleAnalyticsTrackingHandler"/>  
     <handler class="org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler"/>  
   </handlers>  
 </api>


Ex: Client request  http://localhost:8280/testapi/1.0.0?symbol=abc
Actual request required for back end
http://localhost:8080/stockquote/getQuote?symbol=abc&exchange=xyz

Here you have added exchange parameter to the service call. As above you can modify the query string as you required.