Search in sources :

Example 11 with AtmosphereResponse

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

Example 12 with AtmosphereResponse

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

Example 13 with AtmosphereResponse

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

the class DefaultProtocolInterceptor method attachWriter.

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

Example 14 with AtmosphereResponse

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

the class DefaultProtocolInterceptor method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    LOG.log(Level.FINE, "inspect");
    if (AtmosphereResource.TRANSPORT.WEBSOCKET != r.transport() && AtmosphereResource.TRANSPORT.SSE != r.transport() && AtmosphereResource.TRANSPORT.POLLING != r.transport()) {
        LOG.fine("Skipping ignorable request");
        return Action.CONTINUE;
    }
    if (AtmosphereResource.TRANSPORT.POLLING == r.transport()) {
        final String saruuid = (String) r.getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
        final AtmosphereResponse suspendedResponse = suspendedResponses.get(saruuid);
        LOG.fine("Attaching a proxy writer to suspended response");
        r.getResponse().asyncIOWriter(new AtmosphereInterceptorWriter() {

            @Override
            public AsyncIOWriter write(AtmosphereResponse r, String data) throws IOException {
                suspendedResponse.write(data);
                suspendedResponse.flushBuffer();
                return this;
            }

            @Override
            public AsyncIOWriter write(AtmosphereResponse r, byte[] data) throws IOException {
                suspendedResponse.write(data);
                suspendedResponse.flushBuffer();
                return this;
            }

            @Override
            public AsyncIOWriter write(AtmosphereResponse r, byte[] data, int offset, int length) throws IOException {
                suspendedResponse.write(data, offset, length);
                suspendedResponse.flushBuffer();
                return this;
            }

            @Override
            public void close(AtmosphereResponse response) throws IOException {
            }
        });
        // REVISIT we need to keep this response's asyncwriter alive so that data can be written to the
        // suspended response, but investigate if there is a better alternative.
        r.getResponse().destroyable(false);
        return Action.CONTINUE;
    }
    r.addEventListener(new AtmosphereResourceEventListenerAdapter() {

        @Override
        public void onSuspend(AtmosphereResourceEvent event) {
            final String srid = (String) event.getResource().getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
            LOG.log(Level.FINE, "Registrering suspended resource: {}", srid);
            suspendedResponses.put(srid, event.getResource().getResponse());
            AsyncIOWriter writer = event.getResource().getResponse().getAsyncIOWriter();
            if (writer instanceof AtmosphereInterceptorWriter) {
                ((AtmosphereInterceptorWriter) writer).interceptor(interceptor);
            }
        }

        @Override
        public void onDisconnect(AtmosphereResourceEvent event) {
            super.onDisconnect(event);
            final String srid = (String) event.getResource().getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
            LOG.log(Level.FINE, "Unregistrering suspended resource: {}", srid);
            suspendedResponses.remove(srid);
        }
    });
    AtmosphereRequest request = r.getRequest();
    if (request.getAttribute(REQUEST_DISPATCHED) == null) {
        AtmosphereResponse response = null;
        AtmosphereFramework framework = r.getAtmosphereConfig().framework();
        try {
            byte[] data = WebSocketUtils.readBody(request.getInputStream());
            if (data.length == 0) {
                if (AtmosphereResource.TRANSPORT.WEBSOCKET == r.transport() || AtmosphereResource.TRANSPORT.SSE == r.transport()) {
                    r.suspend();
                    return Action.SUSPEND;
                }
                return Action.CANCELLED;
            }
            if (LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "inspecting data {0}", new String(data));
            }
            try {
                AtmosphereRequest ar = createAtmosphereRequest(request, data);
                response = new WrappedAtmosphereResponse(r.getResponse(), ar);
                ar.localAttributes().put(REQUEST_DISPATCHED, "true");
                String refid = ar.getHeader(WebSocketConstants.DEFAULT_REQUEST_ID_KEY);
                if (refid != null) {
                    ar.localAttributes().put(WebSocketConstants.DEFAULT_REQUEST_ID_KEY, refid);
                }
                // This is a new request, we must clean the Websocket AtmosphereResource.
                request.removeAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE);
                response.request(ar);
                attachWriter(r);
                Action action = framework.doCometSupport(ar, response);
                if (action.type() == Action.TYPE.SUSPEND) {
                    ar.destroyable(false);
                    response.destroyable(false);
                }
            } catch (Exception e) {
                LOG.log(Level.WARNING, "Error during request dispatching", e);
                if (response == null) {
                    response = new WrappedAtmosphereResponse(r.getResponse(), request);
                }
                if (e instanceof InvalidPathException) {
                    response.setIntHeader(WebSocketUtils.SC_KEY, 400);
                } else {
                    response.setIntHeader(WebSocketUtils.SC_KEY, 500);
                }
                OutputStream out = response.getOutputStream();
                out.write(createResponse(response, null, true));
                out.close();
            }
            return Action.CANCELLED;
        } catch (IOException e) {
            LOG.log(Level.WARNING, "Error during protocol processing", e);
        }
    } else {
        request.destroyable(false);
    }
    return Action.CONTINUE;
}
Also used : AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) Action(org.atmosphere.cpr.Action) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) AsyncIOWriter(org.atmosphere.cpr.AsyncIOWriter) IOException(java.io.IOException) InvalidPathException(org.apache.cxf.transport.websocket.InvalidPathException) IOException(java.io.IOException) InvalidPathException(org.apache.cxf.transport.websocket.InvalidPathException) AtmosphereResourceEventListenerAdapter(org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereFramework(org.atmosphere.cpr.AtmosphereFramework) AtmosphereResourceEvent(org.atmosphere.cpr.AtmosphereResourceEvent) AtmosphereInterceptorWriter(org.atmosphere.cpr.AtmosphereInterceptorWriter)

Example 15 with AtmosphereResponse

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

AtmosphereResponse (org.atmosphere.cpr.AtmosphereResponse)29 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)21 IOException (java.io.IOException)19 AsyncIOWriter (org.atmosphere.cpr.AsyncIOWriter)12 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)12 AtmosphereInterceptorWriter (org.atmosphere.cpr.AtmosphereInterceptorWriter)10 ServletException (jakarta.servlet.ServletException)9 AsynchronousProcessor (org.atmosphere.cpr.AsynchronousProcessor)7 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)7 BeforeMethod (org.testng.annotations.BeforeMethod)6 AsyncIOInterceptorAdapter (org.atmosphere.cpr.AsyncIOInterceptorAdapter)5 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)5 ServletConfig (jakarta.servlet.ServletConfig)4 ServletContext (jakarta.servlet.ServletContext)4 Enumeration (java.util.Enumeration)4 Action (org.atmosphere.cpr.Action)4 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)3 AtmosphereResourceEvent (org.atmosphere.cpr.AtmosphereResourceEvent)3 Test (org.testng.annotations.Test)3 List (java.util.List)2