public void registerItems() {
    broadcaster
        .getBroadcasterConfig()
        .addFilter(
            new PerRequestBroadcastFilter() {

              @Override
              public BroadcastAction filter(Object originalMessage, Object message) {
                // TODO Auto-generated method stub
                return new BroadcastAction(ACTION.CONTINUE, message);
              }

              @Override
              public BroadcastAction filter(
                  AtmosphereResource resource, Object originalMessage, Object message) {
                HttpServletRequest request = resource.getRequest();
                return new BroadcastAction(ACTION.CONTINUE, getResponseObject(request));
              }
            });

    broadcaster.getBroadcasterConfig().addFilter(new PollingDelayFilter());
    broadcaster.getBroadcasterConfig().addFilter(new SendPageUpdateFilter());
    broadcaster.getBroadcasterConfig().addFilter(new DuplicateBroadcastProtectionFilter());
    broadcaster.getBroadcasterConfig().addFilter(new ResponseObjectFilter());
    broadcaster.getBroadcasterConfig().addFilter(new MessageTypeFilter());

    stateChangeListener =
        new StateChangeListener() {
          // don't react on update events
          public void stateUpdated(Item item, State state) {
            // if the group has a base item and thus might calculate its state
            // as a DecimalType or other, we also consider it to be necessary to
            // send an update to the client as the label of the item might have changed,
            // even though its state is yet the same.
            if (item instanceof GroupItem) {
              GroupItem gItem = (GroupItem) item;
              if (gItem.getBaseItem() != null) {
                if (!broadcaster.getAtmosphereResources().isEmpty()) {
                  broadcaster.broadcast(item);
                }
              }
            }
          }

          public void stateChanged(final Item item, State oldState, State newState) {
            if (!broadcaster.getAtmosphereResources().isEmpty()) {
              broadcaster.broadcast(item);
            }
          }
        };
    registerStateChangeListenerOnRelevantItems(broadcaster.getID(), stateChangeListener);
  }
Exemplo n.º 2
0
 @GET
 @Path("/{sitemapname: [a-zA-Z_0-9]*}/{pageid: [a-zA-Z_0-9]*}")
 @Produces({MediaType.WILDCARD})
 public SuspendResponse<Response> getPageData(
     @Context HttpHeaders headers,
     @PathParam("sitemapname") String sitemapname,
     @PathParam("pageid") String pageId,
     @QueryParam("type") String type,
     @QueryParam("jsoncallback") @DefaultValue("callback") String callback,
     @HeaderParam(HeaderConfig.X_ATMOSPHERE_TRANSPORT) String atmosphereTransport,
     @Context AtmosphereResource resource) {
   logger.debug(
       "Received HTTP GET request at '{}' for media type '{}'.",
       new String[] {uriInfo.getPath(), type});
   if (atmosphereTransport == null || atmosphereTransport.isEmpty()) {
     String responseType =
         MediaTypeHelper.getResponseMediaType(headers.getAcceptableMediaTypes(), type);
     if (responseType != null) {
       Object responseObject =
           responseType.equals(MediaTypeHelper.APPLICATION_X_JAVASCRIPT)
               ? new JSONWithPadding(
                   getPageBean(sitemapname, pageId, uriInfo.getBaseUriBuilder().build()), callback)
               : getPageBean(sitemapname, pageId, uriInfo.getBaseUriBuilder().build());
       throw new WebApplicationException(Response.ok(responseObject, responseType).build());
     } else {
       throw new WebApplicationException(Response.notAcceptable(null).build());
     }
   }
   GeneralBroadcaster sitemapBroadcaster =
       (GeneralBroadcaster)
           BroadcasterFactory.getDefault()
               .lookup(GeneralBroadcaster.class, resource.getRequest().getPathInfo(), true);
   sitemapBroadcaster.addStateChangeListener(new SitemapStateChangeListener());
   return new SuspendResponse.SuspendResponseBuilder<Response>()
       .scope(SCOPE.REQUEST)
       .resumeOnBroadcast(!ResponseTypeHelper.isStreamingTransport(resource.getRequest()))
       .broadcaster(sitemapBroadcaster)
       .outputComments(true)
       .build();
 }