else
{
expires = _presence.getDefaultExpires();
}
SipSession session = subscribe.getSession();
String uri = null;
if (subscribe.isInitial())
{
uri = URIUtil.toCanonical(subscribe.getRequestURI());
}
else
{
uri = (String) session.getAttribute(Constants.SUBSCRIPTION_ATTRIBUTE);
if (uri == null)
{
subscribe.createResponse(SipServletResponse.SC_CALL_LEG_DONE).send();
subscribe.getApplicationSession().invalidate();
return;
}
}
Presentity presentity = _presence.get(uri);
String subscriberUri = null;
if (subscribe.getAddressHeader(Constants.P_ASSERTED_IDENTITY) != null)
subscriberUri = URIUtil.toCanonical(subscribe.getAddressHeader(Constants.P_ASSERTED_IDENTITY).getURI());
else
subscriberUri = URIUtil.toCanonical(subscribe.getFrom().getURI());
try
{
SubHandling subHandling = _policyManager.getPolicy(subscriberUri, presentity);
if (subHandling == SubHandling.BLOCK)
{
_log.debug("Reject presence subscription from {} to {} due to policy", subscriberUri, presentity.getUri());
SipServletResponse response = subscribe.createResponse(SipServletResponse.SC_FORBIDDEN);
response.send();
return;
}
Subscription subscription = null;
if (expires == 0)
{
subscription = presentity.removeSubscription(session.getId());
if (subscription == null)
{
subscription = new Subscription(presentity, session, -1, subscriberUri);
subscription.addListener(_watcherInfo.getSubscriptionListener());
}
else
{
subscription.setExpirationTime(System.currentTimeMillis());
if (_log.isDebugEnabled())
_log.debug("removed presence subscription {} to presentity {}",
subscription.getSession().getId(), presentity.getUri());
}
if (subHandling == SubHandling.CONFIRM)
subscription.setState(Subscription.State.WAITING, Reason.TIMEOUT, subHandling == SubHandling.ALLOW);
else
subscription.setState(Subscription.State.TERMINATED, Reason.TIMEOUT, subHandling == SubHandling.ALLOW);
}
else
{
long now = System.currentTimeMillis();
subscription = presentity.getSubscription(session.getId());
if (subscription == null)
{
subscription = new Subscription(presentity, session, now + expires*1000, subscriberUri);
subscription.addListener(_watcherInfo.getSubscriptionListener());
presentity.addSubscription(subscription);
switch (subHandling)
{
case ALLOW:
subscription.setState(State.ACTIVE, Reason.SUBSCRIBE, true);
break;
case CONFIRM:
subscription.setState(State.PENDING, Reason.SUBSCRIBE, false);
break;
case POLITE_BLOCK:
subscription.setState(State.ACTIVE, Reason.SUBSCRIBE, false);
break;
default:
break;
}
session.setAttribute(Constants.SUBSCRIPTION_ATTRIBUTE, uri);
if (_log.isDebugEnabled())
_log.debug("added presence subscription {} to presentity {}",
subscription.getId(), presentity.getUri());
}