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());
}
else
{
subscription.setExpirationTime(now + expires * 1000);
if (_log.isDebugEnabled())
_log.debug("refreshed presence subscription {} to presentity {}",
subscription.getSession().getId(), presentity.getUri());
}
}
int code = (subscription.getState() != Subscription.State.PENDING) ?
SipServletResponse.SC_OK : SipServletResponse.SC_ACCEPTED;
SipServletResponse response = subscribe.createResponse(code);
response.setExpires(expires);
response.send();