Search in sources :

Example 6 with AtmosphereRequest

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

Aggregations

AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)6 AtmosphereResponse (org.atmosphere.cpr.AtmosphereResponse)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 InvalidPathException (org.apache.cxf.transport.websocket.InvalidPathException)2 AsyncIOWriter (org.atmosphere.cpr.AsyncIOWriter)2 AtmosphereInterceptorWriter (org.atmosphere.cpr.AtmosphereInterceptorWriter)2 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 Map (java.util.Map)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)1 Action (org.atmosphere.cpr.Action)1 AsyncIOInterceptorAdapter (org.atmosphere.cpr.AsyncIOInterceptorAdapter)1 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)1 AtmosphereResourceEvent (org.atmosphere.cpr.AtmosphereResourceEvent)1 AtmosphereResourceEventListenerAdapter (org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter)1 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)1 Test (org.junit.Test)1