Friday, September 14, 2012

REST support in WSO2ESB - Introduction

Version 4.0.3/later versions have an effective REST API mechanism to support REST invocations. The HTTP verbs such as GET/POST/PUT/DELETE..  can be handled with a simple configuration .

Sample REST API configuration to handle a GET request;
 <api name="echoAPI" context="/echo">
      <resource methods="GET"
                uri-template="/{string}"
                inSequence="echo-in-seq"
                outSequence="echo-out-seq"
                faultSequence="fault"/>
   </api> 
If you notice that ,here we define the context as "echo" which would be appear in the http url. User has to define a meaningful context if he wants to allow others  to use this API.
The url of the above API would be;
http://localhost:8280/echo
In this API we restricted ESB to handle only the GET requests. So, if user sends  requests for other verbs, those will be dropped at ESB end.

For the ur-template, we get only single parameter from the enduser.
So, to invoke this API, user has to send a single query parameter.
http://localhost:8280/echo?testString

If user sends multiple query parameters he has to define something like;
uri-template="/{string1}/{string2}"
uri-template="/{string1}&{string2}"

But it totally depends on the service implementation, how user going to handle the template.

If we have number of query parameters we could simply define the url-mapping like;

url-mapping="/*"

This will accept all the GET requests coming with the following URL format;
http://localhost:8280/echo


Related post ;
REST Support in WSO2ESB - Handling JSON messages

Tuesday, September 4, 2012

Executing Carbon admin services from Proxy service

Carbon V4.0.0  provides ability to invoke admin services using proxy service. We could point the admin service as an endpoint.
Each request must have Basic-Auth headers, which has to be set as transport headers.
In mediation flow, we could use property mediator to set the authentication headers.

<property name="Authorization" expression="fn:concat('Basic ', base64Encode('UserName:Password'))"
scope="transport"/>


We have to provide admin username and password to be encoded and set as transport header.

You might need to set <HostnameVerifier> parameter to AllowAll in the HTTPS transport sender configuration which is defined in axis2.xml.

Sample proxy conf;

<proxy name="adminServiceProxy" transports="https http"
          startOnLoad="true" trace="disable">
      <description/>
      <target>
         <endpoint>
            <address uri="https://localhost:9444/services/UserProfileMgtService"/>
         </endpoint>
         <inSequence>
            <property name="Authorization"
                      expression="fn:concat('Basic ', base64Encode('admin:admin'))"
                      scope="transport"/>
         </inSequence>
         <outSequence>
          <send/>
         </outSequence>
      </target>
   </proxy>