use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class PathTest method testSingletonMeteorPath.
@Test
public void testSingletonMeteorPath() throws IOException, ServletException {
instanceCount = 0;
AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/singleton/meteor/test2").method("GET").build();
framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
assertEquals(instanceCount, 0);
assertNotNull(r.get());
assertEquals(r.get(), "/singleton/meteor/test2");
}
use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class AnnotationScanningTest method testAnnotation.
@Test
public void testAnnotation() throws IOException, ServletException {
AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").method("GET").build();
framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
AsynchronousProcessor.class.cast(framework.getAsyncSupport()).endRequest((AtmosphereResourceImpl) request.resource(), true);
assertTrue(suspended.get());
assertTrue(disconnected.get());
}
use of org.atmosphere.runtime.AtmosphereRequest 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();
}
});
}
use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class SSEAtmosphereInterceptorTest method testDataWriter.
@Test
public void testDataWriter() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ServletResponse resp = Mockito.mock(HttpServletResponse.class);
Mockito.when(resp.getOutputStream()).thenReturn(new ServletOutputStream() {
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
@Override
public void write(int b) throws IOException {
baos.write(b);
}
@Override
public void write(byte[] b) throws IOException {
baos.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
baos.write(b, off, len);
}
});
AtmosphereRequest request = AtmosphereRequestImpl.newInstance();
request.header(HeaderConfig.X_ATMOSPHERE_TRANSPORT, "SSE");
AtmosphereResponse response = AtmosphereResponseImpl.newInstance(request);
response.request(request);
response.setResponse(resp);
AtmosphereResourceImpl resource = new AtmosphereResourceImpl();
resource.initialize(framework.getAtmosphereConfig(), framework.getBroadcasterFactory().get(), request, response, Mockito.mock(AsyncSupport.class), null);
resource.suspend();
SSEAtmosphereInterceptor interceptor = new SSEAtmosphereInterceptor();
interceptor.configure(config);
interceptor.inspect(resource);
// no newline
response.write("Good Morning".getBytes());
assertEquals(baos.toString(), "data:Good Morning\r\n\r\n");
baos.reset();
// \n
response.write("Hello World!\nHave a nice day!".getBytes());
assertEquals(baos.toString(), "data:Hello World!\r\ndata:Have a nice day!\r\n\r\n");
baos.reset();
// \r
response.write("Hello World!\rHave a nice day!".getBytes());
assertEquals(baos.toString(), "data:Hello World!\r\ndata:Have a nice day!\r\n\r\n");
baos.reset();
// \r\n
response.write("Hello World!\r\nHave a nice day!".getBytes());
assertEquals(baos.toString(), "data:Hello World!\r\ndata:Have a nice day!\r\n\r\n");
}
use of org.atmosphere.runtime.AtmosphereRequest in project atmosphere by Atmosphere.
the class IOUtils method readEntirelyAsString.
public static StringBuilder readEntirelyAsString(AtmosphereResource r) throws IOException {
final StringBuilder stringBuilder = new StringBuilder();
boolean readGetBody = r.getAtmosphereConfig().getInitParameter(ApplicationConfig.READ_GET_BODY, false);
if (!readGetBody && AtmosphereResourceImpl.class.cast(r).getRequest(false).getMethod().equalsIgnoreCase("GET")) {
logger.debug("Blocking an I/O read operation from a GET request. To enable GET + body, set {} to true", ApplicationConfig.READ_GET_BODY);
return stringBuilder;
}
AtmosphereRequest request = r.getRequest();
if (request.body().isEmpty()) {
BufferedReader bufferedReader = null;
try {
try {
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, request.getCharacterEncoding()));
}
} catch (IllegalStateException ex) {
logger.trace("", ex);
Reader reader = request.getReader();
if (reader != null) {
bufferedReader = new BufferedReader(reader);
}
}
if (bufferedReader != null) {
char[] charBuffer = new char[8192];
int bytesRead = -1;
try {
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} catch (NullPointerException ex) {
// https://java.net/jira/browse/GRIZZLY-1676
}
} else {
stringBuilder.append("");
}
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
logger.warn("", ex);
}
}
}
} else {
AtmosphereRequestImpl.Body body = request.body();
try {
stringBuilder.append(body.hasString() ? body.asString() : new String(body.asBytes(), body.byteOffset(), body.byteLength(), request.getCharacterEncoding()));
} catch (UnsupportedEncodingException e) {
logger.error("", e);
}
}
return stringBuilder;
}
Aggregations