use of javax.sip.ServerTransaction in project load-balancer by RestComm.
the class TestSipListener method processMessage.
private void processMessage(Request request, ServerTransaction serverTransactionId) {
if (request.toString().contains("408 received")) {
txTimeoutReceived = true;
}
ServerTransaction serverTransaction = null;
messageRequest = request;
try {
serverTransaction = (serverTransactionId == null ? sipProvider.getNewServerTransaction(request) : serverTransactionId);
} catch (javax.sip.TransactionAlreadyExistsException ex) {
ex.printStackTrace();
return;
} catch (javax.sip.TransactionUnavailableException ex1) {
ex1.printStackTrace();
return;
}
ContentTypeHeader contentTypeHeader = (ContentTypeHeader) request.getHeader(ContentTypeHeader.NAME);
boolean sendInvitewithJoin = false;
boolean sendInvitewithReplaces = false;
if (contentTypeHeader != null) {
if (TEXT_CONTENT_TYPE.equals(contentTypeHeader.getContentType())) {
this.lastMessageContent = new String(request.getRawContent());
allMessagesContent.add(new String(lastMessageContent));
if (lastMessageContent.indexOf("Join : ") != -1) {
this.lastMessageContent = lastMessageContent.substring("Join : ".length());
sendInvitewithJoin = true;
}
if (lastMessageContent.indexOf("Replaces : ") != -1) {
this.lastMessageContent = lastMessageContent.substring("Replaces : ".length());
sendInvitewithReplaces = true;
}
}
} else {
if (request.getHeader("From").toString().contains("b2bua@sip-servlets"))
b2buamessagereceived = true;
}
try {
Response okResponse = protocolObjects.messageFactory.createResponse(Response.OK, request);
ToHeader toHeader = (ToHeader) okResponse.getHeader(ToHeader.NAME);
if (toHeader.getTag() == null) {
toHeader.setTag(Integer.toString(new Random().nextInt(10000000)));
}
// okResponse.addHeader(contactHeader);
serverTransaction.sendResponse(okResponse);
} catch (Exception ex) {
ex.printStackTrace();
logger.error("error sending OK response to message", ex);
}
if (sendInvitewithJoin) {
try {
String fromUser = "receiver";
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);
String[] headerNames = new String[] { "Join" };
String[] headerContents = new String[] { lastMessageContent };
sendSipRequest("INVITE", fromAddress, toAddress, null, null, false, headerNames, headerContents, true);
} catch (Exception ex) {
ex.printStackTrace();
logger.error("error sending INVITE with Join", ex);
}
}
if (sendInvitewithReplaces) {
try {
String fromUser = "receiver";
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);
String[] headerNames = new String[] { "Replaces" };
String[] headerContents = new String[] { lastMessageContent };
sendSipRequest("INVITE", fromAddress, toAddress, null, null, false, headerNames, headerContents, true);
} catch (Exception ex) {
ex.printStackTrace();
logger.error("error sending INVITE with Join", ex);
}
}
}
use of javax.sip.ServerTransaction 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.ServerTransaction in project load-balancer by RestComm.
the class BackToBackUserAgent method forwardResponse.
private void forwardResponse(ResponseEvent receivedResponse) throws SipException {
try {
ServerTransaction serverTransaction = (ServerTransaction) receivedResponse.getClientTransaction().getApplicationData();
Request stRequest = serverTransaction.getRequest();
Response newResponse = this.messageFactory.createResponse(receivedResponse.getResponse().getStatusCode(), stRequest);
ListeningPoint peerListeningPoint = sp.getListeningPoint(transport);
ContactHeader peerContactHeader = ((ListeningPointExt) peerListeningPoint).createContactHeader();
newResponse.setHeader(peerContactHeader);
serverTransaction.sendResponse(newResponse);
} catch (InvalidArgumentException e) {
throw new SipException("invalid response", e);
} catch (ParseException e) {
throw new SipException("invalid response", e);
}
}
use of javax.sip.ServerTransaction 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.ServerTransaction in project load-balancer by RestComm.
the class TestSipListener method processCancel.
private void processCancel(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
try {
cancelReceived = true;
SipProvider sipProvider = (SipProvider) requestEvent.getSource();
Request request = requestEvent.getRequest();
Response response = protocolObjects.messageFactory.createResponse(Response.OK, request);
ServerTransaction st = requestEvent.getServerTransaction();
if (st == null) {
st = sipProvider.getNewServerTransaction(request);
}
Dialog dialog = st.getDialog();
logger.info("Shootme: dialog = " + dialog);
st.sendResponse(response);
response = protocolObjects.messageFactory.createResponse(Response.REQUEST_TERMINATED, inviteRequest);
inviteServerTid.sendResponse(response);
} catch (Exception ex) {
ex.printStackTrace();
logger.error("error sending CANCEL responses", ex);
}
}
Aggregations