Wednesday, December 10, 2014

Capturing/Decoding messages with JWT using wire logs in WSO2 API Manager 1.7.0

This is intended for the users who have configured WSO2 API Manager 1.7.0 with JWT and wants to see the messages with JWT. If you haven’t configured it, please refer this

1. Enable debug logs for the following loggers in WSO2APIM_HOME/repository/conf/log4j.properties file 

       log4j.category.org.apache.synapse.transport.nhttp.wire=DEBUG

       log4j.category.org.apache.synapse.transport=DEBUG

2. Restart the WSO2 API Manager if already started

3. Access your API and have a look at carbon logs

4. You will get something similar to the following log

[2014-11-30 22:09:22,610] DEBUG - headers http-outgoing-2 >> X-JWT-Assertion: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5tSm1PR1V4TXpabFlqTTJaRFJoTlRabFlUQTF
ZemRoWlRSaU9XRTBOV0kyTTJKbU9UYzFaQT09In0=.eyJpc3MiOiJ3c28yLm9yZy9wcm9kdWN0cy
9hbSIsImV4cCI6MTQxNzM2NjQ2MTk5OCwiaHR0cDovL3dzbzIub3JnL2NsYWltcy9zdWJzY3JpYmV
yIjoicmFqa3VtYXIiLCJodHRwOi8vd3NvMi5vcmcvY2xhaW1zL2FwcGxpY2F0aW9uaWQiOiIxIiwiaHR0
cDovL3dzbzIub3JnL2NsYWltcy9hcHBsaWNhdGlvbm5hbWUiOiJEZWZhdWx0QXBwbGljYXRpb24iLC
JodHRwOi8vd3NvMi5vcmcvY2xhaW1zL2FwcGxpY2F0aW9udGllciI6IlVubGltaXRlZCIsImh0dHA6Ly9
3c28yLm9yZy9jbGFpbXMvYXBpY29udGV4dCI6Ii9waG9uZXZlcmlmeSIsImh0dHA6Ly93c28yLm9yZy
9jbGFpbXMvdmVyc2lvbiI6IjEuMC4wIiwiaHR0cDovL3dzbzIub3JnL2NsYWltcy90aWVyIjoiVW5saW1pd
GVkIiwiaHR0cDovL3dzbzIub3JnL2NsYWltcy9rZXl0eXBlIjoiUFJPRFVDVElPTiIsImh0dHA6Ly93c28yL
m9yZy9jbGFpbXMvdXNlcnR5cGUiOiJBUFBMSUNBVElPTiIsImh0dHA6Ly93c28yLm9yZy9jbGFpbX
MvZW5kdXNlciI6InJhamt1bWFyQGNhcmJvbi5zdXBlciIsImh0dHA6Ly93c28yLm9yZy9jbGFpbXMvZ
W5kdXNlclRlbmFudElkIjoiLTEyMzQiLCAiaHR0cDovL3dzbzIub3JnL2NsYWltcy9lbWFpbGFkZHJlc3Mi
OiJyYWprdW1hcnJAd3NvMi5jb20iLCAiaHR0cDovL3dzbzIub3JnL2NsYWltcy9naXZlbm5hbWUiOiJtb2
1vIiwgImh0dHA6Ly93c28yLm9yZy9jbGFpbXMvbGFzdG5hbWUiOiJtb20iLCAiaHR0cDovL3dzbzIub3J
nL2NsYWltcy9yb2xlIjoiSW50ZXJuYWwvc3Vic2NyaWJlcixJbnRlcm5hbC9ldmVyeW9uZSJ9.eczrlpBg3
+38Y90voBIhei3wXMZqZWV5z4q4nSQoUPPlUYXAo6UGmsiwBkT17BivS16K0I69tGwpxKzju/Tg8slT
06bLVmOwIeFtMH86HLh/VPKqnZ0WKgsZw5fFxccIYDgIfnzo8Gg+x9+tL+R2i71e5EOc7098FXZOXH
nlYm0=

5. As you can see, this is the JWT token in encoded format. The above message contains three parts separated by dots. We can decode this message and see what it represents. You can go https://www.base64decode.org/  and decode this message party by part.

6. If you copy paste the first part of the message (blue) into the decoder, you will get something like below


 And that contains JWT headers.


7. If you copy paste the second part of the message (pink), into the decoder, you will get something like below

 
 And that contains JWT claims set.


 8. That’s it. You have decoded and viewed the end user details that have been sent to the back end.

WSO2 Identity Server - How to add users containing special characters?

You can't create users containing special characters like "raj-kumar", "raj.kumar", "raj_kumar" etc. by default in WSO2 IS.

If you try to create users like above, you will the following error.



However, you can configure WSO2 IS to allow what type of characters a username can contain.

In order to allow usernames with special characters you can do the following.

* Open IS_HOME/repository/conf/user-mgt.xml

* Change the value of the property 'UsernameJavaRegEx' in the user store configuration that is applicable as below.

* Restart the server.

* Now you should be able to add users like "raj-kumar", "raj.kumar" and "raj_kumar"


Limitations - Pipe character

The pipe character is used as a special character in WSO2 code base. Still you will be able to create users like "test|user".


But you will end up with several problems as mentioned below.

* Users will be created and populated in LDAP. But when you list the users from IS management console, you will see only the second part of the username. For example, if you create a user "test|user", then you will see only "user" is listed in management console.
 
* You will not be able to delete this user. It will report "Cannot delete user. Error is: Cannot delete user who is not exist".

 
Hence, avoid creating users with pipe character.

Tuesday, December 9, 2014

WSO2 ESB and RabbitMQ


The Scenario of Interest


1. Client submits an API request with a correlation id and a call back queue to RabbitMQ
2. ESB receives the request from RabbitMQ
3. ESB forwards the request to Endpoint
4. ESB receives the response from Endpoint
5. ESB if receives a response from Endpoint, publishes it at the call back queue specified in the request with the correlation id of the request
6. ESB if doesn't receive a response in timely manner posts an error in the Dead Letter MQ with the correlation id of the request
7. Client receives the response from the call back queue
8. Since request and response have the correlation id, client knows which response is for which request

* Configure the ESB to use rabbitmq transport as explained in https://docs.wso2.com/display/ESB481/RabbitMQ+AMQP+Transport

* Start axis2 server and deploy SimpleStockQuote Service as explained in https://docs.wso2.com/display/ESB481/Setting+Up+the+ESB+Samples

* Go to the management console of ESB and click on 'Source View'

* Copy paste the following synapse configuration into the 'Source View" and click update

* Now you have deployed a rabbitmq proxy service which will,
      consume messages from a queue called "rpc-queue"
      calling the back end simple stock quote service for the symbol found in that request message
      receiving response from back end
      setting the correlation id of the request to the response
      publishes the response to the call back queue found in the request
      if ESB is not getting any response from back end, it will publish an error message, with the correlation id of the request, to a queue called "dead-queue"

* Now run the following RPCClient.java

* Now login to the management console of the rabbitmq server (http://localhost:15672)

* You will see that there is message in your call back queue. This message is the response sent by the ESB.

* Now run the following RPCReceiver.java

* You will get the message in your console. This is the message RPCReceiver pulled from your call back queue. This will have the correlation id of the request too.

* Have a look at in rabbitmq management console, the previous message will not be there, as it is pulled by the RPCReceiver.

* Now shutdown axis2 server

* Run RPCClient.java

* Have a look at rabbitmq management console, you will find that there is a message in a queue called "dead-queue". Since the back end service is unavailable, ESB sends the response to dead queue with the correlation id of the request

I will explain this scenario in detail soon.

Saturday, December 6, 2014

WSO2 API Manager - How to send additional headers in Swagger API Console?

WSO2 API Manager's Swagger API Console allows you to send authorization header by default. If you want to send additional headers, follow these steps.

* Go to the publisher and click on your API.
* Click on Docs.


* Click on Edit Contents of swagger API definition. You will get swagger API definition editor


* Under parameter section of the each HTTP method, you can add what ever parameters you want. It can be headers, body etc.

* For example, if you want to add tenant and enterprise headers to PUT method, you will add the following entries under parameters section of PUT method of your API.

* And then you should add these two headers under Access-Control-Allow-Headers of CORS configuration in api-manager.xml, like below.

* Finally restart the API Manager if already started.

* Go to the API Console in store and click on PUT method


Now you should be able to send additional headers from API Console.

WSO2 API Manager's Swagger API Console does not return any results?

When you click “Try it out!” option, sometimes you will see nothing happens other than some dots are moving.


There can be several reason for this problem.

First you need to verify whether CORS configurations are correct in api-manager.xml
It is clearly explained here. Please make sure your settings are correct according to this guide.

If you are accessing the store via HTTPS, you will not be able to use API Console most of the times.

Reason is swagger base path is set to HTTP production endpoint by default. So swagger client can't connect to it.

You can change the swagger base path as explained here.

* Go to publisher and click on your API.

* Click on Docs


* Click Edit Contents of swagger API definition. You will get swagger API definition editor




* You will see that the base path is set to HTTP production url like http://10.100.5.84:8280/phoneverify/1.0.0

* Change it to HTTPS production url like https://10.100.5.84:8243/phoneverify/1.0.0

* Please note that the ports are different in each url.

* Save and close the swagger API definition editor

* Now click on “Try it out!” option from store, you will get response



There you go! You got the response!

You don't have permission to access / on this server - Openstack Horizon UI

While logging into the Openstack Horizon UI, you might get the following error.

  Forbidden
  You don't have permission to access / on this server.
   Apache/2.4.6 (Ubuntu) Server at 192.168.57.30 Port 80
 
You can simply solve it.

Step1

Modify /etc/apache2/sites-available/horizon.conf with the line "Require all granted" as per below:

Step2 

Restart apache server by running following command on your terminal.

sudo service apache2 restart

You should be able to access horizon now. Hope it will work.
 
 

ssh: connect to host 192.168.57.30 port 22: Connection refused

If you have a machine with a user wso2 and you are trying to ssh to it from another machine, you will do something like below.

ssh wso2@192.168.57.30

Sometimes you will get the following error while doing the above.

ssh: connect to host 192.168.57.30 port 22: Connection refused

There might be several reasons for this.

If you don't have openssh-server installed on your machine, you will get the above error.

Run this command to install openssh-server.

sudo apt-get install openssh-server

You will be able to ssh now.

Monday, August 4, 2014

Using PostgreSQL cartridge on Apache Stratos

This blog post explains how you would use PostgreSQL cartridge on Apache Stratos.

 

Requirements

  • You need to have OpenStack/EC2 (IaaS provider) installed and configured.
  • You need to have Apache Stratos 4.0.0 installed, configured and running.
  • You need to have a Puppet master instance configured and running.

 

Install required puppetforge modules


  1. Login to your puppet master node
  2. Get root access
         sudo -i
  3. Install Git
         apt-get install -y git
  4. Clone puppetforge script
         git clone https://github.com/R-Rajkumar/puppetforge.git
  5. Go into cloned puppetforge directory
         cd /dir/to/puppetforge/
  6. Modify your puppetforge.modules file to have following entry
         puppetlabs-postgresql
  7. Run puppetforge.sh
         ./puppetforge.sh

 


Defining PostgreSQL cartridge node in puppet master

PostgreSQL node will have PostgreSQL server and a web interface to manage the server. You can define a PostgreSQL node as bellow,

Let's look into details about each classes and parameters.

 

PostgreSQL module



This is where we tell puppet to install PostgreSQL server. This module is developed and managed by puppet laps. Let's see a basic usage of this module. Refer here for a complete reference of this module.

ip_mask_deny_postgres_user 
This parameter restricts from where a super user can access the server. That is, server will deny IP addresses matches the given regular expression. I have set it to match 0.0.0.0/32 which will not match any IP addresses, thus a super can access the server from anywhere.

ip_mask_allow_all_users 
This parameter restricts from where a user can access the server. That is, server will accept access from IP addresses matches to the given regular expression. I have set it to 0.0.0.0/0 which will match all IP addresses, thus a user can access the server from anywhere.

postgres_password
This parameter sets the password for super user, postgres.
There are a whole lot of additional arguments which you can give to this class. Refer here for a complete reference.

Then, we are creating a database and a user for that database.


This is one of the many ways we can create databases using this module.

 

phpPgAdmin module


Then, we are installing phpPgAdmin, browser based management console for PostgreSQL.


This module is developed and managed by me, can be found here.

Let's have a look at all parameters one by one.

db_host 
The hostname/IP address of the PostgreSQL server to manage. I am installing phpPgAdmin in the same node as PostgreSQL server, hence set it to localhost. Default value is localhost.

db_port
The PostgreSQL port. Default value is 5432.

owned_only
This parameter restricts who can view what. If it is true, a user can view only his databases. If it is false, a user can view all the databases on the server.

extra_login_security
If extra login security is true, then logins via phpPgAdmin with no password or certain usernames (pgsql, postgres, root, administrator) will be denied.

These are some basic usage of this class. There are some more things will be parameterized in near future.

Finally, we are defining an order of installation.
Class['postgresql::server']->Class['phppgadmin']~> Class['agent']

That's it from puppet master side.

 

Deploying PostgreSQL cartridge


You can deploy PostgreSQL cartridge now. A sample deployment on an openstack environment looks like below,


Be sure to set port to 5432. Otherwise, PostgreSQL instance will not be activated.

You have successfully deployed PostgreSQL cartridge now.



You can subscribe to it now.



Go to My Cartridges,


Click your PostgreSQL cartridge to get your access URLs,


 

Accessing subscribed PostgreSQL


Locate http://$member_public_IP on your browser, you will get access to PostgreSQL server via phpPgAdmin. If you want, you can map postgres.stratos.com to the member public IP in your hosts file and access it on http://postgres.stratos.com/


You can login using any of the user you have defined in your node definition or you can login as a super user(postgres). I have defined a user called root in my node definition (nodes.pp), I can login as root/root. You can login as postgres/postgres if you haven't created any user.


That's it. You can do everything that you would do your own PostgreSQL instance installed on your personal computer.

Saturday, July 5, 2014

Install OpenStack Icehouse on Linux Debian based operating systems (Mint-17, Ubuntu-14.04-LTS)

OpenStack is a free and opensource IaaS platform. I found some difficulties while installing OpenStack on my Ubuntu 14.04 last week. Hence, I am writing to guide those who are like me :)

It would be great if you install OpenStack on a virtual machine. Install Oracle's Virtual Box. Then install Ubuntu or Mint on Virtual Box.

We are going to use DevStack to install OpenStack. DevStack is a script used to create OpenStack environment quickly.

Step-1 : Install Git

Install Git if you don't have Git installed on your system.

sudo apt-get update
sudo apt-get install git
 

Step-2 : Clone DevStack

Clone latest DevStack to a folder of your choice. Lets refer it as $DevStackHome. Run the following command on your terminal. It will create a folder (devstack) inside $DevStackHome and clone it.

git clone https://github.com/openstack-dev/devstack.git

 

Step-3 : Stack!

Navigate to devstack folder and run the following command to install OpenStack.

./stack.sh

Follow the instructions to install it. It will ask passwords for several modules (database, rabbit, service admin, service authentication) while installing. Would be good if you give same password to all.

Once it is installed successfully, you will get something like below on your terminal.


As you can see in the above log, you can access OpenStack @ http://10.0.2.15/project/


You might get the following error when installing it.

Resolving cdn.download.cirros-cloud.net (cdn.download.cirros-cloud.net)... failed: Name or service not known.
wget: unable to resolve host address 'cdn.download.cirros-cloud.net'
+ exit_trap
+ local r=4
++ jobs -p
+ jobs=
+ [[ -n '' ]]
+ kill_spinner
+ '[' '!' -z '' ']'
+ [[ 4 -ne 0 ]]
+ echo 'Error on exit'
Error on exit
+ ./tools/worlddump.py -d
usage: worlddump.py [-h] [-d DIR]
worlddump.py: error: argument -d/--dir: expected one argument

Refer this guide to find a workaround.

Step-4 : Unstack!

Don't forget to unstack OpenStack before shutting down your machine. If you do so, you can rejoin to the same session later.

Navigate to devstack folder and run the following command.

./unstack.sh

 

 Step-5 : Rejoin-Stack!

Don't forget to rejoin-stack OpenStack once you start your machine again. If you rejoin, you can access the previous session. If you stack, it will destroy all the previous sessions (settings, instances, images etc) and create a fresh OpenStack for you.

Navigate to devstack folder and run the following command.

./rejoin-stack.sh

If you rejoined, you can open a new tab/terminal to unstack. 

 

 Step-6 : Again Stack!

If you want to destroy previous sessions, you simply can stack.

Navigate to devstack folder and run the following command.

./stack.sh

That's all. Play with OpenStack!

Unable to resolve host address 'cdn.download.cirros-cloud.net' while installing OpenStack on Virtual Box

If you are installing OpenStack on a machine, running on Virtual Box, which is connected to a private network (lets say, to company wifi), you might get the following error.

Resolving cdn.download.cirros-cloud.net (cdn.download.cirros-cloud.net)... failed: Name or service not known.
wget: unable to resolve host address 'cdn.download.cirros-cloud.net'
+ exit_trap
+ local r=4
++ jobs -p
+ jobs=
+ [[ -n '' ]]
+ kill_spinner
+ '[' '!' -z '' ']'
+ [[ 4 -ne 0 ]]
+ echo 'Error on exit'
Error on exit
+ ./tools/worlddump.py -d
usage: worlddump.py [-h] [-d DIR]
worlddump.py: error: argument -d/--dir: expected one argument

It is trying to download Cirros OS image. As you can see in the error message, reason might be either the server is down or you are not allowed to access the server.

I tried to install OpenStack on a virtual machine running on virtual box using my company wifi. Installation failed several times with above message. PING is also not working.

Solution - 1

Edit stackrc file and give a download url that can be reached.

 

Solution -2 

Simple solution would be to use your dongle to install it. All the modules are downloaded already. Do not worry about data :)

Tuesday, May 13, 2014

Solution for wss4j module compilation failure while building WSO2 Carbon Kernel

Problem - Building WSO2 Carbon often ended up with the following error, if there is a mismatch in the JDK version.

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/rajkumar/workspace/Building-Carbon/kernal/4.2.0/dependencies/wss4j/1.5.11-wso2v6/src/org/apache/ws/security/kerberos/KrbTicketDecoder.java:[10,24] EncryptionKey is internal proprietary API and may be removed in a future release

[ERROR] /home/rajkumar/workspace/Building-Carbon/kernal/4.2.0/dependencies/wss4j/1.5.11-wso2v6/src/org/apache/ws/security/kerberos/KrbTicketDecoder.java:[11,33] EncTicketPart is internal proprietary API and may be removed in a future release

[ERROR] /home/rajkumar/workspace/Building-Carbon/kernal/4.2.0/dependencies/wss4j/1.5.11-wso2v6/src/org/apache/ws/security/kerberos/KrbTicketDecoder.java:[12,33] Ticket is internal proprietary API and may be removed in a future release

[ERROR] /home/rajkumar/workspace/Building-Carbon/kernal/4.2.0/dependencies/wss4j/1.5.11-wso2v6/src/org/apache/ws/security/kerberos/KrbTicketDecoder.java:[13,40] KeyUsage is internal proprietary API and may be removed in a future release

[ERROR] /home/rajkumar/workspace/Building-Carbon/kernal/4.2.0/dependencies/wss4j/1.5.11-wso2v6/src/org/apache/ws/security/kerberos/KrbTicketDecoder.java:[14,24] DerInputStream is internal proprietary API and may be removed in a future release

[ERROR] /home/rajkumar/workspace/Building-Carbon/kernal/4.2.0/dependencies/wss4j/1.5.11-wso2v6/src/org/apache/ws/security/kerberos/KrbTicketDecoder.java:[15,24] DerValue is internal proprietary API and may be removed in a future release

. . . . etc.


Solution - WSO2 Carbon supports Java 1.6, at this point of writing. Hence, if you build using Java 1.7, you will get the above error.
Install Java 1.6, set the environments variables accordingly and try to build.


Solution for java.lang.OutOfMemoryError: PermGen space error while building WSO2 Carbon

Problem - Building WSO2 carbon often ended up with the following error, if you haven't increase memory for Maven.

The system is out of resources.
Consult the following stack trace for details.
java.lang.OutOfMemoryError: PermGen space
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at org.codehaus.plexus.compiler.javac.IsolatedClassLoader.loadClass(IsolatedClassLoader.java:56)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at org.codehaus.plexus.compiler.javac.IsolatedClassLoader.loadClass(IsolatedClassLoader.java:56)
    at com.sun.tools.javac.main.JavaCompiler.(JavaCompiler.java:354)
    at com.sun.tools.javac.main.JavaCompiler.instance(JavaCompiler.java:88)
    at com.sun.tools.javac.main.Main.compile(Main.java:424)
    at com.sun.tools.javac.main.Main.compile(Main.java:353)
    at com.sun.tools.javac.main.Main.compile(Main.java:342)
    at com.sun.tools.javac.main.Main.compile(Main.java:333)
    at com.sun.tools.javac.Main.compile(Main.java:94)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.compiler.javac.JavacCompiler.compileInProcess(JavacCompiler.java:549)
 

Solution - Building WSO2 Carbon needs additional memory allocated to Maven. You can set it as below.

 

Step 1

Run the following command in your terminal to open .bashrc file, where you can set your environment variables.
 
sudo gedit ~/.bashrc

 

Step 2

Add the following line at the end of .bashrc file.
 
MAVEN_OPTS = "-Xms512m -Xmx1024m -XX:MaxPermSize=1024m"
export MAVEN_OPTS

Close the file.

 

Step 3

Reload/Restart the .bashrc file. Run the following command in terminal.
 
source ~/.bashrc

If you don't do this step, the effect will not be taken immediately . Maven will fail due to out of memory.

If you are still getting the same error, you need to increase the memory even more. Increase -XX:MaxPermSize=1024m parameter (like 2048m, 4096m etc.) in the Maven environment variable.


Saturday, May 3, 2014

Hello World Web Service - Apache Axis2


This post explains how you can develop, deploy and access web services using Axis2 from scratch. This is going to be a very basic tutorial. We are not going to use any IDE or building tools like Maven. But, by doing so, we can understand each and every aspects of developing, deploying and consuming web services. Next part of this tutorial explains how we can do the same thing easily using Maven.

We are going to develop a hello world web service using Apache Axis2. Apache Axis2 is nothing but a web service engine. You can develop, deploy and access web services using Axis2. 

 

Installation

1. Download the latest Axis2 binary distribution here.
2. Extract it to a convenient directory. Let's call it AXIS2_HOME.

You will get the following structure under your AXIS2_HOME.

 
That's it. You have installed it.

 

Running the Axis2 server

In your terminal, navigate to "AXIS2_HOME/bin" directory and run the following command.
sh axis2server.sh

If your server starts successfully, you will get something similar as below in your terminal.

 

Deploying services 

"AXIS2_HOME/repository/services" is the deployment directory in Axis2. You have to copy your axis2 service (yourService.aar) to this directory.

Point the browser URL to http://localhost:8080/axis2/services/
You will see all the deployed services.


If you click on one service, you can get access to it's WSDL.

 

Developing an Axis2 service

Okay, let's develop our hello world axis2 service and deploy it. 

Create a folder structure as bellow,



Create a class "HelloService.java" as bellow and save under "Service" directory.

Create a service descriptor "Services.xml" as bellow and save under "Service/target/META-INF" directory.

Run the following command from "Service" directory to compile the HelloService.java and move HelloService.class to target directory.
javac HelloService.java -d target/

 
Run the following command from "Service/target" directory to package your service.
jar -cvf HelloService.aar *

 
Let's deploy our service. Copy "Service/target/HelloService.aar" to "AXIS2_HOME/repository/service" directory.

Point the browser URL to http://localhost:8080/axis2/services/.
You will see that your HelloService is listed under deployed services.

Developing a client 

Having our service up and running, lets develop a client to consume our service.

Lets use wsdl2java tool to generate client stubs which will allow you to access the service.

You don't have to download wsdl2java separately. It is shipped with Axis2 itself. You can find it  at "AXIS2_HOME/bin/wsdl2java.sh".

Create a directory "Client".

In your terminal, navigate to "Client" directory, and run the following command to generate client stubs using wsdl2java.

sh AXIS2_HOME/bin/wsdl2java.sh -uri http://localhost:8080/axis2/services/HelloService?wsdl


 
 
Two classes (HelloServiceStub.java and HelloServiceCallbackHandler.java) will be generated inside your "Client" directory in the following package hierarchy.


These two classes allow you to access the service that we have deployed earlier. Lets create our client.

Create a class "Client.java" inside the same package as the above two classes,

Lets compile client codes. Create a directory "target" inside your "Client" directory. In your terminal run the following command from your "Client" directory. It will compile all java files and move .class files to target directory.
javac -extdirs AXIS2_HOME/lib/   src/org/apache/ws/axis2/*.java  -d target/
Please note that we are setting all required jars to our class path in the above command.

Lets run our client. In your terminal, navigate to "Client/target" directory and run the following command.
java   -Djava.ext.dirs=AXIS2_HOME/lib/   org.apache.ws.axis2.Client

You will get the following output in your terminal.
Response : Hello Raj

That's it. You have successfully implemented, deployed and consumed an axis2 service.

Thursday, May 1, 2014

Developing Web Application Using Jaggery - Part 2

This is part 2 of a series of Jaggery tutorials. This post explains the syntax of Jaggery and how to develop a simple application using Jaggery. 

Please go through part 1, Developing Web Application Using Jaggery - Part 1, if you don't know about Jaggery, how to install it, run it etc.

 

Jaggery Syntax 

Jaggery file extension is .jag (like Java file extension is .java)

The following is the syntax of Jaggery.



Everything wrapped between <% %> is Jaggery code. It means that everything wrapped by <% %> runs on server.

The print(msg); method outputs a server side variable to the document.

.jag file can contains HTML code. The above code can be written as below, 


Create a directory "myApp" and save the above file as "index.jag" inside "myApp" directory. 

You have successfully created your first Jaggery App.

Let's deploy it. If you don't know how to deploy it, please refer to part 1 of this series.

Copy myApp directory to Jaggery deployment directory which is JAGGERY_HOME/apps. 

Let's start the server. If you don't know how to start the server, please refer to part 1 of this series.

Run sh JAGGERY_HOME/bin/server.sh ( JAGGERY_HOME/bin/server.bat in windows) command to start the server.

Let's access your myApp application. Point the browser URL to http://localhost:9763/myApp/. You will have your myApp up and running.

Let's learn some more built-in functions as we need those functions in our part 3 of this series.

request

This is an object which holds properties and functions regarding the HTTP request that the client made to the server.

For example, 

getRemoteAddr() - returns client's IP
getMethod() - returns type of request

For more information, please refer here


session

This is an object which holds properties and functions regarding the current session of a client.

For example, 
invalidate() - destroy the session

For more information, please refer to here

application

This is an object which holds properties which are global to the entire application instance.

For example, 
put('propName', value) - saves the value in the name of probName.

For more information, please refer to here.

I guess these are enough for next part of this series.

Develop a sample e-banking application using Jaggery




Developing Web Application Using Jaggery - Part 1


Introduction

This series of tutorials gives you a basic understanding of Jaggery framework and its capabilities in web application development. This tutorial, part 1, explains a little bit about Jaggery and how we can install it.

Jaggery is a newly designed server side JavaScript framework. You might know about JavaScript that is used at the client side to control the interaction of the user with the browser. It controls the browser, decorate the page, validate fields and do more and more stuff at the client side. At the server side, you normally have your logic in programming languages such as Java or PHP. So JavaScript runs at your computer's browser. Java runs at your server.


Isn't nice if we can write the server side logic using JavaScript? Jaggery is the solution. So you can develop a web application using JavaScript only.

 
Installation


Download the latest Jaggery here
Extract it (jaggery-0.9.0-SNAPSHOT.zip in my case) to a directory you prefer. Lets call it JAGGERY_HOME 


You will get the following folder structure.


├── apps
├── bin
├── carbon
├── etc
├── INSTALL.txt
├── LICENSE.txt
├── modules
├── README.txt
└── release-notes.html



That's it. You have installed it.

 

Running the Server


In your terminal, navigate to JAGGERY_HOME/bin directory which contains all the Jaggery execution scripts.
Run sh JAGGERY_HOME/bin/server.sh ( JAGGERY_HOME/bin/server.bat in windows) command to start the server. It starts the default carbon server bundled with the framework itself.


If your server starts successfully, you will get something like below in your terminal, 

 

Deploy Jaggery Applications

JAGGERY_HOME/apps is the deployment directory. Deploy (copy) your Jaggery application to the deployment directory. You can find some sample application in the deployment directory including the taskmaster application.

 

Accessing Jaggery Applications

Point the browser URL to http://localhost:9763/taskmaster
You will have a web application, taskmaster application, running.

So if you deploy an application $myApp, you can access your application @ http://localhost:9763/$myApp

 

Developing Jaggery Applications

Please refer Developing Web Application Using Jaggery - Part 2