Package org.vertx.java.core.buffer

Examples of org.vertx.java.core.buffer.Buffer


        Yoke yoke = new Yoke(this);
        yoke.use(new com.jetdrone.vertx.yoke.middleware.BodyParser());
        yoke.use(new Handler<YokeRequest>() {
            @Override
            public void handle(YokeRequest request) {
                Buffer body = request.body();
                assertEquals("hello-world", body.toString());
                request.response().end();
            }
        });

        Buffer body = new Buffer("hello-world");

        MultiMap headers = new CaseInsensitiveMultiMap();
        headers.add("content-length", Integer.toString(body.length()));

        new YokeTester(yoke).request("POST", "/upload", headers, body, new Handler<Response>() {
            @Override
            public void handle(Response resp) {
                assertEquals(200, resp.getStatusCode());
View Full Code Here


            public void handle(YokeRequest request) {
                fail("Body should have been too long");
            }
        });

        Buffer body = new Buffer("[1,2,3,4,5]");

        MultiMap headers = new CaseInsensitiveMultiMap();
        headers.add("content-type", "application/json");
        headers.add("transfer-encoding", "chunked");
View Full Code Here

            public void handle(YokeRequest request) {
                fail("Body should have been too long");
            }
        });

        Buffer body = new Buffer("hello world");

        MultiMap headers = new CaseInsensitiveMultiMap();
        headers.add("content-type", "plain/text");
        headers.add("transfer-encoding", "chunked");
View Full Code Here

            public void handle(YokeRequest request) {
                fail("Body should have been too long");
            }
        });

        Buffer body = new Buffer("hello=world");

        MultiMap headers = new CaseInsensitiveMultiMap();
        headers.add("content-type", HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
        headers.add("transfer-encoding", "chunked");
View Full Code Here

        public void flush() throws IOException {
            checkState();
            // Only flush to underlying very.x response if the content-length has been set
            if (buffer.length() > 0 && response.headers().contains(CONTENT_LENGTH)) {
                response.write(buffer);
                buffer = new Buffer();
            }
        }
View Full Code Here

            final String contentType = request.getHeader("content-type");

            final boolean isJSON = contentType != null && contentType.contains("application/json");
            final boolean isMULTIPART = contentType != null && contentType.contains("multipart/form-data");
            final boolean isURLENCODEC = contentType != null && contentType.contains("application/x-www-form-urlencoded");
            final Buffer buffer = (!isMULTIPART && !isURLENCODEC) ? new Buffer(0) : null;

            // enable the parsing at Vert.x level
            request.expectMultiPart(true);

            if (isMULTIPART) {
                request.uploadHandler(new Handler<HttpServerFileUpload>() {
                    @Override
                    public void handle(final HttpServerFileUpload fileUpload) {
                        if (request.files() == null) {
                            request.setFiles(new HashMap<String, YokeFileUpload>());
                        }
                        final YokeFileUpload upload = new YokeFileUpload(vertx(), fileUpload, uploadDir);

                        // setup callbacks
                        fileUpload.exceptionHandler(new Handler<Throwable>() {
                            @Override
                            public void handle(Throwable throwable) {
                                next.handle(throwable);
                            }
                        });

                        // stream to the generated path
                        fileUpload.streamToFileSystem(upload.path());
                        // store a reference in the request
                        request.files().put(fileUpload.name(), upload);
                        // set up a callback to remove the file from the file system when the request completes
                        request.response().endHandler(new Handler<Void>() {
                            @Override
                            public void handle(Void event) {
                                if (upload.isTransient()) {
                                    upload.delete();
                                }
                            }
                        });
                    }
                });
            }

            request.dataHandler(new Handler<Buffer>() {
                long size = 0;
                final long limit = request.bodyLengthLimit();

                @Override
                public void handle(Buffer event) {
                    if (limit != -1) {
                        size += event.length();
                        if (size < limit) {
                            if (!isMULTIPART && !isURLENCODEC) {
                                buffer.appendBuffer(event);
                            }
                        } else {
                            request.dataHandler(null);
                            request.endHandler(null);

                            request.put("canceled", true);
                            next.handle(413);
                        }
                    } else {
                        if (!isMULTIPART && !isURLENCODEC) {
                            buffer.appendBuffer(event);
                        }
                    }
                }
            });

            request.endHandler(new Handler<Void>() {
                @Override
                public void handle(Void _void) {
                    if (isJSON) {
                        if (buffer != null && buffer.length() > 0) {
                            try {
                                String content = buffer.toString();
                                request.setBody(JSON.decode(content));
                            } catch (DecodeException e) {
                                next.handle(400);
                                return;
                            }
                            if (!request.get("canceled", false)) {
                                next.handle(null);
                            }
                        } else if (buffer != null && buffer.length() == 0) {
                            // special case for IE and Safari than even for 0 content length, send content type header
                            if (request.contentLength() == 0) {
                                request.setBody(null);

                                if (!request.get("canceled", false)) {
View Full Code Here

                                    public void handle(Throwable err) {
                                        next.handle(err);
                                    }
                                });

                                final Buffer body = new Buffer(0);

                                // body handler
                                response.dataHandler(new Handler<Buffer>() {
                                    @Override
                                    public void handle(Buffer buffer) {
                                        body.appendBuffer(buffer);
                                    }
                                });
                                // done
                                response.endHandler(new Handler<Void>() {
                                    @Override
                                    public void handle(Void event) {
                                        try {
                                            JsonObject verifierResp = new JsonObject(body.toString());
                                            boolean valid = "okay".equals(verifierResp.getString("status"));
                                            String email = valid ? verifierResp.getString("email") : null;
                                            // assertion is valid:
                                            if (valid) {
                                                // generate a session
View Full Code Here

        }

        @Override
        public void write(final int b) throws IOException {
            checkState();
            final Buffer buffer = new Buffer();
            buffer.appendByte((byte) b);
            response.write(buffer);
        }
View Full Code Here

        }

        @Override
        public void write(final byte[] b) throws IOException {
            checkState();
            response.write(new Buffer(b));
        }
View Full Code Here

        }

        @Override
        public void write(final byte[] b, final int off, final int len) throws IOException {
            checkState();
            final Buffer buffer = new Buffer();
            if (off == 0 && len == b.length) {
                buffer.appendBytes(b);
            } else {
                buffer.appendBytes(Arrays.copyOfRange(b, off, off + len));
            }
            response.write(buffer);
        }
View Full Code Here

TOP

Related Classes of org.vertx.java.core.buffer.Buffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.