View Javadoc

    1   package org.mortbay.cometd.ext;
    2
    3   import java.util.Map;
    4
    5   import org.cometd.Bayeux;
    6   import org.cometd.Client;
    7   import org.cometd.Extension;
    8   import org.cometd.Message;
    9   import org.mortbay.cometd.ClientImpl;
    10  import org.mortbay.log.Log;
    11  import org.mortbay.util.ajax.JSON;
    12
    13  /**
    14   * Acknowledged Messages extension.
    15   *
    16   * Installing this extension in a bayeux server allows it to handle the ack
    17   * extension if a client also supports it.
    18   *
    19   * The main role of this extension is to install the
    20   * {@link AcknowledgedMessagesClientExtension} on the {@link Client} instances
    21   * created during handshake for clients that also support the ack extension.
    22   *
    23   */
    24  public class AcknowledgedMessagesExtension implements Extension
    25  {
    26      JSON.Literal _replyExt=new JSON.Literal("{\"ack\":true}");
    27
    28      public Message rcv(Client from, Message message)
    29      {
    30          return message;
    31      }
    32
    33      public Message rcvMeta(Client from, Message message)
    34      {
    35          return message;
    36      }
    37
    38      public Message send(Client from, Message message)
    39      {
    40          return message;
    41      }
    42
    43      public Message sendMeta(Client from, Message message)
    44      {
    45          if (message.getChannel() == null)
    46              return message;
    47
    48          if (message.getChannel().equals(Bayeux.META_HANDSHAKE) && Boolean.TRUE.equals(message.get(Bayeux.SUCCESSFUL_FIELD)))
    49          {
    50              Message rcv=message.getAssociated();
    51
    52              Map<String,Object> ext=rcv.getExt(false);
    53              boolean clientRequestedAcks=ext != null && ext.get("ack") == Boolean.TRUE;
    54
    55              if (clientRequestedAcks && from != null)
    56              {
    57                  Log.info("Enabled message acknowledgement for client " + from);
    58                  from.addExtension(new AcknowledgedMessagesClientExtension(from));
    59                  ((ClientImpl)from).setMetaConnectDeliveryOnly(true);
    60              }
    61
    62              message.put(Bayeux.EXT_FIELD,_replyExt);
    63          }
    64
    65          return message;
    66      }
    67
    68  }