Monday, July 25, 2011

Add a new servlet to an OSGI bundle



Servlets are useful while handling requests coming from web browsers and setting responses according to the requests and after process them.
In WSO2 carbon environment,OSGI UI bundles are exposed to web and sometimes there might be cases where we need to use a servlet and process on it according to web browser requests.Following steps will help you in such cases when you need to add a servlet for an UI bundle.

Pre-requesties-
  • Any product server from WSO2 Carbon products [1]
  • IntelliJ IDEA 10.0
  • Maven 2.1.0 or higher
  • JDK 1.6 or higher

1.First create a servlet class by extending HttpServlet abstract class inside an OSGI bundle.You can create a new UI-bundle by creating a new maven project and add this servlet class inside it..You can refer the WSO2 dashboard UI bundle code structure from here[2].Else For a new bundle you need to import 'javax.servlet' package from bundle pom.xml as we did for wso2 dashboard UI bundle pom.xml[3].


2.Then you need to add a runtime reference to 'servlet.context.service' and made available it to the bundle by the Service Component Runtime as below.

You can get an idea on this,by refering how we have done for WSO2 dashboard UI bundle as in DashboardUiServiceComponent.java class [4].

3.Then you need to register that created servlet to the OSGI bundle by adding following entry to component.xml file which is in resources/META-INF/component.xml.



In above is used to execute the servlet by mapping this entry value with incoming request URLs to the server. You can have a look on how we have registered GadgetServlet in WSO2 dashboard UI bundle by referring[5].

4.Add the functionalities that you need to process in the servlet to the overridden doGet() method or doPost() methods of the servlet.
4.Build the bundle you added the servlet.Then to test the servlet,if you have created a new bundle(maven project) build it and copy it to the location WSO2_Product_server_home/repository/components/dropins.Then re-start the server and access the following URL.

http(s)://hostName:port/pattern>

Here equal to the value you have added earlier for the entry in component.xml file.

For your easier I have uploaded a sample OSGI UI bundle that has registered one servlet in it.You can download it from here.This UI bundle named as HelloWorld and a servlet called 'HelloWorldServlet' has registered in it by following above steps.Download it,unzip it and have a look on the code of it.Then build it and add the generated .jar file in target folder to the location server_home/repository/components/dropins,and start your WSO2 product server.Try whether you can access the following URL.

http(s)://hostName:port/hello

When accessing above url you'll see 'helloWorld' word will print on web browser.
Such that the HelloworldServlet has successfully registered in UI bundle.



5 comments:

  1. When I deploy the example you have provided to WSO2 BPS and try to access it I am redriected to the login screen. If I am logged in, the servlet works correctly. is this expected?

    ReplyDelete
    Replies
    1. Hi DIG,

      Yes it's the expected way.If you want to access the servlet without login,then you need to by-pass the servlet url by adding the 'bypass' parameter of the framework to component.xml file as in [http://code.google.com/p/open-space-blog-samples/downloads/detail?name=component.xml&can=2&q=].
      You can view how we have done this for dashboard UI component,by referring above link [5].

      Delete
  2. Hi.
    All the URL of this article send to a not found page like this:

    Not Found

    The requested URL /repos/wso2/trunk/carbon/components/dashboard/org.wso2.carbon.dashboard.ui/src/main/resources/META-INF/component.xml was not found on this server.

    ReplyDelete
    Replies
    1. Hi,

      Sorry for the late reply.I have updated the downloaded link.

      Delete
  3. i have manven compilation issues with the provided source. I was planning to add a servlet to pre load some data which we use during mediation. My idea was to add it and configure it tomcat as a listner. Please suggest if there are any issues with that.

    ReplyDelete