use of javax.sip.header.ContactHeader 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();
}
}
use of javax.sip.header.ContactHeader in project load-balancer by RestComm.
the class TestSipListener method processNotify.
public void processNotify(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
SipProvider provider = (SipProvider) requestEvent.getSource();
Request notify = requestEvent.getRequest();
try {
logger.info("subscriber: got a notify count " + this.notifyCount++);
if (serverTransactionId == null) {
logger.info("subscriber: null TID.");
serverTransactionId = provider.getNewServerTransaction(notify);
}
Dialog dialog = serverTransactionId.getDialog();
// if ( dialog != subscriberDialog ) {
// if (forkedDialog == null) {
// forkedDialog = dialog;
// } else {
// AbstractSubsnotifyTestCase.assertTrue("Dialog should be either the subscriber dialog ",
// forkedDialog == dialog);
// }
// }
//
// this.dialogs.add(dialog);
logger.info("Dialog State = " + dialog.getState());
Response response = protocolObjects.messageFactory.createResponse(200, notify);
// SHOULD add a Contact
ContactHeader contact = (ContactHeader) contactHeader.clone();
((SipURI) contact.getAddress().getURI()).setParameter("id", "sub");
response.addHeader(contact);
logger.info("Transaction State = " + serverTransactionId.getState());
serverTransactionId.sendResponse(response);
logger.info("Dialog State = " + dialog.getState());
SubscriptionStateHeader subscriptionState = (SubscriptionStateHeader) notify.getHeader(SubscriptionStateHeader.NAME);
// Subscription is terminated?
String state = subscriptionState.getState();
allSubscriptionStates.add(state.toLowerCase());
if (notify.getRawContent() != null) {
this.lastMessageContent = new String(notify.getRawContent());
allMessagesContent.add(new String(lastMessageContent));
}
if (state.equalsIgnoreCase(SubscriptionStateHeader.TERMINATED)) {
if (subscriptionState.getReasonCode() == null) {
dialog.delete();
}
} else if (state.equalsIgnoreCase(SubscriptionStateHeader.ACTIVE)) {
if ("reg".equalsIgnoreCase(((EventHeader) notify.getHeader(EventHeader.NAME)).getEventType())) {
if (sendByeBeforeTerminatingNotify) {
dialog.terminateOnBye(false);
sendBye();
Thread.sleep(1000);
}
logger.info("Subscriber: sending unSUBSCRIBE");
// Else we end it ourselves
Request unsubscribe = dialog.createRequest(Request.SUBSCRIBE);
logger.info("dialog created:" + unsubscribe);
// SHOULD add a Contact (done by dialog), lets mark it to test updates
// ((SipURI) dialog.getLocalParty().getURI()).setParameter( "id", "unsub" );
ExpiresHeader expires = protocolObjects.headerFactory.createExpiresHeader(0);
unsubscribe.addHeader(expires);
// JvB note : stack should do this!
// copy
unsubscribe.addHeader(notify.getHeader(EventHeader.NAME));
// event
// header
logger.info("Sending Unsubscribe : " + unsubscribe);
logger.info("unsubscribe dialog " + dialog);
ClientTransaction ct = sipProvider.getNewClientTransaction(unsubscribe);
dialog.sendRequest(ct);
if (sendByeAfterTerminatingNotify) {
Thread.sleep(1000);
sendBye();
}
} else if (sendByeBeforeTerminatingNotify) {
sendBye();
}
} else {
// pending
logger.info("Subscriber: state now " + state);
}
} catch (Exception ex) {
logger.error("Unexpected exception", ex);
}
}
use of javax.sip.header.ContactHeader in project load-balancer by RestComm.
the class TestSipListener method processInvite.
/**
* Process the invite request.
*/
public void processInvite(RequestEvent requestEvent, ServerTransaction serverTransaction) {
inviteReceived = true;
SipProvider sipProvider = (SipProvider) requestEvent.getSource();
Request request = requestEvent.getRequest();
inviteRequest = request;
logger.info("shootme: got an Invite " + request);
try {
ServerTransaction st = requestEvent.getServerTransaction();
if (st == null) {
st = sipProvider.getNewServerTransaction(request);
}
inviteServerTid = st;
Dialog dialog = st.getDialog();
if (request.getHeader(JoinHeader.NAME) != null) {
setJoinRequestReceived(true);
this.joinDialog = dialog;
} else if (request.getHeader(ReplacesHeader.NAME) != null) {
setReplacesRequestReceived(true);
this.replacesDialog = dialog;
} else {
this.dialogCount++;
this.dialog = dialog;
}
logger.info("Shootme: dialog = " + dialog);
this.inviteRequest = request;
boolean sendReliably = false;
RequireHeader requireHeader = (RequireHeader) request.getHeader(RequireHeader.NAME);
if (requireHeader != null && "100rel".equalsIgnoreCase(requireHeader.getOptionTag().trim())) {
sendReliably = true;
}
if (respondWithError == null)
for (int provisionalResponseToSend : provisionalResponsesToSend) {
Thread.sleep(getTimeToWaitBetweenProvisionnalResponse());
Response response = protocolObjects.messageFactory.createResponse(provisionalResponseToSend, request);
if (provisionalResponseToSend >= Response.TRYING && provisionalResponseToSend < Response.OK) {
ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
if (provisionalResponseToSend != Response.TRYING && toHeader.getTag() == null) {
// Application is supposed to set.
toHeader.setTag(TO_TAG);
}
if (sendReliably && provisionalResponseToSend != Response.TRYING) {
requireHeader = protocolObjects.headerFactory.createRequireHeader("100rel");
response.addHeader(requireHeader);
Header rseqHeader = protocolObjects.headerFactory.createRSeqHeader(rseqNumber.getAndIncrement());
response.addHeader(rseqHeader);
dialog.sendReliableProvisionalResponse(response);
} else {
st.sendResponse(response);
eventListener.uasAfterResponse(response.getStatusCode(), appServer);
}
}
}
if (respondWithError != null && !sendReliably) {
Response response = protocolObjects.messageFactory.createResponse(respondWithError, request);
response.setReasonPhrase("Unable to setup media services");
st.sendResponse(response);
eventListener.uasAfterResponse(response.getStatusCode(), appServer);
return;
}
ContactHeader contactHeader = (ContactHeader) request.getHeader(ContactHeader.NAME);
if (contactHeader != null && "0.0.0.0".equals(((SipURI) contactHeader.getAddress().getURI()).getHost())) {
abortProcessing = true;
throw new IllegalArgumentException("we received a contact header with 0.0.0.0 in an INVITE !");
}
if (!waitForCancel) {
Address address = null;
if (!isIpv6)
address = protocolObjects.addressFactory.createAddress("Shootme <sip:127.0.0.1:" + myPort + ";transport=" + protocolObjects.transport + ">");
else
address = protocolObjects.addressFactory.createAddress("Shootme <sip:[::1]:" + myPort + ";transport=" + protocolObjects.transport + ">");
contactHeader = protocolObjects.headerFactory.createContactHeader(address);
setFinalResponse(protocolObjects.messageFactory.createResponse(finalResponseToSend, request));
if (testAckViaParam) {
ViaHeader viaHeader = (ViaHeader) getFinalResponse().getHeader(ViaHeader.NAME);
viaHeader.setParameter("testAckViaParam", "true");
}
ToHeader toHeader = (ToHeader) getFinalResponse().getHeader(ToHeader.NAME);
if (toHeader.getTag() == null) {
// Application is supposed to set.
toHeader.setTag(TO_TAG);
}
getFinalResponse().addHeader(contactHeader);
if (!sendReliably) {
Thread.sleep(2000);
st.sendResponse(getFinalResponse());
eventListener.uasAfterResponse(getFinalResponse().getStatusCode(), appServer);
}
} else {
logger.info("Waiting for CANCEL, stopping the INVITE processing ");
return;
}
if (("join").equalsIgnoreCase(((SipUri) request.getRequestURI()).getUser())) {
sendJoinMessage = true;
}
if (("replaces").equalsIgnoreCase(((SipUri) request.getRequestURI()).getUser())) {
sendReplacesMessage = true;
}
} catch (Exception ex) {
logger.error("unexpected exception", ex);
throw new RuntimeException(ex);
}
}
use of javax.sip.header.ContactHeader in project load-balancer by RestComm.
the class Shootist method sendInitialInvite.
public void sendInitialInvite() {
try {
if (!started)
start();
String fromName = "BigGuy";
String fromSipAddress = "here.com";
String fromDisplayName = "The Master Blaster";
String toSipAddress = "there.com";
String toUser = "LittleGuy";
String toDisplayName = "The Little Blister";
// 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<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
String ipAddress = udpListeningPoint.getIPAddress();
ViaHeader viaHeader = headerFactory.createViaHeader(ipAddress, sipProvider.getListeningPoint(transport).getPort(), transport, null);
// add via headers
viaHeaders.add(viaHeader);
// Create ContentTypeHeader
ContentTypeHeader contentTypeHeader = headerFactory.createContentTypeHeader("application", "sdp");
// Create a new CallId header
CallIdHeader callIdHeader = sipProvider.getNewCallId();
// Create a new Cseq header
CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L, Request.INVITE);
// Create a new MaxForwardsHeader
MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
// Create the request.
Request request = messageFactory.createRequest(requestURI, Request.INVITE, 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());
contactUrl.setLrParam();
// 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);
// You can add extension headers of your own making
// to the outgoing SIP request.
// Add the extension header.
Header extensionHeader = headerFactory.createHeader("My-Header", "my header value");
request.addHeader(extensionHeader);
String sdpData = "v=0\r\n" + "o=4855 13760799956958020 13760799956958020" + " IN IP4 129.6.55.78\r\n" + "s=mysession session\r\n" + "p=+46 8 52018010\r\n" + "c=IN IP4 129.6.55.78\r\n" + "t=0 0\r\n" + "m=audio 6022 RTP/AVP 0 4 18\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n" + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
byte[] contents = sdpData.getBytes();
request.setContent(contents, contentTypeHeader);
// You can add as many extension headers as you
// want.
extensionHeader = headerFactory.createHeader("My-Other-Header", "my new header value ");
request.addHeader(extensionHeader);
Header callInfoHeader = headerFactory.createHeader("Call-Info", "<http://www.antd.nist.gov>");
request.addHeader(callInfoHeader);
// Create the client transaction.
inviteTid = sipProvider.getNewClientTransaction(request);
// send the request out.
inviteTid.sendRequest();
dialog = inviteTid.getDialog();
} catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
fail("Unxpected exception ");
}
}
use of javax.sip.header.ContactHeader in project Spark by igniterealtime.
the class TransferProcessing method sayOK.
public void sayOK(int callID) throws CommunicationsException {
try {
Call call = callProcessing.getCallDispatcher().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 ServerTransaction " + "from the call's associated dialog!");
}
ServerTransaction serverTransaction = (ServerTransaction) transaction;
Response ok = null;
try {
ok = sipManCallback.messageFactory.createResponse(Response.OK, dialog.getFirstTransaction().getRequest());
sipManCallback.attachToTag(ok, dialog);
} catch (ParseException ex) {
call.setState(Call.DISCONNECTED);
throw new CommunicationsException("Failed to construct an OK response to an INVITE request", ex);
}
// and should probably be removed
if (((ToHeader) ok.getHeader(ToHeader.NAME)).getTag() == null) {
try {
((ToHeader) ok.getHeader(ToHeader.NAME)).setTag(Integer.toString(dialog.hashCode()));
} catch (ParseException ex) {
call.setState(Call.DISCONNECTED);
throw new CommunicationsException("Unable to set to tag", ex);
}
}
ContactHeader contactHeader = sipManCallback.getContactHeader();
ok.addHeader(contactHeader);
try {
serverTransaction.sendResponse(ok);
} 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);
}
} finally {
}
}
Aggregations