use of javax.sip.ServerTransaction in project Openfire by igniterealtime.
the class SimpleListener method processRequest.
public void processRequest(RequestEvent requestEvent) {
ServerTransaction serverTransaction = requestEvent.getServerTransaction();
Dialog dialog = null;
if (serverTransaction != null) {
Log.debug("SimpleListener(" + myUsername + ").processRequest: Getting dialog");
dialog = serverTransaction.getDialog();
}
int responseCode = 200;
Log.debug("SimpleListener(" + myUsername + ").processRequest: Received a request event: \n" + requestEvent.getRequest().toString());
String fromAddr = "";
Request request = requestEvent.getRequest();
if (request.getHeader(FromHeader.NAME) != null) {
FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
Address fromAddress = fromHeader.getAddress();
// String displayName = fromAddress.getDisplayName();
URI fromUri = fromAddress.getURI();
if (fromUri != null) {
if (fromUri.isSipURI()) {
SipURI fromSipUri = (SipURI) fromUri;
fromAddr = fromSipUri.getUser() + "@" + fromSipUri.getHost();
} else {
fromAddr = fromUri.toString();
}
}
}
Log.debug("SimpleListener(" + myUsername + ").processRequest: FromAddr = " + fromAddr);
Log.debug("SimpleListener(" + myUsername + ").processRequest: Request method = '" + request.getMethod() + "'");
if (request.getMethod().equals(Request.MESSAGE)) {
Log.debug("SimpleListener(" + myUsername + ").processRequest: Starting MESSAGE request handling process.");
JID senderJid = getSession().getTransport().convertIDToJID(fromAddr);
String msgContent = new String((byte[]) request.getContent());
Log.debug("SimpleListener(" + myUsername + ").processRequest: Forwarding MESSAGE request as XMPP message, setting from = " + senderJid + " and content = '" + msgContent + "'");
getSession().getTransport().sendMessage(getSession().getJID(), senderJid, msgContent);
getSession().sendResponse(responseCode, request, serverTransaction);
} else if (request.getMethod().equals(Request.NOTIFY)) {
SubscriptionStateHeader subscriptionStateHeader = (SubscriptionStateHeader) request.getHeader(SubscriptionStateHeader.NAME);
Log.debug("SimpleListener(" + myUsername + ").processRequest: NOTIFY request handling process started.");
if (subscriptionStateHeader.getState().equalsIgnoreCase(SubscriptionStateHeader.ACTIVE)) {
Log.debug("SimpleListener(" + myUsername + ").processRequest: NOTIFY Active!");
int expires = subscriptionStateHeader.getExpires();
Log.debug("SimpleListener(" + myUsername + ").processRequest: NOTIFY Expiry = " + expires);
try {
if (expires > 0) {
String content = "";
if (request.getContent() != null)
content = new String((byte[]) request.getContent());
if (content.length() > 0) {
SimplePresence simplePresence = SimplePresence.parseSimplePresence(content);
try {
SimpleBuddy buddy = getSession().getBuddyManager().getBuddy(getSession().getTransport().convertIDToJID(fromAddr));
String verboseStatus = null;
if (simplePresence.getTupleStatus().isOpen()) {
switch(simplePresence.getRpid()) {
case ON_THE_PHONE:
// TODO: Translate this
verboseStatus = "On Phone";
}
}
buddy.setPresenceAndStatus(((SimpleTransport) getSession().getTransport()).convertSIPStatusToXMPP(simplePresence), verboseStatus);
} catch (NotFoundException e) {
// Not in our contact list. Ignore.
Log.debug("SIMPLE: Received presense notification for contact we don't care about: " + fromAddr);
}
}
} else {
Presence p = new Presence();
p.setType(Presence.Type.unsubscribed);
p.setTo(getSession().getJID());
p.setFrom(getSession().getTransport().convertIDToJID(fromAddr));
getSession().getTransport().sendPacket(p);
}
Log.debug("SimpleListener(" + myUsername + ").processRequest: Sending XMPP presence packet.");
} catch (Exception ex) {
Log.debug("SimpleListener(" + myUsername + ").processRequest: Exception occured when processing NOTIFY packet...", ex);
}
} else if (subscriptionStateHeader.getState().equalsIgnoreCase(SubscriptionStateHeader.TERMINATED)) {
Presence p = new Presence();
p.setType(Presence.Type.unsubscribed);
p.setTo(getSession().getJID());
p.setFrom(getSession().getTransport().convertIDToJID(fromAddr));
getSession().getTransport().sendPacket(p);
}
getSession().sendResponse(responseCode, request, serverTransaction);
} else if (request.getMethod().equals(Request.SUBSCRIBE)) {
Log.debug("SimpleListener for " + myUsername + ": SUBSCRIBE request handling process.");
ServerTransaction transaction = getSession().sendResponse(202, request, serverTransaction);
Log.debug("SimpleListener for " + myUsername + ": SUBSCRIBE should be followed by a NOTIFY");
// Send NOTIFY packet.
try {
if (transaction != null)
getSession().sendNotify(transaction.getDialog());
else
getSession().sendNotify(dialog);
} catch (Exception e) {
Log.debug("SimpleListener for " + myUsername + ": Unable to prepare NOTIFY packet.", e);
}
}
}
use of javax.sip.ServerTransaction in project XobotOS by xamarin.
the class SipProviderImpl method getNewDialog.
/*
* (non-Javadoc)
*
* @see javax.sip.SipProvider#getNewDialog(javax.sip.Transaction)
*/
public Dialog getNewDialog(Transaction transaction) throws SipException {
if (transaction == null)
throw new NullPointerException("Null transaction!");
if (!sipStack.isAlive())
throw new SipException("Stack is stopped.");
if (isAutomaticDialogSupportEnabled())
throw new SipException(" Error - AUTOMATIC_DIALOG_SUPPORT is on");
if (!sipStack.isDialogCreated(transaction.getRequest().getMethod()))
throw new SipException("Dialog cannot be created for this method " + transaction.getRequest().getMethod());
SIPDialog dialog = null;
SIPTransaction sipTransaction = (SIPTransaction) transaction;
if (transaction instanceof ServerTransaction) {
SIPServerTransaction st = (SIPServerTransaction) transaction;
Response response = st.getLastResponse();
if (response != null) {
if (response.getStatusCode() != 100)
throw new SipException("Cannot set dialog after response has been sent");
}
SIPRequest sipRequest = (SIPRequest) transaction.getRequest();
String dialogId = sipRequest.getDialogId(true);
dialog = sipStack.getDialog(dialogId);
if (dialog == null) {
dialog = sipStack.createDialog((SIPTransaction) transaction);
// create and register the dialog and add the inital route set.
dialog.addTransaction(sipTransaction);
dialog.addRoute(sipRequest);
sipTransaction.setDialog(dialog, null);
} else {
sipTransaction.setDialog(dialog, sipRequest.getDialogId(true));
}
if (sipRequest.getMethod().equals(Request.INVITE) && this.isDialogErrorsAutomaticallyHandled()) {
sipStack.putInMergeTable(st, sipRequest);
}
} else {
SIPClientTransaction sipClientTx = (SIPClientTransaction) transaction;
SIPResponse response = sipClientTx.getLastResponse();
if (response == null) {
// A response has not yet been received, then set this up as the
// default dialog.
SIPRequest request = (SIPRequest) sipClientTx.getRequest();
String dialogId = request.getDialogId(false);
dialog = sipStack.getDialog(dialogId);
if (dialog != null) {
throw new SipException("Dialog already exists!");
} else {
dialog = sipStack.createDialog(sipTransaction);
}
sipClientTx.setDialog(dialog, null);
} else {
throw new SipException("Cannot call this method after response is received!");
}
}
dialog.addEventListener(this);
return dialog;
}
use of javax.sip.ServerTransaction in project XobotOS by xamarin.
the class SipHelper method sendRinging.
/**
* @param event the INVITE request event
*/
public ServerTransaction sendRinging(RequestEvent event, String tag) throws SipException {
try {
Request request = event.getRequest();
ServerTransaction transaction = getServerTransaction(event);
Response response = mMessageFactory.createResponse(Response.RINGING, request);
ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
toHeader.setTag(tag);
response.addHeader(toHeader);
if (DEBUG)
Log.d(TAG, "send RINGING: " + response);
transaction.sendResponse(response);
return transaction;
} catch (ParseException e) {
throw new SipException("sendRinging()", e);
}
}
use of javax.sip.ServerTransaction in project Spark by igniterealtime.
the class SipManager method processRequest.
// error occurred
// ============================= SIP LISTENER METHODS ==============================
public void processRequest(RequestEvent requestReceivedEvent) {
Log.debug(requestReceivedEvent.getRequest().toString());
ServerTransaction serverTransaction = requestReceivedEvent.getServerTransaction();
Request request = requestReceivedEvent.getRequest();
if (serverTransaction == null) {
try {
serverTransaction = sipProvider.getNewServerTransaction(request);
} catch (TransactionAlreadyExistsException ex) {
return;
} catch (TransactionUnavailableException ex) {
return;
}
}
Dialog dialog = serverTransaction.getDialog();
if (request.getMethod().equals(Request.NOTIFY)) {
Response ok = null;
try {
ok = messageFactory.createResponse(Response.OK, request);
} catch (ParseException ex) {
ex.printStackTrace();
}
ContactHeader contactHeader = null;
try {
contactHeader = getContactHeader();
ok.addHeader(contactHeader);
attachToTag(ok, dialog);
System.err.println(ok.toString());
serverTransaction.sendResponse(ok);
} catch (CommunicationsException e) {
e.printStackTrace();
} catch (InvalidArgumentException e) {
e.printStackTrace();
} catch (SipException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
fireMessageReceived(request);
return;
}
// TODO REMOVE
@SuppressWarnings("unused") Request requestClone = (Request) request.clone();
// INVITE
if (request.getMethod().equals(Request.INVITE)) {
if (serverTransaction.getDialog().getState() == null) {
callProcessing.processInvite(serverTransaction, request);
} else {
callProcessing.processReInvite(serverTransaction, request);
}
} else // ACK
if (request.getMethod().equals(Request.ACK)) {
if (serverTransaction != null && serverTransaction.getDialog().getFirstTransaction().getRequest().getMethod().equals(Request.INVITE)) {
callProcessing.processAck(serverTransaction, request);
} else {
// just ignore
}
} else // BYE
if (request.getMethod().equals(Request.BYE)) {
if (dialog.getFirstTransaction().getRequest().getMethod().equals(Request.INVITE) || dialog.getFirstTransaction().getRequest().getMethod().equals(Request.REFER)) {
callProcessing.processBye(serverTransaction, request);
}
} else // CANCEL
if (request.getMethod().equals(Request.CANCEL)) {
if (dialog.getFirstTransaction().getRequest().getMethod().equals(Request.INVITE)) {
callProcessing.processCancel(serverTransaction, request);
} else {
sendNotImplemented(serverTransaction, request);
fireUnknownMessageReceived(requestReceivedEvent.getRequest());
}
} else // REFER
if (request.getMethod().equals(Request.REFER)) {
transferProcessing.processRefer(serverTransaction, request);
} else if (request.getMethod().equals(Request.INFO)) {
/**
* @todo add proper request handling
*/
fireMessageReceived(request);
} else if (request.getMethod().equals(Request.MESSAGE)) {
messageProcessing.processMessageRequest(serverTransaction, request);
fireMessageReceived(request);
} else if (request.getMethod().equals(Request.OPTIONS)) {
/**
* @todo add proper request handling
*/
sendNotImplemented(serverTransaction, request);
fireUnknownMessageReceived(requestReceivedEvent.getRequest());
} else if (request.getMethod().equals(Request.PRACK)) {
/**
* @todo add proper request handling
*/
sendNotImplemented(serverTransaction, request);
fireUnknownMessageReceived(requestReceivedEvent.getRequest());
} else if (request.getMethod().equals(Request.REGISTER)) {
/**
* @todo add proper request handling
*/
sendNotImplemented(serverTransaction, request);
fireUnknownMessageReceived(requestReceivedEvent.getRequest());
} else if (request.getMethod().equals(Request.SUBSCRIBE)) {
} else if (request.getMethod().equals(Request.UPDATE)) {
/**
* @todo add proper request handling
*/
sendNotImplemented(serverTransaction, request);
fireUnknownMessageReceived(requestReceivedEvent.getRequest());
} else if (request.getMethod().equals(Request.INFO)) {
sendNotImplemented(serverTransaction, request);
fireUnknownMessageReceived(requestReceivedEvent.getRequest());
} else {
// We couldn't recognise the message
sendNotImplemented(serverTransaction, request);
fireUnknownMessageReceived(requestReceivedEvent.getRequest());
}
}
use of javax.sip.ServerTransaction in project Spark by igniterealtime.
the class CallProcessing method sayBusyHere.
// cancel
// busy here
private void sayBusyHere(Dialog dialog) throws CommunicationsException {
Request request = dialog.getFirstTransaction().getRequest();
Response busyHere = null;
try {
busyHere = sipManCallback.messageFactory.createResponse(Response.BUSY_HERE, request);
sipManCallback.attachToTag(busyHere, dialog);
} catch (ParseException ex) {
throw new CommunicationsException("Failed to create the BUSY_HERE response!", ex);
}
if (!dialog.isServer()) {
throw new CommunicationsException("Cannot send BUSY_HERE in a client transaction");
}
ServerTransaction serverTransaction = (ServerTransaction) dialog.getFirstTransaction();
try {
serverTransaction.sendResponse(busyHere);
} catch (SipException ex) {
throw new CommunicationsException("Failed to send the BUSY_HERE response", ex);
} catch (InvalidArgumentException e) {
throw new CommunicationsException("Failed to send the BUSY_HERE response", e);
}
}
Aggregations