Search in sources :

Example 1 with BlockingHandler

use of io.undertow.server.handlers.BlockingHandler in project undertow by undertow-io.

the class FormDataParserTestCase method handlerChains.

@Parameterized.Parameters
public static Collection<Object[]> handlerChains() {
    List<Object[]> ret = new ArrayList<>();
    final FormParserFactory parserFactory = FormParserFactory.builder().build();
    HttpHandler fd = new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            final FormDataParser parser = parserFactory.createParser(exchange);
            parser.parse(new HttpHandler() {

                @Override
                public void handleRequest(final HttpServerExchange exchange) throws Exception {
                    FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);
                    Iterator<String> it = data.iterator();
                    while (it.hasNext()) {
                        String fd = it.next();
                        for (FormData.FormValue val : data.get(fd)) {
                            exchange.getResponseHeaders().add(new HttpString(fd), val.getValue());
                        }
                    }
                }
            });
        }
    };
    ret.add(new Object[] { fd });
    final BlockingHandler blocking = new BlockingHandler();
    blocking.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            final FormDataParser parser = parserFactory.createParser(exchange);
            try {
                FormData data = parser.parseBlocking();
                Iterator<String> it = data.iterator();
                while (it.hasNext()) {
                    String fd = it.next();
                    for (FormData.FormValue val : data.get(fd)) {
                        exchange.getResponseHeaders().add(new HttpString(fd), val.getValue());
                    }
                }
            } catch (IOException e) {
                exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
            }
        }
    });
    ret.add(new Object[] { blocking });
    return ret;
}
Also used : HttpHandler(io.undertow.server.HttpHandler) ArrayList(java.util.ArrayList) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) IOException(java.io.IOException) HttpServerExchange(io.undertow.server.HttpServerExchange) BlockingHandler(io.undertow.server.handlers.BlockingHandler) Iterator(java.util.Iterator) HttpString(io.undertow.util.HttpString)

Example 2 with BlockingHandler

use of io.undertow.server.handlers.BlockingHandler in project undertow by undertow-io.

the class MultipartFormDataParserTestCase method testFileUpload.

@Test
public void testFileUpload() throws Exception {
    DefaultServer.setRootHandler(new BlockingHandler(createHandler()));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        //post.setHeader(Headers.CONTENT_TYPE, MultiPartHandler.MULTIPART_FORM_DATA);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
        entity.addPart("file", new FileBody(new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile())));
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) BlockingHandler(io.undertow.server.handlers.BlockingHandler) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 3 with BlockingHandler

use of io.undertow.server.handlers.BlockingHandler in project undertow by undertow-io.

the class MultipartFormDataParserTestCase method testQuotedBoundary.

@Test
public void testQuotedBoundary() throws Exception {
    DefaultServer.setRootHandler(new BlockingHandler(createHandler()));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        post.setHeader(Headers.CONTENT_TYPE_STRING, "multipart/form-data; boundary=\"s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\"");
        StringEntity entity = new StringEntity("--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\r\n" + "Content-Disposition: form-data; name=\"formValue\"\r\n" + "\r\n" + "myValue\r\n" + "--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"uploadfile.txt\"\r\n" + "Content-Type: application/octet-stream\r\n" + "\r\n" + "file contents\r\n" + "\r\n" + "--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje--\r\n");
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BlockingHandler(io.undertow.server.handlers.BlockingHandler) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 4 with BlockingHandler

use of io.undertow.server.handlers.BlockingHandler in project undertow by undertow-io.

the class SimpleBlockingServerTestCase method setup.

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                if (exchange.getRequestMethod().equals(Methods.POST)) {
                    //for a post we just echo back what was sent
                    //we need to fully buffer it, as otherwise the send buffer fills up, and the client will still be blocked
                    //on writing and will never read
                    byte[] buffer = new byte[1024];
                    final ByteArrayOutputStream b = new ByteArrayOutputStream();
                    int r = 0;
                    final OutputStream outputStream = exchange.getOutputStream();
                    final InputStream inputStream = exchange.getInputStream();
                    while ((r = inputStream.read(buffer)) > 0) {
                        b.write(buffer, 0, r);
                    }
                    outputStream.write(b.toByteArray());
                    outputStream.close();
                } else {
                    if (exchange.getQueryParameters().containsKey("useFragmentedSender")) {
                        //we send it byte at a time
                        exchange.getResponseSender().send("", new IoCallback() {

                            int i = 0;

                            @Override
                            public void onComplete(final HttpServerExchange exchange, final Sender sender) {
                                if (i == message.length()) {
                                    sender.close();
                                    exchange.endExchange();
                                } else {
                                    sender.send("" + message.charAt(i++), this);
                                }
                            }

                            @Override
                            public void onException(final HttpServerExchange exchange, final Sender sender, final IOException exception) {
                                exchange.endExchange();
                            }
                        });
                    } else if (exchange.getQueryParameters().containsKey("useSender")) {
                        exchange.getResponseSender().send(message, IoCallback.END_EXCHANGE);
                    } else {
                        final OutputStream outputStream = exchange.getOutputStream();
                        outputStream.write(message.getBytes());
                        outputStream.close();
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) Sender(io.undertow.io.Sender) HttpHandler(io.undertow.server.HttpHandler) BlockingHandler(io.undertow.server.handlers.BlockingHandler) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IoCallback(io.undertow.io.IoCallback) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 5 with BlockingHandler

use of io.undertow.server.handlers.BlockingHandler in project undertow by undertow-io.

the class MaxRequestSizeTestCase method setup.

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            final OutputStream outputStream = exchange.getOutputStream();
            final InputStream inputStream = exchange.getInputStream();
            String m = HttpClientUtils.readResponse(inputStream);
            Assert.assertEquals(A_MESSAGE, m);
            inputStream.close();
            outputStream.close();
        }
    });
}
Also used : BlockingHandler(io.undertow.server.handlers.BlockingHandler) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Aggregations

BlockingHandler (io.undertow.server.handlers.BlockingHandler)6 HttpHandler (io.undertow.server.HttpHandler)3 HttpServerExchange (io.undertow.server.HttpServerExchange)3 IOException (java.io.IOException)3 BeforeClass (org.junit.BeforeClass)3 TestHttpClient (io.undertow.testutils.TestHttpClient)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 HttpResponse (org.apache.http.HttpResponse)2 HttpPost (org.apache.http.client.methods.HttpPost)2 Test (org.junit.Test)2 IoCallback (io.undertow.io.IoCallback)1 Sender (io.undertow.io.Sender)1 HttpString (io.undertow.util.HttpString)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 StringEntity (org.apache.http.entity.StringEntity)1 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)1