use of javax.sip.header.ViaHeader in project Spark by igniterealtime.
the class RegisterProcessing method subscribe.
/**
* Synchronize because of timer tasks
*
* @throws CommunicationsException
*/
synchronized void subscribe(String registrarAddress, int registrarPort, String registrarTransport) throws CommunicationsException {
FromHeader fromHeader = sipManCallback.getFromHeader(true);
Address fromAddress = fromHeader.getAddress();
// Request URI
URI requestURI = null;
try {
requestURI = sipManCallback.addressFactory.createSipURI(sipManCallback.getCurrentUsername(), registrarAddress);
} catch (ParseException e) {
Log.error(e);
}
// Call ID Header
CallIdHeader callIdHeader = sipManCallback.sipProvider.getNewCallId();
// CSeq Header
CSeqHeader cSeqHeader = null;
try {
cSeqHeader = sipManCallback.headerFactory.createCSeqHeader((long) 1, Request.SUBSCRIBE);
} 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.SUBSCRIBE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwardsHeader);
request.setHeader(uaHeader);
} catch (ParseException ex) {
throw new CommunicationsException("Could not create the register request!", ex);
}
// Expires Header
ExpiresHeader expHeader = null;
try {
expHeader = sipManCallback.headerFactory.createExpiresHeader(3000);
} catch (InvalidArgumentException ex) {
}
request.addHeader(expHeader);
try {
EventHeader event = sipManCallback.headerFactory.createEventHeader("message-summary");
request.addHeader(event);
} catch (ParseException e) {
Log.error(e);
}
// Contact Header should contain IP - bug report - Eero Vaarnas
ContactHeader contactHeader = sipManCallback.getRegistrationContactHeader();
request.addHeader(contactHeader);
AllowHeader allow = null;
try {
allow = sipManCallback.headerFactory.createAllowHeader("INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
request.addHeader(allow);
} catch (ParseException e) {
Log.error(e);
}
// 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);
}
}
use of javax.sip.header.ViaHeader in project Spark by igniterealtime.
the class SipManager method getLocalViaHeaders.
/**
* Initializes (if null) and returns an ArrayList with a single ViaHeader
* containing localhost's address. This ArrayList may be used when sending
* requests.
*
* @return ViaHeader-s list to be used when sending requests.
* @throws CommunicationsException if a ParseException is to occur while initializing the array
* list.
*/
public ArrayList<ViaHeader> getLocalViaHeaders() throws CommunicationsException {
ListeningPoint lp = sipProvider.getListeningPoints()[0];
viaHeaders = new ArrayList<ViaHeader>();
try {
ViaHeader viaHeader = headerFactory.createViaHeader(SIPConfig.getIPAddress(), lp.getPort(), lp.getTransport(), null);
viaHeader.setParameter("rport", null);
viaHeaders.add(viaHeader);
return viaHeaders;
} catch (ParseException ex) {
throw new CommunicationsException("A ParseException occurred while creating Via Headers!");
} catch (InvalidArgumentException ex) {
throw new CommunicationsException("Unable to create a via header for port " + lp.getPort(), ex);
}
}
use of javax.sip.header.ViaHeader in project XobotOS by xamarin.
the class SipHelper method sendReinvite.
public ClientTransaction sendReinvite(Dialog dialog, String sessionDescription) throws SipException {
try {
Request request = dialog.createRequest(Request.INVITE);
request.setContent(sessionDescription, mHeaderFactory.createContentTypeHeader("application", "sdp"));
// Adding rport argument in the request could fix some SIP servers
// in resolving the initiator's NAT port mapping for relaying the
// response message from the other end.
ViaHeader viaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
if (viaHeader != null)
viaHeader.setRPort();
ClientTransaction clientTransaction = mSipProvider.getNewClientTransaction(request);
if (DEBUG)
Log.d(TAG, "send RE-INVITE: " + request);
dialog.sendRequest(clientTransaction);
return clientTransaction;
} catch (ParseException e) {
throw new SipException("sendReinvite()", e);
}
}
use of javax.sip.header.ViaHeader in project XobotOS by xamarin.
the class SipHelper method createRequest.
private Request createRequest(String requestType, SipProfile caller, SipProfile callee, String tag) throws ParseException, SipException {
FromHeader fromHeader = createFromHeader(caller, tag);
ToHeader toHeader = createToHeader(callee);
SipURI requestURI = callee.getUri();
List<ViaHeader> viaHeaders = createViaHeaders();
CallIdHeader callIdHeader = createCallIdHeader();
CSeqHeader cSeqHeader = createCSeqHeader(requestType);
MaxForwardsHeader maxForwards = createMaxForwardsHeader();
Request request = mMessageFactory.createRequest(requestURI, requestType, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
request.addHeader(createContactHeader(caller));
return request;
}
use of javax.sip.header.ViaHeader in project load-balancer by RestComm.
the class BackToBackUserAgent method processRequest.
public void processRequest(RequestEvent requestEvent) {
try {
Request request = requestEvent.getRequest();
if (request.getMethod().equals(Request.INVITE)) {
ServerTransaction st = sp.getNewServerTransaction(requestEvent.getRequest());
incomingDialog = st.getDialog();
try {
replyToRequestEvent(requestEvent.getRequest(), st, Response.TRYING);
Request newRequest = (Request) request.clone();
newRequest.removeHeader(RouteHeader.NAME);
newRequest.removeHeader(RecordRouteHeader.NAME);
FromHeader fromHeader = (FromHeader) newRequest.getHeader(FromHeader.NAME);
fromHeader.setTag(Long.toString(Math.abs(new Random().nextLong())));
ViaHeader viaHeader = ((ListeningPointExt) sp.getListeningPoint(transport)).createViaHeader();
newRequest.setHeader(viaHeader);
ContactHeader contactHeader = ((ListeningPointExt) sp.getListeningPoint(transport)).createContactHeader();
newRequest.setHeader(contactHeader);
SipURI route = this.protocolObjects.addressFactory.createSipURI("lbint", lbAddress + ":" + lbPort);
route.setParameter("node_host", "127.0.0.1");
route.setParameter("node_port", "" + port);
route.setLrParam();
RouteHeader routeHeader = (RouteHeader) this.protocolObjects.headerFactory.createRouteHeader(this.protocolObjects.addressFactory.createAddress(route));
newRequest.setHeader(routeHeader);
clientTransaction = sp.getNewClientTransaction(newRequest);
outgoingDialog = clientTransaction.getDialog();
clientTransaction.setApplicationData(st);
clientTransaction.sendRequest();
} catch (Throwable e) {
e.printStackTrace();
replyToRequestEvent(request, st, Response.SERVICE_UNAVAILABLE);
}
} else if (request.getMethod().equals(Request.BYE)) {
ServerTransaction st = requestEvent.getServerTransaction();
replyToRequestEvent(requestEvent.getRequest(), st, Response.OK);
Dialog peerDialog = getPeerDialog(requestEvent.getDialog());
Request outgoingRequest = peerDialog.createRequest(requestEvent.getRequest().getMethod());
final ClientTransaction ct = sp.getNewClientTransaction(outgoingRequest);
peerDialog.sendRequest(ct);
} else if (request.getMethod().equals(Request.CANCEL)) {
try {
final Dialog peerDialog = outgoingDialog;
final DialogState peerDialogState = peerDialog.getState();
if (peerDialogState == null || peerDialogState == DialogState.EARLY) {
Request cancelRequest = clientTransaction.createCancel();
sp.sendRequest(cancelRequest);
} else {
clientTransaction = sp.getNewClientTransaction(peerDialog.createRequest(Request.BYE));
clientTransaction.sendRequest();
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
Aggregations