Package com.sun.jersey.spi.container

Examples of com.sun.jersey.spi.container.ContainerResponse


    public final static void broadcast(final AtmosphereResource<?,?> r, final AtmosphereResourceEvent e) {
        HttpServletRequest res = (HttpServletRequest) r.getRequest();

        try {
            ContainerResponse cr = (ContainerResponse) res.getAttribute(AtmosphereServlet.CONTAINER_RESPONSE);

            if (cr == null) {
                logger.debug("Retrieving HttpServletRequest {} with ContainerResponse {}", res, cr);
                logger.error("Unexpected state. ContainerResponse cannot be null. The connection hasn't been suspended yet");
                return;
            }

            MediaType m = (MediaType) cr.getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
            if (e.getMessage() instanceof Response) {
                cr.setResponse((Response) e.getMessage());
                cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m);
                cr.write();
            } 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();
                    cr.getOutputStream().flush();
                }
            } else {
                cr.setResponse(Response.ok(e.getMessage()).build());
                cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m);
                cr.write();
            }
            cr.getOutputStream().flush();
        } catch (Throwable t) {
            onException(t, r);
        } finally {
            Boolean resumeOnBroadcast = (Boolean) res.getAttribute(AtmosphereServlet.RESUME_ON_BROADCAST);
            if (resumeOnBroadcast != null && resumeOnBroadcast) {
View Full Code Here


            logger.debug("Base URI: " + baseUri);
            logger.debug("Complete URI: " + completeUri);
        }

        MuleResponseWriter writer = new MuleResponseWriter(message);
        ContainerResponse res = new ContainerResponse(application, req, writer);

        application.handleRequest(req, res);

        return writer.getResponse();
    }
View Full Code Here

                base, u,
                new InBoundHeaders(), new ByteArrayInputStream(new byte[0]));
        _request.setSecurityContext(request.getSecurityContext());

        // Propagate security context
        final ContainerResponse _response = new ContainerResponse(app,
                _request, null);

        return new WebApplicationContext(app,
                _request,
                _response);
View Full Code Here

                if (cacheResponse.isPresent()) {
                    // Throw an exception to try and prevent other dispatchers, plugins, etc from modifying the response
                    throw new WebApplicationException(cacheResponse.get());
                } else {
                    ContainerResponse response = (ContainerResponse) context.getResponse();
                    response.setContainerResponseWriter(new CachingResponseWriter(response.getContainerResponseWriter(), request, _cache, _cacheControlHeader));
                    _dispatcher.dispatch(resource, context);
                    context.getResponse().getHttpHeaders().add(VARY, _varyHeader);
                }
            } catch (Exception ex) {
                throw Throwables.propagate(ex);
View Full Code Here

                if (cacheResponse.isPresent()) {
                    // Throw an exception to try and prevent other dispatchers, plugins, etc from modifying the response
                    throw new WebApplicationException(cacheResponse.get());
                } else {
                    ContainerResponse response = (ContainerResponse) context.getResponse();
                    response.setContainerResponseWriter(new CachingResponseWriter(response.getContainerResponseWriter(), request, _cache, _cacheControlHeader));
                    _dispatcher.dispatch(resource, context);
                    context.getResponse().getHttpHeaders().add(VARY, _varyHeader);
                }
            } catch (Exception ex) {
                throw Throwables.propagate(ex);
View Full Code Here

    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) {
View Full Code Here

    }

    @Override
    public void handleRequest(ContainerRequest request, ContainerResponseWriter responseWriter)
            throws IOException {
        final ContainerResponse response = new ContainerResponse(
                this,
                request,
                responseWriter);
        handleRequest(request, response);
    }
View Full Code Here

    }

    @Override
    public void handleRequest(ContainerRequest request, ContainerResponseWriter responseWriter)
            throws IOException {
        final ContainerResponse response = new ContainerResponse(
                this,
                request,
                responseWriter);
        handleRequest(request, response);
    }
View Full Code Here

                base, u,
                new InBoundHeaders(), new ByteArrayInputStream(new byte[0]));
        _request.setSecurityContext(request);

        // Propagate security context
        final ContainerResponse _response = new ContainerResponse(app,
                _request, null);

        return new WebApplicationContext(app,
                _request,
                _response);
View Full Code Here

        return bodyFactory;
    }

    public void handleRequest(ContainerRequest request, ContainerResponseWriter responseWriter)
            throws IOException {
        final ContainerResponse response = new ContainerResponse(
                this,
                request,
                responseWriter);
        handleRequest(request, response);
    }
View Full Code Here

TOP

Related Classes of com.sun.jersey.spi.container.ContainerResponse

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.