use of javax.sip.header.SIPETagHeader 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.SIPETagHeader in project load-balancer by RestComm.
the class TestSipListener method processPublish.
private void processPublish(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
try {
SipProvider sipProvider = (SipProvider) requestEvent.getSource();
Request request = requestEvent.getRequest();
logger.info("shootist: got a publish . ServerTxId = " + serverTransactionId);
ServerTransaction st = requestEvent.getServerTransaction();
if (st == null) {
st = sipProvider.getNewServerTransaction(request);
}
inviteServerTid = st;
Dialog dialog = st.getDialog();
this.dialogCount++;
this.dialog = dialog;
logger.info("Shootme: dialog = " + dialog);
if (request.getRawContent() != null) {
this.lastMessageContent = new String(request.getRawContent());
allMessagesContent.add(new String(lastMessageContent));
}
SIPIfMatchHeader sipIfMatchHeader = (SIPIfMatchHeader) request.getHeader(SIPIfMatchHeader.NAME);
boolean sipIfMatchFound = true;
if (sipIfMatchHeader != null && sipIfMatchHeader.getETag() != null && !sipIfMatchHeader.getETag().equals(sipETag)) {
sipIfMatchFound = false;
}
if (sipIfMatchFound) {
Response response = protocolObjects.messageFactory.createResponse(200, request);
sipETag = Integer.toString(new Random().nextInt(10000000));
SIPETagHeader sipTagHeader = protocolObjects.headerFactory.createSIPETagHeader(sipETag);
response.addHeader(sipTagHeader);
response.addHeader(request.getHeader(ExpiresHeader.NAME));
st.sendResponse(response);
logger.info("shootist: Sending OK.");
} else {
Response response = protocolObjects.messageFactory.createResponse(500, request);
serverTransactionId.sendResponse(response);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
Aggregations