use of javax.sip.header.RequireHeader in project XobotOS by xamarin.
the class SIPDialog method sendReliableProvisionalResponse.
/*
* (non-Javadoc)
*
* @see javax.sip.Dialog#sendReliableProvisionalResponse(javax.sip.message.Response)
*/
public void sendReliableProvisionalResponse(Response relResponse) throws SipException {
if (!this.isServer()) {
throw new SipException("Not a Server Dialog");
}
SIPResponse sipResponse = (SIPResponse) relResponse;
if (relResponse.getStatusCode() == 100)
throw new SipException("Cannot send 100 as a reliable provisional response");
if (relResponse.getStatusCode() / 100 > 2)
throw new SipException("Response code is not a 1xx response - should be in the range 101 to 199 ");
/*
* Do a little checking on the outgoing response.
*/
if (sipResponse.getToTag() == null) {
throw new SipException("Badly formatted response -- To tag mandatory for Reliable Provisional Response");
}
ListIterator requireList = (ListIterator) relResponse.getHeaders(RequireHeader.NAME);
boolean found = false;
if (requireList != null) {
while (requireList.hasNext() && !found) {
RequireHeader rh = (RequireHeader) requireList.next();
if (rh.getOptionTag().equalsIgnoreCase("100rel")) {
found = true;
}
}
}
if (!found) {
Require require = new Require("100rel");
relResponse.addHeader(require);
if (sipStack.isLoggingEnabled()) {
sipStack.getStackLogger().logDebug("Require header with optionTag 100rel is needed -- adding one");
}
}
SIPServerTransaction serverTransaction = (SIPServerTransaction) this.getFirstTransaction();
/*
* put into the dialog table before sending the response so as to avoid race condition
* with PRACK
*/
this.setLastResponse(serverTransaction, sipResponse);
this.setDialogId(sipResponse.getDialogId(true));
serverTransaction.sendReliableProvisionalResponse(relResponse);
this.startRetransmitTimer(serverTransaction, relResponse);
}
use of javax.sip.header.RequireHeader in project load-balancer by RestComm.
the class TestSipListener method processResponse.
public void processResponse(ResponseEvent responseReceivedEvent) {
if (abortProcessing) {
logger.error("Processing aborted");
return;
}
Response response = (Response) responseReceivedEvent.getResponse();
System.out.println("Process response : " + response);
eventListener.uacAfterResponse(response.getStatusCode(), appServer);
if (response.getStatusCode() == 491)
numberOf491s++;
RecordRouteHeader recordRouteHeader = (RecordRouteHeader) response.getHeader(RecordRouteHeader.NAME);
ContactHeader contactHeader = (ContactHeader) response.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 a response !");
}
if (response.getStatusCode() >= 400 && response.getStatusCode() < 999) {
this.serverErrorReceived = true;
}
if (response.getStatusCode() == 503) {
this.serviceUnavailableReceived = true;
}
if (response.toString().toLowerCase().contains("info")) {
lastInfoResponseTime = System.currentTimeMillis();
}
ClientTransaction tid = responseReceivedEvent.getClientTransaction();
CSeqHeader cseq = (CSeqHeader) response.getHeader(CSeqHeader.NAME);
logger.info("Response received : Status Code = " + response.getStatusCode() + " " + cseq);
// not dropping in PRACK case on REINVITE the ClientTx can be null it seems
if (tid == null && !prackSent) {
if (countRetrans) {
nbRetrans++;
}
logger.info("Stray response -- dropping ");
return;
}
if (tid != null) {
logger.info("transaction state is " + tid.getState());
logger.info("Dialog = " + tid.getDialog());
if (tid.getDialog() != null) {
logger.info("Dialog State is " + tid.getDialog().getState());
}
}
try {
if (response.getStatusCode() > 100 && response.getStatusCode() < 200) {
informationalResponse = response;
}
if (response.getStatusCode() >= 200 && response.getStatusCode() < 700) {
logger.info("final response received : status code " + response.getStatusCode());
finalResponseReceived = true;
setFinalResponseStatus(response.getStatusCode());
setFinalResponse(response);
}
if (response.getStatusCode() == Response.OK) {
if (cseq.getMethod().equals(Request.INVITE) && sendAck) {
inviteOkResponse = response;
@SuppressWarnings("deprecation") Request ackRequest = responseReceivedEvent.getClientTransaction().createAck();
if (useToURIasRequestUri) {
ackRequest.setRequestURI(requestURI);
}
if (timeToWaitBeforeAck > 0) {
Thread.sleep(timeToWaitBeforeAck);
}
if (!sendSubsequentRequestsThroughSipProvider && tid.getDialog() != null) {
tid.getDialog().sendAck(ackRequest);
} else {
sipProvider.sendRequest(ackRequest);
}
ackSent = true;
// If the caller is supposed to send the bye
if (sendReinvite && !reinviteSent) {
List<Header> headers = new ArrayList<Header>();
Header reinviteHeader = protocolObjects.headerFactory.createHeader("ReInvite", "true");
headers.add(reinviteHeader);
if (prackSent) {
headers.add(protocolObjects.headerFactory.createHeader(RequireHeader.NAME, "100rel"));
}
sendInDialogSipRequest("INVITE", null, null, null, headers, null);
reinviteSent = true;
return;
}
if (sendBye) {
// Thread.sleep(30000);
sendBye();
}
if (sendByeAfterTerminatingNotify) {
tid.getDialog().terminateOnBye(false);
}
} else if (cseq.getMethod().equals(Request.BYE)) {
okToByeReceived = true;
} else if (cseq.getMethod().equals(Request.CANCEL)) {
this.cancelOkReceived = true;
// if (tid.getDialog().getState() == DialogState.CONFIRMED) {
// // oops cancel went in too late. Need to hang up the
// // dialog.
// logger.info("Sending BYE -- cancel went in too late !!");
// Request byeRequest = dialog.createRequest(Request.BYE);
// ClientTransaction ct = sipProvider
// .getNewClientTransaction(byeRequest);
// tid.getDialog().sendRequest(ct);
// }
} else if (cseq.getMethod().equals(Request.PUBLISH)) {
SIPETagHeader sipTagHeader = (SIPETagHeader) response.getHeader(SIPETag.NAME);
sipETag = sipTagHeader.getETag();
} else if (cseq.getMethod().equals(Request.PRACK)) {
okToPrackReceived = true;
}
} else if (response.getStatusCode() == Response.MOVED_TEMPORARILY) {
// Dialog dies as soon as you get an error response.
if (cseq.getMethod().equals(Request.INVITE)) {
// lookup the contact header
ContactHeader contHdr = (ContactHeader) response.getHeader(ContactHeader.NAME);
// we can re-use the from header
FromHeader from = ((FromHeader) response.getHeader(FromHeader.NAME));
// we use the to-address, but without the tag
ToHeader to = (ToHeader) (response.getHeader(ToHeader.NAME)).clone();
to.removeParameter("tag");
// the call-id can be re-used
CallIdHeader callID = ((CallIdHeader) response.getHeader(CallIdHeader.NAME));
// we take the next cseq
long seqNo = (((CSeqHeader) response.getHeader(CSeqHeader.NAME)).getSeqNumber());
logger.info("seqNo = " + seqNo);
CSeqHeader cseqNew = protocolObjects.headerFactory.createCSeqHeader(++seqNo, "INVITE");
// Create ViaHeaders (either use tcp or udp)
ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
ViaHeader viaHeader = null;
if (!isIpv6)
viaHeader = protocolObjects.headerFactory.createViaHeader("127.0.0.1", sipProvider.getListeningPoint(protocolObjects.transport).getPort(), protocolObjects.transport, null);
else
viaHeader = protocolObjects.headerFactory.createViaHeader("::1", sipProvider.getListeningPoint(protocolObjects.transport).getPort(), protocolObjects.transport, null);
// add via headers
viaHeaders.add(viaHeader);
// create max forwards
MaxForwardsHeader maxForwardsHeader = protocolObjects.headerFactory.createMaxForwardsHeader(10);
// create invite Request
SipURI newUri = (SipURI) this.requestURI.clone();
newUri.setParameter("redirection", "true");
requestURI = newUri;
Request invRequest = protocolObjects.messageFactory.createRequest(newUri, "INVITE", callID, cseqNew, from, to, viaHeaders, maxForwardsHeader);
// we set the Request URI to the address given
SipURI contactURI = protocolObjects.addressFactory.createSipURI(null, this.listeningPoint.getIPAddress());
contactURI.setPort(this.listeningPoint.getPort());
contactURI.setTransportParam(protocolObjects.transport);
Address address = protocolObjects.addressFactory.createAddress(contactURI);
ContactHeader contact = protocolObjects.headerFactory.createContactHeader(address);
invRequest.addHeader(contact);
// the contacat header in the response contains where to redirect
// the request to -- which in this case happens to be back to the
// same location.
ContactHeader chdr = (ContactHeader) response.getHeader(ContactHeader.NAME);
SipURI sipUri = (SipURI) chdr.getAddress().getURI();
// sipUri.setLrParam();
RouteHeader routeHeader = protocolObjects.headerFactory.createRouteHeader(chdr.getAddress());
invRequest.addHeader(routeHeader);
invRequest.setRequestURI(sipUri);
logger.info("Sending INVITE to " + contHdr.getAddress().getURI().toString());
inviteClientTid = sipProvider.getNewClientTransaction(invRequest);
logger.info("New TID = " + inviteClientTid);
inviteClientTid.sendRequest();
logger.info("sendReqeust succeeded " + inviteClientTid);
Dialog dialog = inviteClientTid.getDialog();
this.dialogCount++;
this.dialog = dialog;
}
} else if (response.getStatusCode() == Response.REQUEST_TERMINATED) {
if (cseq.getMethod().equals(Request.INVITE)) {
this.requestTerminatedReceived = true;
}
} else if (response.getStatusCode() == Response.RINGING && sendUpdateOn180) {
Request updateRequest = dialog.createRequest(Request.UPDATE);
ClientTransaction ct = sipProvider.getNewClientTransaction(updateRequest);
dialog.sendRequest(ct);
} else if (response.getStatusCode() > Response.TRYING && response.getStatusCode() < Response.OK) {
RequireHeader requireHeader = (RequireHeader) response.getHeader(RequireHeader.NAME);
if (requireHeader != null && "100rel".equalsIgnoreCase(requireHeader.getOptionTag().trim())) {
Request prack = dialog.createPrack(response);
ClientTransaction ct = sipProvider.getNewClientTransaction(prack);
dialog.sendRequest(ct);
prackSent = true;
}
}
/**
* end of modified code
*/
} catch (Exception ex) {
logger.error("An unexpected exception occured while processing the response", ex);
}
}
use of javax.sip.header.RequireHeader 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);
}
}
Aggregations