Pages

Subscribe:

Ads 468x60px

Wednesday, November 5, 2014

[WSO2] Sample Web Application to Demonstrate Insertion, Retrieval and Deletion of a resource to Registry

Here is a sample web application to test Insertion, Retrieval and Deletion of a  resource to Registry.
Here is the sample servlet code. Github Link

package org.wso2.carbon.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.registry.api.Registry;
import org.wso2.carbon.registry.api.Resource;
/**
 * Servlet implementation class RegistryTest
 */
@WebServlet("/RegistryTest")
public class RegistryTest extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /**
     * Default constructor.
     */
    public RegistryTest() {
        // TODO Auto-generated constructor stub
    }
    protected void processRequest(HttpServletRequest request,
                                  HttpServletResponse response) throws ServletException, IOException {
        try {
            PrintWriter out = response.getWriter();
            System.out.print("URL = " + request.getRequestURL());
            System.out.print(" :: action = " + request.getParameter("action"));
            System.out.print(" :: path = " + request.getParameter("path"));
            System.out.println(" :: resource = "+ request.getParameter("resource"));
            String resourcePath = request.getParameter("path");
            String resourceValue = request.getParameter("resource");
            String action = request.getParameter("action");
            CarbonContext cCtx = CarbonContext.getThreadLocalCarbonContext();
            Registry registry = cCtx.getRegistry(RegistryType.SYSTEM_CONFIGURATION);
            if (resourcePath != null && action != null) {
                if (action.equalsIgnoreCase("add")) {
                    if( resourceValue != null){
                        Resource resource = registry.newResource();
                        resource.setContent(resourceValue);
                        registry.put(resourcePath, resource);
                        out.println("Resource added successfully!!");
                        out.println("Registry path :: " + resourcePath);
                        out.println("Registry value :: " + resourceValue);
                    }else{
                        out.println("ERROR :: Resource Value Empty!!!");
                    }
                } else if (action.equals("get")) {
                    if (registry.resourceExists(resourcePath)) {
                        Resource resource = registry.get(resourcePath);
                        String content = new String((byte[]) resource.getContent());
                        response.addHeader("resource-content", content);
                        out.println("Resource Found in Registry returned!!!");
                        out.println("Registry path :: " + resourcePath);
                        out.println("Registry value :: " + content);
                    } else {
                        out.println("ERROR :: Resource Not Found in Registry!!!");
                        out.println("Registry path :: " + resourcePath);
                    }
                } else if (action.equalsIgnoreCase("delete")) {
                    if (registry.resourceExists(resourcePath)) {
                        Resource resource = registry.get(resourcePath);
                        String content = new String((byte[]) resource.getContent());
                        registry.delete(resourcePath);
                        out.println("Resource Found and deleted!!!");
                        out.println("Registry path :: " + resourcePath);
                        out.println("Registry value :: " + content);
                    } else {
                        out.println("ERROR :: Resource Not Found in Registry!!!");
                        out.println("Registry path :: " + resourcePath);
                    }
                }
            } else {
                out.println("ERROR :: Resource Error!!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
}

To test the application host it to WSO2 Application Server. Execute the following commands.

Adding resource

curl --data "action=add&path=&resource=" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest
e.g

curl --data "action=add&amp;path=/foo/bar&amp;resource=AAAAAAAAA" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest<br />

Getting resource

curl --data "action=get&amp;path=" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest
e.g
curl --data "action=get&amp;path=/foo/bar" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest<br />

Deleting resource

curl --data "action=delete&amp;path=" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest
e.g

curl --data "action=delete&amp;path=/foo/bar" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest<br />

Monday, November 3, 2014

Create a WSO2 Worker-Manager Cluster in Just 2 Minutes !

I've been working on an application(WSO2 Cluster Wizard) which creates a Worker-Manager Separated cluster for a given WSO2 Product. The objective of this application is to reduce the time spent on creating clusters in developers/testing local machines. Though puppet scripts can automate the process AFAIK, no one uses puppets to create clusters in their local setups'. 
This is a simple GUI application which is very easy to use. Here is a screen shot of the UI.
 
 Basic Functionality.
  1. Creates Worker-Manager separated cluster (WKA based)
  2. Choice of enabling registry mounting
  3. Works on almost every WSO2 product
  4. Works on both Windows and Linux environments 
How to use 
Run the application using java -jar ClusterWizard.jar
  1. Select the zip file of product you want to cluster
  2. Select the destination folder
  3. Fill out the Manager and Worker settings
  4. If registry mounting enabled fill out the mysql connection details
  5. Hit execute button.
Hope this will be useful, and highly appreciate your feedback. :)
In the next version I'll be hoping to add the WSO2ELB configuration support also.
Source can be found at. Github Link