basemessagefactory.java

来自「结构非常清晰的SIP协议栈」· Java 代码 · 共 373 行 · 第 1/2 页

JAVA
373
字号
      String local_tag=dialog.getLocalTag();
      String remote_tag=dialog.getRemoteTag();
      //String branch=SipStack.pickBranch();
      Message req=createRequest(method,request_uri,to,from,contact,proto,via_addr,host_port,rport,call_id,cseq,local_tag,remote_tag,null,body);
      Vector route=dialog.getRoute();
      if (route!=null && route.size()>0)
         req.addRoutes(new MultipleHeader(SipHeaders.Route,route));
      req.rfc2543RouteAdapt();
      return req;
   }


   /** Creates a new INVITE request out of any pre-existing dialogs.
     * @see #createRequest(String,SipURL,NameAddress,NameAddress,NameAddress,String,String,int,boolean,String,long,String,String,String,String) */
   public static Message createInviteRequest(SipProvider sip_provider, SipURL request_uri, NameAddress to, NameAddress from, NameAddress contact, String body)
   {  String call_id=sip_provider.pickCallId();
      int cseq=SipProvider.pickInitialCSeq();
      String local_tag=SipProvider.pickTag();
      //String branch=SipStack.pickBranch();
      if (contact==null) contact=from;
      return createRequest(sip_provider,SipMethods.INVITE,request_uri,to,from,contact,call_id,cseq,local_tag,null,null,body);
   }


   /** Creates a new INVITE request within a dialog (re-invite). 
     * @see #createRequest(Dialog,String,String) */
   public static Message createInviteRequest(Dialog dialog, String body)
   {  return createRequest(dialog,SipMethods.INVITE,body);
   }


   /** Creates an ACK request for a 2xx response.
     * @see #createRequest(Dialog,String,String) */
   public static Message create2xxAckRequest(Dialog dialog, String body)
   {  return createRequest(dialog,SipMethods.ACK,body);
   }


   /** Creates an ACK request for a non-2xx response */
   public static Message createNon2xxAckRequest(SipProvider sip_provider, Message method, Message resp)
   {  SipURL request_uri=method.getRequestLine().getAddress();
      FromHeader from=method.getFromHeader();
      ToHeader to=resp.getToHeader();
      String via_addr=sip_provider.getViaAddress();
      int host_port=sip_provider.getPort();
      boolean rport=sip_provider.isRportSet();
      String proto;
      if (request_uri.hasTransport()) proto=request_uri.getTransport();
      else proto=sip_provider.getDefaultTransport();    
      String branch=method.getViaHeader().getBranch();
      NameAddress contact=null;
      Message ack=createRequest(SipMethods.ACK,request_uri,to.getNameAddress(),from.getNameAddress(),contact,proto,via_addr,host_port,rport,method.getCallIdHeader().getCallId(),method.getCSeqHeader().getSequenceNumber(),from.getParameter("tag"),to.getParameter("tag"),branch,null);
      ack.removeExpiresHeader();
      if (method.hasRouteHeader()) ack.setRoutes(method.getRoutes());
      return ack;
   }


   /** Creates an ACK request for a 2xx-response. Contact value is taken from SipStack */
   /*public static Message create2xxAckRequest(Message resp, String body)
   {  ToHeader to=resp.getToHeader();
      FromHeader from=resp.getFromHeader();
      int code=resp.getStatusLine().getCode();
      SipURL request_uri;
      request_uri=resp.getContactHeader().getNameAddress().getAddress();
      if (request_uri==null) request_uri=to.getNameAddress().getAddress();
      String branch=SipStack.pickBranch();
      NameAddress contact=null;
      if (SipStack.contact_url!=null) contact=new NameAddress(SipStack.contact_url);
      return createRequest(SipMethods.ACK,request_uri,to.getNameAddress(),from.getNameAddress(),contact,resp.getCallIdHeader().getCallId(),resp.getCSeqHeader().getSequenceNumber(),from.getParameter("tag"),to.getParameter("tag"),branch,body);
   }*/


   /** Creates an ACK request for a 2xx-response within a dialog */
   /*public static Message create2xxAckRequest(Dialog dialog, NameAddress contact, String body)
   {  return createRequest(SipMethods.ACK,dialog,contact,body);
   }*/


   /** Creates an ACK request for a 2xx-response within a dialog */
   /*public static Message create2xxAckRequest(Dialog dialog, String body)
   {  return createRequest(SipMethods.ACK,dialog,body);
   }*/

   
   /** Creates a CANCEL request. */
   public static Message createCancelRequest(Message method)
   {  ToHeader to=method.getToHeader();
      FromHeader from=method.getFromHeader();
      SipURL request_uri=method.getRequestLine().getAddress();
      NameAddress contact=method.getContactHeader().getNameAddress();
      ViaHeader via=method.getViaHeader();
      String host_addr=via.getHost();
      int host_port=via.getPort();
      boolean rport=via.hasRport();
      String proto=via.getProtocol();
      String branch=method.getViaHeader().getBranch();
      return createRequest(SipMethods.CANCEL,request_uri,to.getNameAddress(),from.getNameAddress(),contact,proto,host_addr,host_port,rport,method.getCallIdHeader().getCallId(),method.getCSeqHeader().getSequenceNumber(),from.getParameter("tag"),to.getParameter("tag"),branch,"");
   }


   /** Creates a BYE request. */
   public static Message createByeRequest(Dialog dialog)
   {  Message msg=createRequest(dialog,SipMethods.BYE,null);
      msg.removeExpiresHeader();
      msg.removeContacts();
      return msg;
   }


   /** Creates a new REGISTER request.
     * <p> If contact is null, set contact as star * (register all) */
   public static Message createRegisterRequest(SipProvider sip_provider, NameAddress to, NameAddress from, NameAddress contact)
   {  SipURL to_url=to.getAddress();
      SipURL registrar=new SipURL(to_url.getHost(),to_url.getPort());     
      String via_addr=sip_provider.getViaAddress();
      int host_port=sip_provider.getPort();
      boolean rport=sip_provider.isRportSet();
      String proto;
      if (to_url.hasTransport()) proto=to_url.getTransport();
      else proto=sip_provider.getDefaultTransport();    
      String call_id=sip_provider.pickCallId();
      int cseq=SipProvider.pickInitialCSeq();
      String local_tag=SipProvider.pickTag();
      //String branch=SipStack.pickBranch();
      Message req=createRequest(SipMethods.REGISTER,registrar,to,from,contact,proto,via_addr,host_port,rport,call_id,cseq,local_tag,null,null,null);
      // if no contact, deregister all
      if (contact==null)
      {  ContactHeader star=new ContactHeader(); // contact is *
         req.setContactHeader(star);
         req.setExpiresHeader(new ExpiresHeader(String.valueOf(SipStack.default_expires)));
      }
      return req;
   }


   //################ Can be removed? ################
   /** Creates a new REGISTER request.
     * <p> If contact is null, set contact as star * (register all) */
   /*public static Message createRegisterRequest(SipProvider sip_provider, NameAddress to, NameAddress contact)
   {  return createRegisterRequest(sip_provider,to,to,contact);
   }*/


   /** Creates a SIP response message.
     * @param req the request message
     * @param code the response code
     * @param reason the response reason
     * @param contact the contact address
     * @param local_tag the local tag in the 'To' header
     * @param body the message body */
   public static Message createResponse(Message req, int code, String reason, String local_tag, NameAddress contact, String content_type, String body)
   {  Message resp=new Message(); 
      resp.setStatusLine(new StatusLine(code,reason));
      resp.setVias(req.getVias());
      if (code>=180 && code<300 && req.hasRecordRouteHeader())
         resp.setRecordRoutes(req.getRecordRoutes());
      ToHeader toh=req.getToHeader();
      if (local_tag!=null)
         toh.setParameter("tag",local_tag);
      resp.setToHeader(toh);
      resp.setFromHeader(req.getFromHeader());
      resp.setCallIdHeader(req.getCallIdHeader());
      resp.setCSeqHeader(req.getCSeqHeader());
      if (contact!=null) resp.setContactHeader(new ContactHeader(contact));
      // add Server header field
      if (SipStack.server_info!=null) resp.setServerHeader(new ServerHeader(SipStack.server_info));
      //if (body!=null) resp.setBody(body); else resp.setBody("");
      if (content_type==null) resp.setBody(body);
      else resp.setBody(content_type,body);
      //System.out.println("DEBUG: MessageFactory: response:\n"+resp.toString());
      return resp;
   }

   /** Creates a SIP response message. For 2xx responses generates the local tag by means of the SipStack.pickTag(req) method.
     * @see #createResponse(Message,int,String,NameAddress,String,String body) */
   public static Message createResponse(Message req, int code, String reason, NameAddress contact)
   {  //String reason=SipResponses.reasonOf(code);
      String localtag=null;
      if (req.createsDialog() && !req.getToHeader().hasTag())
      {  if (SipStack.early_dialog || (code>=200 && code<300)) localtag=SipProvider.pickTag(req);
      }
      return createResponse(req,code,reason,localtag,contact,null,null);
   }

}  

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?