Converged Http Sessions in SailFin

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值