Search in sources :

Example 1 with AsyncIOInterceptorAdapter

use of org.atmosphere.cpr.AsyncIOInterceptorAdapter 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.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 2 with AsyncIOInterceptorAdapter

use of org.atmosphere.cpr.AsyncIOInterceptorAdapter 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 3 with AsyncIOInterceptorAdapter

use of org.atmosphere.cpr.AsyncIOInterceptorAdapter 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 4 with AsyncIOInterceptorAdapter

use of org.atmosphere.cpr.AsyncIOInterceptorAdapter 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 5 with AsyncIOInterceptorAdapter

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

Aggregations

AsyncIOInterceptorAdapter (org.atmosphere.cpr.AsyncIOInterceptorAdapter)5 AsyncIOWriter (org.atmosphere.cpr.AsyncIOWriter)5 AtmosphereInterceptorWriter (org.atmosphere.cpr.AtmosphereInterceptorWriter)5 AtmosphereResponse (org.atmosphere.cpr.AtmosphereResponse)5 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)4 IOException (java.io.IOException)3 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)1