Package org.cipango.kaleo.presence

Examples of org.cipango.kaleo.presence.Presentity


    _policyManager = (PolicyManager) getServletContext().getAttribute(PolicyManager.class.getName());
  }
 
  protected void doPublish(SipServletRequest publish) throws ServletException, IOException
  {
    Presentity presentity = null;
    try
    {
      String event = publish.getHeader(Constants.EVENT);
 
      if (event == null || !event.equals(_presence.getName()))
      {
        SipServletResponse response = publish.createResponse(SipServletResponse.SC_BAD_EVENT);
        response.addHeader(Constants.ALLOW_EVENTS, _presence.getName());
        response.send();
        return;
      }
 
      String uri = URIUtil.toCanonical(publish.getRequestURI());
 
      int expires = publish.getExpires();
      if (expires != -1)
      {
        if(expires != 0)
        {
          if (expires < _presence.getMinStateExpires())
          {
            SipServletResponse response = publish.createResponse(SipServletResponse.SC_INTERVAL_TOO_BRIEF);
            response.addHeader(Constants.MIN_EXPIRES, Integer.toString(_presence.getMinStateExpires()));
            response.send();
            return;
          }
          else if (expires > _presence.getMaxStateExpires())
            expires = _presence.getMaxStateExpires();
        }
      }
      else
      {
        expires = _presence.getDefaultStateExpires();
      }
 
      String contentType = publish.getContentType();
 
      List<String> supported = _presence.getSupportedContentTypes();
      if (contentType != null && !(supported.contains(contentType)))
      {
        SipServletResponse response = publish.createResponse(SipServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        for (String s : supported)
          response.addHeader(Constants.ACCEPT, s);
        response.send();
        return;
      }
 
      byte[] raw = publish.getRawContent();
      Object content = null;
 
      if (raw != null)
      {
        if (contentType == null)
        {
          SipServletResponse response = publish.createResponse(SipServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
          for (String s : supported)
            response.addHeader(Constants.ACCEPT, s);
          response.send();
          return;
        }
        ContentHandler<?> handler = _presence.getContentHandler(contentType);   
        try
        {
          content = handler.getContent(raw);
        }
        catch (Exception e)
        {
          if (_log.isDebugEnabled())
            _log.debug("invalid content {}", e);
         
          publish.createResponse(SipServletResponse.SC_BAD_REQUEST).send();
          return;
        }
      }
      String etag = publish.getHeader(Constants.SIP_IF_MATCH);
      long now = System.currentTimeMillis();
 
      presentity = _presence.get(uri);
   

      if (etag == null)
      {
        if (content == null)
        {
          publish.createResponse(SipServletResponse.SC_BAD_REQUEST).send();
          return;
        }
        SoftState state = presentity.addState(contentType, content, now + expires*1000);
       
        if (_log.isDebugEnabled())
          _log.debug("added state {} to presentity {}", state.getETag(), presentity);
       
        SipServletResponse response = publish.createResponse(SipServletResponse.SC_OK);
        response.setExpires(expires);
        response.setHeader(Constants.SIP_ETAG, state.getETag());
        response.send();
      }
      else
      {
        SoftState state = presentity.getState(etag);
        if (state == null)
        {
          publish.createResponse(SipServletResponse.SC_CONDITIONAL_REQUEST_FAILED).send();
          return;
        }
        if (expires == 0)
        {
          if (_log.isDebugEnabled())
            _log.debug("removed state {} from presentity {}", state.getETag(), presentity);
         
          presentity.removeState(etag);
        }
        else
        {
          if (content != null)
          {
            presentity.modifyState(state, contentType, content, now + expires*1000);
           
            if (_log.isDebugEnabled())
              _log.debug("modified state {} (new etag {}) from presentity {}",
                  new Object[] {etag, state.getETag(), presentity});
          }
          else
          {
            presentity.refreshState(state, now + expires*1000);
            if (_log.isDebugEnabled())
              _log.debug("refreshed state {} (new etag {}) from presentity {}",
                  new Object[] {etag, state.getETag(), presentity});
          }
        }
View Full Code Here


        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());
        }
        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;
View Full Code Here

  }
 
 
  protected WatcherResource newResource(String uri)
  {
    Presentity presentity = _presenceEventPackage.get(uri);
    try
    {
      WatcherResource watcherResource = new WatcherResource(uri, presentity);
      watcherResource.addListener(getEventNotifier());
      return watcherResource;
View Full Code Here

  }
 
  public void testGetPolicyNicolas() throws Exception
  {
    setContent(PRES_RULES_1);
    Presentity presentity = new Presentity("sip:nicolas@cipango.org");

    assertEquals(SubHandling.ALLOW,
        _policyManager.getPolicy("sip:user@example.com", presentity));
   
    assertEquals(SubHandling.BLOCK,
        _policyManager.getPolicy("sip:user@example.com", new Presentity("sip:unknown@cipango.org")));
   
    assertEquals(SubHandling.BLOCK,
        _policyManager.getPolicy("sip:unknown@example.com", presentity));
  }
View Full Code Here

 
  public void testGetPolicyAlice() throws Exception
  {
    setContent("/org.openmobilealliance.pres-rules/users/sip:alice@cipango.org/pres-rules");
    setContent("/resource-lists/users/sip:alice@cipango.org/index");
    Presentity presentity = new Presentity("sip:alice@cipango.org");
    XcapPolicy policy = (XcapPolicy) _policyManager.getPolicy(presentity);
    assertEquals(SubHandling.CONFIRM,
        _policyManager.getPolicy("sip:unknown@cipango.org", presentity));
   
    assertEquals(2, policy.getXcapResources().size());
View Full Code Here

 
 
  public void testGetPolicyIetf() throws Exception
  {
    setContent("/org.openmobilealliance.pres-rules/users/sip:ietf@cipango.org/pres-rules");
    Presentity presentity = new Presentity("sip:ietf@cipango.org");
   
    // Match rule a
    assertEquals(SubHandling.POLITE_BLOCK,
        _policyManager.getPolicy("sip:polite-block@example.com", presentity));
    // Match rule b
View Full Code Here

 
  public void testGetPolicyOma() throws Exception
  {
    setContent("/org.openmobilealliance.pres-rules/users/sip:oma@cipango.org/pres-rules");
    setContent("/resource-lists/users/sip:oma@cipango.org/index");
    Presentity presentity = new Presentity("sip:oma@cipango.org");

    // Granted by resource list
    assertEquals(SubHandling.ALLOW,
        _policyManager.getPolicy("sip:carol@cipango.org", presentity));
    // Own URI
View Full Code Here

   * @throws Exception
   */
  public void testPolicyListener() throws Exception
  {
    setContent(PRES_RULES_1);
    Presentity presentity = new Presentity("sip:nicolas@cipango.org");
    final List<Long> policyUpdated = new ArrayList<Long>();
    PolicyListener l = new PolicyListener()
    {
      public void policyHasChanged(Policy policy)
      {
View Full Code Here

  private static final String AOR = "sip:alice@cipango.org";
  private static final String SUBSCRIBER_AOR = "sip:bob@cipango.org";

  public void testGetState() throws Exception
  {
    Presentity presentity = new Presentity(AOR);
    WatcherResource resource = new WatcherResource(AOR, presentity);
    EventListener eventListener = new EventListener();
    resource.addListener(eventListener);
    //System.out.println(resource.getState().getContent());
    Watcherinfo watcherinfo = ((WatcherinfoDocument) resource.getState().getContent()).getWatcherinfo();
View Full Code Here

TOP

Related Classes of org.cipango.kaleo.presence.Presentity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.