Search in sources :

Example 11 with AtmosphereResource

use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.

the class ProtocolUtil method constructRequest.

protected static AtmosphereRequestImpl.Builder constructRequest(WebSocket webSocket, String pathInfo, String requestURI, String methodType, String contentType, boolean destroyable) {
    AtmosphereResource resource = webSocket.resource();
    AtmosphereRequest request = AtmosphereResourceImpl.class.cast(resource).getRequest(false);
    Map<String, Object> m = attributes(webSocket, request);
    // We need to create a new AtmosphereRequest as WebSocket message may arrive concurrently on the same connection.
    AtmosphereRequestImpl.Builder b = (new AtmosphereRequestImpl.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;
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Example 12 with AtmosphereResource

use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.

the class AnnotationScanningTest method create.

@BeforeMethod
public void create() throws Throwable {
    framework = new AtmosphereFramework();
    framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
    framework.addAnnotationPackage(AnnotationScanningTest.class);
    framework.setAsyncSupport(new AsynchronousProcessor(framework.getAtmosphereConfig()) {

        @Override
        public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
            return suspended(req, res);
        }

        public void action(AtmosphereResourceImpl r) {
            try {
                resumed(r.getRequest(), r.getResponse());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }
    }).init().addAtmosphereHandler("/a", new AtmosphereHandlerAdapter() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            resource.suspend();
        }
    });
}
Also used : SimpleBroadcaster(org.atmosphere.util.SimpleBroadcaster) ServletException(javax.servlet.ServletException) AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) Action(org.atmosphere.runtime.Action) AtmosphereHandlerAdapter(org.atmosphere.handler.AtmosphereHandlerAdapter) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) AsynchronousProcessor(org.atmosphere.runtime.AsynchronousProcessor) AtmosphereFramework(org.atmosphere.runtime.AtmosphereFramework) IOException(java.io.IOException) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 13 with AtmosphereResource

use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.

the class ExcludeSessionBroadcaster method broadcast.

/**
     * the AtmosphereResource r will be exclude for this broadcast
     *
     * @param msg
     * @param r
     * @return
     */
@Override
public Future<Object> broadcast(Object msg, AtmosphereResource r) {
    if (destroyed.get()) {
        throw new IllegalStateException("This Broadcaster has been destroyed and cannot be used");
    }
    Set<AtmosphereResource> sub = new HashSet<AtmosphereResource>();
    sub.addAll(resources);
    sub.remove(r);
    start();
    Object newMsg = filter(msg);
    if (newMsg == null) {
        return null;
    }
    BroadcasterFuture<Object> f = new BroadcasterFuture<Object>(newMsg, sub.size());
    dispatchMessages(new Deliver(newMsg, sub, f, msg));
    return f;
}
Also used : Deliver(org.atmosphere.runtime.Deliver) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) BroadcasterFuture(org.atmosphere.runtime.BroadcasterFuture) HashSet(java.util.HashSet)

Example 14 with AtmosphereResource

use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.

the class ExcludeSessionBroadcaster method broadcast.

/**
     * session will be exclude for this broadcast
     *
     * @param msg
     * @param s
     * @return
     */
public Future<Object> broadcast(Object msg, HttpSession s) {
    if (destroyed.get()) {
        return futureDone(msg);
    }
    Set<AtmosphereResource> subset = new HashSet<AtmosphereResource>();
    subset.addAll(resources);
    for (AtmosphereResource r : resources) {
        if (!r.getAtmosphereResourceEvent().isCancelled() && s.equals(r.getRequest().getSession())) {
            subset.remove(r);
        }
    }
    start();
    Object newMsg = filter(msg);
    if (newMsg == null) {
        return futureDone(msg);
    }
    BroadcasterFuture<Object> f = new BroadcasterFuture<Object>(newMsg, subset.size());
    dispatchMessages(new Deliver(newMsg, subset, f, msg));
    return f;
}
Also used : Deliver(org.atmosphere.runtime.Deliver) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) BroadcasterFuture(org.atmosphere.runtime.BroadcasterFuture) HashSet(java.util.HashSet)

Example 15 with AtmosphereResource

use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.

the class ManagedAtmosphereHandlerTest method testHeartbeat.

@Test
public void testHeartbeat() throws IOException, ServletException {
    // Open connection
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/heartbeat").method("GET").build();
    request.header(X_ATMOSPHERE_TRANSPORT, WEBSOCKET_TRANSPORT);
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    // Check suspend
    final AtmosphereResource res = r.get();
    assertNotNull(res);
    // Send heartbeat
    request = new AtmosphereRequestImpl.Builder().pathInfo("/heartbeat").method("POST").body(Heartbeat.paddingData).build();
    request.header(X_ATMOSPHERE_TRANSPORT, WEBSOCKET_TRANSPORT);
    request.setAttribute(HeartbeatInterceptor.INTERCEPTOR_ADDED, "");
    res.initialize(res.getAtmosphereConfig(), res.getBroadcaster(), request, AtmosphereResponseImpl.newInstance(), framework.getAsyncSupport(), res.getAtmosphereHandler());
    request.setAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE, res);
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertNotNull(message.get());
    assertEquals(message.get(), Heartbeat.paddingData);
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) Test(org.testng.annotations.Test)

Aggregations

AtmosphereResource (org.atmosphere.runtime.AtmosphereResource)19 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)8 AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)7 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 AsynchronousProcessor (org.atmosphere.runtime.AsynchronousProcessor)3 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)3 AtmosphereResourceEventImpl (org.atmosphere.runtime.AtmosphereResourceEventImpl)3 BroadcasterFuture (org.atmosphere.runtime.BroadcasterFuture)3 Deliver (org.atmosphere.runtime.Deliver)3 ArrayList (java.util.ArrayList)2 Future (java.util.concurrent.Future)2 ServletException (javax.servlet.ServletException)2 Action (org.atmosphere.runtime.Action)2 AtmosphereRequestImpl (org.atmosphere.runtime.AtmosphereRequestImpl)2 AtmosphereResponse (org.atmosphere.runtime.AtmosphereResponse)2 BroadcasterConfig (org.atmosphere.runtime.BroadcasterConfig)2 BroadcasterLifeCyclePolicy (org.atmosphere.runtime.BroadcasterLifeCyclePolicy)2 WebSocketEvent (org.atmosphere.websocket.WebSocketEventListener.WebSocketEvent)2 LinkedList (java.util.LinkedList)1