Myservlet source code V1

Source: Internet
Author: User

 

<Br/> /*********************************** * ****************** <br/> * This is a sip servlet. and it is not finished yet. <br/> * current version is 1.0 <br/> * Author norz <br/> * 03/06/2010 <br/> ************** **************************************** **/<br/> package myservlet; <br/> Import javax. annotation. resource; <br/> Import javax. naming. context; <br/> Import javax. naming. initialcontext; <br/> Import J Avax. naming. namingexception; <br/> Import javax. servlet. sip. address; <br/> Import javax. servlet. sip. proxy; <br/> Import javax. servlet. sip. siperrorevent; <br/> Import javax. servlet. sip. siperrorlistener; <br/> Import javax. servlet. sip. sipservlet; <br/> Import javax. servlet. sip. sipfactory; <br/> Import javax. servlet. sip. sipservletresponse; <br/> imports javax. servlet. sip. sipsession; <br/> Import Java. io. ioexce Ption; <br/> Import Java. util. hashmap; <br/> Import Java. util. properties; <br/> Import javax. servlet. servlet; <br/> Import javax. servlet. servletconfig; <br/> Import javax. servlet. servletexception; <br/> Import javax. servlet. sip. sipservletrequest; <br/> @ suppresswarnings ("UNUSED") <br/> @ javax. servlet. sip. annotation. sipservlet <br/> public class myservlet extends sipservlet implements siperrorlistener, servl Et {<br/>/** <br/> * serial version uid. <br/> */<br/> Private Static final long serialversionuid = 1l; </P> <p> Private Static final string contact_header = "Contact "; <br/> Private Static final string sdpcontent = "application/SDP"; <br/> Private Static final string holdflag = "A = sendonly "; <br/> Private Static final string resflag = "A = recvonly"; </P> <p> // call status <br/> Private Static final string B USY = "busy"; <br/> Private Static final string held = "held"; <br/> Private Static final string waiting = "Waiting "; </P> <p> Private Static callstatuscontainer callsc; <br/>/** <br/> * The SIP factory. can be used to create Uri and requests. <br/> */<br/> @ resource <br/> private sipfactory; </P> <p> Public myservlet () {<br/>}< br/>/** <br/> * @ inheritdoc <br/> */<br/> @ suppresswarnings ("unchec Ked ") <br/> protected void doregister (sipservletrequest req) <br/> throws servletexception, ioexception {<br/> int response = sipservletresponse. SC _ OK; <br/> sipservletresponse resp = req. createresponse (response); </P> <p> hashmap <string, string> Users = (hashmap <string, string>) getservletcontext (). getattribute ("registeredusersmap"); <br/> If (users = NULL) <br/> Users = new hashmap <string, string> (); <br/> G Etservletcontext (). setattribute ("registeredusermap", users); <br/> Address = req. getaddressheader (contact_header); <br/> string fromuri = req. getfrom (). geturi (). tostring (); </P> <p> int expires = address. getexpires (); <br/> If (expires <0) {<br/> expires = req. getexpires (); <br/>}< br/> If (expires = 0) {<br/> users. remove (fromuri); <br/> If (callsc. getspecstatus (req. getfrom (). tostring ())! = NULL) {<br/> callsc. removespeccall (req. getfrom (). tostring (); <br/>}< br/>} else {<br/> resp. setaddressheader (contact_header, address); <br/> users. put (fromuri, address. geturi (). tostring (); <br/>}</P> <p> resp. send (); <br/>}< br/>/** <br/> * @ inheritdoc <br/> */<br/> protected void dosuccessresponse (sipservletresponse resp) <br/> throws servletexception, ioexception {<br/> int response = sipservletr Esponse. SC _ OK; <br/> If (resp. getstatus () = Response) <br/>{< br/> string status = callsc. getspecstatus (resp. getto (). tostring (); <br/> If (status = held) <br/>{< br/> resp. setcontentlength (0); <br/> resp. send (); <br/> keystepofcallwait (RESP); <br/>}else <br/>{< br/> callsc. addcall (resp. getfrom (). tostring (), resp. getto (). tostring (), busy); <br/> callsc. addcall (resp. getto (). tostring (), resp. getfrom (). to String (), busy); <br/>}< br/>/** <br/> * If receive a 182 queued, set the call to held, and change response to 183. <br/> * This mechanic focus on. so when dosuccessresponse also focus on <br/> * callsc. getspecstatus (string to) <br/> */<br/> protected void doprovisionalresponse (sipservletresponse resp) <br/> throws servletexception, ioexception {<br/> int response = sipservletresp Onse. SC _session_progress; <br/> If (resp. getstatus () = sipservletresponse. SC _call_queued) <br/>{< br/> call = new call (resp. getfrom (). tostring (), resp. getto (). tostring (); <br/> call. setstatus (held); <br/> callsc. addcall (call. getfrom (), call. getto (), held); // actually only to is held, and is needed. <br/> resp. setstatus (response); <br/> resp. send (); <br/>}< br/>/** <br/> * Check the session The request, if it's a initial state, handle it as <br/> * a normal invite, else if it's a confirmed state, check if it contains <br/> * SDP message. and send 200 back. <br/> */<br/> protected void doinvite (sipservletrequest req) <br/> throws servletexception, ioexception {<br/> sipsession session = req. getsession (); <br/> If (Session. getstate (). equals (sipsession. state. initial) // This is a normal invite <Br/> {<br/> proxy = req. getproxy (); <br/> proxy. setrecordroute (true); <br/> proxy. setsupervised (true); <br/> proxy. proxyto (req. getrequesturi (); <br/>} else if (Session. getstate (). equals (sipsession. state. confirmed) // This's sent by a confirmed session, so it's a re-invite <br/>{< br/> If (req. getcontenttype (). equalsignorecase (sdpcontent) & req. getcontentlength ()! = 0) {// The request contains a SDP info <br/> string STR = req. getcontent (). tostring (); <br/> If (Str. indexof (holdflag )! =-1) {// check if is a call-hold re-invite <br/> int response = sipservletresponse. SC _ OK; <br/> sipservletresponse resp = req. createresponse (response); <br/> resp. send (); <br/> callsc. addcall (req. getfrom (). tostring (), req. getto (). tostring (), held); <br/>/* <br/> * Add SDP Mrs to request <br/> */<br/> proxy = req. getproxy (); <br/> proxy. setsupervised (true); <br/> proxy. setrecordroute (true); <br/> proxy. proxyto (req. getrequesturi ()); <br/>}</P> <p >}< br/>/** <br/> * @ inheritdoc <br/> */<br/> protected void doack (sipservletrequest req) <br/> throws servletexception, ioexception {<br/> // todo: implement this method <br/>}< br/>/** <br/> * @ inheritdoc <br/> */<br/> protected void dobye (sipservletrequest req) <br/> throws servletexception, ioexception {<br/> callsc. removecall (req. getfrom (). tostring (), req. getto (). tostring (); <br/> callsc. removecall (req. getto (). tostring (), req. getfrom (). tostring ()); <br/>}< br/>/** <br/> * @ inheritdoc <br/> */<br/> protected void doerrorresponse (sipservletresponse resp) <br/> throws servletexception, ioexception {<br/> callsc. removecall (resp. getfrom (). tostring (), resp. getto (). tostring (); <br/> callsc. removecall (resp. getto (). tostring (), resp. getfrom (). tostring (); <br/>}< br/> @ override <br/> Public void noackreceived (siperrorevent arg0) {<br/> // todo auto-generated method stub </P> <p >}< br/> @ override <br/> Public void noprackreceived (siperrorevent arg0) {<br/> // todo auto-generated method stub </P> <p >}</P> <p> Public void keystepofcallwait (sipservletresponse resp) throws ioexception {<br/> address to = (Address) resp. getto (). geturi (); <br/> address from = (Address) resp. getfrom (). geturi (); <br/> sipsession session = resp. getsession (); <br/> sipservletrequest request = session. createrequest ("invite"); <br/> request. setaddressheader ("from", from); <br/> request. setaddressheader ("to", to); <br/> request. send (); <br/> request. setaddressheader ("to", from); <br/> request. setaddressheader ("from", to); <br/> request. send (); <br/>}< br/> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.