Search in sources :

Example 11 with AtmosphereResource

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

the class AnnotationScanningTest method create.

@BeforeMethod
public void create() throws Throwable {
    framework = new AtmosphereFramework();
    framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
    framework.addAnnotationPackage(AnnotationScanningTest.class);
    framework.setAsyncSupport(new AsynchronousProcessor(framework.getAtmosphereConfig()) {

        @Override
        public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
            return suspended(req, res);
        }

        public void action(AtmosphereResourceImpl r) {
            try {
                resumed(r.getRequest(), r.getResponse());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }
    }).init().addAtmosphereHandler("/a", new AtmosphereHandlerAdapter() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            resource.suspend();
        }
    });
}
Also used : SimpleBroadcaster(org.atmosphere.util.SimpleBroadcaster) ServletException(jakarta.servlet.ServletException) AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) Action(org.atmosphere.cpr.Action) AtmosphereHandlerAdapter(org.atmosphere.handler.AtmosphereHandlerAdapter) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) AsynchronousProcessor(org.atmosphere.cpr.AsynchronousProcessor) AtmosphereFramework(org.atmosphere.cpr.AtmosphereFramework) IOException(java.io.IOException) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 12 with AtmosphereResource

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

the class DefaultWebSocketProcessor method executeClose.

public void executeClose(WebSocket webSocket, int closeCode) {
    AtmosphereResource r = webSocket.resource();
    boolean isClosedByClient = r == null ? true : r.getAtmosphereResourceEvent().isClosedByClient();
    try {
        if (r != null) {
            asynchronousProcessor.endRequest(AtmosphereResourceImpl.class.cast(r), true);
        }
    } finally {
        if (!isClosedByClient) {
            notifyListener(webSocket, new WebSocketEventListener.WebSocketEvent(closeCode, CLOSE, webSocket));
        }
    }
}
Also used : AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) WebSocketEvent(org.atmosphere.websocket.WebSocketEventListener.WebSocketEvent)

Example 13 with AtmosphereResource

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

the class DefaultWebSocketProcessor method notifyListener.

@Override
public void notifyListener(WebSocket webSocket, WebSocketEventListener.WebSocketEvent event) {
    AtmosphereResource resource = webSocket.resource();
    if (resource == null)
        return;
    AtmosphereResourceImpl r = AtmosphereResourceImpl.class.cast(resource);
    for (AtmosphereResourceEventListener l : r.atmosphereResourceEventListener()) {
        if (WebSocketEventListener.class.isAssignableFrom(l.getClass())) {
            try {
                switch(event.type()) {
                    case CONNECT:
                        WebSocketEventListener.class.cast(l).onConnect(event);
                        break;
                    case DISCONNECT:
                        WebSocketEventListener.class.cast(l).onDisconnect(event);
                        onDisconnect(event, l);
                        break;
                    case CONTROL:
                        WebSocketEventListener.class.cast(l).onControl(event);
                        break;
                    case MESSAGE:
                        WebSocketEventListener.class.cast(l).onMessage(event);
                        break;
                    case HANDSHAKE:
                        WebSocketEventListener.class.cast(l).onHandshake(event);
                        break;
                    case CLOSE:
                        boolean isClosedByClient = r.getAtmosphereResourceEvent().isClosedByClient();
                        l.onDisconnect(new AtmosphereResourceEventImpl(r, !isClosedByClient, false, isClosedByClient, null));
                        onDisconnect(event, l);
                        WebSocketEventListener.class.cast(l).onClose(event);
                        break;
                }
            } catch (Throwable t) {
                logger.debug("Listener error {}", t);
                try {
                    WebSocketEventListener.class.cast(l).onThrowable(new AtmosphereResourceEventImpl(r, false, false, t));
                } catch (Throwable t2) {
                    logger.warn("Listener error {}", t2);
                }
            }
        } else {
            switch(event.type()) {
                case CLOSE:
                    boolean isClosedByClient = r.getAtmosphereResourceEvent().isClosedByClient();
                    l.onDisconnect(new AtmosphereResourceEventImpl(r, !isClosedByClient, false, isClosedByClient, null));
                    break;
            }
        }
    }
}
Also used : AtmosphereResourceEventListener(org.atmosphere.cpr.AtmosphereResourceEventListener) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) AtmosphereResourceEventImpl(org.atmosphere.cpr.AtmosphereResourceEventImpl)

Example 14 with AtmosphereResource

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

the class DefaultWebSocketProcessor method close.

@Override
public void close(final WebSocket webSocket, int closeCode) {
    WebSocketHandler webSocketHandler = webSocket.webSocketHandler();
    // A message might be in the process of being processed and the websocket gets closed. In that corner
    // case the webSocket.resource will be set to false and that might cause NPE in some WebSocketProcol implementation
    // We could potentially synchronize on webSocket but since it is a rare case, it is better to not synchronize.
    // synchronized (webSocket) {
    final AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
    if (resource == null) {
        logger.trace("Already closed {}", webSocket);
    } else {
        final boolean allowedToClose = allowedCloseCode(closeCode);
        final AtmosphereRequest r = resource.getRequest(false);
        final AtmosphereResponse s = resource.getResponse(false);
        boolean ff = r.getAttribute("firefox") != null;
        boolean completeLifecycle = true;
        try {
            webSocketProtocol.onClose(webSocket);
            if (webSocketHandler != null) {
                webSocketHandler.onClose(webSocket);
            }
            logger.trace("About to close AtmosphereResource for {} with code {}", resource, closeCode);
            if (!resource.getAtmosphereResourceEvent().isClosedByClient() && !resource.getAtmosphereResourceEvent().isClosedByApplication() && !resource.isCancelled()) {
                // Better to call onDisconnect that onResume.
                if (allowedToClose) {
                    if (ff || closingTime > 0) {
                        completeLifecycle = false;
                        logger.debug("Delaying closing operation for firefox and resource {}", resource.uuid());
                        ExecutorsFactory.getScheduler(framework.getAtmosphereConfig()).schedule(new Callable<Object>() {

                            @Override
                            public Object call() throws Exception {
                                AtmosphereResource currentResource = framework.atmosphereFactory().find(resource.uuid());
                                if (currentResource != null && currentResource.isSuspended()) {
                                    // Do not close if the resource has reconnected already
                                    executeClose(webSocket, 1005);
                                    finish(webSocket, currentResource, r, s, !allowedToClose);
                                }
                                return null;
                            }
                        }, ff ? (closingTime == 0 ? 1000 : closingTime) : closingTime, TimeUnit.MILLISECONDS);
                        resource.getAndSetPendingClose();
                    } else {
                        executeClose(webSocket, closeCode);
                    }
                } else {
                    logger.debug("Timeout {}", resource.uuid());
                    asynchronousProcessor.endRequest(AtmosphereResourceImpl.class.cast(webSocket.resource()), false);
                }
            } else {
                logger.trace("Unable to properly complete {}", resource == null ? "null" : resource.uuid());
                completeLifecycle = false;
            }
        } finally {
            if (completeLifecycle) {
                finish(webSocket, resource, r, s, !allowedToClose);
            }
        }
    }
}
Also used : AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) ServletException(jakarta.servlet.ServletException) AtmosphereMappingException(org.atmosphere.cpr.AtmosphereMappingException) IOException(java.io.IOException)

Example 15 with AtmosphereResource

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

the class LifecycleHandler method offIfEmpty.

public LifecycleHandler offIfEmpty(DefaultBroadcaster broadcaster) {
    BroadcasterConfig bc = broadcaster.getBroadcasterConfig();
    Collection<AtmosphereResource> resources = broadcaster.getAtmosphereResources();
    final BroadcasterLifeCyclePolicy lifeCyclePolicy = broadcaster.getBroadcasterLifeCyclePolicy();
    if (resources.isEmpty()) {
        notifyEmptyListener(broadcaster);
        if (broadcaster.getScope() != Broadcaster.SCOPE.REQUEST && lifeCyclePolicy.getLifeCyclePolicy() == EMPTY) {
            broadcaster.releaseExternalResources();
        } else if (broadcaster.getScope() == Broadcaster.SCOPE.REQUEST || lifeCyclePolicy.getLifeCyclePolicy() == EMPTY_DESTROY) {
            bc.getAtmosphereConfig().getBroadcasterFactory().remove(broadcaster, broadcaster.getID());
            broadcaster.destroy();
        }
    }
    return this;
}
Also used : BroadcasterLifeCyclePolicy(org.atmosphere.cpr.BroadcasterLifeCyclePolicy) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) BroadcasterConfig(org.atmosphere.cpr.BroadcasterConfig)

Aggregations

AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)37 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)14 IOException (java.io.IOException)10 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)9 Broadcaster (org.atmosphere.cpr.Broadcaster)8 Test (org.junit.Test)7 UI (com.vaadin.flow.component.UI)5 SessionExpiredException (com.vaadin.flow.server.SessionExpiredException)4 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)4 VaadinSession (com.vaadin.flow.server.VaadinSession)4 InvalidUIDLSecurityKeyException (com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException)4 JsonException (elemental.json.JsonException)4 AsynchronousProcessor (org.atmosphere.cpr.AsynchronousProcessor)4 AtmosphereResourceEvent (org.atmosphere.cpr.AtmosphereResourceEvent)4 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)3 ServletException (jakarta.servlet.ServletException)3 HashSet (java.util.HashSet)3 Future (java.util.concurrent.Future)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 AtmosphereResourceEventImpl (org.atmosphere.cpr.AtmosphereResourceEventImpl)3