use of javax.sip.header.ViaHeader in project load-balancer by RestComm.
the class TestSipListener method processSubscribe.
/**
* Process the invite request.
*/
public void processSubscribe(RequestEvent requestEvent, ServerTransaction serverTransaction) {
SipProvider sipProvider = (SipProvider) requestEvent.getSource();
Request request = requestEvent.getRequest();
try {
logger.info("notifier: got an Subscribe sending OK");
logger.info("notifier: " + request);
logger.info("notifier : dialog = " + requestEvent.getDialog());
EventHeader eventHeader = (EventHeader) request.getHeader(EventHeader.NAME);
// this.gotSubscribeRequest = true;
// Always create a ServerTransaction, best as early as possible in the code
Response response = null;
ServerTransaction st = requestEvent.getServerTransaction();
if (st == null) {
st = sipProvider.getNewServerTransaction(request);
}
// Check if it is an initial SUBSCRIBE or a refresh / unsubscribe
boolean isInitial = requestEvent.getDialog() == null;
if (isInitial) {
// JvB: need random tags to test forking
String toTag = Integer.toHexString((int) (Math.random() * Integer.MAX_VALUE));
response = protocolObjects.messageFactory.createResponse(202, request);
ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
// Sanity check: to header should not ahve a tag. Else the dialog
// should have matched
// Application is supposed to set.
toHeader.setTag(toTag);
this.dialog = st.getDialog();
// subscribe dialogs do not terminate on bye.
this.dialog.terminateOnBye(false);
} else {
response = protocolObjects.messageFactory.createResponse(202, request);
this.dialog = st.getDialog();
// subscribe dialogs do not terminate on bye.
this.dialog.terminateOnBye(false);
}
// Both 2xx response to SUBSCRIBE and NOTIFY need a Contact
Address address = null;
if (!isIpv6)
address = protocolObjects.addressFactory.createAddress("Notifier <sip:127.0.0.1>");
else
address = protocolObjects.addressFactory.createAddress("Notifier <sip:[::1]>");
((SipURI) address.getURI()).setPort(sipProvider.getListeningPoint(ListeningPoint.UDP).getPort());
ContactHeader contactHeader = protocolObjects.headerFactory.createContactHeader(address);
response.addHeader(contactHeader);
// Expires header is mandatory in 2xx responses to SUBSCRIBE
ExpiresHeader expires = (ExpiresHeader) request.getHeader(ExpiresHeader.NAME);
if (expires == null) {
// rather short
expires = protocolObjects.headerFactory.createExpiresHeader(30);
}
response.addHeader(expires);
/*
* JvB: The SUBSCRIBE MUST be answered first. See RFC3265 3.1.6.2:
* "[...] a NOTIFY message is always sent immediately after any 200-
* class response to a SUBSCRIBE request"
*
* Do this before creating the NOTIFY request below
*/
st.sendResponse(response);
/*
* NOTIFY requests MUST contain a "Subscription-State" header with a
* value of "active", "pending", or "terminated". The "active" value
* indicates that the subscription has been accepted and has been
* authorized (in most cases; see section 5.2.). The "pending" value
* indicates that the subscription has been received, but that
* policy information is insufficient to accept or deny the
* subscription at this time. The "terminated" value indicates that
* the subscription is not active.
*/
if (sendNotify) {
Request notifyRequest = dialog.createRequest("NOTIFY");
// Mark the contact header, to check that the remote contact is updated
// ((SipURI)contactHeader.getAddress().getURI()).setParameter("id","not");
// Initial state is pending, second time we assume terminated (Expires==0)
SubscriptionStateHeader sstate = protocolObjects.headerFactory.createSubscriptionStateHeader(expires.getExpires() != 0 ? SubscriptionStateHeader.PENDING : SubscriptionStateHeader.TERMINATED);
allSubscriptionStates.add(sstate.getState().toLowerCase());
// Need a reason for terminated
if (sstate.getState().equalsIgnoreCase("terminated")) {
sstate.setReasonCode("deactivated");
}
notifyRequest.addHeader(sstate);
notifyRequest.setHeader(eventHeader);
notifyRequest.setHeader(contactHeader);
// notifyRequest.setHeader(routeHeader);
ClientTransaction ct = sipProvider.getNewClientTransaction(notifyRequest);
if (sstate.getState().equals(SubscriptionStateHeader.TERMINATED)) {
Thread.sleep(timeToWaitBetweenSubsNotify);
}
// Let the other side know that the tx is pending acceptance
//
dialog.sendRequest(ct);
logger.info("NOTIFY Branch ID " + ((ViaHeader) request.getHeader(ViaHeader.NAME)).getParameter("branch"));
logger.info("Dialog " + dialog);
logger.info("Dialog state after pending NOTIFY: " + dialog.getState());
if (expires.getExpires() != 0) {
Thread myEventSource = new Thread(new MyEventSource(this, eventHeader));
myEventSource.start();
}
}
} catch (Throwable ex) {
logger.info(ex.getMessage(), ex);
}
}
use of javax.sip.header.ViaHeader 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.ViaHeader in project load-balancer by RestComm.
the class TestSipListener method processAck.
public void processAck(Request request, ServerTransaction serverTransactionId) {
try {
logger.info("shootist: got a " + request);
logger.info("shootist: got an ACK. ServerTxId = " + serverTransactionId);
ackReceived = true;
// we don't count retransmissions
if (serverTransactionId != null) {
ackCount++;
}
if (testAckViaParam) {
ViaHeader viaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
String param = viaHeader.getParameter("testAckViaParam");
if (param != null) {
abortProcessing = true;
logger.error("the Via Param set in the response shouldn't be present in the ACK");
return;
}
}
if (sendBye) {
Thread.sleep(timeToWaitBeforeBye);
if (serverTransactionId != null && serverTransactionId.getDialog() != null) {
Request byeRequest = serverTransactionId.getDialog().createRequest(Request.BYE);
logger.info("BYE created : " + byeRequest);
ClientTransaction ct = sipProvider.getNewClientTransaction(byeRequest);
logger.info("Sending BYE : " + byeRequest);
serverTransactionId.getDialog().sendRequest(ct);
logger.info("Dialog State = " + serverTransactionId.getDialog().getState());
}
}
if (!joinRequestReceived && sendJoinMessage) {
String fromUser = "join";
String fromHost = "sip-servlets.com";
SipURI fromAddress = protocolObjects.addressFactory.createSipURI(fromUser, fromHost);
String toUser = "join-receiver";
String toHost = "sip-servlets.com";
SipURI toAddress = protocolObjects.addressFactory.createSipURI(toUser, toHost);
CallIdHeader callIdHeader = (CallIdHeader) request.getHeader(CallIdHeader.NAME);
FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
ToHeader toHeader = (ToHeader) request.getHeader(ToHeader.NAME);
JoinHeader joinHeader = (JoinHeader) ((HeaderFactoryExt) protocolObjects.headerFactory).createJoinHeader(callIdHeader.getCallId(), toHeader.getTag(), fromHeader.getTag());
sendSipRequest("MESSAGE", fromAddress, toAddress, joinHeader.toString(), null, false);
}
if (!isReplacesRequestReceived() && sendReplacesMessage) {
String fromUser = "replaces";
String fromHost = "sip-servlets.com";
SipURI fromAddress = protocolObjects.addressFactory.createSipURI(fromUser, fromHost);
String toUser = "replaces-receiver";
String toHost = "sip-servlets.com";
SipURI toAddress = protocolObjects.addressFactory.createSipURI(toUser, toHost);
CallIdHeader callIdHeader = (CallIdHeader) request.getHeader(CallIdHeader.NAME);
FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
ToHeader toHeader = (ToHeader) request.getHeader(ToHeader.NAME);
ReplacesHeader replacesHeader = (ReplacesHeader) ((HeaderFactoryExt) protocolObjects.headerFactory).createReplacesHeader(callIdHeader.getCallId(), toHeader.getTag(), fromHeader.getTag());
sendSipRequest("MESSAGE", fromAddress, toAddress, replacesHeader.toString(), null, false);
}
if (joinRequestReceived) {
sendBye();
sendBye(joinDialog);
}
if (isReplacesRequestReceived()) {
sendBye();
sendBye(replacesDialog);
}
if (sendReinvite && !reinviteSent) {
List<Header> headers = new ArrayList<Header>();
Header reinviteHeader = protocolObjects.headerFactory.createHeader("ReInvite", "true");
headers.add(reinviteHeader);
sendInDialogSipRequest("INVITE", null, null, null, headers, null);
reinviteSent = true;
return;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of javax.sip.header.ViaHeader in project camel by apache.
the class SipSubscriptionListener method processRequest.
public void processRequest(RequestEvent requestReceivedEvent) {
Request request = requestReceivedEvent.getRequest();
ServerTransaction serverTransactionId = requestReceivedEvent.getServerTransaction();
String viaBranch = ((ViaHeader) (request.getHeaders(ViaHeader.NAME).next())).getParameter("branch");
LOG.debug("Request: {}", request.getMethod());
LOG.debug("Server Transaction Id: {}", serverTransactionId);
LOG.debug("Received From Branch: {}", viaBranch);
if (request.getMethod().equals(Request.NOTIFY)) {
processNotify(requestReceivedEvent, serverTransactionId);
}
}
use of javax.sip.header.ViaHeader in project camel by apache.
the class SipConfiguration method createViaHeaders.
private void createViaHeaders() throws ParseException, InvalidArgumentException {
viaHeaders = new ArrayList<ViaHeader>();
ViaHeader viaHeader = headerFactory.createViaHeader(getFromHost(), getFromPort(), getTransport(), null);
viaHeaders.add(viaHeader);
}
Aggregations