use of javax.sip.header.ViaHeader in project load-balancer by RestComm.
the class TestSipListener method sendSipRequest.
public Request sendSipRequest(String method, URI fromURI, URI toURI, String messageContent, SipURI route, boolean useToURIasRequestUri, String[] headerNames, String[] headerContents, boolean setHeader) throws SipException, ParseException, InvalidArgumentException {
this.useToURIasRequestUri = useToURIasRequestUri;
// create >From Header
Address fromNameAddress = protocolObjects.addressFactory.createAddress(fromURI);
FromHeader fromHeader = protocolObjects.headerFactory.createFromHeader(fromNameAddress, Integer.toString(new Random().nextInt(10000000)));
// create To Header
Address toNameAddress = protocolObjects.addressFactory.createAddress(toURI);
ToHeader toHeader = protocolObjects.headerFactory.createToHeader(toNameAddress, null);
if (toURI instanceof SipURI) {
SipURI toSipUri = (SipURI) toURI;
// create Request URI
this.requestURI = protocolObjects.addressFactory.createSipURI(toSipUri.getUser(), peerHostPort);
((SipURI) this.requestURI).setPort(peerPort);
if (setTransport) {
((SipURI) this.requestURI).setTransportParam(listeningPoint.getTransport());
}
}
if (useToURIasRequestUri || toURI instanceof TelURL) {
this.requestURI = toURI;
}
// Create ViaHeaders
List<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
ViaHeader viaHeader = null;
if (!isIpv6)
viaHeader = protocolObjects.headerFactory.createViaHeader("127.0.0.1", sipProvider.getListeningPoint(protocolObjects.transport).getPort(), listeningPoint.getTransport(), null);
else
viaHeader = protocolObjects.headerFactory.createViaHeader("::1", sipProvider.getListeningPoint(protocolObjects.transport).getPort(), listeningPoint.getTransport(), null);
// add via headers
viaHeaders.add(viaHeader);
// Create ContentTypeHeader
// ContentTypeHeader contentTypeHeader = protocolObjects.headerFactory
// .createContentTypeHeader("application", "sdp");
// Create a new CallId header
CallIdHeader callIdHeader = sipProvider.getNewCallId();
// Create a new Cseq header
CSeqHeader cSeqHeader = protocolObjects.headerFactory.createCSeqHeader(1L, method);
// Create a new MaxForwardsHeader
MaxForwardsHeader maxForwards = protocolObjects.headerFactory.createMaxForwardsHeader(70);
// Create the request.
Request request = protocolObjects.messageFactory.createRequest(requestURI, method, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
// Create contact headers
String host = null;
if (!isIpv6)
host = "127.0.0.1";
else
host = "::1";
URI contactUrl = null;
if (fromURI instanceof SipURI) {
contactUrl = protocolObjects.addressFactory.createSipURI(((SipURI) fromURI).getUser(), host);
/**
* either use tcp or udp
*/
((SipURI) contactUrl).setPort(listeningPoint.getPort());
if (setTransport) {
((SipURI) contactUrl).setTransportParam(listeningPoint.getTransport());
((SipURI) contactUrl).setLrParam();
}
} else {
contactUrl = fromURI;
}
// Create the contact name address.
Address contactAddress = protocolObjects.addressFactory.createAddress(contactUrl);
// Add the contact address.
// contactAddress.setDisplayName(fromName);
contactHeader = protocolObjects.headerFactory.createContactHeader(contactAddress);
request.addHeader(contactHeader);
SipURI uri = null;
if (!isIpv6)
uri = protocolObjects.addressFactory.createSipURI(null, "127.0.0.1");
else
uri = protocolObjects.addressFactory.createSipURI(null, "::1");
uri.setLrParam();
uri.setTransportParam(protocolObjects.transport);
uri.setPort(this.peerPort);
if (route != null) {
Address address = protocolObjects.addressFactory.createAddress(route);
RouteHeader routeHeader = protocolObjects.headerFactory.createRouteHeader(address);
request.addHeader(routeHeader);
} else if (useDefaultRoute) {
Address address = protocolObjects.addressFactory.createAddress(uri);
RouteHeader routeHeader = protocolObjects.headerFactory.createRouteHeader(address);
request.addHeader(routeHeader);
}
// set the message content
if (messageContent != null) {
ContentLengthHeader contentLengthHeader = protocolObjects.headerFactory.createContentLengthHeader(messageContent.length());
ContentTypeHeader contentTypeHeader = protocolObjects.headerFactory.createContentTypeHeader(TEXT_CONTENT_TYPE, PLAIN_UTF8_CONTENT_SUBTYPE);
byte[] contents = messageContent.getBytes();
request.setContent(contents, contentTypeHeader);
request.setContentLength(contentLengthHeader);
}
if (headerNames != null) {
for (int q = 0; q < headerNames.length; q++) {
Header h = protocolObjects.headerFactory.createHeader(headerNames[q], headerContents[q]);
if (setHeader) {
request.setHeader(h);
} else {
request.addHeader(h);
}
}
}
addSpecificHeaders(method, request);
// Create the client transaction.
inviteClientTid = sipProvider.getNewClientTransaction(request);
// send the request out.
inviteClientTid.sendRequest();
logger.info("client tx = " + inviteClientTid);
if (!Request.MESSAGE.equalsIgnoreCase(method)) {
dialog = inviteClientTid.getDialog();
}
if (Request.INVITE.equalsIgnoreCase(method)) {
inviteRequest = request;
}
this.dialogCount++;
return inviteRequest;
}
use of javax.sip.header.ViaHeader in project smscgateway by RestComm.
the class SipMessageTest method init.
public void init() {
SipFactory sipFactory = null;
sipStack = null;
sipFactory = SipFactory.getInstance();
sipFactory.setPathName("gov.nist");
Properties properties = new Properties();
// If you want to try TCP transport change the following to
String transport = "udp";
String peerHostPort = "127.0.0.1:5060";
properties.setProperty("javax.sip.OUTBOUND_PROXY", peerHostPort + "/" + transport);
// If you want to use UDP then uncomment this.
properties.setProperty("javax.sip.STACK_NAME", "shootist");
// The following properties are specific to nist-sip
// and are not necessarily part of any other jain-sip
// implementation.
// You can set a max message size for tcp transport to
// guard against denial of service attack.
properties.setProperty("gov.nist.javax.sip.MAX_MESSAGE_SIZE", "1048576");
properties.setProperty("gov.nist.javax.sip.DEBUG_LOG", "shootistdebuglog.txt");
properties.setProperty("gov.nist.javax.sip.SERVER_LOG", "shootistlog.txt");
// Drop the client connection after we are done with the transaction.
properties.setProperty("gov.nist.javax.sip.CACHE_CLIENT_CONNECTIONS", "false");
// Set to 0 in your production code for max speed.
// You need 16 for logging traces. 32 for debug + traces.
// Your code will limp at 32 but it is best for debugging.
properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "DEBUG");
try {
// Create SipStack object
sipStack = sipFactory.createSipStack(properties);
System.out.println("createSipStack " + sipStack);
} catch (PeerUnavailableException e) {
// could not find
// gov.nist.jain.protocol.ip.sip.SipStackImpl
// in the classpath
e.printStackTrace();
System.err.println(e.getMessage());
System.exit(0);
}
try {
headerFactory = sipFactory.createHeaderFactory();
addressFactory = sipFactory.createAddressFactory();
messageFactory = sipFactory.createMessageFactory();
udpListeningPoint = sipStack.createListeningPoint("127.0.0.1", 5066, "udp");
sipProvider = sipStack.createSipProvider(udpListeningPoint);
SipMessageTest listener = this;
sipProvider.addSipListener(listener);
String fromName = "123456789";
String fromSipAddress = "127.0.0.1:5066";
String fromDisplayName = "123456789";
String toSipAddress = "127.0.0.1:5060";
String toUser = "6666";
String toDisplayName = "6666";
// create >From Header
SipURI fromAddress = addressFactory.createSipURI(fromName, fromSipAddress);
Address fromNameAddress = addressFactory.createAddress(fromAddress);
fromNameAddress.setDisplayName(fromDisplayName);
FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, "12345");
// create To Header
SipURI toAddress = addressFactory.createSipURI(toUser, toSipAddress);
Address toNameAddress = addressFactory.createAddress(toAddress);
toNameAddress.setDisplayName(toDisplayName);
ToHeader toHeader = headerFactory.createToHeader(toNameAddress, null);
// create Request URI
SipURI requestURI = addressFactory.createSipURI(toUser, peerHostPort);
// Create ViaHeaders
ArrayList viaHeaders = new ArrayList();
ViaHeader viaHeader = headerFactory.createViaHeader("127.0.0.1", sipProvider.getListeningPoint(transport).getPort(), transport, null);
// add via headers
viaHeaders.add(viaHeader);
// Create ContentTypeHeader
ContentTypeHeader contentTypeHeader = headerFactory.createContentTypeHeader("text", "plain");
// Create a new CallId header
CallIdHeader callIdHeader = sipProvider.getNewCallId();
// Create a new Cseq header
CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L, Request.MESSAGE);
// Create a new MaxForwardsHeader
MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
// Create the request.
Request request = messageFactory.createRequest(requestURI, Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
// Create contact headers
String host = "127.0.0.1";
SipURI contactUrl = addressFactory.createSipURI(fromName, host);
contactUrl.setPort(udpListeningPoint.getPort());
// Create the contact name address.
SipURI contactURI = addressFactory.createSipURI(fromName, host);
contactURI.setPort(sipProvider.getListeningPoint(transport).getPort());
Address contactAddress = addressFactory.createAddress(contactURI);
// Add the contact address.
contactAddress.setDisplayName(fromName);
contactHeader = headerFactory.createContactHeader(contactAddress);
request.addHeader(contactHeader);
// UDH Header
Header udhHeader = headerFactory.createHeader("X-SMS-UDH", "06050413011301");
request.addHeader(udhHeader);
// Add the extension header.
Header extensionHeader = headerFactory.createHeader("My-Header", "my header value");
request.addHeader(extensionHeader);
String s = this.getBody();
byte[] contents = s.getBytes();
request.setContent(contents, contentTypeHeader);
// Create the client transaction.
inviteTid = sipProvider.getNewClientTransaction(request);
// send the request out.
inviteTid.sendRequest();
} catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
use of javax.sip.header.ViaHeader in project smscgateway by RestComm.
the class RxSipServerSbb method sendMessage.
// *********
// Main service methods
/**
* Sending of a SIP message after initial message or when all sent messages was sent
*
* @param smsSet
* @throws SmscProcessingException
*/
private void sendMessage(SmsSet smsSet) throws SmscProcessingException {
smscStatAggregator.updateMsgOutTryAll();
smscStatAggregator.updateMsgOutTrySip();
Sms sms = this.obtainNextMessage(ProcessingType.SIP);
if (sms == null) {
this.markDeliveringIsEnded(true);
return;
}
try {
// TODO: let make here a special check if SIP is in a good state
// if not - skip sending and set temporary error
String fromAddressStr = sms.getSourceAddr();
String toAddressStr = smsSet.getDestAddr();
Sip sip = sipManagement.getSipByName(SipManagement.SIP_NAME);
ListeningPoint listeningPoint = sipRA.getListeningPoints()[0];
SipURI fromAddressUri = addressFactory.createSipURI(fromAddressStr, listeningPoint.getIPAddress() + ":" + listeningPoint.getPort());
javax.sip.address.Address fromAddress = addressFactory.createAddress(fromAddressUri);
FromHeader fromHeader = headerFactory.createFromHeader(fromAddress, null);
SipURI toAddressUri = addressFactory.createSipURI(toAddressStr, sip.getSipAddress());
javax.sip.address.Address toAddress = addressFactory.createAddress(toAddressUri);
ToHeader toHeader = headerFactory.createToHeader(toAddress, null);
List<ViaHeader> viaHeaders = new ArrayList<ViaHeader>(1);
ViaHeader viaHeader = headerFactory.createViaHeader(listeningPoint.getIPAddress(), listeningPoint.getPort(), listeningPoint.getTransport(), null);
viaHeaders.add(viaHeader);
ContentTypeHeader contentTypeHeader = headerFactory.createContentTypeHeader("text", "plain");
CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(2L, Request.MESSAGE);
MaxForwardsHeader maxForwardsHeader = headerFactory.createMaxForwardsHeader(70);
CallIdHeader callId = this.sipRA.getNewCallId();
String msgStr = sms.getShortMessageText();
byte[] msgUdh = sms.getShortMessageBin();
byte[] msg;
msg = recodeShortMessage(sms.getDataCoding(), msgStr, msgUdh);
// create request
Request request = messageFactory.createRequest(toAddressUri, Request.MESSAGE, callId, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwardsHeader, contentTypeHeader, msg);
// Custom X Headers
// SMSC-ID
String originEsmeName = sms.getOrigEsmeName();
if (originEsmeName != null) {
Header smsIdHeader = headerFactory.createHeader(SipXHeaders.XSmscId, originEsmeName);
request.addHeader(smsIdHeader);
}
// data-coding
DataCodingScheme dataCodingScheme = new DataCodingSchemeImpl(sms.getDataCoding());
Header smsIdHeader = headerFactory.createHeader(SipXHeaders.XSmsCoding, Integer.toString(dataCodingScheme.getCharacterSet().getCode()));
request.addHeader(smsIdHeader);
// TODO X header message class
// X header delivery time, use SUBMIT_DATE
Date submitDate = sms.getSubmitDate();
if (submitDate != null) {
String submitDateStr = MessageUtil.formatDate(submitDate);
Header submitDateHeader = headerFactory.createHeader(SipXHeaders.XDeliveryTime, submitDateStr);
request.addHeader(submitDateHeader);
}
// Validity Period
Date validityPeriod = sms.getValidityPeriod();
if (validityPeriod != null) {
String validityPeriodStr = MessageUtil.formatDate(validityPeriod);
Header validityHeader = headerFactory.createHeader(SipXHeaders.XSmsValidty, validityPeriodStr);
request.addHeader(validityHeader);
}
// X header UDH
if (msgUdh != null) {
String udhString = hexStringToByteArray(msgUdh);
Header udhHeader = headerFactory.createHeader(SipXHeaders.XSmsUdh, udhString);
request.addHeader(udhHeader);
}
// create client transaction and send request
ClientTransaction clientTransaction = sipRA.getNewClientTransaction(request);
ActivityContextInterface sipClientTxaci = this.sipACIFactory.getActivityContextInterface(clientTransaction);
sipClientTxaci.attach(this.sbbContext.getSbbLocalObject());
clientTransaction.sendRequest();
} catch (Exception e) {
throw new SmscProcessingException("RxSipServerSbb.sendMessage(): Exception while trying to send SIP Message =" + e.getMessage() + "\nMessage: " + sms, 0, 0, SmscProcessingException.HTTP_ERROR_CODE_NOT_SET, null, e);
}
}
use of javax.sip.header.ViaHeader in project Spark by igniterealtime.
the class MessageProcessing method sendMessage.
/**
* Sends an instant message in pager-mode using a SIMPLE/SIP MESSAGE
* request. In pager-mode, each message is independent of any other
* messages. An instant message will be the body of the MESSAGE request to
* be sent, therefore, its format must conform to the values in the
* "Content-Type" and "Content-Encoding" header fields. Refer to Message for
* details.
*
* @param to the address of receiver.
* @param messageBody the message to be sent. The messageBody will be the body of
* the MESSAGE request to be sent and its format must conform to
* the values in the parameters contentType and contentEncoding.
* Please refer to the setBody method for details.
* @param contentType the Internet media type of the messageBody. Please refer to
* the Message.setBody method for details.
* @param contentEncoding the encodings that have been applied to the messageBody in
* addition to those specified by contentType. Please refer to
* the Message.setBody method for details.
* @return the transaction ID associated with the MESSAGE request sent by
* this method.
* @throws CommunicationsException
*/
public java.lang.String sendMessage(java.lang.String to, byte[] messageBody, java.lang.String contentType, java.lang.String contentEncoding) throws CommunicationsException {
try {
to = to.trim();
// Handle default domain name (i.e. transform 1234 -> 1234@sip.com
String defaultDomainName = SIPConfig.getDefaultDomain();
if (// no sip scheme
defaultDomainName != null && !to.trim().startsWith("tel:") && // most
to.indexOf('@') == -1) // probably
// a
// sip
// uri
{
to = to + "@" + defaultDomainName;
}
// Let's be uri fault tolerant
if (// no sip scheme
to.toLowerCase().indexOf("sip:") == -1 && // most probably a sip uri
to.indexOf('@') != -1) {
to = "sip:" + to;
}
// Request URI
URI requestURI;
try {
requestURI = sipManCallback.addressFactory.createURI(to);
} catch (ParseException ex) {
throw new CommunicationsException(to + " is not a legal SIP uri!", ex);
}
// Call ID
CallIdHeader callIdHeader = sipManCallback.sipProvider.getNewCallId();
// CSeq
CSeqHeader cSeqHeader;
try {
cSeqHeader = sipManCallback.headerFactory.createCSeqHeader(1L, Request.MESSAGE);
} catch (Exception ex) {
// Shouldn't happen
throw new CommunicationsException("An unexpected erro occurred while" + "constructing the CSeqHeadder", ex);
}
// FromHeader
FromHeader fromHeader = sipManCallback.getFromHeader();
// ToHeader
Address toAddress = sipManCallback.addressFactory.createAddress(requestURI);
ToHeader toHeader;
try {
toHeader = sipManCallback.headerFactory.createToHeader(toAddress, null);
} catch (ParseException ex) {
// Shouldn't happen
throw new CommunicationsException("Null is not an allowed tag for the to header!", ex);
}
ContentTypeHeader contentTypeHeader = null;
try {
String[] contentTypeTab = contentType.split("/");
contentTypeHeader = sipManCallback.headerFactory.createContentTypeHeader(contentTypeTab[0], contentTypeTab[1]);
} catch (ParseException ex) {
throw new CommunicationsException("ContentType Header must look like type/subtype!", ex);
}
ContentLengthHeader contentLengthHeader = null;
try {
contentLengthHeader = sipManCallback.headerFactory.createContentLengthHeader(messageBody.length);
} catch (InvalidArgumentException ex) {
throw new CommunicationsException("Cseq Header must contain a integer value!", ex);
}
// ExpiresHeader expiresHeader = null;
try {
sipManCallback.headerFactory.createExpiresHeader(30);
} catch (InvalidArgumentException ex) {
throw new CommunicationsException("Expires Header must be an integer!", ex);
}
// ViaHeaders
ArrayList<ViaHeader> viaHeaders = sipManCallback.getLocalViaHeaders();
// MaxForwards
MaxForwardsHeader maxForwards = sipManCallback.getMaxForwardsHeader();
Request message = null;
try {
message = sipManCallback.messageFactory.createRequest(requestURI, Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
message.setContent(messageBody, contentTypeHeader);
message.setContentLength(contentLengthHeader);
// message.addHeader(eventHeader);
} catch (ParseException ex) {
throw new CommunicationsException("Failed to create message Request!", ex);
}
ClientTransaction messageTransaction = null;
try {
messageTransaction = sipManCallback.sipProvider.getNewClientTransaction(message);
//
} catch (TransactionUnavailableException ex) {
throw new CommunicationsException("Failed to create messageTransaction.", ex);
}
try {
messageTransaction.sendRequest();
} catch (SipException ex) {
throw new CommunicationsException("An error occurred while sending message request", ex);
}
return messageTransaction.toString();
} finally {
}
}
use of javax.sip.header.ViaHeader in project Spark by igniterealtime.
the class MessageProcessing method sendKeepAlive.
public void sendKeepAlive() throws CommunicationsException {
String to = "";
byte[] messageBody = "".getBytes();
try {
to = to.trim();
// Handle default domain name (i.e. transform 1234 -> 1234@sip.com
String defaultDomainName = SIPConfig.getDefaultDomain();
if (// no sip scheme
defaultDomainName != null && !to.trim().startsWith("tel:") && to.indexOf('@') == -1) {
to = to + "@" + defaultDomainName;
}
// Let's be uri fault tolerant
if (// no sip scheme
to.toLowerCase().indexOf("sip:") == -1 && // most probably a sip uri
to.indexOf('@') != -1) {
to = "sip:" + to;
}
// Request URI
URI requestURI;
try {
requestURI = sipManCallback.addressFactory.createURI(to);
} catch (ParseException ex) {
throw new CommunicationsException(to + " is not a legal SIP uri!", ex);
}
// Call ID
CallIdHeader callIdHeader = sipManCallback.sipProvider.getNewCallId();
// CSeq
CSeqHeader cSeqHeader;
try {
cSeqHeader = sipManCallback.headerFactory.createCSeqHeader(1L, Request.MESSAGE);
} catch (Exception ex) {
// Shouldn't happen
throw new CommunicationsException("An unexpected erro occurred while" + "constructing the CSeqHeadder", ex);
}
// FromHeader
FromHeader fromHeader = sipManCallback.getFromHeader();
// ToHeader
Address toAddress = sipManCallback.addressFactory.createAddress(requestURI);
ToHeader toHeader;
try {
toHeader = sipManCallback.headerFactory.createToHeader(toAddress, null);
} catch (ParseException ex) {
// Shouldn't happen
throw new CommunicationsException("Null is not an allowed tag for the to header!", ex);
}
ContentLengthHeader contentLengthHeader = null;
try {
contentLengthHeader = sipManCallback.headerFactory.createContentLengthHeader(messageBody.length);
} catch (InvalidArgumentException ex) {
throw new CommunicationsException("Cseq Header must contain a integer value!", ex);
}
// ExpiresHeader expiresHeader = null;
try {
sipManCallback.headerFactory.createExpiresHeader(30);
} catch (InvalidArgumentException ex) {
throw new CommunicationsException("Expires Header must be an integer!", ex);
}
String contentType = "text/plain";
ContentTypeHeader contentTypeHeader = null;
try {
String[] contentTypeTab = contentType.split("/");
contentTypeHeader = sipManCallback.headerFactory.createContentTypeHeader(contentTypeTab[0], contentTypeTab[1]);
} catch (ParseException ex) {
throw new CommunicationsException("ContentType Header must look like type/subtype!", ex);
}
// ViaHeaders
ArrayList<ViaHeader> viaHeaders = sipManCallback.getLocalViaHeaders();
// MaxForwards
MaxForwardsHeader maxForwards = sipManCallback.getMaxForwardsHeader();
Request message = null;
try {
message = sipManCallback.messageFactory.createRequest(requestURI, Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
message.setContentLength(contentLengthHeader);
message.setContent(messageBody, contentTypeHeader);
// message.addHeader(eventHeader);
} catch (Exception e) {
Log.error("sendKeepAlive", e);
}
try {
sipManCallback.sipProvider.getNewClientTransaction(message);
} catch (TransactionUnavailableException e) {
e.printStackTrace();
}
try {
// TODO : Anpassen
// ((SIPClientTransaction) messageTransaction).sendRequest("\0".getBytes(), InetAddress.getByName(SIPConfig.getDefaultDomain()), SIPConfig.getRegistrarPort());
} catch (Exception e) {
Log.error("sendKeepAlive", e);
}
} finally {
try {
this.finalize();
} catch (Throwable e) {
}
}
}
Aggregations