Sunday, May 30, 2010

Fedora add sudoers

I install a fresh copy of Fedora in my virtual machine and it troubled me with sudo. When I use sudo in the terminal it gave me something like the current user is not in the /etc/sudoers file. So I did as follows and rid from it.
First I log into root using "su -" (then I had to enter the password)
Then I change the mode of /etc/sudoers file so that we can write to it by executing the command chmod +w /etc/sudoers.
Thirdly I add eranda ALL=(ALL) ALL
eranda is my username of my user account in fedora.
Then I remove the writing mode if the /etc/sudoers file using chmod -w /etc/sudoers

I thought it was the finish and execute sudo but it gave me another error.
sudo: /etc/sudoers is mode 0640, should be 0440 (to execute this you need root privileges)

then I change the mode of /etc/sudoers to 440 by executing chmod 440 /etc/sudoers
Finally I can use sudo in my machine.
Search Amazon.com for movies

Tuesday, May 25, 2010

Web Service Client

I discuss about how to create the stub of the web service and check whether it works. Now I am going to discuss how to create a client program which uses the web service. Here I am going to create a client program to my favorite web service "HelloService".
I thought it would be better to know about how things work.
First of all what is a stub? In simple language it is the interface which represent web service in the client side. So using stub we can work with the web service as it is in the same project/package. 
Then why we use Axis2? This is which do the communication between stub and the web service. For this Axis2 use SOAP(Simple Object Access Protocol) messages. Here two Axis2 servers are involved. 
The first one is in the client side and the other one is in the server side. Client side Axis2 server is responsible for create SOAP messages which invoke the web service through the stub, and catch and execute the response SOAP message which send by the  server side Axis2 server. The Server side Axis2 server is responsible for catch the SOAP message which the client side Axis2 server send and invoke the web service, and create a SOAP message to the response of the web service and send it to the client side Axis2 server. (The process is more complex than this)
Now lets finish the client implementation.
First of all take a look at the stub to see how it gave an interface to the remote service(need to have a rough idea).
You will see all the parameters of the the web service are represent as java classes and their values should set through setter methods and the output is also implemented as a class and we have to use getter method to get the real output (you may not understand this but you will realize your self when creating the client class).
now follow the following steps to create a client class.....
1. Create a java class with main method.
2. Import the package which include the "stub" into your class. This step can be skipped if you creating the class in the same package.
3. Create a object of the stub class (my stub class name is HelloServiceStub.java) in the your class. 
4. Let my stub object be "stub" and the method I need to invoke in the web service be sayHello().
    Then you see that you can access that method using the stub object. (for example stub.sayHello())
    But the thing is the parameters you need to parse to that method cannot be directly send as a String, Integer or  like that data type.
    We can use the IDE or you can look at the stub to see what parameters we need to parse through sayHello() . In my case it need to parse a SayHello object. (This SayHello java class is in the stub)
    Then create a object of the class it need. 
                  SayHello sayHello = new Sayhello();
    Then set the value you need to send as parameters for the actual method. 
                  sayHello.setName(name);
    Then send the sayHello object as the parameter of the method as stub.sayHello(sayHello);
    
5. Now its time to get the response
    Response is always come as an java class object. So we need to get it to a related class reference object.
    Use IDE or stub to see the response class type and get the response to it as follows. For my case it returns a SayHelloResponse object.(This SayHelloResponse java class is in the stub)
              SayHelloResponse helloMsg = stub.sayHello(sayHello);
   So at the end we can use that respond class to get the needed output.
               String output = helloMsg.get_return();
   If you use System.out.print(output); to print you can see the output print in the console.
   After all my Hello client look like as follows

6. After creating the client class you can run it as a java application which print the web service result.
   I send Eranda as the input and and I got the result as "Hello Eranda" 


PS: When you  creating the object you have to use a try-catch around some lines to catch the RemoteException. and AxisFault exceptions (so you need to import java.rmi.RemoteException and org.apache.axis2.AxisFault classes)

Monday, May 24, 2010

Web service client using eclipse axis2 plugin

Few months ago I blog how to deploy a web service and today I am going to create a client for that web service.
Here is the java class which I am going to deploy as the service.

package WebServices;

/**
* @author Eranda
*
*/
public class HelloService {
public String sayHello(String name){
return "Hello "+name;
}

}

This we service take a "name" and return Hello name.

The following things should be done after deploying the service to verify it's existance.

Go to your browser and check whether your web service is deployed or not. Use http://localhost:8080/ProjectName/services/listServices to check it. If it doesn't show the web service list then you have to deploy your service again properly.

Here is the page you have to have.



Now click on the service name of what you deployed. It will show you the wsdl of the web service. Now copy the URL of the wsdl.

Then go to eclipse  and then select File->new->Other->Web Service Client and click Next.

You can see a window like follows,


Copy the wsdl URL to Service Definition textbox.
Set the slider to Start Client.
Then click Server link and see whether the server is selected and set Web service runtime to axis2.



Click OK
Then goto Client project: Axis2 link and set the Web service client name as your preference or leave it as it is.


Click OK
And then click Next

Then the project will create and the following window appears.


Set the parameters with your values or you may use default.
Check the box Generate a JUnit test case to test the service and click Finish and the web service stub and the test case will create after that.

Here is the look of the project after doing that.


To test the web service you can use the test case which create automatically. To do this right click on the test class (here it is HelloServiceTest.java) Run As->JUnit Test
If all the test are successful then your client side is working correctly. Later we will see how to write a class to invoke the web service methods and get results.

PS: Some times you may get Axis faults. This exception throws because of  the wrong server host in stub. By correcting the stub(there are 2 places to correct) with the correct host name you can make it correct. (This host change thing mostly occurred because of the dynamic IP of the internet connections.)
Search Amazon.com for web services java

Sunday, May 9, 2010

PATH variable went wrong

Today I update /etc/bash.bashrc and set PATH variable wrong. This cause the terminal malfunction. Because the files need to function the commands are not exported. So I can't even open a file with terminal.
Shoot this trouble is simple(but I tried last 2 hours to shoot this :D)
What I have done is add /usr/bin to the PATH by executing export PATH=$PATH:/usr/bin
Then I could used the terminal as normal and correct the bash.bashrc file.