Search in sources :

Example 1 with AtmosphereResponse

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

the class SimpleRestInterceptor method attachWriter.

private void attachWriter(final AtmosphereResource r) {
    AtmosphereResponse res = r.getResponse();
    AsyncIOWriter writer = res.getAsyncIOWriter();
    if (writer instanceof AtmosphereInterceptorWriter) {
        ((AtmosphereInterceptorWriter) writer).interceptor(interceptor, 0);
    }
}
Also used : AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) AsyncIOWriter(org.atmosphere.runtime.AsyncIOWriter) AtmosphereInterceptorWriter(org.atmosphere.runtime.AtmosphereInterceptorWriter)

Example 2 with AtmosphereResponse

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

the class OnMessage method onStateChange.

@Override
public final void onStateChange(AtmosphereResourceEvent event) throws IOException {
    AtmosphereResponse response = ((AtmosphereResourceImpl) event.getResource()).getResponse(false);
    logger.trace("{} with event {}", event.getResource().uuid(), event);
    if (event.isCancelled() || event.isClosedByApplication() || event.isClosedByClient()) {
        onDisconnect(response);
    } else if (event.getMessage() != null && List.class.isAssignableFrom(event.getMessage().getClass())) {
        List<T> messages = List.class.cast(event.getMessage());
        for (T t : messages) {
            onMessage(response, t);
        }
    } else if (event.isResuming()) {
        onResume(response);
    } else if (event.isResumedOnTimeout()) {
        onTimeout(response);
    } else if (event.isSuspended()) {
        onMessage(response, (T) event.getMessage());
    }
    postStateChange(event);
}
Also used : AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) List(java.util.List) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Example 3 with AtmosphereResponse

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

the class CacheHeadersInterceptor method inspect.

@Override
public Action inspect(AtmosphereResource r) {
    if (Utils.webSocketMessage(r))
        return Action.CONTINUE;
    final AtmosphereResponse response = r.getResponse();
    final AtmosphereRequest request = r.getRequest();
    // For extension that aren't supporting this interceptor (like Jersey)
    request.setAttribute(ApplicationConfig.NO_CACHE_HEADERS, injectCacheHeaders);
    if (writeHeaders && injectCacheHeaders) {
        // Set to expire far in the past.
        response.setHeader(EXPIRES, "-1");
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader(CACHE_CONTROL, "no-store, no-cache, must-revalidate");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader(PRAGMA, "no-cache");
    }
    return Action.CONTINUE;
}
Also used : AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest)

Example 4 with AtmosphereResponse

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

the class HeartbeatInterceptor method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    final AtmosphereResourceImpl impl = AtmosphereResourceImpl.class.cast(r);
    final AtmosphereRequest request = impl.getRequest(false);
    final AtmosphereResponse response = impl.getResponse(false);
    // Check heartbeat
    if (clientHeartbeatFrequencyInSeconds > 0) {
        byte[] body = new byte[0];
        try {
            if (!request.getMethod().equalsIgnoreCase("GET")) {
                body = IOUtils.readEntirelyAsByte(r);
            }
        } catch (IOException e) {
            logger.warn("", e);
            cancelF(request);
            return Action.CONTINUE;
        }
        if (Arrays.equals(paddingBytes, body)) {
            // Dispatch an event to notify that a heartbeat has been intercepted
            // TODO: see https://github.com/Atmosphere/atmosphere/issues/1561
            final AtmosphereResourceEvent event = new HeartbeatAtmosphereResourceEvent(AtmosphereResourceImpl.class.cast(r));
            if (AtmosphereResourceHeartbeatEventListener.class.isAssignableFrom(r.getAtmosphereHandler().getClass())) {
                r.addEventListener(new AtmosphereResourceEventListenerAdapter.OnHeartbeat() {

                    @Override
                    public void onHeartbeat(AtmosphereResourceEvent event) {
                        AtmosphereResourceHeartbeatEventListener.class.cast(r.getAtmosphereHandler()).onHeartbeat(event);
                    }
                });
            }
            // Fire event
            r.notifyListeners(event);
            return Action.CANCELLED;
        }
        request.body(body);
    }
    if (Utils.webSocketMessage(r))
        return Action.CONTINUE;
    final int interval = extractHeartbeatInterval(impl);
    if (interval != 0) {
        if (!(Utils.pollableTransport(r.transport()) || r.transport() == AtmosphereResource.TRANSPORT.UNDEFINED)) {
            super.inspect(r);
            final boolean wasSuspended = r.isSuspended();
            // Otherwise, the listener will do the job
            if (wasSuspended) {
                clock(interval, r, request, response);
            }
            r.addEventListener(new Clock() {

                @Override
                public void onSuspend(AtmosphereResourceEvent event) {
                    // We did not clocked when this listener was added because connection was not already suspended
                    if (!wasSuspended) {
                        clock(interval, r, request, response);
                    }
                }

                @Override
                public void onResume(AtmosphereResourceEvent event) {
                    cancelF(request);
                }

                @Override
                public void onDisconnect(AtmosphereResourceEvent event) {
                    cancelF(request);
                }

                @Override
                public void onClose(AtmosphereResourceEvent event) {
                    cancelF(request);
                }
            });
        } else {
            return Action.CONTINUE;
        }
        final AsyncIOWriter writer = response.getAsyncIOWriter();
        if (!Utils.resumableTransport(r.transport()) && AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass()) && request.getAttribute(INTERCEPTOR_ADDED) == null) {
            AtmosphereInterceptorWriter.class.cast(writer).interceptor(new AsyncIOInterceptorAdapter() {

                @Override
                public byte[] transformPayload(AtmosphereResponse response, byte[] responseDraft, byte[] data) throws IOException {
                    cancelF(request);
                    return responseDraft;
                }

                @Override
                public void postPayload(final AtmosphereResponse response, byte[] data, int offset, int length) {
                    logger.trace("Scheduling heartbeat for {}", r.uuid());
                    clock(interval, r, request, response);
                }
            });
            request.setAttribute(INTERCEPTOR_ADDED, Boolean.TRUE);
        }
    }
    return Action.CONTINUE;
}
Also used : AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) AsyncIOWriter(org.atmosphere.runtime.AsyncIOWriter) IOException(java.io.IOException) HeartbeatAtmosphereResourceEvent(org.atmosphere.runtime.HeartbeatAtmosphereResourceEvent) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResourceEventListenerAdapter(org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter) AtmosphereResourceEvent(org.atmosphere.runtime.AtmosphereResourceEvent) HeartbeatAtmosphereResourceEvent(org.atmosphere.runtime.HeartbeatAtmosphereResourceEvent) AtmosphereInterceptorWriter(org.atmosphere.runtime.AtmosphereInterceptorWriter) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) AsyncIOInterceptorAdapter(org.atmosphere.runtime.AsyncIOInterceptorAdapter)

Example 5 with AtmosphereResponse

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

the class PaddingAtmosphereInterceptor method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    if (Utils.webSocketMessage(r))
        return Action.CONTINUE;
    final AtmosphereResponse response = r.getResponse();
    final AtmosphereRequest request = r.getRequest();
    String uuid = request.getHeader(HeaderConfig.X_ATMOSPHERE_TRACKING_ID);
    boolean padding = r.transport().equals(TRANSPORT.STREAMING) || r.transport().equals(TRANSPORT.LONG_POLLING);
    if (uuid != null && !uuid.equals("0") && r.transport().equals(TRANSPORT.WEBSOCKET) && request.getAttribute(INJECTED_ATMOSPHERE_RESOURCE) != null) {
        padding = true;
    }
    if (padding) {
        r.addEventListener(new ForcePreSuspend(response));
        super.inspect(r);
        AsyncIOWriter writer = response.getAsyncIOWriter();
        if (AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass())) {
            AtmosphereInterceptorWriter.class.cast(writer).interceptor(new AsyncIOInterceptorAdapter() {

                private void padding() {
                    if (!r.isSuspended()) {
                        writePadding(response);
                        request.setAttribute("paddingWritten", "true");
                    }
                }

                @Override
                public void prePayload(AtmosphereResponse response, byte[] data, int offset, int length) {
                    padding();
                }

                @Override
                public void postPayload(AtmosphereResponse response, byte[] data, int offset, int length) {
                }
            });
        } else {
            logger.warn("Unable to apply {}. Your AsyncIOWriter must implement {}", getClass().getName(), AtmosphereInterceptorWriter.class.getName());
        }
    }
    return Action.CONTINUE;
}
Also used : AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AsyncIOWriter(org.atmosphere.runtime.AsyncIOWriter) AtmosphereInterceptorWriter(org.atmosphere.runtime.AtmosphereInterceptorWriter) AsyncIOInterceptorAdapter(org.atmosphere.runtime.AsyncIOInterceptorAdapter)

Aggregations

AtmosphereResponse (org.atmosphere.runtime.AtmosphereResponse)25 AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)18 IOException (java.io.IOException)16 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)12 AsyncIOWriter (org.atmosphere.runtime.AsyncIOWriter)11 AtmosphereInterceptorWriter (org.atmosphere.runtime.AtmosphereInterceptorWriter)9 ServletException (javax.servlet.ServletException)8 AsynchronousProcessor (org.atmosphere.runtime.AsynchronousProcessor)6 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 AsyncIOInterceptorAdapter (org.atmosphere.runtime.AsyncIOInterceptorAdapter)5 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)5 Enumeration (java.util.Enumeration)4 ServletConfig (javax.servlet.ServletConfig)4 ServletContext (javax.servlet.ServletContext)4 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)3 Test (org.testng.annotations.Test)3 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Action (org.atmosphere.runtime.Action)2