Search in sources :

Example 36 with AtmosphereRequest

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

the class PathTest method testPathVar.

@Test
public void testPathVar() throws IOException, ServletException {
    instanceCount = 0;
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/pathVar/aaa/pathTest/b123").method("GET").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertEquals(instanceCount, 1);
    assertNotNull(r.get());
    assertEquals(r.get(), "aaa#b123");
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) Test(org.testng.annotations.Test)

Example 37 with AtmosphereRequest

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

the class PathTest method testAtmosphereResourceInjection.

@Test
public void testAtmosphereResourceInjection() throws IOException, ServletException {
    instanceCount = 0;
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/resource/b123").method("GET").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertEquals(instanceCount, 1);
    assertNotNull(r.get());
    assertEquals(r.get(), "/resource/b123");
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) Test(org.testng.annotations.Test)

Example 38 with AtmosphereRequest

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

the class PathTest method testAtmosphereResourceEventInjection.

@Test(enabled = true)
public void testAtmosphereResourceEventInjection() throws IOException, ServletException {
    instanceCount = 0;
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/resourceEvent/b123").method("GET").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertEquals(instanceCount, 1);
    assertNotNull(r.get());
    assertEquals(r.get(), "/resourceEvent/b123");
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) Test(org.testng.annotations.Test)

Example 39 with AtmosphereRequest

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

the class PathTest method testNamedInjection.

@Test
public void testNamedInjection() throws IOException, ServletException {
    instanceCount = 0;
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/inject/b123").method("GET").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertEquals(instanceCount, 1);
    assertNotNull(r.get());
    assertEquals(r.get(), "/b123");
}
Also used : AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) Test(org.testng.annotations.Test)

Example 40 with AtmosphereRequest

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

the class IOUtils method readEntirelyAsByte.

public static byte[] readEntirelyAsByte(AtmosphereResource r) throws IOException {
    AtmosphereRequest request = r.getRequest();
    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 new byte[0];
    }
    AtmosphereRequestImpl.Body body = request.body();
    if (request.body().isEmpty()) {
        BufferedInputStream bufferedStream = null;
        ByteArrayOutputStream bbIS = new ByteArrayOutputStream();
        try {
            try {
                InputStream inputStream = request.getInputStream();
                if (inputStream != null) {
                    bufferedStream = new BufferedInputStream(inputStream);
                }
            } catch (IllegalStateException ex) {
                logger.trace("", ex);
                Reader reader = request.getReader();
                if (reader != null) {
                    bufferedStream = new BufferedInputStream(new ReaderInputStream(reader));
                }
            }
            if (bufferedStream != null) {
                byte[] bytes = new byte[8192];
                int bytesRead = 0;
                while (bytesRead != -1) {
                    bytesRead = bufferedStream.read(bytes);
                    if (bytesRead > 0)
                        bbIS.write(bytes, 0, bytesRead);
                }
            } else {
                bbIS.write("".getBytes());
            }
        } finally {
            if (bufferedStream != null) {
                try {
                    bufferedStream.close();
                } catch (IOException ex) {
                    logger.warn("", ex);
                }
            }
        }
        return bbIS.toByteArray();
    } else if (body.hasString()) {
        try {
            return readEntirelyAsString(r).toString().getBytes(request.getCharacterEncoding());
        } catch (UnsupportedEncodingException e) {
            logger.error("", e);
        }
    } else if (body.hasBytes()) {
        return Arrays.copyOfRange(body.asBytes(), body.byteOffset(), body.byteOffset() + body.byteLength());
    }
    throw new IllegalStateException("No body " + r);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AtmosphereRequestImpl(org.atmosphere.runtime.AtmosphereRequestImpl) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) BufferedInputStream(java.io.BufferedInputStream)

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