Package org.atmosphere.cpr

Examples of org.atmosphere.cpr.AtmosphereRequest$LazyComputation


        }
    }

    public static StringBuilder readEntirelyAsString(AtmosphereResource r) {
        final StringBuilder stringBuilder = new StringBuilder();
        AtmosphereRequest request = r.getRequest();
        if (request.body().isEmpty()) {
            BufferedReader bufferedReader = null;
            try {
                try {
                    InputStream inputStream = request.getInputStream();
                    if (inputStream != null) {
                        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    }
                } catch (IllegalStateException ex) {
                    logger.trace("", ex);
                    Reader reader = request.getReader();
                    if (reader != null) {
                        bufferedReader = new BufferedReader(reader);
                    }
                }

                if (bufferedReader != null) {
                    char[] charBuffer = new char[8192];
                    int bytesRead = -1;
                    try {
                        while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                            stringBuilder.append(charBuffer, 0, bytesRead);
                        }
                    } catch (NullPointerException ex) {
                        // https://java.net/jira/browse/GRIZZLY-1676
                    }
                } else {
                    stringBuilder.append("");
                }
            } catch (IOException ex) {
                logger.warn("", ex);
            } finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException ex) {
                        logger.warn("", ex);
                    }
                }
            }
        } else {
            AtmosphereRequest.Body body = request.body();
            try {
                stringBuilder.append(body.hasString() ? body.asString() : new String(body.asBytes(), body.byteOffset(), body.byteLength(), request.getCharacterEncoding()));
            } catch (UnsupportedEncodingException e) {
                logger.error("", e);
            }
        }
        return stringBuilder;
View Full Code Here


        }
        return stringBuilder;
    }

    public static byte[] readEntirelyAsByte(AtmosphereResource r) {
        AtmosphereRequest request = r.getRequest();
        AtmosphereRequest.Body body = request.body();
        if (request.body().isEmpty()) {
            BufferedInputStream bufferedStream = null;
            ByteArrayOutputStream bbIS = new ByteArrayOutputStream();
            try {
                try {
                    InputStream inputStream = request.getInputStream();
                    if (inputStream != null) {
                        bufferedStream = new BufferedInputStream(inputStream);
                    }
                } catch (IllegalStateException ex) {
                    logger.trace("", ex);
                    Reader reader = request.getReader();
                    if (reader != null) {
                        bufferedStream = new BufferedInputStream(new ReaderInputStream(reader));
                    }
                }

                if (bufferedStream != null) {
                    byte[] bytes = new byte[8192];
                    int bytesRead = 0;
                    while (bytesRead != -1) {
                        bytesRead = bufferedStream.read(bytes);
                        if (bytesRead > 0)
                            bbIS.write(bytes, 0, bytesRead);
                    }

                } else {
                    bbIS.write("".getBytes());
                }
            } catch (IOException ex) {
                logger.warn("", ex);
            } finally {
                if (bufferedStream != null) {
                    try {
                        bufferedStream.close();
                    } catch (IOException ex) {
                        logger.warn("", ex);
                    }
                }
            }
            return bbIS.toByteArray();
        } else if (body.hasString()) {
            try {
                return readEntirelyAsString(r).toString().getBytes(request.getCharacterEncoding());
            } catch (UnsupportedEncodingException e) {
                logger.error("", e);
            }
        } else if (body.hasBytes()) {
            return Arrays.copyOfRange(body.asBytes(), body.byteOffset(), body.byteLength());
View Full Code Here

        WebSocket webSocket = new WebLogicWebSocket(webSocketConnection, config);
        // TODO: Dangerous
        webSocketConnection.getWebSocketContext().getServletContext().setAttribute(webSocketConnection.toString(), webSocket);

        AtmosphereRequest ar = AtmosphereRequest.cloneRequest(request.get(), true, false, true);
        request.set(null);
        try {
            webSocketProcessor.open(webSocket, ar, AtmosphereResponse.newInstance(config, ar, webSocket));
        } catch (IOException e) {
            logger.error("{}", e);
View Full Code Here

        String p = r.getHeader(HeaderConfig.X_ATMO_PROTOCOL);
        return (p != null && Boolean.valueOf(p));
    }

    public final static boolean webSocketMessage(AtmosphereResource r) {
        AtmosphereRequest request = AtmosphereResourceImpl.class.cast(r).getRequest(false);
        return request.getAttribute(FrameworkConfig.WEBSOCKET_MESSAGE) != null;
    }
View Full Code Here

            throws IOException {

        Object message = event.getMessage();
        AtmosphereResource resource = event.getResource();
        AtmosphereResponse r = resource.getResponse();
        AtmosphereRequest request = resource.getRequest();

        boolean writeAsBytes = IOUtils.isBodyBinary(request);
        if (message == null) {
            logger.trace("Message was null for AtmosphereEvent {}", event);
            return;
View Full Code Here

        return this;
    }

    @Override
    public void onRequest(final AtmosphereResource resource) throws IOException {
        AtmosphereRequest request = resource.getRequest();
        String method = request.getMethod();
        boolean polling = Utils.pollableTransport(resource.transport());
        boolean webSocketMessage = Utils.webSocketMessage(resource);

        if (!webSocketMessage) {
            if (onReadyMethod != null && !polling) {
View Full Code Here

    private Object invoke(Method m, Object o) {
        return Utils.invoke(proxiedInstance, m, o);
    }

    private MethodInfo.EncoderObject message(AtmosphereResource resource, Object o) {
        AtmosphereRequest request = AtmosphereResourceImpl.class.cast(resource).getRequest(false);
        try {

            Method invokedMethod = (Method) request.getAttribute(getClass().getName());
            request.removeAttribute(getClass().getName());

            if (invokedMethod != null) {
                for (MethodInfo m : onRuntimeMethod) {
                    if (invokedMethod.equals(m.method)) {
                        return m.encode(encoders, o);
                    }
                }
            }

            for (MethodInfo m : onRuntimeMethod) {
                if (m.useReader) {
                    o = request.getReader();
                } else if (m.useStream) {
                    o = request.getInputStream();
                } else if (o == null) {
                    o = readEntirely(resource);
                    if (isBodyEmpty(o)) {
                        logger.warn("{} received an empty body", request);
                        return null;
                    }
                }

                Object decoded = Invoker.decode(decoders.get(m.method), o);
                if (decoded == null) {
                    decoded = o;
                }
                Object objectToEncode = null;

                if (m.method.getParameterTypes().length > 2) {
                    logger.warn("Injection of more than 2 parameters not supported {}", m);
                }

                if (invokedMethod == null) {
                    if (m.method.getParameterTypes().length == 2) {
                        request.setAttribute(getClass().getName(), m.method);
                        objectToEncode = Invoker.invokeMethod(m.method, proxiedInstance, resource, decoded);
                    } else {
                        objectToEncode = Invoker.invokeMethod(m.method, proxiedInstance, decoded);
                    }
                } else {
View Full Code Here

public final class JerseyBroadcasterUtil {

    private static final Logger logger = LoggerFactory.getLogger(JerseyBroadcasterUtil.class);

    public final static void broadcast(final AtmosphereResource r, final AtmosphereResourceEvent e, final Broadcaster broadcaster) {
        AtmosphereRequest request = r.getRequest();
        ContainerResponse cr = null;

        // Make sure only one thread can play with the ContainerResponse. Threading issue can arise if there is a scheduler
        // or if ContainerResponse is associated with more than Broadcaster.
        cr = (ContainerResponse) request.getAttribute(FrameworkConfig.CONTAINER_RESPONSE);

        if (cr == null || !r.isSuspended() && !r.getAtmosphereResourceEvent().isResumedOnTimeout()) {
            if (cr == null) {
                logger.warn("Unexpected state. ContainerResponse has been resumed. Caching message {} for {}",
                        e.getMessage(), r.uuid());
            } else {
                logger.warn("The AtmosphereResource {} hasn't been suspended yet.",
                        r.uuid(), e);
            }

            if (DefaultBroadcaster.class.isAssignableFrom(broadcaster.getClass())) {
                DefaultBroadcaster.class.cast(broadcaster).cacheLostMessage(r, true);
            }
            AtmosphereResourceImpl.class.cast(r)._destroy();
            return;
        }

        synchronized (cr) {
            try {
                // This is required when you change the response's type
                String m = null;

                if (request.getAttribute(FrameworkConfig.EXPECTED_CONTENT_TYPE) != null) {
                    m = (String) request.getAttribute(FrameworkConfig.EXPECTED_CONTENT_TYPE);
                }

                if (m == null || m.equalsIgnoreCase("text/event-stream")) {
                    if (cr.getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE) != null) {
                        m = cr.getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE).toString();
                    }

                    if (m == null || m.equalsIgnoreCase("application/octet-stream")) {
                        m = r.getAtmosphereConfig().getInitParameter(ApplicationConfig.SSE_CONTENT_TYPE);
                        if (m == null) {
                            m = "text/plain";
                        }
                    }
                }

                if (e.getMessage() instanceof Response) {
                    cr.setResponse((Response) e.getMessage());
                    cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m);
                    cr.write();
                    try {
                        cr.getOutputStream().flush();
                    } catch (IOException ex) {
                        logger.trace("", ex);
                    }
                } else if (e.getMessage() instanceof List) {
                    for (Object msg : (List<Object>) e.getMessage()) {
                        cr.setResponse(Response.ok(msg).build());
                        cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m);
                        cr.write();
                    }

                    // https://github.com/Atmosphere/atmosphere/issues/169
                    try {
                        cr.getOutputStream().flush();
                    } catch (IOException ex) {
                        logger.trace("", ex);
                    }
                } else {
                    if (e.getMessage() == null) {
                        logger.warn("Broadcasted message is null");
                        return;
                    }

                    cr.setResponse(Response.ok(e.getMessage()).build());
                    cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m);
                    cr.write();
                    try {
                        cr.getOutputStream().flush();
                    } catch (IOException ex) {
                        logger.trace("", ex);
                    }
                }
            } catch (Throwable t) {
                boolean notifyAndCache = true;
                logger.trace("Unexpected exception for AtmosphereResource {} and Broadcaster {}", r.uuid(), broadcaster.getID());
                if (isJetty(r)) {
                    for (StackTraceElement element : t.getStackTrace()) {
                        if (element.getClassName().equals("java.io.BufferedWriter")
                                && element.getMethodName().equals("flush")) {
                            logger.trace("Workaround issue https://github.com/Atmosphere/atmosphere/issues/710");
                            notifyAndCache = false;
                        }
                    }
                }

                if (DefaultBroadcaster.class.isAssignableFrom(broadcaster.getClass())) {
                    DefaultBroadcaster.class.cast(broadcaster).onException(t, r, notifyAndCache);
                } else {
                    onException(t, r);
                }
            } finally {
                if (cr != null) {
                    cr.setEntity(null);
                }

                Boolean resumeOnBroadcast = (Boolean) request.getAttribute(ApplicationConfig.RESUME_ON_BROADCAST);
                if (resumeOnBroadcast != null && resumeOnBroadcast) {

                    String uuid = (String) request.getAttribute(AtmosphereFilter.RESUME_UUID);
                    if (uuid != null) {
                        if (request.getAttribute(AtmosphereFilter.RESUME_CANDIDATES) != null) {
                            ((ConcurrentHashMap<String, AtmosphereResource>) request.getAttribute(AtmosphereFilter.RESUME_CANDIDATES)).remove(uuid);
                        }
                    }
                    r.getRequest().setAttribute(FrameworkConfig.CONTAINER_RESPONSE, null);
                    r.resume();
                }
View Full Code Here

            public void onReady(Results.Chunks.Out<String> oout) {
                out = oout;
                boolean keepAlive = false;

                try {
                    final AtmosphereRequest r = AtmosphereUtils.request(request, additionalAttributes);
                    out.onDisconnected(new F.Callback0() {
                        @Override
                        public void invoke() throws Throwable {
                            _close(r);
                        }
View Full Code Here

        int port = uri == null ? 0 : uri.getPort();
        String uriString = uri == null ? request.remoteAddress() : uri.toString();
        String host = uri == null ? request.remoteAddress() : uri.getHost();
        AtmosphereRequest.Builder requestBuilder = new AtmosphereRequest.Builder();
        AtmosphereRequest r = requestBuilder.requestURI(url.substring(l))
                .requestURL(u)
                .pathInfo(url.substring(l))
                .headers(getHeaders(request))
                .method(method)
                .contentType(ct)
View Full Code Here

TOP

Related Classes of org.atmosphere.cpr.AtmosphereRequest$LazyComputation

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.