Package org.atmosphere.cpr

Examples of org.atmosphere.cpr.AtmosphereRequest


    public Action inspect(final AtmosphereResource ar) {

        if (Utils.webSocketMessage(ar)) return Action.CONTINUE;

        final AtmosphereResourceImpl r = AtmosphereResourceImpl.class.cast(ar);
        final AtmosphereRequest request = r.getRequest(false);
        final AtmosphereResponse response = r.getResponse(false);

        String uuid = request.getHeader(HeaderConfig.X_ATMOSPHERE_TRACKING_ID);
        String handshakeUUID = request.getHeader(HeaderConfig.X_ATMO_PROTOCOL);
        if (uuid != null && uuid.equals("0") && handshakeUUID != null) {

            if (enforceAtmosphereVersion) {
                String javascriptVersion = request.getHeader(HeaderConfig.X_ATMOSPHERE_FRAMEWORK);
                int version = parseVersion(javascriptVersion.split("-")[0]);
                if (version < 221) {
                    logger.error("Invalid Atmosphere Version {}", javascriptVersion);
                    response.setStatus(501);
                    response.addHeader(X_ATMOSPHERE_ERROR, "Atmosphere Protocol version not supported.");
                    try {
                        response.flushBuffer();
                    } catch (IOException e) {
                    }
                    return Action.CANCELLED;
                }
            }

            request.header(HeaderConfig.X_ATMO_PROTOCOL, null);

            // Extract heartbeat data
            int heartbeatInterval = 0;
            String heartbeatData = "";

            for (final AtmosphereInterceptor interceptor : framework.interceptors()) {
                if (HeartbeatInterceptor.class.isAssignableFrom(interceptor.getClass())) {
                    final HeartbeatInterceptor heartbeatInterceptor = HeartbeatInterceptor.class.cast(interceptor);
                    heartbeatInterval = heartbeatInterceptor.clientHeartbeatFrequencyInSeconds() * 1000;
                    heartbeatData = new String(heartbeatInterceptor.getPaddingBytes());
                    break;
                }
            }

            // Since 1.0.10
            final StringBuffer message = new StringBuffer(r.uuid()).append(wsDelimiter);

            // since 2.2
            if (enforceAtmosphereVersion) {
                message.append(heartbeatInterval)
                    .append(wsDelimiter)
                    .append(heartbeatData)
                    .append(wsDelimiter);
            }

            // https://github.com/Atmosphere/atmosphere/issues/993
            final AtomicReference<String> protocolMessage = new AtomicReference<String>(message.toString());
            if (r.getBroadcaster().getBroadcasterConfig().hasFilters()) {
                for (BroadcastFilter bf : r.getBroadcaster().getBroadcasterConfig().filters()) {
                    if (TrackMessageSizeFilter.class.isAssignableFrom(bf.getClass())) {
                        protocolMessage.set((String) f.filter(r.getBroadcaster().getID(), r, protocolMessage.get(), protocolMessage.get()).message());
                        break;
                    }
                }
            }

            if (!Utils.resumableTransport(r.transport())) {
                OnSuspend a = new OnSuspend() {
                    @Override
                    public void onSuspend(AtmosphereResourceEvent event) {
                        response.write(protocolMessage.get());
                        try {
                            response.flushBuffer();
                        } catch (IOException e) {
                            logger.trace("", e);
                        }
                        r.removeEventListener(this);
                    }
                };
                // Pass the information to Servlet Based Framework
                request.setAttribute(CALLBACK_JAVASCRIPT_PROTOCOL, a);
                r.addEventListener(a);
            } else {
                response.write(protocolMessage.get());
            }

View Full Code Here


            }

            DefaultWebSocket g2WebSocket = DefaultWebSocket.class.cast(socket);
            try {

                AtmosphereRequest r = AtmosphereRequest.wrap(g2WebSocket.getUpgradeRequest());
                org.atmosphere.websocket.WebSocket webSocket = new Grizzly2WebSocket(g2WebSocket, config);
                g2WebSocket.getUpgradeRequest().setAttribute("grizzly.webSocket", webSocket);
                webSocketProcessor.open(webSocket, r, AtmosphereResponse.newInstance(config, r, webSocket));
            } catch (Exception e) {
                LOGGER.warn("failed to connect to web socket", e);
View Full Code Here

                                                                String pathInfo,
                                                                String requestURI,
                                                                String methodType,
                                                                String contentType,
                                                                boolean destroyable) {
        AtmosphereRequest request = AtmosphereResourceImpl.class.cast(resource).getRequest(false);
        Map<String, Object> m = attributes(request);

        // We need to create a new AtmosphereRequest as WebSocket message may arrive concurrently on the same connection.
        AtmosphereRequest.Builder b = (new AtmosphereRequest.Builder()
                .request(request)
                .method(methodType)
                .contentType(contentType == null ? request.getContentType() : contentType)
                .attributes(m)
                .pathInfo(pathInfo)
                .contextPath(request.getContextPath())
                .servletPath(request.getServletPath())
                .requestURI(requestURI)
                .requestURL(request.requestURL())
                .destroyable(destroyable)
                .headers(request.headersMap())
                .session(resource.session()));
        return b;
    }
View Full Code Here

        AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
        if (resource == null) {
            logger.trace("The WebSocket has been closed before the message was processed.");
            return null;
        }
        AtmosphereRequest request = resource.getRequest(false);

        if (!resource.isInScope()) return Collections.emptyList();

        String pathInfo = request.getPathInfo();
        String requestURI = request.getRequestURI();

        // This confuse some JAXRS servers like RestEasy
        if (rewriteUri && (requestURI.startsWith("http://") || requestURI.startsWith("https://"))) {
            logger.debug("Rewriting requestURI {}. To disable, add {} set to true as init-param",
                    requestURI, ApplicationConfig.REWRITE_WEBSOCKET_REQUESTURI);
            requestURI = URI.create(requestURI).getPath();
            request.requestURI(requestURI);
        }

        if (message.startsWith(delimiter)) {
            int delimiterLength = delimiter.length();
            int bodyBeginIndex = message.indexOf(delimiter, delimiterLength);
View Full Code Here

        if (resource == null) {
            logger.trace("The WebSocket has been closed before the message was processed.");
            return null;
        }

        AtmosphereRequest request = resource.getRequest(false);

        if (!resource.isInScope()) return Collections.emptyList();

        List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
        list.add(constructRequest(resource, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).body(d, offset, length).build());

        return list;
    }
View Full Code Here

        if (resource == null) {
            logger.trace("The WebSocket has been closed before the message was processed.");
            return null;
        }

        AtmosphereRequest request = resource.getRequest();
        List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
        list.add(constructRequest(resource, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).reader(r).build());

        return list;
    }
View Full Code Here

        if (resource == null) {
            logger.trace("The WebSocket has been closed before the message was processed.");
            return null;
        }

        AtmosphereRequest request = resource.getRequest();
        List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
        list.add(constructRequest(resource, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).inputStream(stream).build());

        return list;
    }
View Full Code Here

        AtmosphereResourceImpl resource = AtmosphereResourceImpl.class.cast(webSocket.resource());
        if (resource == null) {
            return;
        }

        AtmosphereRequest request = resource.getRequest(false);
        if (String.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
            request.body(String.class.cast(webSocketMessageAsBody));
        } else if (Reader.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
            request.body(Reader.class.cast(webSocketMessageAsBody));
        } else if (InputStream.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
            request.body(InputStream.class.cast(webSocketMessageAsBody));
        } else {
            request.body(new ByteArrayInputStream((byte[]) webSocketMessageAsBody, offset, length));
        }

        // Globally defined
        int tracing = 0;
        Action a = asynchronousProcessor.invokeInterceptors(framework.interceptors(), resource, tracing);
        if (a.type() != Action.TYPE.CONTINUE && a.type() != Action.TYPE.SKIP_ATMOSPHEREHANDLER) {
            return;
        }

        WebSocketHandlerProxy proxy = WebSocketHandlerProxy.class.cast(webSocketHandler);
        if (a.type() != Action.TYPE.SKIP_ATMOSPHEREHANDLER) {
            // Per AtmosphereHandler
            a = asynchronousProcessor.invokeInterceptors(proxy.interceptors(), resource, tracing);
            if (a.type() != Action.TYPE.CONTINUE) {
                return;
            }
        }

        //Unit test mock the request and will throw NPE.
        boolean skipAtmosphereHandler = request.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) != null
                ? (Boolean) request.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) : Boolean.FALSE;
        if (!skipAtmosphereHandler) {
            try {
                if (String.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
                    webSocketHandler.onTextMessage(webSocket, String.class.cast(webSocketMessageAsBody));
                } else if (Reader.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
                    WebSocketStreamingHandler.class.cast(webSocketHandler.proxied).onTextStream(webSocket, Reader.class.cast(webSocketMessageAsBody));
                } else if (InputStream.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
                    WebSocketStreamingHandler.class.cast(webSocketHandler.proxied()).onBinaryStream(webSocket, InputStream.class.cast(webSocketMessageAsBody));
                } else {
                    webSocketHandler.onByteMessage(webSocket, (byte[]) webSocketMessageAsBody, offset, length);
                }
            } catch (IOException t) {
                resource.onThrowable(t);
                throw t;
            }
            asynchronousProcessor.postInterceptors(proxy.interceptors(), resource);
        }
        request.setAttribute(SKIP_ATMOSPHEREHANDLER.name(), Boolean.FALSE);

        asynchronousProcessor.postInterceptors(framework.interceptors(), resource);
    }
View Full Code Here

        if (resource == null) {
            logger.trace("Already closed {}", webSocket);
        } else {
            final boolean allowedToClose = allowedCloseCode(closeCode);

            final AtmosphereRequest r = resource.getRequest(false);
            final AtmosphereResponse s = resource.getResponse(false);
            boolean ff = r.getAttribute("firefox") != null;
            boolean completeLifecycle = true;
            try {
                webSocketProtocol.onClose(webSocket);

                if (webSocketHandler != null) {
View Full Code Here

        if (!webSocketProcessor.handshake(socket.getServletRequest())) {
            socket.closeSocket();
            throw new IOException();
        }

        AtmosphereRequest r = AtmosphereRequest.wrap(socket.getServletRequest());
        webSocket = new JBossWebSocket(socket, config);
        webSocketProcessor.open(webSocket, r, AtmosphereResponse.newInstance(config, r, webSocket));
    }
View Full Code Here

TOP

Related Classes of org.atmosphere.cpr.AtmosphereRequest

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.