Search in sources :

Example 66 with ServletInputStream

use of javax.servlet.ServletInputStream in project nutz by nutzam.

the class UploadingUnitTest method test_cast_dt01.

@Test
public void test_cast_dt01() throws UploadException {
    MockHttpServletRequest req = Mock.servlet.request();
    req.setHeader("content-type", "multipart/form-data; boundary=----ESDT-321271401654cc6d669eef664aac");
    Uploading up = UploadUnit.TYPE.born();
    ServletInputStream ins = Mock.servlet.ins("org/nutz/mvc/upload/files/cast_dt01");
    req.setInputStream(ins);
    req.init();
    Map<String, Object> map = up.parse(req, UploadingContext.create(tmps));
    assertEquals(1, map.size());
    assertEquals("Shapes100.jpg", ((TempFile) map.get("fileData")).getSubmittedFileName());
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Uploading(org.nutz.mvc.upload.Uploading) Test(org.junit.Test)

Example 67 with ServletInputStream

use of javax.servlet.ServletInputStream in project OpenRefine by OpenRefine.

the class RefineBrokerTests method call.

private JSONObject call(boolean successful, RefineBroker broker, HttpServletRequest request, HttpServletResponse response, String service, String... params) throws Exception {
    if (params != null) {
        for (int i = 0; i < params.length; ) {
            String name = params[i++];
            String value = params[i++];
            if ("data".equals(name)) {
                final ByteArrayInputStream inputStream = new ByteArrayInputStream(value.getBytes("UTF-8"));
                when(request.getInputStream()).thenReturn(new ServletInputStream() {

                    public int read() throws IOException {
                        return inputStream.read();
                    }
                });
            } else {
                when(request.getParameter(name)).thenReturn(value);
            }
        }
    }
    StringWriter writer = new StringWriter();
    when(response.getWriter()).thenReturn(new PrintWriter(writer));
    broker.process(service, request, response);
    JSONObject result = new JSONObject(writer.toString());
    if (successful) {
        assertJSON(result, "status", "ok");
    } else {
        assertJSON(result, "status", "error");
    }
    logger.info(result.toString());
    return result;
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 68 with ServletInputStream

use of javax.servlet.ServletInputStream in project undertow by undertow-io.

the class ServletWebSocketHttpExchange method readRequestData.

@Override
public IoFuture<byte[]> readRequestData() {
    final ByteArrayOutputStream data = new ByteArrayOutputStream();
    try {
        final ServletInputStream in = request.getInputStream();
        byte[] buf = new byte[1024];
        int r;
        while ((r = in.read(buf)) != -1) {
            data.write(buf, 0, r);
        }
        return new FinishedIoFuture<>(data.toByteArray());
    } catch (IOException e) {
        final FutureResult<byte[]> ioFuture = new FutureResult<>();
        ioFuture.setException(e);
        return ioFuture.getIoFuture();
    }
}
Also used : FinishedIoFuture(org.xnio.FinishedIoFuture) ServletInputStream(javax.servlet.ServletInputStream) FutureResult(org.xnio.FutureResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 69 with ServletInputStream

use of javax.servlet.ServletInputStream in project undertow by undertow-io.

the class BlockingInputStreamServlet method doPost.

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final ServletInputStream inputStream = req.getInputStream();
    byte[] buf = new byte[1024];
    int read;
    while ((read = inputStream.read(buf)) != -1) {
        out.write(buf, 0, read);
    }
    resp.getOutputStream().write(out.toByteArray());
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 70 with ServletInputStream

use of javax.servlet.ServletInputStream in project undertow by undertow-io.

the class EarlyCloseClientServlet method doPost.

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    try {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final ServletInputStream inputStream = req.getInputStream();
        byte[] buf = new byte[1024];
        int read;
        while ((read = inputStream.read(buf)) != -1) {
            out.write(buf, 0, read);
        }
        resp.getOutputStream().write(out.toByteArray());
        completedNormally = true;
    } catch (IOException e) {
        exceptionThrown = true;
    } finally {
        latch.countDown();
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

ServletInputStream (javax.servlet.ServletInputStream)76 IOException (java.io.IOException)50 Test (org.junit.Test)43 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 ServletException (javax.servlet.ServletException)41 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 CountDownLatch (java.util.concurrent.CountDownLatch)26 HttpServlet (javax.servlet.http.HttpServlet)20 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)20 InterruptedIOException (java.io.InterruptedIOException)18 AsyncContext (javax.servlet.AsyncContext)17 ReadListener (javax.servlet.ReadListener)17 ServletOutputStream (javax.servlet.ServletOutputStream)16 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)16 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)15 Request (org.eclipse.jetty.server.Request)14 Response (org.eclipse.jetty.client.api.Response)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 PrintWriter (java.io.PrintWriter)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8