Search in sources :

Example 6 with AsyncIOWriter

use of org.atmosphere.cpr.AsyncIOWriter 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.cpr.AtmosphereResponse) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AsyncIOWriter(org.atmosphere.cpr.AsyncIOWriter) AtmosphereInterceptorWriter(org.atmosphere.cpr.AtmosphereInterceptorWriter) AsyncIOInterceptorAdapter(org.atmosphere.cpr.AsyncIOInterceptorAdapter)

Example 7 with AsyncIOWriter

use of org.atmosphere.cpr.AsyncIOWriter 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.cpr.AtmosphereResponse) AsyncIOWriter(org.atmosphere.cpr.AsyncIOWriter) AtmosphereInterceptorWriter(org.atmosphere.cpr.AtmosphereInterceptorWriter)

Example 8 with AsyncIOWriter

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

the class WebSocket method transform.

protected byte[] transform(AtmosphereResponse response, byte[] b, int offset, int length) throws IOException {
    AsyncIOWriter a = response.getAsyncIOWriter();
    // NOTE #1961 for now, create a new buffer par transform call and release it after the transform call.
    // Alternatively, we may cache the buffer in thread-local and use it while this thread invokes
    // multiple writes and release it when this thread invokes the close method.
    ByteArrayAsyncWriter buffer = new ByteArrayAsyncWriter();
    try {
        response.asyncIOWriter(buffer);
        invokeInterceptor(response, b, offset, length);
        return buffer.stream().toByteArray();
    } finally {
        buffer.close(null);
        response.asyncIOWriter(a);
    }
}
Also used : ByteArrayAsyncWriter(org.atmosphere.util.ByteArrayAsyncWriter) AsyncIOWriter(org.atmosphere.cpr.AsyncIOWriter)

Example 9 with AsyncIOWriter

use of org.atmosphere.cpr.AsyncIOWriter in project cxf by apache.

the class SseAtmosphereInterceptor method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    if (Utils.webSocketMessage(r)) {
        return Action.CONTINUE;
    }
    final AtmosphereRequest request = r.getRequest();
    final String accept = request.getHeader("Accept") == null ? "text/plain" : request.getHeader("Accept").trim();
    if (r.transport().equals(AtmosphereResource.TRANSPORT.SSE) || SERVER_SENT_EVENTS.equalsIgnoreCase(accept)) {
        final AtmosphereResponse response = r.getResponse();
        if (response.getAsyncIOWriter() == null) {
            response.asyncIOWriter(new SseAtmosphereInterceptorWriter());
        }
        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) {
                    padding();
                }

                @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) {
                            LOG.log(Level.FINEST, "", e);
                        }
                        r.resume();
                    }
                }
            });
        } else {
            LOG.warning(String.format("Unable to apply %s. Your AsyncIOWriter must implement %s", getClass().getName(), AtmosphereInterceptorWriter.class.getName()));
        }
    }
    return Action.CONTINUE;
}
Also used : AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AsyncIOWriter(org.atmosphere.cpr.AsyncIOWriter) AtmosphereInterceptorWriter(org.atmosphere.cpr.AtmosphereInterceptorWriter) IOException(java.io.IOException) AsyncIOInterceptorAdapter(org.atmosphere.cpr.AsyncIOInterceptorAdapter)

Example 10 with AsyncIOWriter

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

the class NettyCometSupport method complete.

@Override
public AsyncSupport<AtmosphereResourceImpl> complete(AtmosphereResourceImpl r) {
    try {
        AtmosphereResponse response = r.getResponse(false);
        AsyncIOWriter a = response.getAsyncIOWriter();
        if (a != null) {
            a.close(response);
        }
    } catch (IOException e) {
        logger.trace("", e);
    }
    return this;
}
Also used : AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) AsyncIOWriter(org.atmosphere.cpr.AsyncIOWriter) IOException(java.io.IOException)

Aggregations

AsyncIOWriter (org.atmosphere.cpr.AsyncIOWriter)14 AtmosphereResponse (org.atmosphere.cpr.AtmosphereResponse)12 AtmosphereInterceptorWriter (org.atmosphere.cpr.AtmosphereInterceptorWriter)10 IOException (java.io.IOException)7 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)7 AsyncIOInterceptorAdapter (org.atmosphere.cpr.AsyncIOInterceptorAdapter)5 Action (org.atmosphere.cpr.Action)2 AtmosphereResourceEvent (org.atmosphere.cpr.AtmosphereResourceEvent)2 AtmosphereResourceEventListenerAdapter (org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter)2 ServletException (jakarta.servlet.ServletException)1 OutputStream (java.io.OutputStream)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)1 InvalidPathException (org.apache.cxf.transport.websocket.InvalidPathException)1 AsyncIOWriterAdapter (org.atmosphere.cpr.AsyncIOWriterAdapter)1 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)1 AtmosphereRequestImpl (org.atmosphere.cpr.AtmosphereRequestImpl)1 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)1 ByteArrayAsyncWriter (org.atmosphere.util.ByteArrayAsyncWriter)1