use of javax.sip.address.SipURI in project Spark by igniterealtime.
the class SipCommRouter method getNextHops.
/**
* Return the default address to forward the request to. The list is
* organized in the following priority.
* <p/>
* If the outboung proxy has been specified, then it is used to construct
* the first element of the list.
* <p/>
* If the requestURI refers directly to a host, the host and port
* information are extracted from it and made the next hop on the list.
*
* @param sipRequest is the sip request to route.
*/
public ListIterator<Hop> getNextHops(Request sipRequest) {
URI requestURI = sipRequest.getRequestURI();
if (requestURI == null) {
throw new IllegalArgumentException("Bad message: Null requestURI");
}
LinkedList<Hop> hops = new LinkedList<Hop>();
if (outboundProxy != null) {
hops.add(outboundProxy);
}
ListIterator<RouteHeader> routes = sipRequest.getHeaders(RouteHeader.NAME);
if (routes != null && routes.hasNext()) {
while (routes.hasNext()) {
RouteHeader route = routes.next();
SipURI uri = (SipURI) route.getAddress().getURI();
int port = uri.getPort();
port = (port == -1) ? 5060 : port;
String host = uri.getHost();
Log.debug("getNextHops", host);
String transport = uri.getTransportParam();
if (transport == null) {
transport = "udp";
}
Hop hop = new SipCommHop(host + ':' + port + '/' + transport);
hops.add(hop);
}
} else if (requestURI instanceof SipURI && ((SipURI) requestURI).getMAddrParam() != null) {
SipURI sipURI = ((SipURI) requestURI);
String maddr = sipURI.getMAddrParam();
String transport = sipURI.getTransportParam();
if (transport == null) {
transport = "udp";
}
int port = 5060;
Hop hop = new SipCommHop(maddr, port, transport);
hops.add(hop);
} else if (requestURI instanceof SipURI) {
SipURI sipURI = ((SipURI) requestURI);
int port = sipURI.getPort();
if (port == -1) {
port = 5060;
}
String host = sipURI.getHost();
String transport = sipURI.getTransportParam();
if (transport == null) {
transport = "UDP";
}
Hop hop = new SipCommHop(host + ":" + port + "/" + transport);
hops.add(hop);
} else {
throw new IllegalArgumentException("Malformed requestURI");
}
return (hops.size() == 0) ? null : hops.listIterator();
}
use of javax.sip.address.SipURI in project Spark by igniterealtime.
the class CallProcessing method processInvite.
// -------------------------- Requests ---------------------------------
void processInvite(ServerTransaction serverTransaction, Request invite) {
Dialog dialog = serverTransaction.getDialog();
if (!sipManCallback.isBusy() && !(PhoneManager.isUseStaticLocator() && PhoneManager.isUsingMediaLocator())) {
Call call = callDispatcher.createCall(dialog, invite);
sipManCallback.fireCallReceived(call);
// change status
call.setState(Call.ALERTING);
// sdp description may be in acks - bug report Laurent Michel
ContentLengthHeader cl = invite.getContentLength();
if (cl != null && cl.getContentLength() > 0) {
call.setRemoteSdpDescription(new String(invite.getRawContent()));
}
// Are we the one they are looking for?
URI calleeURI = ((ToHeader) invite.getHeader(ToHeader.NAME)).getAddress().getURI();
/**
* @todo We shoud rather ask the user what to do here as some
* would add prefixes or change user URIs
*/
if (calleeURI.isSipURI()) {
boolean assertUserMatch = SIPConfig.isFailCallInUserMismatch();
// user info is case sensitive according to rfc3261
if (assertUserMatch) {
String calleeUser = ((SipURI) calleeURI).getUser();
String localUser = sipManCallback.getLocalUser();
if (calleeUser != null && !calleeUser.equals(localUser)) {
sipManCallback.fireCallRejectedLocally("The user specified by the caller did not match the local user!", invite, call);
call.setState(Call.DISCONNECTED);
Response notFound = null;
try {
notFound = sipManCallback.messageFactory.createResponse(Response.NOT_FOUND, invite);
sipManCallback.attachToTag(notFound, dialog);
} catch (ParseException ex) {
call.setState(Call.DISCONNECTED);
sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to create a NOT_FOUND response to an INVITE request!", ex));
return;
}
try {
serverTransaction.sendResponse(notFound);
} catch (SipException ex) {
call.setState(Call.DISCONNECTED);
sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to send a NOT_FOUND response to an INVITE request!", ex));
return;
} catch (InvalidArgumentException e) {
call.setState(Call.DISCONNECTED);
sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to send a NOT_FOUND response to an INVITE request!", e));
return;
}
return;
}
}
}
// Send RINGING
Response ringing = null;
try {
ringing = sipManCallback.messageFactory.createResponse(Response.RINGING, invite);
sipManCallback.attachToTag(ringing, dialog);
} catch (ParseException ex) {
call.setState(Call.DISCONNECTED);
sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to create a RINGING response to an INVITE request!", ex));
return;
}
try {
serverTransaction.sendResponse(ringing);
} catch (SipException ex) {
call.setState(Call.DISCONNECTED);
sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to send a RINGING response to an INVITE request!", ex));
return;
} catch (InvalidArgumentException e) {
call.setState(Call.DISCONNECTED);
sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to send a NOT_FOUND response to an INVITE request!", e));
return;
}
} else {
// Send BUSY_HERE
Response busy = null;
try {
busy = sipManCallback.messageFactory.createResponse(Response.BUSY_HERE, invite);
sipManCallback.attachToTag(busy, dialog);
} catch (ParseException ex) {
sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to create a RINGING response to an INVITE request!", ex));
return;
}
try {
serverTransaction.sendResponse(busy);
} catch (SipException ex) {
sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to send a RINGING response to an INVITE request!", ex));
return;
} catch (InvalidArgumentException e) {
sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to send a RINGING response to an INVITE request!", e));
return;
}
}
}
use of javax.sip.address.SipURI 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.address.SipURI 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.address.SipURI in project Spark by igniterealtime.
the class Call method getRemoteName.
public String getRemoteName() {
Address address;
if (dialog.getState() != null) {
address = dialog.getRemoteParty();
} else {
if (dialog.isServer()) {
FromHeader fromHeader = (FromHeader) initialRequest.getHeader(FromHeader.NAME);
address = fromHeader.getAddress();
} else {
ToHeader toHeader = (ToHeader) initialRequest.getHeader(ToHeader.NAME);
address = toHeader.getAddress();
}
}
String retVal = null;
if (address.getDisplayName() != null && address.getDisplayName().trim().length() > 0) {
retVal = address.getDisplayName();
} else {
URI uri = address.getURI();
if (uri.isSipURI()) {
retVal = ((SipURI) uri).getUser();
}
}
return retVal == null ? "" : retVal;
}
Aggregations