Converged Http Sessions in SailFin
方奕
2023-12-01
HttpSessionßàSipSession
(1)HttpSessionàSipSession的方法:
import javax.servlet.sip.ConvergedHttpSession;
import javax.servlet.sip.SipApplicationSession;
import javax.servlet.sip.SipSession;
ConvergedHttpSession chs = (ConvergedHttpSession) httpReq.getSession();
SipApplicationSession sas = chs.getApplicationSession();
Iterator sipSessions = sas.getSessions("SIP");
(2)HttpSessionßSipSession的方法:
目前,还没有找到明确讲解SipSessionàHttpSession的理论方法或示例代码。是不是可以这样:如果已知某SipApplicationSession是converged的,则执行以下代码找到HTTP Session。
个人想法。
SipSession ss = sipReq.getSession();
SipApplicationSession sas = ss.getApplicationSession();
Iterator httpSessions = sas.getSessions("HTTP");
use case
举例的聚合应用包含一个HTTP Servlet(ClickToDialServlet)和一个SIP Servlet(CallServlet),下面主要针对HTTP Servlet进行说明,因为它与ConvergedHttpSession的使用相关。
ClickToDialServlet允许初始化和结束一个SIP呼叫。示例代码如下:
@Resource(mappedName = "sip/convergedapp")
private SipFactory sipFactory;
public class ClickToDialServlet extends HttpServlet {
public void doGet(HttpServletRequest httpReq, HttpServletResponse httpRes)
throws ServletException, IOException {
// 初始化或结束一个SIP呼叫,取决于请求参数"action"的值
String action = httpReq.getParameter("action");
if (action.equals("call")) {
initiateSipCall(httpReq, httpRes);
} else if (action.equals("bye")) {
terminateSipCall(httpReq, httpRes);
} else {
throw new ServletException("Invalid action");
}
}
[...]
}
/**
* 初始化一个SIP call
*/
private void initiateSipCall(HttpServletRequest httpReq, HttpServletResponse httpRes) throws ServletException, IOException {
// 由一个HTTP请求生成一个新的ConvergedHttpSession
ConvergedHttpSession chs = (ConvergedHttpSession) httpReq.getSession();
// 由ConvergedHttpSession生成一个新的SipApplicationSession,并将二者关联起来。
SipApplicationSession sas = chs.getApplicationSession();
// 生成SIP URI
String localIp = InetAddress.getLocalHost().getAddress().toString();
Address fromAddr = sipFactory.createAddress("sip:foo@" + localIp + ":" + System.getProperty("SIP_PORT"));
Address toAddr = sipFactory.createAddress("sip:bar@" + httpReq.getParameter("remoteIp") + ":" + httpReq.getParameter("remotePort"));
// 生成SIP INVITE请求,此处请求是与SipApplicationSession相关的。
SipServletRequest sipReq = sipFactory.createRequest(sas, "INVITE", fromAddr, toAddr);
// 由SIP请求生成一个SipSession
SipSession sipSession = sipReq.getSession();
// 所有与新的SipSession相关的请求和响应都将交由聚合应用的CallServlet来处理
sipSession.setHandler("CallServlet");
// 将SipSession的id作为一个session属性"CallId"存储到SipApplicationSession中,从而保证稍后可以找到SipSession,结束呼叫。
sas.setAttribute("CallId", sipSession.getId());
// 发送请求
sipReq.send();
// 返回一个用于结束呼叫的web form
PrintWriter out = httpRes.getWriter();
out.println("<form action=\"ClickToDialServlet\" method=\"GET\">");
out.println("<input type=\"hidden\" name=\"action\" value=\"bye\"/>");
out.println("<input type=\"submit\" value=\"Terminate call\"/>");
out.println("</form>");
}
/**
* 结束一个SIP call.
*/
private void terminateSipCall(HttpServletRequest httpReq, HttpServletResponse httpRes) throws ServletException, IOException {
// 维护ConvergedHttpSession,根据与请求一起发送的JSESSIONID cookie进行识别
ConvergedHttpSession chs = (ConvergedHttpSession) httpReq.getSession(false);
// 得到SipApplicationSession
SipApplicationSession sas = chs.getApplicationSession();
// 得到"CallId"属性对应的SipSession
SipSession sipSession = sas.getSipSession((String) sas.getAttribute("CallId"));
// 发送BYE请求结束呼叫
sipSession.createRequest("BYE").send();
}
参考:http://blogs.sun.com/jluehe/entry/converged_http_sessions_in_sailfin