Search in sources :

Example 11 with AtmosphereResourceImpl

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

the class AbstractReflectorAtmosphereHandler method postStateChange.

/**
     * Inspect the event and decide if the underlying connection must be resumed.
     *
     * @param event
     */
protected final void postStateChange(AtmosphereResourceEvent event) {
    if (event.isCancelled() || event.isResuming())
        return;
    AtmosphereResourceImpl r = AtmosphereResourceImpl.class.cast(event.getResource());
    // Between event.isCancelled and resource, the connection has been remotly closed.
    if (r == null) {
        logger.trace("Event {} returned a null AtmosphereResource", event);
        return;
    }
    Boolean resumeOnBroadcast = r.resumeOnBroadcast();
    if (!resumeOnBroadcast) {
        // For legacy reason, check the attribute as well
        Object o = r.getRequest(false).getAttribute(ApplicationConfig.RESUME_ON_BROADCAST);
        if (o != null && Boolean.class.isAssignableFrom(o.getClass())) {
            resumeOnBroadcast = Boolean.class.cast(o);
        }
    }
    if (resumeOnBroadcast != null && resumeOnBroadcast) {
        r.resume();
    }
}
Also used : AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Example 12 with AtmosphereResourceImpl

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

the class AtmosphereResourceLifecycleInterceptor method postInspect.

@Override
public void postInspect(final AtmosphereResource r) {
    if (r.transport().equals(UNDEFINED) || Utils.webSocketMessage(r) || r.transport().equals(POLLING))
        return;
    AtmosphereResourceImpl impl = AtmosphereResourceImpl.class.cast(r);
    if ((force || impl.getRequest(false).getMethod().equalsIgnoreCase(method)) && !impl.action().equals(Action.CANCELLED) && impl.isInScope()) {
        logger.trace("Marking AtmosphereResource {} for suspend operation", r.uuid());
        r.addEventListener(new OnBroadcast() {

            @Override
            public void onBroadcast(AtmosphereResourceEvent event) {
                switch(r.transport()) {
                    case JSONP:
                    case AJAX:
                    case LONG_POLLING:
                        break;
                    default:
                        try {
                            r.getResponse().flushBuffer();
                        } catch (IOException e) {
                            logger.trace("", e);
                        }
                        break;
                }
            }
        }).suspend(timeoutInMilli);
    }
}
Also used : OnBroadcast(org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter.OnBroadcast) AtmosphereResourceEvent(org.atmosphere.runtime.AtmosphereResourceEvent) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) IOException(java.io.IOException)

Example 13 with AtmosphereResourceImpl

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

the class SimpleHttpProtocol method onMessage.

@Override
public List<AtmosphereRequest> onMessage(WebSocket webSocket, String message) {
    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);
    request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.SIMPLE_HTTP_OVER_WEBSOCKET);
    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);
        if (bodyBeginIndex != -1) {
            pathInfo = message.substring(delimiterLength, bodyBeginIndex);
            requestURI += pathInfo;
            message = message.substring(bodyBeginIndex + delimiterLength);
        }
    }
    List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
    list.add(constructRequest(webSocket, pathInfo, requestURI, methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).body(message).build());
    return list;
}
Also used : AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) ArrayList(java.util.ArrayList) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Example 14 with AtmosphereResourceImpl

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

the class SimpleHttpProtocol method onMessage.

@Override
public List<AtmosphereRequest> onMessage(WebSocket webSocket, byte[] d, final int offset, final int length) {
    //Converting to a string and delegating to onMessage(WebSocket webSocket, String d) causes issues because the binary data may not be a valid string.
    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);
    request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.SIMPLE_HTTP_OVER_WEBSOCKET);
    if (!resource.isInScope())
        return Collections.emptyList();
    List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
    list.add(constructRequest(webSocket, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).body(d, offset, length).build());
    return list;
}
Also used : AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) ArrayList(java.util.ArrayList) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Example 15 with AtmosphereResourceImpl

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

the class StreamingHttpProtocol method onTextStream.

@Override
public List<AtmosphereRequest> onTextStream(WebSocket webSocket, Reader r) {
    //Converting to a string and delegating to onMessage(WebSocket webSocket, String d) causes issues because the binary data may not be a valid string.
    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();
    request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.STREAMING_HTTP_OVER_WEBSOCKET);
    List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
    list.add(constructRequest(webSocket, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).reader(r).build());
    return list;
}
Also used : AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) ArrayList(java.util.ArrayList) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Aggregations

AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)21 AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)15 IOException (java.io.IOException)13 AtmosphereResponse (org.atmosphere.runtime.AtmosphereResponse)11 ServletException (javax.servlet.ServletException)8 AsynchronousProcessor (org.atmosphere.runtime.AsynchronousProcessor)7 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)7 BeforeMethod (org.testng.annotations.BeforeMethod)6 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)5 ArrayList (java.util.ArrayList)4 Enumeration (java.util.Enumeration)4 ServletConfig (javax.servlet.ServletConfig)4 ServletContext (javax.servlet.ServletContext)4 Action (org.atmosphere.runtime.Action)3 AtmosphereResource (org.atmosphere.runtime.AtmosphereResource)3 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 AtmosphereInterceptor (org.atmosphere.runtime.AtmosphereInterceptor)2 AtmosphereMappingException (org.atmosphere.runtime.AtmosphereMappingException)2 AtmosphereResourceEventImpl (org.atmosphere.runtime.AtmosphereResourceEventImpl)2