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

Concat two xml values with XSLT

The use-case described in this blog-post,is there's an WSO2 ESB node setup to proxy an incoming message to a particular back-end endpoint.  Before delivering the message to the back-end endpoint,from the ESB node itself,this incoming message need to processed and change its inside xml payload format. For eg: Below is the incoming message <?xml version="1.0" encoding="UTF-8"?> <CinemaHall name="liberty"> <OwnerData> <Name>John Smith</Name> <openedDate>12/12/80</openedDate> <quality>good</quality> </OwnerData> <CinemaHallData> <rows>100</rows> <seats> <seat>50</seat> <seat>60</seat> </seats> </CinemaHallData> </CinemaHall> This message need to be changed as  below; <?xml version="1.0" encoding="UTF-8"?> <CinemaHall name="liberty"...

Passing end-user details from client to real backend endpoint via JWT token

In real-world business system,WSO2 API Manager useful on exposing company APIs, in a secured and controlled manner with the features provided by APIManager as; OAuth support [To secure API invocations] Throttling support [To control API invocations] Monitoring support [To track API usage] More technically what happening is when a user sends a particular API request,it will goes to WSO2 APIManager node and from there,the request will route to the real implemented back-end endpoint of the particular API and get back the response and returned it to the API invoked user. There can be a use-case,that this back-end endpoint may expect the details of API invoked user as to pass those details to some internal company usage  as; Additional authentication/authorization Track usage data from an internal system. So how to support above requirement from WSO2 AM. There comes the use of JSON Web Token[JWT] implementation done inside WSO2 AM. JWT is a means of representing claims to...