Search in sources :

Example 6 with AtmosphereRequest

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

the class IdleResourceInterceptor method idleResources.

protected void idleResources() {
    if (logger.isTraceEnabled()) {
        logger.trace("{} monitoring {} AtmosphereResources", getClass().getSimpleName(), config.resourcesFactory().findAll().size());
    }
    for (AtmosphereResource r : config.resourcesFactory().findAll()) {
        if (Utils.pollableTransport(r.transport())) {
            continue;
        }
        AtmosphereRequest req = AtmosphereResourceImpl.class.cast(r).getRequest(false);
        try {
            if (req.getAttribute(MAX_INACTIVE) == null) {
                logger.warn("Invalid state {}", r);
                r.removeFromAllBroadcasters();
                config.resourcesFactory().unRegisterUuidForFindCandidate(r);
                continue;
            }
            long l = (Long) req.getAttribute(MAX_INACTIVE);
            if (logger.isTraceEnabled() && l > 0) {
                logger.trace("Expiring {} in {}", r.uuid(), System.currentTimeMillis() - l);
            }
            if (l > 0 && System.currentTimeMillis() - l > maxInactiveTime) {
                try {
                    req.setAttribute(MAX_INACTIVE, (long) -1);
                    logger.debug("IdleResourceInterceptor disconnecting {}", r);
                    Future<?> f = (Future<?>) req.getAttribute(HeartbeatInterceptor.HEARTBEAT_FUTURE);
                    if (f != null)
                        f.cancel(false);
                    req.removeAttribute(HeartbeatInterceptor.HEARTBEAT_FUTURE);
                    WebSocket webSocket = AtmosphereResourceImpl.class.cast(r).webSocket();
                    if (webSocket != null) {
                        webSocket.close();
                    } else {
                        AsynchronousProcessor.class.cast(config.framework().getAsyncSupport()).endRequest(AtmosphereResourceImpl.class.cast(r), true);
                    }
                } finally {
                    r.removeFromAllBroadcasters();
                    config.resourcesFactory().unRegisterUuidForFindCandidate(r);
                }
            }
        } catch (Throwable e) {
            logger.warn("IdleResourceInterceptor", e);
        }
    }
}
Also used : AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) AsynchronousProcessor(org.atmosphere.runtime.AsynchronousProcessor) Future(java.util.concurrent.Future) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) WebSocket(org.atmosphere.websocket.WebSocket)

Example 7 with AtmosphereRequest

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

the class PaddingAtmosphereInterceptor method writePadding.

private void writePadding(AtmosphereResponse response) {
    AtmosphereRequest request = response.request();
    if (request != null && request.getAttribute("paddingWritten") != null)
        return;
    if (response.resource() != null && response.resource().transport().equals(TRANSPORT.STREAMING)) {
        request.setAttribute(FrameworkConfig.TRANSPORT_IN_USE, HeaderConfig.STREAMING_TRANSPORT);
        response.setContentType("text/plain");
    }
    response.write(padding, true);
    try {
        response.flushBuffer();
    } catch (IOException e) {
        logger.trace("", e);
    }
}
Also used : AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) IOException(java.io.IOException)

Example 8 with AtmosphereRequest

use of org.atmosphere.runtime.AtmosphereRequest 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)

Example 9 with AtmosphereRequest

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

the class SSEAtmosphereInterceptor 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 accept = request.getHeader("Accept") == null ? "text/plain" : request.getHeader("Accept").trim();
    if (r.transport().equals(AtmosphereResource.TRANSPORT.SSE) || contentType.equalsIgnoreCase(accept)) {
        super.inspect(r);
        r.addEventListener(new P(response));
        AsyncIOWriter writer = response.getAsyncIOWriter();
        if (AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass())) {
            AtmosphereInterceptorWriter.class.cast(writer).interceptor(new AsyncIOInterceptorAdapter() {

                private boolean padding() {
                    if (!r.isSuspended()) {
                        return writePadding(response);
                    }
                    return false;
                }

                @Override
                public void prePayload(AtmosphereResponse response, byte[] data, int offset, int length) {
                    boolean noPadding = padding();
                    // In that case, we must pad/protocol indenendently of the state of the AtmosphereResource
                    if (!noPadding || r.getRequest().getAttribute(CALLBACK_JAVASCRIPT_PROTOCOL) != null) {
                        // write other meta info such as (id, event, etc)?
                        response.write(DATA, true);
                    }
                }

                @Override
                public byte[] transformPayload(AtmosphereResponse response, byte[] responseDraft, byte[] data) throws IOException {
                    boolean noPadding = padding();
                    // In that case, we must pad/protocol indenendently of the state of the AtmosphereResource
                    if (!noPadding || r.getRequest().getAttribute(CALLBACK_JAVASCRIPT_PROTOCOL) != null) {
                        if (isMultilineData(responseDraft)) {
                            // return a padded multiline-data
                            return encodeMultilineData(responseDraft);
                        }
                    }
                    return responseDraft;
                }

                @Override
                public void postPayload(AtmosphereResponse response, byte[] data, int offset, int length) {
                    // In that case, we must pad/protocol indenendently of the state of the AtmosphereResource
                    if (r.isSuspended() || r.getRequest().getAttribute(CALLBACK_JAVASCRIPT_PROTOCOL) != null || r.getRequest().getAttribute(CONTAINER_RESPONSE) != null) {
                        response.write(END, true);
                    }
                    /**
                         * When used with https://github.com/remy/polyfills/blob/master/EventSource.js , we
                         * resume after every message.
                         */
                    String ua = r.getRequest().getHeader("User-Agent");
                    if (ua != null && ua.contains("MSIE")) {
                        try {
                            response.flushBuffer();
                        } catch (IOException e) {
                            logger.trace("", e);
                        }
                        r.resume();
                    }
                }
            });
        } 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) IOException(java.io.IOException) AsyncIOInterceptorAdapter(org.atmosphere.runtime.AsyncIOInterceptorAdapter)

Example 10 with AtmosphereRequest

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

the class BroadcastOnPostAtmosphereInterceptor method postInspect.

@Override
public void postInspect(AtmosphereResource r) {
    if (r.getRequest().getMethod().equalsIgnoreCase("POST")) {
        AtmosphereRequest request = r.getRequest();
        Object o;
        try {
            o = readEntirely(r);
        } catch (IOException e) {
            logger.warn("", e);
            return;
        }
        if (isBodyEmpty(o)) {
            logger.warn("{} received an empty body", request);
            return;
        }
        r.getBroadcaster().broadcast(o);
    }
}
Also used : AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) IOException(java.io.IOException)

Aggregations

AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)76 Test (org.testng.annotations.Test)39 AtmosphereRequestImpl (org.atmosphere.runtime.AtmosphereRequestImpl)38 IOException (java.io.IOException)23 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)20 AtmosphereResponse (org.atmosphere.runtime.AtmosphereResponse)18 ServletException (javax.servlet.ServletException)10 AsynchronousProcessor (org.atmosphere.runtime.AsynchronousProcessor)8 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)7 AtmosphereResource (org.atmosphere.runtime.AtmosphereResource)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 AsyncIOWriter (org.atmosphere.runtime.AsyncIOWriter)6 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 ArrayList (java.util.ArrayList)5 AtmosphereInterceptorWriter (org.atmosphere.runtime.AtmosphereInterceptorWriter)5 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)5 Reader (java.io.Reader)4 Enumeration (java.util.Enumeration)4 ServletConfig (javax.servlet.ServletConfig)4