Search in sources :

Example 1 with AtmosphereResourceImpl

use of org.atmosphere.cpr.AtmosphereResourceImpl in project atmosphere-play by Atmosphere.

the class AtmosphereCoordinator method route.

public boolean route(AtmosphereRequest request, AtmosphereResponse response) throws IOException {
    boolean resumeOnBroadcast = false;
    boolean keptOpen = true;
    boolean skipClose = false;
    final PlayAsyncIOWriter w = PlayAsyncIOWriter.class.cast(response.getAsyncIOWriter());
    try {
        Action a = framework.doCometSupport(request, response);
        final AtmosphereResourceImpl impl = (AtmosphereResourceImpl) request.getAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE);
        String transport = (String) request.getAttribute(FrameworkConfig.TRANSPORT_IN_USE);
        if (transport == null) {
            transport = request.getHeader(X_ATMOSPHERE_TRANSPORT);
        }
        if (a.type() == Action.TYPE.SUSPEND) {
            if (transport.equalsIgnoreCase(HeaderConfig.LONG_POLLING_TRANSPORT) || transport.equalsIgnoreCase(HeaderConfig.JSONP_TRANSPORT)) {
                resumeOnBroadcast = true;
            }
        } else {
            keptOpen = false;
        }
        logger.trace("Transport {} resumeOnBroadcast {}", transport, resumeOnBroadcast);
        final Action action = (Action) request.getAttribute(NettyCometSupport.SUSPEND);
        if (action != null && action.type() == Action.TYPE.SUSPEND && action.timeout() != -1) {
            final AtomicReference<Future<?>> f = new AtomicReference<Future<?>>();
            f.set(suspendTimer.scheduleAtFixedRate(new Runnable() {

                @Override
                public void run() {
                    if (!w.isClosed() && (System.currentTimeMillis() - w.lastTick()) > action.timeout()) {
                        asynchronousProcessor.endRequest(impl, false);
                        f.get().cancel(true);
                    }
                }
            }, action.timeout(), action.timeout(), TimeUnit.MILLISECONDS));
        } else if (action != null && action.type() == Action.TYPE.RESUME) {
            resumeOnBroadcast = false;
        }
        w.resumeOnBroadcast(resumeOnBroadcast);
    } catch (Throwable e) {
        logger.error("Unable to process request", e);
        keptOpen = false;
    } finally {
        if (w != null && !resumeOnBroadcast && !keptOpen) {
            if (!skipClose) {
                w.close((AtmosphereResponse) null);
            }
        }
    }
    return keptOpen;
}
Also used : Action(org.atmosphere.cpr.Action) Future(java.util.concurrent.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl)

Example 2 with AtmosphereResourceImpl

use of org.atmosphere.cpr.AtmosphereResourceImpl in project cxf by apache.

the class DefaultProtocolInterceptorTest method testCreateResponseWithHeadersFiltering.

@Test
public void testCreateResponseWithHeadersFiltering() throws Exception {
    DefaultProtocolInterceptor dpi = new DefaultProtocolInterceptor();
    AtmosphereRequest request = AtmosphereRequestImpl.newInstance();
    AtmosphereResponse response = AtmosphereResponseImpl.newInstance();
    AtmosphereResourceImpl resource = new AtmosphereResourceImpl();
    resource.transport(AtmosphereResource.TRANSPORT.WEBSOCKET);
    request.localAttributes().put(FrameworkConfig.ATMOSPHERE_RESOURCE, resource);
    response.request(request);
    String payload = "hello cxf";
    String contentType = "text/plain";
    response.headers().put("Content-Type", contentType);
    byte[] transformed = dpi.createResponse(response, payload.getBytes(), true);
    verifyTransformed("200", new String[] { "Content-Type", contentType }, payload, transformed);
    response.headers().put("X-fruit", "peach");
    response.headers().put("X-vegetable", "tomato");
    transformed = dpi.createResponse(response, payload.getBytes(), true);
    verifyTransformed("200", new String[] { "Content-Type", contentType }, payload, transformed);
    dpi.includedheaders("X-f.*");
    transformed = dpi.createResponse(response, payload.getBytes(), true);
    verifyTransformed("200", new String[] { "Content-Type", contentType, "X-Fruit", "peach" }, payload, transformed);
    dpi.includedheaders("X-.*");
    transformed = dpi.createResponse(response, payload.getBytes(), true);
    verifyTransformed("200", new String[] { "Content-Type", contentType, "X-Fruit", "peach", "X-vegetable", "tomato" }, payload, transformed);
    dpi.excludedheaders(".*able");
    transformed = dpi.createResponse(response, payload.getBytes(), true);
    verifyTransformed("200", new String[] { "Content-Type", contentType, "X-Fruit", "peach" }, payload, transformed);
}
Also used : AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) Test(org.junit.Test)

Aggregations

AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)2 Future (java.util.concurrent.Future)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Action (org.atmosphere.cpr.Action)1 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)1 AtmosphereResponse (org.atmosphere.cpr.AtmosphereResponse)1 Test (org.junit.Test)1