Wednesday, May 5, 2010

Use Jetty with Spring

1) Insert these beans to the application-context.xml

    <bean name="smpp.gw.httpListener" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="destroy">
        <constructor-arg index="0" value="8080"/>
        <property name="handler" ref="smsc.mtHttpRequestHandler"/>
    </bean>

    <bean id="anyHandler" class="path.to.AnyHandler">
            ..................................
            ..................................
    </bean>

2) The "AnyHandler" class shold have to extends the AbstractHandler class.

     import org.eclipse.jetty.server.Request;
     import org.eclipse.jetty.server.handler.AbstractHandler;

     import javax.servlet.ServletException;
     import javax.servlet.http.HttpServletRequest;
     import javax.servlet.http.HttpServletResponse;
     import java.io.IOException;
    
     public class HttpRequestHandler extends AbstractHandler {

        public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
            System.out.println(">>>> Request received. <<<");
            // Can do anything using request and response
            ......................................
            ......................................
        }
    }