August 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

How to Write Axis2 Web Service Clients for the Cloud

  • May 13, 2009
  • By Deepal Jayasinghe
  • Send Email »
  • More Articles »

Setting up the public key and certificate

The next step of writing the client is setting up the public key and the certificate, for that you first need to have an EC2 account. If you do not have one, create an account and then use the EC2 tools to generate the public key and the certificate. For more information about creating those two keys refer to the resource section.

Writing the client

As we have a number of times to use Amazon EC2 we need to use WS-Security so we need to setup the required security for the client. One of the key requirements of that is to add the required modules into the classpath. In this case we need to have Axis2 security module in the classpath, otherwise the code we are going to write will not work. So download the Axis2 security module (Apache Rampart) and put that into classpath.

There are a number of ways of creating the instance of Stub, and that does not have a direct impact on the invocation. So here we are going to create the instance of Stub inside the constructor of the client as follow:

  ConfigurationContext configCtx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
  
  Create an instance of the stub.
  
              stub = new AmazonEC2Stub(configCtx);

We first create a configuration context, for the most part we do not need that. However there are situations where we need to create ConfigurationContext using the custom Axis2 configuration file and our own repository, in those cases we need this step.

Next we need to initialize some of the required parameters, like populate key stores and configure security handler. You can find the required code for those below. As we discussed previously we are going to use the attached utils.jar for this because that particular library file has all the required methods and classes we are going to use.

ec2PrivateKey = EC2Utils.pem2der(ec2PrivateKey);
ec2Cert = EC2Utils.pem2der(ec2Cert);
File jks = new File(EC2_JKS);
if (jks.exists()) {
     jks.delete();
}
SecurityHandler.password = EC2_KS_PASSWORD;
EC2Utils.doImport(EC2_JKS, ec2PrivateKey, ec2Cert, EC2_USER, EC2_KS_PASSWORD);

Now we have done everything we need to setup the client, and we are ready to write the code to invoke the Amazon EC2. We are going to discuss only one of the methods (RunInstances) in the stub, we can use the same procedure for all the other methods once we understand how to use the one method.

Amazon has different kinds of instance types, so we have to write the code to cope with that. The following few lines does that.

if (instanceType == null) {
    instanceType = InstanceType.SMALL;
}

To start an instance in EC2, we need to pass the required request parameters. The following code segment handles that. The code is so simple and we do not need to discuss them one by one.

          RunInstances req = new RunInstances();
          RunInstancesType params = new RunInstancesType();
          params.setInstanceType(instanceType.getType());
          params.setImageId(amiID);
          params.setKeyName(keyName);
          params.setMaxCount(numberOfInstances);
          params.setMinCount(numberOfInstances);
  
          if (groupName == null) {
              groupName = "default";
          }
          GroupSetType gst = new GroupSetType();
          GroupItemType git = new GroupItemType();
          git.setGroupId(groupName);
          gst.setItem(new GroupItemType[]{git});
          params.setGroupSet(gst);
  
          req.setRunInstances(params);

We need to configure the security parameters for each invocation, so the following code will do that for us. This means setting up the correct security policy for the method.

          enableSecurity("RunInstances");
    

Now we have all we need, so let's call the Service. We'll use the following code to invoke the service.

          RunInstancesResponse respone = stub.RunInstances(req);

Now its time to process the response.

RunInstancesResponseType resType = respone.getRunInstancesResponse();
RunningInstancesSetType instances = resType.getInstancesSet();
RunningInstancesItemType[] items = instances.getItem();
ArrayList instanceIds = new ArrayList();
for (int i = 0; i < items.length; i++) {
    RunningInstancesItemType item = items[i];
    instanceIds.add(item.getInstanceId());
    System.out.println("InstanceID " + item.getInstanceId());
}

You can find the complete client for invoking RunInstances in the resource section. Once you run this code it will start an EC2 instance for you. As we discussed before, we need to create the Amazon EC2 account, then the key files as well as the Amazon Machine Image. If we do not have them then it would be hard to follow this document. However the concept will remain the same, so if you want to invoke some other service you can follow the same steps.

To run the client, you need to pass the location of the public key, location of the certificate and name of the AMI. You can find a sample client in the resource section, edit and run it.

Summary

In this article we discussed how to use Axis2 tools to generate client side code, and then we discussed how to use the generated client to invoke a service. Since the Amazon EC2 has become one of the most commonly used Cloud APIs, we use Amazon EC2 WSDL to generate and invoke the service. In addition to that we discussed some of the client side security of Axis2 and how to use it. Now, you can complete the client to use the rest of the methods in the stub.

Resources

  1. utils.jar
  2. AWSClient.java
  3. SecurityHandler.java
  4. Amazon Elastic Compute Cloud




Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel