Search in sources :

Example 16 with AtmosphereResource

use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.

the class ExcludeSessionBroadcaster method broadcast.

/**
     * a list of sessions will be exclude for this broadcast
     *
     * @param msg
     * @param sessions
     * @return
     */
public Future<Object> broadcast(Object msg, List<HttpSession> sessions) {
    if (destroyed.get()) {
        return futureDone(msg);
    }
    Set<AtmosphereResource> subset = new HashSet<AtmosphereResource>();
    subset.addAll(resources);
    for (AtmosphereResource r : resources) {
        if (!r.getAtmosphereResourceEvent().isCancelled() && sessions.contains(r.getRequest().getSession())) {
            subset.remove(r);
        }
    }
    start();
    Object newMsg = filter(msg);
    if (newMsg == null) {
        return futureDone(msg);
    }
    BroadcasterFuture<Object> f = new BroadcasterFuture<Object>(newMsg, subset.size());
    dispatchMessages(new Deliver(newMsg, subset, f, msg));
    return f;
}
Also used : Deliver(org.atmosphere.runtime.Deliver) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) BroadcasterFuture(org.atmosphere.runtime.BroadcasterFuture) HashSet(java.util.HashSet)

Example 17 with AtmosphereResource

use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.

the class OnDisconnectInterceptor method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    if (Utils.webSocketMessage(r))
        return Action.CONTINUE;
    AtmosphereRequest request = AtmosphereResourceImpl.class.cast(r).getRequest(false);
    String uuid = r.uuid();
    if (closeMessage(request)) {
        AtmosphereResource ss = config.resourcesFactory().find(uuid);
        if (ss == null) {
            logger.debug("No Suspended Connection found for {}. Using the AtmosphereResource associated with the close message", uuid);
            ss = r;
        }
        if (ss == null) {
            logger.debug("Was unable to execute onDisconnect on {}", r.uuid());
            return Action.CONTINUE;
        }
        logger.debug("AtmosphereResource {} disconnected", uuid);
        // Block websocket closing detection
        AtmosphereResourceEventImpl.class.cast(ss.getAtmosphereResourceEvent()).isClosedByClient(true);
        p.completeLifecycle(ss, false);
        return Action.CANCELLED;
    }
    return Action.CONTINUE;
}
Also used : AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) AtmosphereResourceEventImpl(org.atmosphere.runtime.AtmosphereResourceEventImpl)

Example 18 with AtmosphereResource

use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.

the class ManagedAtmosphereHandler method onRequest.

@Override
public void onRequest(final AtmosphereResource resource) throws IOException {
    AtmosphereRequest request = resource.getRequest();
    String method = request.getMethod();
    boolean polling = Utils.pollableTransport(resource.transport());
    boolean webSocketMessage = Utils.webSocketMessage(resource);
    if (!webSocketMessage && !polling) {
        if (onReadyMethod != null) {
            resource.addEventListener(new OnSuspend() {

                @Override
                public void onSuspend(AtmosphereResourceEvent event) {
                    processReady(event.getResource());
                    resource.removeEventListener(this);
                }
            });
        }
        if (onResumeMethod != null) {
            resource.addEventListener(new OnResume() {

                @Override
                public void onResume(AtmosphereResourceEvent event) {
                    invoke(onResumeMethod, event);
                    resource.removeEventListener(this);
                }
            });
        }
        resource.addEventListener(new OnClose() {

            @Override
            public void onClose(AtmosphereResourceEvent event) {
                invoke(onDisconnectMethod, event);
            }
        });
    }
    if (method.equalsIgnoreCase("get")) {
        invoke(onGetMethod, resource);
    } else if (method.equalsIgnoreCase("post")) {
        Object body = null;
        if (onPostMethod != null) {
            body = readEntirely(resource);
            if (body != null && String.class.isAssignableFrom(body.getClass())) {
                resource.getRequest().body((String) body);
            } else if (body != null) {
                resource.getRequest().body((byte[]) body);
            }
            invoke(onPostMethod, resource);
        }
        MethodInfo.EncoderObject e = message(resource, body);
        if (e != null && e.encodedObject != null) {
            AtmosphereResource r = resource;
            if (e.methodInfo.deliverTo == DeliverTo.DELIVER_TO.RESOURCE && !resource.transport().equals(AtmosphereResource.TRANSPORT.WEBSOCKET)) {
                r = resourcesFactory.find(resource.uuid());
            }
            IOUtils.deliver(new Managed(e.encodedObject), null, e.methodInfo.deliverTo, r);
        }
    } else if (method.equalsIgnoreCase("delete")) {
        invoke(onDeleteMethod, resource);
    } else if (method.equalsIgnoreCase("put")) {
        invoke(onPutMethod, resource);
    }
}
Also used : OnResume(org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter.OnResume) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) OnSuspend(org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter.OnSuspend) AtmosphereResourceEvent(org.atmosphere.runtime.AtmosphereResourceEvent) OnClose(org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter.OnClose)

Example 19 with AtmosphereResource

use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.

the class AbstractReflectorAtmosphereHandler method onStateChange.

/**
     * Write the {@link AtmosphereResourceEvent#getMessage()} back to the client using
     * the {@link AtmosphereResponseImpl#getOutputStream()} or {@link AtmosphereResponseImpl#getWriter()}.
     * If a {@link org.atmosphere.runtime.Serializer} is defined, it will be invoked and the write operation
     * will be delegated to it.
     * <p/>
     * By default, this method will try to use {@link AtmosphereResponseImpl#getWriter()}.
     *
     * @param event the {@link AtmosphereResourceEvent#getMessage()}
     * @throws java.io.IOException
     */
@Override
public void onStateChange(AtmosphereResourceEvent event) throws IOException {
    Object message = event.getMessage();
    AtmosphereResource resource = event.getResource();
    AtmosphereResponse r = resource.getResponse();
    AtmosphereRequest request = resource.getRequest();
    boolean writeAsBytes = IOUtils.isBodyBinary(request);
    if (message == null) {
        logger.trace("Message was null for AtmosphereEvent {}", event);
        return;
    }
    if (resource.getSerializer() != null) {
        try {
            if (message instanceof List) {
                for (Object s : (List<Object>) message) {
                    resource.getSerializer().write(resource.getResponse().getOutputStream(), s);
                }
            } else {
                resource.getSerializer().write(resource.getResponse().getOutputStream(), message);
            }
        } catch (Throwable ex) {
            logger.warn("Serializer exception: message: {}", message, ex);
            throw new IOException(ex);
        }
    } else {
        boolean isUsingStream = true;
        Object o = resource.getRequest().getAttribute(PROPERTY_USE_STREAM);
        if (o != null) {
            isUsingStream = (Boolean) o;
        }
        if (!isUsingStream) {
            try {
                r.getWriter();
            } catch (IllegalStateException e) {
                isUsingStream = true;
            }
            if (writeAsBytes) {
                throw new IllegalStateException("Cannot write bytes using PrintWriter");
            }
        }
        if (message instanceof List) {
            Iterator<Object> i = ((List) message).iterator();
            try {
                Object s;
                while (i.hasNext()) {
                    s = i.next();
                    if (String.class.isAssignableFrom(s.getClass())) {
                        if (isUsingStream) {
                            r.getOutputStream().write(s.toString().getBytes(r.getCharacterEncoding()));
                        } else {
                            r.getWriter().write(s.toString());
                        }
                    } else if (byte[].class.isAssignableFrom(s.getClass())) {
                        if (isUsingStream) {
                            r.getOutputStream().write((byte[]) s);
                        } else {
                            r.getWriter().write(s.toString());
                        }
                    } else {
                        if (isUsingStream) {
                            r.getOutputStream().write(s.toString().getBytes(r.getCharacterEncoding()));
                        } else {
                            r.getWriter().write(s.toString());
                        }
                    }
                    i.remove();
                }
            } catch (IOException ex) {
                event.setMessage(new ArrayList<String>().addAll((List) message));
                throw ex;
            }
            if (isUsingStream) {
                r.getOutputStream().flush();
            } else {
                r.getWriter().flush();
            }
        } else {
            if (isUsingStream) {
                r.getOutputStream().write(writeAsBytes ? (byte[]) message : message.toString().getBytes(r.getCharacterEncoding()));
                r.getOutputStream().flush();
            } else {
                r.getWriter().write(message.toString());
                r.getWriter().flush();
            }
        }
    }
    postStateChange(event);
}
Also used : AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Aggregations

AtmosphereResource (org.atmosphere.runtime.AtmosphereResource)19 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)8 AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)7 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 AsynchronousProcessor (org.atmosphere.runtime.AsynchronousProcessor)3 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)3 AtmosphereResourceEventImpl (org.atmosphere.runtime.AtmosphereResourceEventImpl)3 BroadcasterFuture (org.atmosphere.runtime.BroadcasterFuture)3 Deliver (org.atmosphere.runtime.Deliver)3 ArrayList (java.util.ArrayList)2 Future (java.util.concurrent.Future)2 ServletException (javax.servlet.ServletException)2 Action (org.atmosphere.runtime.Action)2 AtmosphereRequestImpl (org.atmosphere.runtime.AtmosphereRequestImpl)2 AtmosphereResponse (org.atmosphere.runtime.AtmosphereResponse)2 BroadcasterConfig (org.atmosphere.runtime.BroadcasterConfig)2 BroadcasterLifeCyclePolicy (org.atmosphere.runtime.BroadcasterLifeCyclePolicy)2 WebSocketEvent (org.atmosphere.websocket.WebSocketEventListener.WebSocketEvent)2 LinkedList (java.util.LinkedList)1