Search in sources :

Example 51 with AtmosphereRequest

use of org.atmosphere.runtime.AtmosphereRequest 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 52 with AtmosphereRequest

use of org.atmosphere.runtime.AtmosphereRequest 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 53 with AtmosphereRequest

use of org.atmosphere.runtime.AtmosphereRequest 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)

Example 54 with AtmosphereRequest

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

the class StreamingHttpProtocol method onBinaryStream.

@Override
public List<AtmosphereRequest> onBinaryStream(WebSocket webSocket, InputStream stream) {
    //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).inputStream(stream).build());
    return list;
}
Also used : AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) ArrayList(java.util.ArrayList) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl)

Example 55 with AtmosphereRequest

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

the class EncoderDecoderTest method create.

@BeforeMethod
public void create() throws Throwable {
    framework = new AtmosphereFramework();
    framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
    framework.addAnnotationPackage(ManagedMessage.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(new ServletConfig() {

        @Override
        public String getServletName() {
            return "void";
        }

        @Override
        public ServletContext getServletContext() {
            return mock(ServletContext.class);
        }

        @Override
        public String getInitParameter(String name) {
            return null;
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return null;
        }
    });
    latch.set(new CountDownLatch(1));
}
Also used : AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) Enumeration(java.util.Enumeration) ServletConfig(javax.servlet.ServletConfig) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) SimpleBroadcaster(org.atmosphere.util.SimpleBroadcaster) ServletException(javax.servlet.ServletException) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AsynchronousProcessor(org.atmosphere.runtime.AsynchronousProcessor) AtmosphereFramework(org.atmosphere.runtime.AtmosphereFramework) ServletContext(javax.servlet.ServletContext) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)76 Test (org.testng.annotations.Test)39 AtmosphereRequestImpl (org.atmosphere.runtime.AtmosphereRequestImpl)38 IOException (java.io.IOException)23 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)20 AtmosphereResponse (org.atmosphere.runtime.AtmosphereResponse)18 ServletException (javax.servlet.ServletException)10 AsynchronousProcessor (org.atmosphere.runtime.AsynchronousProcessor)8 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)7 AtmosphereResource (org.atmosphere.runtime.AtmosphereResource)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 AsyncIOWriter (org.atmosphere.runtime.AsyncIOWriter)6 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 ArrayList (java.util.ArrayList)5 AtmosphereInterceptorWriter (org.atmosphere.runtime.AtmosphereInterceptorWriter)5 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)5 Reader (java.io.Reader)4 Enumeration (java.util.Enumeration)4 ServletConfig (javax.servlet.ServletConfig)4