Apr 21, 2009

Servicemix - HttpConnectorDemo – Example

This post explains how to create a servicemix httpconnector component using spring so if you are not using spring don’t waste your time.

(1) First of all create one configuration file containing component declaration. Put this file in your configuration folder (e.g. WEB-INF).
E.g.
<sm:activationspec componentname=" HttpConnectorDemo" service="foo:MobileHttpBinding">
    <sm:component>
        <bean class="com.component.pollar.MobileHttpBinding">
            <property name="defaultInOut" value="false">
        </property>
        </bean>
    </sm:component>
</sm:activationspec>

(2) Map the configuration file from your web.xml file so that your component takes part in web application.
E.g.
<context-param>
    <param-name>contextConfigLocation
    <param-value>/WEB-INF/applicationContext.xml
</param-value>

Then after to load this component whenever container starts, add following snippet of code to your web.xml.

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
       org.apache.xbean.spring.context.XmlWebApplicationContext
    </param-value>
</context-param>
<listener>
    <listener-class>
       org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

(3) Now make a class file containing implementation of HttpConnector class of servicemix. Add your actual logic to processInOnly method as we have defined that <property name="defaultInOut" value="false"> so a call to this class will go to processInOnly method. Yet for safety define processInOut method and call processInOnly method from there.
E.g.
public class HttpConnectorDemo extends HttpConnector {
    public void processInOnly(HttpServletRequest request, HttpServletResponse response){
        // Your actual logic.
    }
    public void processInOut(HttpServletRequest request, HttpServletResponse response){
        processInOnly(request, response);
    }
}

(4) Now this class can act as a servlet. All we have to do is define a servlet in web.xml and map it to this class.
E. g.
<servlet>
    <servlet-name>MobileServlet</servlet-name>
    <servlet-class>
       org.apache.servicemix.components.http.SpringBindingServlet
    </servlet-class>
    <init-param>
       <param-name>endpoint</param-name>
       <param-value>MobileHttpBinding</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
        <servlet-name>Mobile<servlet-name>
        <url-pattern>/form.do</url-pattern>
</servlet-mapping>

(5) Now the part left is most easy part just make a war file and deploy it in your server. Start your browser, hit the URL and see the picture, your job is done. Just include necessary jar files in lib folder so that server can load your component properly.

Now start making components and Enjoy Servicemix ESB.