use of org.atmosphere.runtime.AtmosphereResourceImpl 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;
}
use of org.atmosphere.runtime.AtmosphereResourceImpl 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));
}
use of org.atmosphere.runtime.AtmosphereResourceImpl in project atmosphere by Atmosphere.
the class DefaultWebSocketProcessor method invokeInterceptors.
private void invokeInterceptors(WebSocketHandlerProxy webSocketHandler, WebSocket webSocket, Object webSocketMessageAsBody, int offset, int length) throws IOException {
AtmosphereResourceImpl resource = AtmosphereResourceImpl.class.cast(webSocket.resource());
if (resource == null) {
return;
}
AtmosphereRequest request = resource.getRequest(false);
if (String.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
request.body(String.class.cast(webSocketMessageAsBody));
} else if (Reader.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
request.body(Reader.class.cast(webSocketMessageAsBody));
} else if (InputStream.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
request.body(InputStream.class.cast(webSocketMessageAsBody));
} else {
request.body(new ByteArrayInputStream((byte[]) webSocketMessageAsBody, offset, length));
}
String path = webSocketHandler.proxied.getClass().isAnnotationPresent(WebSocketHandlerService.class) ? webSocketHandler.proxied.getClass().getAnnotation(WebSocketHandlerService.class).path() : "/";
AtmosphereFramework.AtmosphereHandlerWrapper w = framework.getAtmosphereHandlers().get(framework.normalizePath(path));
List<AtmosphereInterceptor> l;
if (w == null) {
l = framework.interceptors();
} else {
l = w.interceptors;
}
try {
// Globally defined
int tracing = 0;
Action a = asynchronousProcessor.invokeInterceptors(l, resource, tracing);
if (a.type() != Action.TYPE.CONTINUE && a.type() != Action.TYPE.SKIP_ATMOSPHEREHANDLER) {
return;
}
//Unit test mock the request and will throw NPE.
boolean skipAtmosphereHandler = request.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) != null ? (Boolean) request.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) : Boolean.FALSE;
if (!skipAtmosphereHandler) {
try {
if (String.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
webSocketHandler.onTextMessage(webSocket, String.class.cast(webSocketMessageAsBody));
} else if (Reader.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
WebSocketStreamingHandler.class.cast(webSocketHandler.proxied).onTextStream(webSocket, Reader.class.cast(webSocketMessageAsBody));
} else if (InputStream.class.isAssignableFrom(webSocketMessageAsBody.getClass())) {
WebSocketStreamingHandler.class.cast(webSocketHandler.proxied()).onBinaryStream(webSocket, InputStream.class.cast(webSocketMessageAsBody));
} else {
webSocketHandler.onByteMessage(webSocket, (byte[]) webSocketMessageAsBody, offset, length);
}
} catch (IOException t) {
resource.onThrowable(t);
throw t;
}
}
request.setAttribute(SKIP_ATMOSPHEREHANDLER.name(), Boolean.FALSE);
} finally {
asynchronousProcessor.postInterceptors(l, resource);
}
}
use of org.atmosphere.runtime.AtmosphereResourceImpl in project atmosphere by Atmosphere.
the class ManagedAtmosphereHandlerTest method create.
@BeforeMethod
public void create() throws Throwable {
framework = new AtmosphereFramework();
framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
framework.addAnnotationPackage(BroadcasterCacheTest.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;
}
});
}
use of org.atmosphere.runtime.AtmosphereResourceImpl in project atmosphere by Atmosphere.
the class CustomAnnotationTest method create.
@BeforeMethod
public void create() throws Throwable {
framework = new AtmosphereFramework();
framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
framework.addAnnotationPackage(MyAnnotation.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;
}
});
}
Aggregations