use of javax.sip.header.FromHeader 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.FromHeader in project Openfire by igniterealtime.
the class SimpleSession method prepareRequest.
/**
* @param requestType Type of request
* @param destUri The SipURI for the destination. Leave <code>null</code> if a loopback request (e.g. REGISTER) is being made.
* @param toTag The tag for to header. Can leave null.
* @param requestUri The Request URI to set in the message. Leave null if the default destination SipURI should be used.
* @param callId ID of call
* @param seqNum Sequence number
* @return Prepared request
*/
private Request prepareRequest(RequestType requestType, SipURI destUri, String toTag, SipURI requestUri, String callId, long seqNum) {
Request request = null;
String myXMPPUsername = this.jid.getNode();
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing request packet of type '" + requestType + "'");
try {
// Prepare request packet first
request = messageFactory.createRequest(null);
request.setMethod(requestType.toString());
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing request.", e);
}
// Prepare "From" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"From\" header...");
String mySipUsername = registration.getUsername();
try {
SipURI fromUri = addressFactory.createSipURI(mySipUsername, sipHost);
Address fromNameAddress = addressFactory.createAddress(fromUri);
fromNameAddress.setDisplayName(mySipUsername);
FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, getTag());
// Use "set" because this header is mandatory.
request.setHeader(fromHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing FromHeader.", e);
return null;
}
// Prepare "To" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"To\" header...");
try {
if (destUri == null)
destUri = addressFactory.createSipURI(mySipUsername, sipHost);
Address toNameAddress = addressFactory.createAddress(destUri);
ToHeader toHeader = headerFactory.createToHeader(toNameAddress, toTag);
// Use "set" because this header is mandatory.
request.setHeader(toHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing ToHeader.", e);
return null;
}
// Prepare "Via" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"Via\" header...");
try {
ViaHeader viaHeader = headerFactory.createViaHeader(InetAddress.getLocalHost().getHostAddress(), sipPort, ListeningPoint.UDP, null);
// Use "set" because this header is mandatory.
request.setHeader(viaHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing ViaHeader.", e);
return null;
}
// Prepare "CallId" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"CallId\" header...");
CallIdHeader callIdHeader;
try {
if (callId != null)
callIdHeader = headerFactory.createCallIdHeader(callId);
else
callIdHeader = udpSipProvider.getNewCallId();
// Use "set" because this header is mandatory.
request.setHeader(callIdHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing CallIdHeader.", e);
return null;
}
// Prepare "CSeq" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"CSeq\" header...");
try {
CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(seqNum, requestType.toString());
// Use "set" because this header is mandatory.
request.setHeader(cSeqHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing CSeqHeader.", e);
return null;
}
// Prepare "MaxForwards" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"MaxForwards\" header...");
try {
MaxForwardsHeader maxForwardsHeader = headerFactory.createMaxForwardsHeader(70);
// Use "set" because this header is mandatory.
request.setHeader(maxForwardsHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing MaxForwardsHeader.", e);
return null;
}
// Setting Request URI
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: setting request URI...");
try {
if (requestUri == null) {
requestUri = (SipURI) destUri.clone();
requestUri.setTransportParam(ListeningPoint.UDP);
}
request.setRequestURI(requestUri);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when setting request URI.", e);
return null;
}
// Add "Contact" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"Contact\" header...");
try {
SipURI contactURI = addressFactory.createSipURI(mySipUsername, InetAddress.getLocalHost().getHostAddress());
contactURI.setPort(sipPort);
Address contactAddress = addressFactory.createAddress(contactURI);
contactAddress.setDisplayName(mySipUsername);
ContactHeader contactHeader = headerFactory.createContactHeader(contactAddress);
request.setHeader(contactHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when adding ContactHeader.", e);
return null;
}
return request;
}
use of javax.sip.header.FromHeader 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.FromHeader in project XobotOS by xamarin.
the class SipHelper method createRequest.
private Request createRequest(String requestType, SipProfile caller, SipProfile callee, String tag) throws ParseException, SipException {
FromHeader fromHeader = createFromHeader(caller, tag);
ToHeader toHeader = createToHeader(callee);
SipURI requestURI = callee.getUri();
List<ViaHeader> viaHeaders = createViaHeaders();
CallIdHeader callIdHeader = createCallIdHeader();
CSeqHeader cSeqHeader = createCSeqHeader(requestType);
MaxForwardsHeader maxForwards = createMaxForwardsHeader();
Request request = mMessageFactory.createRequest(requestURI, requestType, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
request.addHeader(createContactHeader(caller));
return request;
}
Aggregations