use of javax.sip.header.SubscriptionStateHeader 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.header.SubscriptionStateHeader in project camel by apache.
the class SipSubscriptionListener method processNotify.
public synchronized void processNotify(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
LOG.debug("Notification received at Subscriber");
SipProvider provider = (SipProvider) requestEvent.getSource();
Request notify = requestEvent.getRequest();
try {
if (serverTransactionId == null) {
LOG.info("ServerTransaction is null. Creating new Server transaction");
serverTransactionId = provider.getNewServerTransaction(notify);
}
Dialog dialog = serverTransactionId.getDialog();
if (dialog != subscriberDialog) {
forkedDialog = dialog;
}
//Dispatch the response along the route
dispatchExchange(notify.getContent());
// Send back an success response
Response response = sipSubscriber.getConfiguration().getMessageFactory().createResponse(200, notify);
response.addHeader(sipSubscriber.getConfiguration().getContactHeader());
serverTransactionId.sendResponse(response);
SubscriptionStateHeader subscriptionState = (SubscriptionStateHeader) notify.getHeader(SubscriptionStateHeader.NAME);
// Subscription is terminated?
if (subscriptionState.getState().equalsIgnoreCase(SubscriptionStateHeader.TERMINATED)) {
LOG.info("Subscription state is terminated. Deleting the current dialog");
dialog.delete();
}
} catch (Exception e) {
LOG.error("Exception thrown during Notify processing in the SipSubscriptionListener.", e);
}
}
use of javax.sip.header.SubscriptionStateHeader in project Openfire by igniterealtime.
the class SimpleSession method prepareNotifyRequest.
private Request prepareNotifyRequest(Dialog dialog) throws ParseException {
if (dialog == null) {
return null;
}
printDialog(dialog);
String fromTag = dialog.getRemoteTag();
Address fromAddress = dialog.getRemoteParty();
SipURI destUri = (SipURI) fromAddress.getURI();
dialog.incrementLocalSequenceNumber();
long seqNum = dialog.getLocalSeqNumber();
String callId = dialog.getCallId().getCallId();
SipURI fromReqUri = null;
Log.debug("Getting request URI from dialog");
Address fromReqAddr = dialog.getRemoteTarget();
if (fromReqAddr != null && fromReqAddr.getURI() != null && fromReqAddr.getURI() instanceof SipURI)
fromReqUri = (SipURI) fromReqAddr.getURI();
if (fromReqUri == null) {
Log.debug("Getting request URI from destination URI");
fromReqUri = destUri;
}
// Instantiate request packet
Request notifyRequest = prepareRequest(RequestType.NOTIFY, destUri, fromTag, fromReqUri, callId, seqNum);
// Request notifyRequest = dialog.createRequest(Request.NOTIFY);
((FromHeader) notifyRequest.getHeader(FromHeader.NAME)).setTag(dialog.getLocalTag());
// Set "subscription state" header
SubscriptionStateHeader subscriptionStateHeader = headerFactory.createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE.toLowerCase());
// if (expires > 0) subscriptionStateHeader.setExpires(expires);
notifyRequest.setHeader(subscriptionStateHeader);
// Set "event" header
notifyRequest.setHeader(headerFactory.createEventHeader("presence"));
return notifyRequest;
}
use of javax.sip.header.SubscriptionStateHeader in project camel by apache.
the class SipPresenceAgentListener method sendNotification.
private void sendNotification(EventHeader eventHeader, boolean isInitial, Object body) throws SipException, ParseException {
/*
* 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.
*/
Request notifyRequest = dialog.createRequest("NOTIFY");
// Mark the contact header, to check that the remote contact is updated
((SipURI) sipPresenceAgent.getConfiguration().getContactHeader().getAddress().getURI()).setParameter(sipPresenceAgent.getConfiguration().getFromUser(), sipPresenceAgent.getConfiguration().getFromHost());
SubscriptionStateHeader sstate;
if (isInitial) {
// Initial state is pending, second time we assume terminated (Expires==0)
sstate = sipPresenceAgent.getConfiguration().getHeaderFactory().createSubscriptionStateHeader(isInitial ? SubscriptionStateHeader.PENDING : SubscriptionStateHeader.TERMINATED);
// Need a reason for terminated
if (sstate.getState().equalsIgnoreCase("terminated")) {
sstate.setReasonCode("deactivated");
}
} else {
sstate = sipPresenceAgent.getConfiguration().getHeaderFactory().createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE);
}
notifyRequest.addHeader(sstate);
notifyRequest.setHeader(eventHeader);
notifyRequest.setHeader(sipPresenceAgent.getConfiguration().getContactHeader());
notifyRequest.setContent(body, sipPresenceAgent.getConfiguration().getContentTypeHeader());
LOG.debug("Sending the following NOTIFY request to Subscriber: {}", notifyRequest);
ClientTransaction clientTransactionId = sipPresenceAgent.getProvider().getNewClientTransaction(notifyRequest);
dialog.sendRequest(clientTransactionId);
}
use of javax.sip.header.SubscriptionStateHeader in project load-balancer by RestComm.
the class TestSipListener method processRefer.
public void processRefer(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
try {
SipProvider sipProvider = (SipProvider) requestEvent.getSource();
Request request = requestEvent.getRequest();
logger.info("shootist: got a refer . 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);
Response response = protocolObjects.messageFactory.createResponse(referResponseToSend, request);
sipETag = Integer.toString(new Random().nextInt(10000000));
st.sendResponse(response);
logger.info("shootist: Sending " + referResponseToSend);
List<Header> headers = new ArrayList<Header>();
EventHeader eventHeader = (EventHeader) protocolObjects.headerFactory.createHeader(EventHeader.NAME, "Refer");
headers.add(eventHeader);
if (sendNotifyForRefer) {
if (!referReceived) {
referReceived = true;
SubscriptionStateHeader subscriptionStateHeader = (SubscriptionStateHeader) protocolObjects.headerFactory.createHeader(SubscriptionStateHeader.NAME, "active;expires=3600");
headers.add(subscriptionStateHeader);
allMessagesContent.add("SIP/2.0 100 Trying");
sendInDialogSipRequest(Request.NOTIFY, "SIP/2.0 100 Trying", "message", "sipfrag;version=2.0", headers, null);
Thread.sleep(1000);
headers.remove(subscriptionStateHeader);
subscriptionStateHeader = (SubscriptionStateHeader) protocolObjects.headerFactory.createHeader(SubscriptionStateHeader.NAME, "terminated;reason=noresource");
headers.add(subscriptionStateHeader);
if (inviteRequest == null) {
ExtensionHeader extensionHeader = (ExtensionHeader) protocolObjects.headerFactory.createHeader("Out-Of-Dialog", "true");
headers.add(extensionHeader);
}
allMessagesContent.add("SIP/2.0 200 OK");
sendInDialogSipRequest(Request.NOTIFY, "SIP/2.0 200 OK", "message", "sipfrag;version=2.0", headers, null);
} else {
SubscriptionStateHeader subscriptionStateHeader = (SubscriptionStateHeader) protocolObjects.headerFactory.createHeader(SubscriptionStateHeader.NAME, "active;expires=3600");
headers.add(subscriptionStateHeader);
sendInDialogSipRequest(Request.NOTIFY, "SIP/2.0 100 Subsequent", "message", "sipfrag;version=2.0", headers, null);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
Aggregations