SipServlet使得开发Sip程序大大简化,且其HttpServlet的同根性使得对Sip协议不是很熟悉的人也能够很快上手.
下面给出一个简单的例子:
/*
* wgb
* 创建于 2007-1-1
*
*/
package com.wgb.sipservlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.sip.SipServlet;
import javax.servlet.sip.SipServletRequest;
import javax.servlet.sip.SipServletResponse;
import javax.servlet.sip.SipURI;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author wgb
* 于 2007-1-1
*/
public class EchoServlet extends SipServlet
{
/**
* keeps the mapping between sign-in name and actual contact address
* 保存登录名称和实际联系地址
*/
private Map addresses = new HashMap();
/**
* 记录日志
*/
private Log logger = LogFactory.getLog(EchoServlet.class);
/**
* Version ID
*/
private static final long serialVersionUID = -5396693750763357846L;
/* (non-Javadoc)
* @see javax.servlet.GenericServlet#init()
*/
public void init() throws ServletException
{
super.init();
}
/* 收到一个 Invite 发起请求
* @see javax.servlet.sip.SipServlet#doInvite(javax.servlet.sip.SipServletRequest)
*/
protected void doInvite(SipServletRequest req) throws ServletException,
IOException
{
logger.debug(req);
req.createResponse(SipServletResponse.SC_OK).send();
}
/* 接受注册
* @see javax.servlet.sip.SipServlet#doRegister(javax.servlet.sip.SipServletRequest)
*/
protected void doRegister(SipServletRequest req) throws ServletException,
IOException
{
logger.info(req);
String aor = req.getFrom().getURI().toString().toLowerCase();
synchronized (addresses)
{
//非0值表示注册
if (req.getExpires() != 0)
{
addresses.put(aor, req.getAddressHeader("Contact").getURI());
}
//0值表示注销
else
{
addresses.remove(aor);
}
}
//发送200OK响应
req.createResponse(SipServletResponse.SC_OK).send();
}
/* (non-Javadoc)
* @see javax.servlet.sip.SipServlet#doSubscribe(javax.servlet.sip.SipServletRequest)
*/
protected void doSubscribe(SipServletRequest req) throws ServletException,
IOException
{
logger.debug(req);
req.createResponse(SipServletResponse.SC_OK).send();
}
/* (non-Javadoc)
* @see javax.servlet.sip.SipServlet#doMessage(javax.servlet.sip.SipServletRequest)
*/
protected void doMessage(SipServletRequest req) throws ServletException,
IOException
{
SipURI sipURI = null;
synchronized (addresses)
{
sipURI = (SipURI) addresses.get(req.getFrom().getURI().toString()
.toLowerCase());
}
//没有注册过的,拒绝响应
if (sipURI == null)
{
req.createResponse(SipServletResponse.SC_FORBIDDEN,
"Not sign in yet").send();
return;
}
//响应200OK
req.createResponse(SipServletResponse.SC_OK).send();
//响应消息
SipServletRequest echo = req.getSession().createRequest("Message");
String charset = req.getCharacterEncoding();
if (charset != null)
{
echo.setCharacterEncoding(charset);
}
echo.setRequestURI(sipURI);
echo.setContent("你给我发的消息是: " + req.getContent(), req.getContentType());
echo.send();
}
/* 收到2xx成功响应
* @see javax.servlet.sip.SipServlet#doSuccessResponse(javax.servlet.sip.SipServletResponse)
*/
protected void doSuccessResponse(SipServletResponse res)
throws ServletException, IOException
{
if (res.getMethod().equalsIgnoreCase("Message"))
{
logger.debug("/"" + res.getContent() + "/"" + " was rejected "
+ res.getStatus());
}
}
/* 收到Bye
* @see javax.servlet.sip.SipServlet#doBye(javax.servlet.sip.SipServletRequest)
*/
protected void doBye(SipServletRequest req) throws ServletException,
IOException
{
req.createResponse(SipServletResponse.SC_OK).send();
}
}