use of javax.sip.header.ContactHeader in project Spark by igniterealtime.
the class CallProcessing method sayBye.
// send message
// private void sendInfoMessage(Dialog dialog, String body)
// throws CommunicationsException {
// Request request = dialog.getFirstTransaction().getRequest();
// Request info = null;
// String contentType = "application/dtmd-relay";
// String[] contentTypeTab = contentType.split("/");
// ContentTypeHeader contentTypeHeader = null;
// try {
// info = dialog.createRequest(Request.INFO);
// try {
// contentTypeHeader = sipManCallback.headerFactory
// .createContentTypeHeader(contentTypeTab[0],
// contentTypeTab[1]);
// info.setContent(body, contentTypeHeader);
// }
// catch (ParseException ex) {
//
// throw new CommunicationsException(
// "ContentType Header must look like type/subtype!",
// ex);
// }
// }
// catch (SipException ex) {
//
// throw new CommunicationsException(
// "Failed to create bye request!", ex);
// }
// ClientTransaction clientTransaction = null;
// try {
// clientTransaction = sipManCallback.sipProvider
// .getNewClientTransaction(info);
// }
// catch (TransactionUnavailableException ex) {
//
// throw new CommunicationsException(
// "Failed to construct a client transaction from the INFO request",
// ex);
// }
// try {
// dialog.sendRequest(clientTransaction);
// }
// catch (SipException ex1) {
// throw new CommunicationsException(
// "Failed to send the INFO request");
// }
//
// } // send message
// Bye
private void sayBye(Dialog dialog) throws CommunicationsException {
Request bye = null;
bye = dialog.getFirstTransaction().getRequest();
try {
bye = dialog.createRequest(Request.BYE);
} catch (SipException e) {
Log.error("bye", e);
}
long cseq = ((CSeq) (bye.getHeader(CSeq.NAME))).getSequenceNumber() + 1;
bye.removeHeader(CSeq.NAME);
try {
bye.addHeader(sipManCallback.headerFactory.createCSeqHeader(cseq, Request.BYE));
} catch (Exception e) {
Log.error("bye", e);
}
bye.removeHeader(ViaHeader.NAME);
for (ViaHeader via : sipManCallback.getLocalViaHeaders()) bye.addHeader(via);
ContactHeader contactHeader = sipManCallback.getContactHeader();
bye.addHeader(contactHeader);
AllowHeader allow = null;
try {
allow = sipManCallback.headerFactory.createAllowHeader("INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
bye.addHeader(allow);
} catch (ParseException e) {
Log.error(e);
}
// Transaction
ClientTransaction inviteTransaction;
try {
inviteTransaction = sipManCallback.sipProvider.getNewClientTransaction(bye);
dialog.sendRequest(inviteTransaction);
} catch (SipException ee) {
Log.error("bye", ee);
}
return;
}
use of javax.sip.header.ContactHeader in project Spark by igniterealtime.
the class RegisterProcessing method processOK.
void processOK(ClientTransaction clientTransatcion, Response response) {
isRegistered = true;
FromHeader fromHeader = ((FromHeader) response.getHeader(FromHeader.NAME));
Address address = fromHeader.getAddress();
int expires = 0;
if (!isUnregistering) {
ContactHeader contactHeader = (ContactHeader) response.getHeader(ContactHeader.NAME);
// TODO check if the registrar created the contact address
if (contactHeader != null) {
expires = contactHeader.getExpires();
} else {
ExpiresHeader expiresHeader = response.getExpires();
if (expiresHeader != null) {
expires = expiresHeader.getExpires();
}
}
}
// fix by Luca Bincoletto <Luca.Bincoletto@tilab.com>
if (expires == 0) {
isUnregistering = false;
sipManCallback.fireUnregistered(address.toString());
} else {
if (reRegisterTimer != null)
reRegisterTimer.cancel();
if (keepAliveTimer != null)
keepAliveTimer.cancel();
reRegisterTimer = new Timer();
keepAliveTimer = new Timer();
// if (expires > 0 && expires < 60) {
// [issue 2] Schedule re registrations
// bug reported by LynlvL@netscape.com
// use the value returned by the server to reschedule
// registration
SipURI uri = (SipURI) address.getURI();
scheduleReRegistration(uri.getHost(), uri.getPort(), uri.getTransportParam(), expires);
scheduleKeepAlive(SIPConfig.getKeepAliveDelay());
// }
/*
* else{ SipURI uri = (SipURI) address.getURI();
* scheduleReRegistration(uri.getHost(), uri.getPort(),
* uri.getTransportParam(), expires); }
*/
sipManCallback.fireRegistered(address.toString());
}
}
use of javax.sip.header.ContactHeader in project Spark by igniterealtime.
the class RegisterProcessing method register.
synchronized void register(String registrarAddress, int registrarPort, String registrarTransport, int expires) throws CommunicationsException {
try {
isUnregistering = false;
// From
FromHeader fromHeader = sipManCallback.getFromHeader(true);
Address fromAddress = fromHeader.getAddress();
sipManCallback.fireRegistering(fromAddress.toString());
// Request URI
SipURI requestURI = null;
try {
requestURI = sipManCallback.addressFactory.createSipURI(null, registrarAddress);
} catch (ParseException ex) {
throw new CommunicationsException("Bad registrar address:" + registrarAddress, ex);
} catch (NullPointerException ex) {
// Do not throw an exc, we should rather silently notify the
// user
// throw new CommunicationsException("A registrar address was
// not specified!", ex);
sipManCallback.fireUnregistered(fromAddress.getURI().toString() + " (registrar not specified)");
return;
}
/*
requestURI.setPort(registrarPort);
try {
requestURI.setTransportParam(registrarTransport);
}
catch (ParseException ex) {
throw new CommunicationsException(registrarTransport
+ " is not a valid transport!", ex);
}
*/
// Call ID Header
CallIdHeader callIdHeader = sipManCallback.sipProvider.getNewCallId();
// CSeq Header
CSeqHeader cSeqHeader = null;
try {
cSeqHeader = sipManCallback.headerFactory.createCSeqHeader((long) 1, Request.REGISTER);
} catch (ParseException ex) {
// Should never happen
Log.error("register", ex);
} catch (InvalidArgumentException ex) {
// Should never happen
Log.error("register", ex);
}
// To Header
ToHeader toHeader = null;
try {
toHeader = sipManCallback.headerFactory.createToHeader(fromAddress, null);
} catch (ParseException ex) {
// throw was missing - reported by Eero Vaarnas
throw new CommunicationsException("Could not create a To header " + "for address:" + fromHeader.getAddress(), ex);
}
// User Agent Header
UserAgentHeader uaHeader = null;
ArrayList<String> userAgentList = new ArrayList<String>();
userAgentList.add(SoftPhoneManager.userAgent);
try {
uaHeader = sipManCallback.headerFactory.createUserAgentHeader(userAgentList);
} catch (ParseException ex) {
// throw was missing - reported by Eero Vaarnas
throw new CommunicationsException("Could not create a To header " + "for address:" + fromHeader.getAddress(), ex);
}
// Via Headers
ArrayList<ViaHeader> viaHeaders = sipManCallback.getLocalViaHeaders();
// MaxForwardsHeader
MaxForwardsHeader maxForwardsHeader = sipManCallback.getMaxForwardsHeader();
// Request
Request request = null;
try {
request = sipManCallback.messageFactory.createRequest(requestURI, Request.REGISTER, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwardsHeader);
request.setHeader(uaHeader);
} catch (ParseException ex) {
// throw was missing - reported by Eero Vaarnas
throw new CommunicationsException("Could not create the register request!", ex);
}
// Expires Header
ExpiresHeader expHeader = null;
for (int retry = 0; retry < 2; retry++) {
try {
expHeader = sipManCallback.headerFactory.createExpiresHeader(expires);
} catch (InvalidArgumentException ex) {
if (retry == 0) {
expires = 3600;
continue;
}
throw new CommunicationsException("Invalid registrations expiration parameter - " + expires, ex);
}
}
request.addHeader(expHeader);
// Contact Header should contain IP - bug report - Eero Vaarnas
ContactHeader contactHeader = sipManCallback.getRegistrationContactHeader();
request.addHeader(contactHeader);
AllowHeader allow = sipManCallback.headerFactory.createAllowHeader("INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
request.addHeader(allow);
// Transaction
ClientTransaction regTrans = null;
try {
regTrans = sipManCallback.sipProvider.getNewClientTransaction(request);
} catch (TransactionUnavailableException ex) {
throw new CommunicationsException("Could not create a register transaction!\n" + "Check that the Registrar address is correct!");
}
try {
regTrans.sendRequest();
}// we sometimes get a null pointer exception here so catch them all
catch (Exception ex) {
// throw was missing - reported by Eero Vaarnas
throw new CommunicationsException("Could not send out the register request!", ex);
}
this.registerRequest = request;
} catch (Exception e) {
Log.error("register", e);
sipManCallback.fireRegistrationFailed(registrarAddress == null ? "" : registrarAddress, RegistrationEvent.Type.TimeOut);
}
}
use of javax.sip.header.ContactHeader in project Spark by igniterealtime.
the class CallProcessing method sayInternalError.
// answer call
// ------------------ Internal Error
void sayInternalError(int callID) throws CommunicationsException {
Call call = callDispatcher.getCall(callID);
if (call == null) {
throw new CommunicationsException("Failed to find call with id=" + callID);
}
Dialog dialog = call.getDialog();
if (dialog == null) {
call.setState(Call.DISCONNECTED);
throw new CommunicationsException("Failed to extract call's associated dialog! Ending Call!");
}
Transaction transaction = dialog.getFirstTransaction();
if (transaction == null || !dialog.isServer()) {
call.setState(Call.DISCONNECTED);
throw new CommunicationsException("Failed to extract a transaction from the call's associated dialog!");
}
ServerTransaction serverTransaction = (ServerTransaction) transaction;
Response internalError = null;
try {
internalError = sipManCallback.messageFactory.createResponse(Response.SERVER_INTERNAL_ERROR, dialog.getFirstTransaction().getRequest());
sipManCallback.attachToTag(internalError, dialog);
} catch (ParseException ex) {
call.setState(Call.DISCONNECTED);
throw new CommunicationsException("Failed to construct an OK response to an INVITE request", ex);
}
ContactHeader contactHeader = sipManCallback.getContactHeader();
internalError.addHeader(contactHeader);
try {
serverTransaction.sendResponse(internalError);
} catch (SipException ex) {
call.setState(Call.DISCONNECTED);
throw new CommunicationsException("Failed to send an OK response to an INVITE request", ex);
} catch (InvalidArgumentException e) {
call.setState(Call.DISCONNECTED);
throw new CommunicationsException("Failed to send an OK response to an INVITE request", e);
}
}
use of javax.sip.header.ContactHeader in project Spark by igniterealtime.
the class CallProcessing method invite.
// -------------------------------- User Initiated processing
// ---------------------------------
Call invite(String callee, String sdpContent) throws CommunicationsException {
callee = callee.trim();
// Remove excessive characters from phone numbers such as '
// ','(',')','-'
String excessiveChars = SIPConfig.getExcessiveURIChar();
if (excessiveChars != null) {
StringBuffer calleeBuff = new StringBuffer(callee);
for (int i = 0; i < excessiveChars.length(); i++) {
String charToDeleteStr = excessiveChars.substring(i, i + 1);
int charIndex = -1;
while ((charIndex = calleeBuff.indexOf(charToDeleteStr)) != -1) calleeBuff.delete(charIndex, charIndex + 1);
}
callee = calleeBuff.toString();
}
// Handle default domain name (i.e. transform 1234 -> 1234@sip.com
String defaultDomainName = SIPConfig.getDefaultDomain();
if (// no sip scheme
defaultDomainName != null && !callee.trim().startsWith("tel:") && // most probably a sip uri
callee.indexOf('@') == -1) {
callee = callee + "@" + defaultDomainName;
}
// Let's be uri fault tolerant
if (// no sip scheme
callee.toLowerCase().indexOf("sip:") == -1 && // most probably a sip uri
callee.indexOf('@') != -1) {
callee = "sip:" + callee;
}
// Request URI
URI requestURI;
try {
requestURI = sipManCallback.addressFactory.createURI(callee);
} catch (ParseException ex) {
throw new CommunicationsException(callee + " is not a legal SIP uri!", ex);
}
// Call ID
CallIdHeader callIdHeader = sipManCallback.sipProvider.getNewCallId();
// CSeq
CSeqHeader cSeqHeader;
try {
cSeqHeader = sipManCallback.headerFactory.createCSeqHeader(1L, Request.INVITE);
} catch (ParseException ex) {
throw new CommunicationsException("An unexpected erro occurred while" + "constructing the CSeqHeadder", ex);
} catch (InvalidArgumentException 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);
}
// ViaHeaders
ArrayList<ViaHeader> viaHeaders = sipManCallback.getLocalViaHeaders();
// MaxForwards
MaxForwardsHeader maxForwards = sipManCallback.getMaxForwardsHeader();
// Contact
ContactHeader contactHeader = sipManCallback.getContactHeader();
Request invite = null;
try {
invite = sipManCallback.messageFactory.createRequest(requestURI, Request.INVITE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
} catch (ParseException ex) {
throw new CommunicationsException("Failed to create invite Request!", ex);
}
//
invite.addHeader(contactHeader);
AllowHeader allow = null;
try {
allow = sipManCallback.headerFactory.createAllowHeader("INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
invite.addHeader(allow);
} catch (ParseException e) {
Log.error(e);
}
// Content
ContentTypeHeader contentTypeHeader = null;
try {
// content type should be application/sdp (not applications)
// reported by Oleg Shevchenko (Miratech)
contentTypeHeader = sipManCallback.headerFactory.createContentTypeHeader("application", "sdp");
} catch (ParseException ex) {
// Shouldn't happen
throw new CommunicationsException("Failed to create a content type header for the INVITE request", ex);
}
try {
invite.setContent(sdpContent, contentTypeHeader);
} catch (ParseException ex) {
throw new CommunicationsException("Failed to parse sdp data while creating invite request!", ex);
}
// Transaction
ClientTransaction inviteTransaction;
try {
inviteTransaction = sipManCallback.sipProvider.getNewClientTransaction(invite);
} catch (TransactionUnavailableException ex) {
throw new CommunicationsException("Failed to create inviteTransaction.\n" + "This is most probably a network connection error.", ex);
}
try {
inviteTransaction.sendRequest();
} catch (SipException ex) {
throw new CommunicationsException("An error occurred while sending invite request", ex);
}
Call call = callDispatcher.createCall(inviteTransaction.getDialog(), invite);
call.setState(Call.DIALING);
return call;
}
Aggregations