This is the second example that demonstrates the ease of integrating security and reliable messaging to the WSO2 Application Server.
It demonstrates trading interactions between a stock exchange, trader (stock broker) and a client (shareholder). A shareholder who needs to buy and sell shares, should first become a client of that particular trader. A trader can register his client (the shareholder) in the stock exchange. Once registered, the shareholder uses his Central Depository System (CDS) account to buy and sell.
The following section describes the services offered by each party involved.
Scenario Description
- Client creates an account with a trader and receives a user ID. The Trader registers the Client with the stock exchange using client information.
- The Client obtains a CDS account using which he is allowed to trade. The Client can only buy and sell shares on the market through the Trader who registered him/her.
- The Trader places the client's buying and selling orders in the stock exchange.
- The trader also registers feed targets to the Exchange.
- To access any of the services from the stock exchange, the Trader needs to first login to the Exchange using the Trader's username and password.
- The Clients get stock quotes (company name, symbol, selling price, high, low), and portfolio information from the Trader. He also deposits money in his account through the Trader.
- The User (shareholder) can directly gain market information such as total traded shares, average price, and average size of trade from the stock exchange.
- Trader provides market data updates to the stock exchange.
In summary the services are as follows:
1. Services offered by Stock Exchange to Trader
- Registering clients
- Logging in to Stock Exchange
- Buying and selling shares for Clients
- Registering feed targets
2. Services offered by Stock Exchange to Client
- Provide market information such as total traded shares, average price, average size of trade
3. Services offered by Trader to Client
- Create accounts
- Ability to make deposits to account
- Buying and selling shares
- Get stock quote (name, symbol, price, high, low) and portfolio
4. Services offered by Trader to Stock Exchange
- Provide market data updates
Note: Attributes of Stock Quote are name, symbol, selling price, high price and low price
Building the sample
The sample is located in <AS_HOME>\Samples\Trader
directory.
For more information, see Building Axis2 Samples .
The build automatically copies the .aar
file containing the service into the repository.
On the Main menu, under Services, click List. The Deployed Services page will appear with the ExchangeTrader, ExchangeClient, TraderClient and TraderExchange services deployed.
If you do not see the services deployed, wait for a while for the deployment engine of Axis2 to pick up the newly added .aar
and the refresh/reload the page.
Running the service
From the terminal go to
<AS_HOME>\samples\Trader
directory.Execute one of the following commands to run the client:
OS Command MS Windows run-client.bat [ -qos (rm | secure | securerm)] [-te <TraderService URL>] [-ee <ExchangeService URL> ] Unix/Linux run-client.sh [ -qos (rm | secure | securerm)] [-te <TraderService URL>] [-ee <ExchangeService URL> ]
Terms used
TraderService URL is the End Point Reference (EPR) of the TraderClient. This can be obtained from the Management Console.
Go to the " Deployed Services" window and click on the service "TraderClient". This will open up its dashboard, where you can find its endpoints. (under services column) > End Points. For this sample the default URLs for the standalone distribution and the embeddable version are as follows:
The same for ExchangeService, according to this example is as follows:
If rm, secure or securerm is to be used, the following modules should be engaged. This can be done by selecting the Engage Module option from the Service Information page for the services listed in the management console.
- rm - Sandesha (Sandesha2)
- secure - Security (Rampart)
- securerm - Both Sandesha and Security
- If security is used, the default username / password for the client is test / test123.
- The listed EPR's are for the stand-alone server. When WSO2 Application Server is running inside a servlet container, the EPR's are different. It is recommended to get the EPR through the management console.
The options shown above are illustrated using the following samples.
Example 1: Without any Module Engaged
From the Trader
directory, type the following command:
$ ./run-client.sh
You should see the following:
Populating the stock market ................Done. Using following parameters TraderService URL : http://localhost:9762/services/TraderClient ExchangeService URL : http://localhost:9762/services/ExchangeClient STARTING TRADE SAMPLE CLIENT ============================= ... Please select your operation... --------------------------------- (1) createAccount (2) getQuote (3) getSymbols (4) deposit (5) buy (6) sell (7) getPortFolio (8) getMarketInfo (9) Exit
Example 2: With the Security Module Engaged
- Login to the Application Server Management Console.
- Add a user (under configure menu -> Users and Roles) named "test" with password "test123."
- Similarly, add a role "testrole" and add user "bob" to that role.
- Similarly, add a role "testrole" and add user "test" to that role.
- Provide all permissions for this role.
- Then go to the "Deployed Services" window in the AS Management Console. (Manage > Service > List).
- Access the dashboard of the service "TraderClient" service by clicking on it.
- In the dashboard, under "Security," select "Username Token." Press "Next."
- Then select "testrole" from the user groups listed.
- Click "Finish."
Follow the same steps for ExchangeClient service. Then go to <AS_HOME>/samples/Trader
directory and type: ./run-client.sh -qos secureYou would see a prompt asking for the username and password before listing the operations available as follows:
Populating the stock market ................Done. Using following parameters TraderService URL : http://localhost:9763/services/TraderClient ExchangeService URL : http://localhost:9763/services/ExchangeClient QOS : security STARTING TRADE SAMPLE CLIENT ============================= ... Please enter your username :test Please enter your password :test123
Example 3: Creating an account
...Creating account... Please enter the Name:test1 Please enter the password:test1 Results ------- User ID :test1
Example 4: Depositing money in the exchange
...Doing deposit... Please enter the username:test1 Please enter the password:test1 Please enter the amount:100 Deposit Successful !!
Sample 5: Buying '100 mn' stocks in the exchange
...Buying stocks... Please enter the username:test1 Please enter the password:test1 Please enter the symbol:mn Please enter the quantity:100 Results ------- Status :true Reason :Success
Example 6: Selling '50 mn' stocks in the exchange
...Selling stocks... Please enter the username:test1 Please enter the password:test1 Please enter the symbol:mn Please enter the quantity:50 Results -------- Status :true Reason :Success
Example 7: Getting the portfolio of the user 'test1' after buying a further '200 h' stocks
...Getting Portfolio... Please enter the username:test1 Please enter the password:test1 Results ------- Symbol:mn Amount:50 Symbol:h Amount:200
Similarly, you can explore the options available in the Trader client and see how it works for yourself.
Trader service internally calls the Stock exchange services. Since the internally-used stubs try to use the configuration of the container by default, engaging QoS on the Stock Exchange service will break the trader sample. Therefore, for the trader sample to run properly, QoS should not be engaged on the Stock Exchange service.
The WSDL files of the services can be found in: <AS_HOME>\samples\Trader\wsdl