basemessagefactory.java
来自「结构非常清晰的SIP协议栈」· Java 代码 · 共 373 行 · 第 1/2 页
JAVA
373 行
/*
* Copyright (C) 2005 Luca Veltri - University of Parma - Italy
*
* This file is part of MjSip (http://www.mjsip.org)
*
* MjSip is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MjSip is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MjSip; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author(s):
* Luca Veltri (luca.veltri@unipr.it)
*/
package org.zoolu.sip.message;
import org.zoolu.sip.address.*;
import org.zoolu.sip.header.*;
import org.zoolu.sip.dialog.Dialog;
import org.zoolu.sip.provider.SipStack;
import org.zoolu.sip.provider.SipProvider;
import org.zoolu.sip.message.Message;
import org.zoolu.sip.message.SipMethods;
import org.zoolu.sip.message.SipResponses;
import java.util.Vector;
/** BaseMessageFactory is used to create SIP messages, requests and
* responses by means of
* two static methods: createRequest(), createResponse().
* <BR> A valid SIP request sent by a UAC MUST, at least, contain
* the following header fields: To, From, CSeq, Call-ID, Max-Forwards,
* and Via; all of these header fields are mandatory in all SIP
* requests. These sip header fields are the fundamental building
* blocks of a SIP message, as they jointly provide for most of the
* critical message routing services including the addressing of
* messages, the routing of responses, limiting message propagation,
* ordering of messages, and the unique identification of transactions.
* These header fields are in addition to the mandatory request line,
* which contains the method, Request-URI, and SIP version.
*/
public abstract class BaseMessageFactory
{
/** Creates a SIP request message.
* @param method method name
* @param request_uri request-uri
* @param to ToHeader NameAddress
* @param from FromHeader NameAddress
* @param contact Contact NameAddress (if null, no ContactHeader is added)
* @param host_addr Via address
* @param host_port Via port number
* @param call_id Call-ID value
* @param cseq CSeq value
* @param local_tag tag in FromHeader
* @param remote_tag tag in ToHeader (if null, no tag is added)
* @param branch branch value (if null, a random value is picked)
* @param body body (if null, no body is added) */
public static Message createRequest(String method, SipURL request_uri, NameAddress to, NameAddress from, NameAddress contact, String proto, String via_addr, int host_port, boolean rport, String call_id, long cseq, String local_tag, String remote_tag, String branch, String body)
{ Message req=new Message();
//mandatory headers first (To, From, Via, Max-Forwards, Call-ID, CSeq):
req.setRequestLine(new RequestLine(method,request_uri));
ViaHeader via=new ViaHeader(proto,via_addr,host_port);
if (rport) via.setRport();
if (branch==null) branch=SipProvider.pickBranch();
via.setBranch(branch);
req.addViaHeader(via);
req.setMaxForwardsHeader(new MaxForwardsHeader(70));
if (remote_tag==null) req.setToHeader(new ToHeader(to));
else req.setToHeader(new ToHeader(to,remote_tag));
req.setFromHeader(new FromHeader(from,local_tag));
req.setCallIdHeader(new CallIdHeader(call_id));
req.setCSeqHeader(new CSeqHeader(cseq,method));
//optional headers:
if (contact!=null)
{ MultipleHeader contacts=new MultipleHeader(SipHeaders.Contact);
contacts.addBottom(new ContactHeader(contact));
//System.out.println("DEBUG: Contact: "+contact.toString());
req.setContacts(contacts);
}
req.setExpiresHeader(new ExpiresHeader(String.valueOf(SipStack.default_expires)));
// add User-Agent header field
if (SipStack.ua_info!=null) req.setUserAgentHeader(new UserAgentHeader(SipStack.ua_info));
//if (body!=null) req.setBody(body); else req.setBody("");
req.setBody(body);
//System.out.println("DEBUG: MessageFactory: request:\n"+req);
return req;
}
/** Creates a SIP request message.
* Where <UL>
* <LI> via address and port are taken from SipProvider
* <LI> transport protocol is taken from request-uri (if transport parameter is present)
* or the default transport for the SipProvider is used.
* </UL>
* @param sip_provider the SipProvider used to fill the Via field
* @see #createRequest(String,SipURL,NameAddress,NameAddress,NameAddress,String,String,int,String,long,String,String,String,String) */
public static Message createRequest(SipProvider sip_provider, String method, SipURL request_uri, NameAddress to, NameAddress from, NameAddress contact, String call_id, long cseq, String local_tag, String remote_tag, String branch, String body)
{ 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();
return createRequest(method,request_uri,to,from,contact,proto,via_addr,host_port,rport,call_id,cseq,local_tag,remote_tag,branch,body);
}
/** Creates a SIP request message.
* Where <UL>
* <LI> request-uri equals the To sip url
* <LI> via address and port are taken from SipProvider
* <LI> transport protocol is taken from request-uri (if transport parameter is present)
* or the default transport for the SipProvider is used.
* <LI> call_id is picked random
* <LI> cseq is picked random
* <LI> local_tag is picked random
* <LI> branch is picked random
* </UL>
* @see #createRequest(String,SipURL,NameAddress,NameAddress,NameAddress,String,String,int,String,long,String,String,String,String) */
public static Message createRequest(SipProvider sip_provider, String method, SipURL request_uri, NameAddress to, NameAddress from, NameAddress contact, String body)
{ //SipURL request_uri=to.getAddress();
String call_id=sip_provider.pickCallId();
int cseq=SipProvider.pickInitialCSeq();
String local_tag=SipProvider.pickTag();
//String branch=SipStack.pickBranch();
return createRequest(sip_provider,method,request_uri,to,from,contact,call_id,cseq,local_tag,null,null,body);
}
/** Creates a SIP request message.
* Where <UL>
* <LI> request-uri equals the To sip url
* <LI> via address and port are taken from SipProvider
* <LI> transport protocol is taken from request-uri (if transport parameter is present)
* or the default transport for the SipProvider is used.
* <LI> contact is formed by the 'From' user-name and by the address and port taken from SipProvider
* <LI> call_id is picked random
* <LI> cseq is picked random
* <LI> local_tag is picked random
* <LI> branch is picked random
* </UL>
* @see #createRequest(SipProvider,String,NameAddress,NameAddress,NameAddress,String) */
public static Message createRequest(SipProvider sip_provider, String method, NameAddress to, NameAddress from, String body)
{ String contact_user=from.getAddress().getUserName();
NameAddress contact=new NameAddress(new SipURL(contact_user,sip_provider.getViaAddress(),sip_provider.getPort()));
return createRequest(sip_provider,method,to.getAddress(),to,from,contact,body);
}
/** Creates a SIP request message within a dialog, with a new branch via-parameter.
* @param dialog the Dialog used to compose the various Message headers
* @param method the request method
* @param body the message body */
public static Message createRequest(Dialog dialog, String method, String body)
{ NameAddress to=dialog.getRemoteName();
NameAddress from=dialog.getLocalName();
NameAddress target=dialog.getRemoteContact();
if (target==null) target=to;
SipURL request_uri=target.getAddress();
if (request_uri==null) request_uri=dialog.getRemoteName().getAddress();
SipProvider sip_provider=dialog.getSipProvider();
String via_addr=sip_provider.getViaAddress();
int host_port=sip_provider.getPort();
boolean rport=sip_provider.isRportSet();
String proto;
if (target.getAddress().hasTransport()) proto=target.getAddress().getTransport();
else proto=sip_provider.getDefaultTransport();
NameAddress contact=dialog.getLocalContact();
if (contact==null) contact=from;
// increment the CSeq, if method is not ACK nor CANCEL
if (!SipMethods.isAck(method) && !SipMethods.isCancel(method)) dialog.incLocalCSeq();
String call_id=dialog.getCallID();
long cseq=dialog.getLocalCSeq();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?