Powered By Blogger

Dec 29, 2014

Get a JSON response with WSO2 DSS


WSO2 Data Services Server supports for both XML and JSON outputs and in order to receive the message as JSON you need to change following configurations.
You can achieve this by enabling the 'content negotiation' property in the axis2.xml file and axis2_client.xml file. Also you should send the requests to the server by adding the "Accept:Application/json" to the request header, and as a result, DSS will return the response in JSON format. Please follow the below steps 

1. In axis2.xml file at <DSS_HOME>/repository/conf/axis2, include the following property 

<parameter name="httpContentNegotiation>true</parameter> 

2. In axis2_client.xml file at <DSS_HOME>/repository/conf/axis2 enable the following property 

<parameter name="httpContentNegotiation>true</parameter> 

3. When sending the request please make sure to add the "Accept:Application/json" to the request header 

Note that if you are using tenant, then the above parameter need to be set in 'tenant-axis2.xml' as well.
Now you can use the ResourceSample, which is a sample available in your DSS distribution to test the result. Send a request to the server using  CURL by adding “Accept:Application/json” to the request header as shown below.

curl -X GET -H "Accept:application/json" http://localhost:9763/services/samples/ResourcesSample.HTTPEndpoint/product/S10_1678

XML to JSON conversion using a proxy in WSO2 ESB

This post describes about the way to convert an XML payload into JSON. Basically this can be achieved by using the property "messageType" in synapse configuration.

The property is as follows.

<property name="messageType" value="application/json" scope="axis2"/>

I have created a proxy called TestProxy with the  SimpleStockQuote endpoint as follows.

 <?xml version="1.0" encoding="UTF-8"?>  

 <proxy xmlns="http://ws.apache.org/ns/synapse"  

 name="TestProxy"  

 transports="https,http"  

 statistics="disable"  

 trace="disable"  

 startOnLoad="true">  

 <target>  

 <inSequence>  

 <property name="messageType" value="application/json" scope="axis2"/>  

 <log level="full"/>  

 <send>

 <endpoint>  

 <address uri="http://localhost:9000/services/SimpleStockQuoteService/"/>  

 </endpoint>  

</send>
 </inSequence>  

 <outSequence>  

 <send/>  

 </outSequence> 

 </target>  

 <description/>  

 </proxy>   



In order to invoke this proxy you can follow below steps.

1. Build the SimpleStockQuote Service at <ESB_HOME>/samples/axis2Server/src/SimpleStockQuoteService by running "ant" command

2. Then start sample backend to recover the response which is provided with WSO2 ESB by navigating to <ESB_HOME>/samples/axis2Server and enter the command "sh axis2Server.sh"

3. Send the below SOAP request to proxy service using SOAP UI.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m0="http://services.samples" xmlns:xsd="http://services.samples/xsd">  

 <soapenv:Header/>  

 <soapenv:Body>  

 <m0:getQuote xmlns:m0="http://services.samples" id="12345">  

 <person>Anvar</person>  

 <person>Porter</person>  

 <person>Raj</person>  

 <m0:request>  

 <m0:symbol>A</m0:symbol>  

 </m0:request>  

 </m0:getQuote>  

 </soapenv:Body>  

 </soapenv:Envelope>  

Response should be as follows:

 HTTP/1.1 200 OK  

 Host: sohani-ThinkPad-T530:8280  

 SOAPAction: "urn:getQuote"  

 Accept-Encoding: gzip,deflate  

 Content-Type: application/json  

 Date: Fri, 29 Dec 2014 09:00:00 GMT  

 Server: WSO2-PassThrough-HTTP  

 Transfer-Encoding: chunked  

 Connection: Keep-Alive  

 {"getQuote":{"@id":"12345","person":{"@id":"12345","@sex":"female","@target":"urn:getQuote","firstname":"Anna","lastname":{"@id":"12345678","$":"Smith"}},"request":{"symbol":"A"}}}  

Dec 1, 2014

OAuth 2.0 Implicit Grant with WSO2 API Manager

Implicit Grant type is the recommended practice if the client is a browser based application such as a JavaScript client. Mainly client receives access token as the result of the authorization request. Also this doesn't include client authentication because it does not make use of client secret. 

You can find the sample web application at http://sourceforge.net/projects/charithablogsam/files/.

Setting up Client

Download playground2.0.war from the above link and copy it to TOMCAT_HOME/webapps directory. Make sure to update the following parameters in WEB-INF/web.xml 

<servlet>
        <servlet-name>oAuth2ClientServlet</servlet-name>
        <servlet-class>com.wso2.identity.oauth.sample.OAuth2ClientServlet</servlet-class>
        <init-param>
             <description>serverUrl</description>
             <param-name>serverUrl</param-name>
             <param-value>https://localhost:9443/services/</param-value>

<servlet>
        <servlet-name>oAuth2AccessResourcePage</servlet-name>
        <jsp-file>/oauth2-access-resource.jsp</jsp-file>
        <init-param>
            <description>setup</description>
            <param-name>setup</param-name>
            <param-value>AM</param-value>
        </init-param>


Now you can follow the below steps:

1. After setting up the client you can login to http://localhost:8080/playground2.0/ and click on  "Import Photos" icon

2. Then from the page you are getting Select Implicit as the Authorization Grant Type. Copy the consumer key value from the application you have subscribed in WSO2 API Manager and include it in Client Id text box.

3. Then include a value for scope.

4. Then include the Callback URL as  http://localhost:8080/playground2.0/oauth2client

5. After that include Authorize endpoint. This should be the endpoint of authorization server where it accepts the authorization requests. In WSO2 API Manager, there is an API to handle all authorization requests and it can be accessed through http://localhost:8280/authorize.

6. Next click on  Authorize.

Then you have to provide user name and password (username and password of the resource owner/end user).

Type admin/admin as user name and password respectively and click on login

Now you will receive the Access Token