Search in sources :

Example 11 with AsyncIOWriter

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

the class AndroidAtmosphereInterceptor method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    if (!r.transport().equals(TRANSPORT.STREAMING))
        return Action.CONTINUE;
    final AtmosphereResponse response = AtmosphereResourceImpl.class.cast(r).getResponse(false);
    String userAgent = AtmosphereResourceImpl.class.cast(r).getRequest(false).getHeader("User-Agent");
    if (userAgent != null && (userAgent.indexOf("Android 2.") != -1 || userAgent.indexOf("Android 3.") != -1)) {
        super.inspect(r);
        AsyncIOWriter writer = response.getAsyncIOWriter();
        if (AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass())) {
            AtmosphereInterceptorWriter.class.cast(writer).interceptor(new AsyncIOInterceptorAdapter() {

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

                @Override
                public void postPayload(AtmosphereResponse response, byte[] data, int offset, int length) {
                    response.write(padding, true);
                }
            });
        } 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) AsyncIOWriter(org.atmosphere.cpr.AsyncIOWriter) AtmosphereInterceptorWriter(org.atmosphere.cpr.AtmosphereInterceptorWriter) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) AsyncIOInterceptorAdapter(org.atmosphere.cpr.AsyncIOInterceptorAdapter)

Example 12 with AsyncIOWriter

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

the class JSONPAtmosphereInterceptor method inspect.

@Override
public Action inspect(AtmosphereResource r) {
    if (Utils.webSocketMessage(r))
        return Action.CONTINUE;
    final AtmosphereRequest request = r.getRequest();
    final AtmosphereResponse response = r.getResponse();
    // Shield from Broken server
    String uri = request.getRequestURI() == null ? "" : request.getRequestURI();
    final boolean contain = uri.contains("jsonp");
    if (r.transport().equals(AtmosphereResource.TRANSPORT.JSONP) || contain) {
        super.inspect(r);
        if (contain) {
            startChunk = "(\"";
            endChunk = "\");\r\n\r\n";
        }
        response.setContentType(CONTENT_TYPE);
        AsyncIOWriter writer = response.getAsyncIOWriter();
        if (AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass())) {
            AtmosphereInterceptorWriter.class.cast(writer).interceptor(new AsyncIOInterceptorAdapter() {

                String callbackName() {
                    String callback = escapeForJavaScript(request.getParameter(HeaderConfig.JSONP_CALLBACK_NAME)).replaceAll(PATTERN, "");
                    if (!callback.isEmpty()) {
                        // Look for extension
                        String jsonp = escapeForJavaScript((String) config.properties().get(HeaderConfig.JSONP_CALLBACK_NAME)).replaceAll(PATTERN, "");
                        if (!jsonp.isEmpty()) {
                            callback = request.getParameter(jsonp);
                        }
                    }
                    return callback;
                }

                @Override
                public void prePayload(AtmosphereResponse response, byte[] data, int offset, int length) {
                    String callbackName = callbackName();
                    response.write(callbackName + startChunk);
                }

                @Override
                public byte[] transformPayload(AtmosphereResponse response, byte[] responseDraft, byte[] data) throws IOException {
                    String charEncoding = response.getCharacterEncoding() == null ? "UTF-8" : response.getCharacterEncoding();
                    return escapeForJavaScript(new String(responseDraft, charEncoding)).getBytes(charEncoding);
                }

                @Override
                public void postPayload(AtmosphereResponse response, byte[] data, int offset, int length) {
                    response.write(endChunk, true);
                }
            });
        } 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) IOException(java.io.IOException) AsyncIOInterceptorAdapter(org.atmosphere.cpr.AsyncIOInterceptorAdapter)

Example 13 with AsyncIOWriter

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

the class SimpleRestInterceptor method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    if (AtmosphereResource.TRANSPORT.WEBSOCKET != r.transport() && AtmosphereResource.TRANSPORT.SSE != r.transport() && AtmosphereResource.TRANSPORT.POLLING != r.transport()) {
        LOG.debug("Skipping for non websocket 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.debug("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.debug("Registrering suspended resource: {}", srid);
            suspendedResponses.put(srid, event.getResource().getResponse());
            AsyncIOWriter writer = event.getResource().getResponse().getAsyncIOWriter();
            if (writer == null) {
                writer = new AtmosphereInterceptorWriter();
                r.getResponse().asyncIOWriter(writer);
            }
            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.debug("Unregistrering suspended resource: {}", srid);
            suspendedResponses.remove(srid);
        }
    });
    AtmosphereRequest request = r.getRequest();
    if (request.getAttribute(REQUEST_DISPATCHED) == null) {
        try {
            // REVISIT use a more efficient approach for the detached mode (i.e.,avoid reading the message into a string)
            // read the message entity and dispatch a service call
            String body = IOUtils.readEntirelyAsString(r).toString();
            LOG.debug("Request message: '{}'", body);
            if (body.length() == 0) {
                // TODO we might want to move this heartbeat scheduling after the handshake phase (if that is added)
                if ((AtmosphereResource.TRANSPORT.WEBSOCKET == r.transport() || AtmosphereResource.TRANSPORT.SSE == r.transport()) && request.getAttribute(HEARTBEAT_SCHEDULED) == null) {
                    r.suspend();
                    scheduleHeartbeat(r);
                    request.setAttribute(HEARTBEAT_SCHEDULED, "true");
                    return Action.SUSPEND;
                }
                return Action.CANCELLED;
            }
            AtmosphereRequest ar = createAtmosphereRequest(request, body);
            if (ar == null) {
                return Action.CANCELLED;
            }
            AtmosphereResponse response = r.getResponse();
            ar.localAttributes().put(REQUEST_DISPATCHED, "true");
            request.removeAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE);
            response.request(ar);
            attachWriter(r);
            Action action = r.getAtmosphereConfig().framework().doCometSupport(ar, response);
            if (action.type() == Action.TYPE.SUSPEND) {
                ar.destroyable(false);
                response.destroyable(false);
            }
            return Action.CANCELLED;
        } catch (IOException | ServletException e) {
            LOG.error("Failed to process", e);
        }
    }
    return Action.CONTINUE;
}
Also used : AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) Action(org.atmosphere.cpr.Action) AsyncIOWriter(org.atmosphere.cpr.AsyncIOWriter) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) AtmosphereResourceEventListenerAdapter(org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResourceEvent(org.atmosphere.cpr.AtmosphereResourceEvent) AtmosphereInterceptorWriter(org.atmosphere.cpr.AtmosphereInterceptorWriter)

Example 14 with AsyncIOWriter

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

the class TrackMessageSizeInterceptor method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    if (AtmosphereResource.TRANSPORT.UNDEFINED == r.transport() || Utils.webSocketMessage(r))
        return Action.CONTINUE;
    final AtmosphereResponse response = r.getResponse();
    super.inspect(r);
    AsyncIOWriter writer = response.getAsyncIOWriter();
    if (AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass())) {
        ((AtmosphereInterceptorWriter) writer).interceptor(interceptor);
    } 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) AsyncIOWriter(org.atmosphere.cpr.AsyncIOWriter) AtmosphereInterceptorWriter(org.atmosphere.cpr.AtmosphereInterceptorWriter)

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