3. ForwardOnNoResponse

img3.gif

This servlet is very similar to the previous one except it has a timer. doInvite() starts a timer and forward request to registered contacts. After 20(This value is specified in XML descriptor) seconds later, there was no response from any contacts, it will forwards the original request to new branch(This is also specified in descriptor). For timer function, I used ServletTimer class and TimerService class, which is defined in SIP Sevlet API 1.0 Final Version .

 

<Full Source>

/*
 * ForwardOnNoResponse.java
 *
 * When request arrives, FONR SIP servlet starts timer, and send the request
 * to the original requestURI. If there is OK response from the callee,
 * the timer is cancelled. Otherwise, timer expires and the servlet sends
 * the original request to another destination that is specified as parameter.
 *
 * Sangho Shin (ss2020@cs.columbia.edu)
 *
 */
import java.util.*;
import javax.servlet.sip.*;
import javax.servlet.*;
 
public class ForwardOnNoResponse extends SipServlet implements TimerListener   {
    ServletTimer timer;
    long waitingTime;
    SipServletRequest request;
    ServletConfig config;
    ServletContext context;
    String requestURI;
    TimerService timerService;
    LinkedList contacts;
 
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        config = getServletConfig();
        context = getServletContext();
        /* Get contacts */
        contacts = ((SipServletConfig)config).getContacts();
        /* Set waiting time */
        String timeStr = (String)config.getInitParameter("waiting-time");
        requestURI = (String)config.getInitParameter("request-uri");
        timerService  
            = (TimerService)context.getAttribute("javax.servlet.sip.TimerService");
        
        if (timeStr == null)
            waitingTime = 20;
        else
            waitingTime = Long.parseLong(timeStr);
    }
 
    public void doInvite(SipServletRequest req) throws java.io.IOException  {
        
        request = req;
 
        /* Start timer */
        timer = timerService.createTimer(req.getApplicationSession(),
                                         waitingTime*1000, false, null);
 
        /* Forward request to contacts */
        ListIterator iter = contacts.listIterator();
        Address contact = null;
        try {
            while(iter.hasNext()) {
                contact = (Address)iter.next();
                req.setRequestURI(contact.getURI());
                req.send();
            }
        } catch (java.io.IOException e) {}
    }
    
    public void doResponse(SipServletResponse res) {
            timer.cancel();
    }
 
    public void timeout(ServletTimer timer) {
        SipFactory sipFactory
            = (SipFactory)context.getAttribute("javax.servlet.sip.SipFactory");
        if (requestURI == null)
            return;
        try {
            URI uri = sipFactory.createURI(requestURI);
            request.setRequestURI(uri);
                try {
                    request.send();
                }catch (java.io.IOException e) {
                }
        }catch(ServletParseException e) {
        }
    }
 
}