Ejemplo n.º 1
0
    @Override
    public void onRequest(AtmosphereResource resource) throws IOException {

      AtmosphereResourceSession resourceSession =
          AtmosphereResourceSessionFactory.getDefault().getSession(resource);

      AtmosphereChannel resourceChannel =
          resourceSession.getAttribute(WAVE_CHANNEL_ATTRIBUTE, AtmosphereChannel.class);

      if (resourceChannel == null) {

        ParticipantId loggedInUser =
            provider.sessionManager.getLoggedInUser(resource.getRequest().getSession(false));

        AtmosphereConnection connection = new AtmosphereConnection(loggedInUser, provider);
        resourceChannel = connection.getAtmosphereChannel();
        resourceSession.setAttribute(WAVE_CHANNEL_ATTRIBUTE, resourceChannel);
        resourceChannel.onConnect(resource);
      }

      resource.setBroadcaster(resourceChannel.getBroadcaster()); // on every
      // request

      if (resource.getRequest().getMethod().equalsIgnoreCase("GET")) {

        resource.suspend();
      }

      if (resource.getRequest().getMethod().equalsIgnoreCase("POST")) {

        StringBuilder b = IOUtils.readEntirely(resource);
        resourceChannel.onMessage(b.toString());
      }
    }
  /** {@inheritDoc} */
  @Override
  public void configure(final AtmosphereConfig config) {
    framework = config.framework();
    arsf = AtmosphereResourceSessionFactory.getDefault();
    setStompFormat(PropertyClass.STOMP_FORMAT_CLASS.retrieve(StompFormat.class, config));

    try {
      // TODO: user must map AtmosphereServlet to /stomp in web.xml, can we offer a chance to set a
      // custom location ?
      framework.addAtmosphereHandler(
          "/stomp",
          framework.newClassInstance(
              AtmosphereHandler.class, AbstractReflectorAtmosphereHandler.Default.class));

      interceptors =
          new ConcurrentHashMap<org.atmosphere.stomp.protocol.Action, StompInterceptor>();
      configureInterceptor(
          config,
          ConnectInterceptor.class,
          org.atmosphere.stomp.protocol.Action.CONNECT,
          org.atmosphere.stomp.protocol.Action.STOMP,
          org.atmosphere.stomp.protocol.Action.NULL);
      configureInterceptor(
          config, SubscribeInterceptor.class, org.atmosphere.stomp.protocol.Action.SUBSCRIBE);
      configureInterceptor(
          config, UnsubscribeInterceptor.class, org.atmosphere.stomp.protocol.Action.UNSUBSCRIBE);
      configureInterceptor(
          config, SendInterceptor.class, org.atmosphere.stomp.protocol.Action.SEND);

      final BroadcastFilterLifecycle filter =
          framework.newClassInstance(BroadcastFilterLifecycle.class, StompBroadcastFilter.class);
      framework.broadcasterFilters(filter);
      filter.init(config);
    } catch (InstantiationException e) {
      logger.error("", e);
    } catch (IllegalAccessException e) {
      logger.error("", e);
    }
  }
Ejemplo n.º 3
0
    @Override
    public void onStateChange(AtmosphereResourceEvent event) throws IOException {

      AtmosphereResponse response = event.getResource().getResponse();
      AtmosphereResource resource = event.getResource();

      if (event.isSuspended()) {

        // Set content type before do response.getWriter()
        // http://docs.oracle.com/javaee/5/api/javax/servlet/ServletResponse.html#setContentType(java.lang.String)
        response.setContentType("text/plain; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        if (event.getMessage().getClass().isArray()) {

          LOG.fine("SEND MESSAGE ARRAY " + event.getMessage().toString());

          List<Object> list = Arrays.asList(event.getMessage());

          response.getOutputStream().write(MSG_SEPARATOR.getBytes(MSG_CHARSET));
          for (Object object : list) {
            String message = (String) object;
            message += MSG_SEPARATOR;
            response.getOutputStream().write(message.getBytes(MSG_CHARSET));
          }

        } else if (event.getMessage() instanceof List) {

          LOG.fine("SEND MESSAGE LIST " + event.getMessage().toString());

          @SuppressWarnings("unchecked")
          List<Object> list = List.class.cast(event.getMessage());

          response.getOutputStream().write(MSG_SEPARATOR.getBytes(MSG_CHARSET));
          for (Object object : list) {
            String message = (String) object;
            message += MSG_SEPARATOR;
            response.getOutputStream().write(message.getBytes(MSG_CHARSET));
          }

        } else if (event.getMessage() instanceof String) {

          LOG.fine("SEND MESSAGE " + event.getMessage().toString());

          String message = (String) event.getMessage();
          response.getOutputStream().write(message.getBytes(MSG_CHARSET));
        }

        try {

          response.flushBuffer();

          switch (resource.transport()) {
            case JSONP:
            case LONG_POLLING:
              event.getResource().resume();
              break;
            case WEBSOCKET:
            case STREAMING:
            case SSE:
              response.getOutputStream().flush();
              break;
            default:
              LOG.info("Unknown transport");
              break;
          }
        } catch (IOException e) {
          LOG.info("Error resuming resource response", e);
        }

      } else if (event.isResuming()) {

        LOG.fine("RESUMING");

      } else if (event.isResumedOnTimeout()) {

        LOG.fine("RESUMED ON TIMEOUT");

      } else if (event.isClosedByApplication() || event.isClosedByClient()) {

        LOG.fine("CONNECTION CLOSED");

        AtmosphereResourceSession resourceSession =
            AtmosphereResourceSessionFactory.getDefault().getSession(resource);

        AtmosphereChannel resourceChannel =
            resourceSession.getAttribute(WAVE_CHANNEL_ATTRIBUTE, AtmosphereChannel.class);

        if (resourceChannel != null) {
          resourceChannel.onDisconnect();
        }
      }
    }