Skip to main content

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.



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

Post a Comment

Popular posts from this blog

Convert an InputStream to XML

For that we can use DocumentBuilder class in java. By using the method parse(InputStream) ; A new DOM Document object will return. InputStream input; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); Document dc= parser.parse(input); In the above code segment,by using the created Document object,the corresponding XML file for the inputStream can be accessed. References: http://www.w3schools.com/dom/dom_intro.asp http:// download.oracle.com/javase/1.4.2/docs/api/javax/xml/parsers/DocumentBuilder.html

CORS support from WSO2 API Manager 2.0.0

Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources  on a web page to be requested from another domain outside the domain from which the first restricted resource was served. For example, an HTML page of a web application served from http://domain-a.com makes an <img src >  request for a different domain as 'domain-b.com' to get an image via an API request.  For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts as in above example and only allows to make HTTP requests to its own domain. To avoid this limitation modern browsers have been used CORS standard to allow cross domain requests. Modern browsers use CORS in an API container - such as  XMLHttpRequest  or Fetch - to mitigate risks of cross-origin HTTP requests.Thing to  note is it's not only sufficient that the browsers handle client side of cross-origin sharing,but also the servers from which these resources getting need to handl

[WSO2 AM] APIStore User Signup as an approval process

In previous versions of WSO2 APIManager before 1.6.0, it was allowed any user who's accessible the running APIStore come and register to the app.But there will be requirement like,without allowing any user to signup by him/her self alone,first get an approve by a privileged user and then allow to complete app registration.Same requirement can be apply to application creation and subscription creation as well.To fulfill that,we have introduced workflow extension support for  WSO2 APIManager  and you can find the introductory post on this feature from my previous blog post on " workflow-extentions-with-wso2-am-160 " . From this blog-post,I'll explain how to achieve simple workflow integration with default shipped resources with  WSO2 APIManager 1.6.0 and WSO2 Business Process Server 3.1.0 with targeting "user-signup" process. Steps First download the WSO2 APIManager 1.6.0[AM] binary pack from product download page . Extract it and navigate to