Package org.vertx.java.core.http

Examples of org.vertx.java.core.http.HttpClientRequest


import org.vertx.java.platform.Verticle;

public class Client extends Verticle {

  public void start() {
    HttpClientRequest req = vertx.createHttpClient().setPort(8080).setHost("localhost").put("/some-url", new Handler<HttpClientResponse>() {
      public void handle(HttpClientResponse response) {
        response.dataHandler(new Handler<Buffer>() {
          public void handle(Buffer data) {
            System.out.println("Got response data:" + data);
          }
        });
      }
    });
    //Write a few chunks
    req.setChunked(true);
    for (int i = 0; i < 10; i++) {
      req.write("client-data-chunk-" + i);
    }
    req.end();
  }
View Full Code Here


  public void start() {

    HttpClient client = vertx.createHttpClient().setPort(8080).setHost("localhost");

    final HttpClientRequest req = client.put("/some-url", new Handler<HttpClientResponse>() {
      public void handle(HttpClientResponse response) {
        System.out.println("File uploaded " + response.statusCode());
      }
    });

    String filename = "upload/upload.txt";

    // For a non-chunked upload you need to specify size of upload up-front
    try {
      req.headers().set("Content-Length", String.valueOf(Files.size(Paths.get(filename))));

      // For a chunked upload you don't need to specify size, just do:
      // req.setChunked(true);

      vertx.fileSystem().open(filename, new AsyncResultHandler<AsyncFile>() {
        public void handle(AsyncResult<AsyncFile> ar) {
          final AsyncFile file = ar.result();
          Pump pump = Pump.createPump(file, req);
          pump.start();

          file.endHandler(new VoidHandler() {
            public void handle() {

              file.close(new AsyncResultHandler<Void>() {
                public void handle(AsyncResult<Void> ar) {
                  if (ar.succeeded()) {
                    req.end();
                    System.out.println("Sent request");
                  } else {
                    ar.cause().printStackTrace(System.err);
                  }
                }
View Full Code Here

    final HttpClient client = vertx.createHttpClient().setHost("localhost").setPort(8282);

    vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
      public void handle(final HttpServerRequest req) {
        System.out.println("Proxying request: " + req.uri());
        final HttpClientRequest cReq = client.request(req.method(), req.uri(), new Handler<HttpClientResponse>() {
          public void handle(HttpClientResponse cRes) {
            System.out.println("Proxying response: " + cRes.statusCode());
            req.response().setStatusCode(cRes.statusCode());
            req.response().headers().set(cRes.headers());
            req.response().setChunked(true);
            cRes.dataHandler(new Handler<Buffer>() {
              public void handle(Buffer data) {
                System.out.println("Proxying response body:" + data);
                req.response().write(data);
              }
            });
            cRes.endHandler(new VoidHandler() {
              public void handle() {
                req.response().end();
              }
            });
          }
        });
        cReq.headers().set(req.headers());
        cReq.setChunked(true);
        req.dataHandler(new Handler<Buffer>() {
          public void handle(Buffer data) {
            System.out.println("Proxying request body:" + data);
            cReq.write(data);
          }
        });
        req.endHandler(new VoidHandler() {
          public void handle() {
            System.out.println("end of the request");
            cReq.end();
          }
        });
      }
    }).listen(8080);
  }
View Full Code Here

import org.vertx.java.deploy.Verticle;

public class Client extends Verticle {

  public void start() {
    HttpClientRequest req = vertx.createHttpClient().setPort(8080).setHost("localhost").put("/some-url", new Handler<HttpClientResponse>() {
      public void handle(HttpClientResponse response) {
        response.dataHandler(new Handler<Buffer>() {
          public void handle(Buffer data) {
            System.out.println("Got response data:" + data);
          }
        });
      }
    });
    //Write a few chunks
    req.setChunked(true);
    for (int i = 0; i < 10; i++) {
      req.write("client-data-chunk-" + i);
    }
    req.end();
  }
View Full Code Here

    final HttpClient client = vertx.createHttpClient().setHost("localhost").setPort(8282);

    vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
      public void handle(final HttpServerRequest req) {
        System.out.println("Proxying request: " + req.uri);
        final HttpClientRequest cReq = client.request(req.method, req.uri, new Handler<HttpClientResponse>() {
          public void handle(HttpClientResponse cRes) {
            System.out.println("Proxying response: " + cRes.statusCode);
            req.response.statusCode = cRes.statusCode;
            req.response.headers().putAll(cRes.headers());
            req.response.setChunked(true);
            cRes.dataHandler(new Handler<Buffer>() {
              public void handle(Buffer data) {
                System.out.println("Proxying response body:" + data);
                req.response.write(data);
              }
            });
            cRes.endHandler(new SimpleHandler() {
              public void handle() {
                req.response.end();
              }
            });
          }
        });
        cReq.headers().putAll(req.headers());
        cReq.setChunked(true);
        req.dataHandler(new Handler<Buffer>() {
          public void handle(Buffer data) {
            System.out.println("Proxying request body:" + data);
            cReq.write(data);
          }
        });
        req.endHandler(new SimpleHandler() {
          public void handle() {
            cReq.end();
          }
        });
      }
    }).listen(8080);
  }
View Full Code Here

  public void start() throws Exception {

    HttpClient client = vertx.createHttpClient().setPort(8080).setHost("localhost");

    final HttpClientRequest req = client.put("/some-url", new Handler<HttpClientResponse>() {
      public void handle(HttpClientResponse response) {
        System.out.println("File uploaded " + response.statusCode);
      }
    });

    String filename = "upload/upload.txt";

    // For a non-chunked upload you need to specify size of upload up-front
    req.headers().put("Content-Length", Files.size(Paths.get(filename)));

    // For a chunked upload you don't need to specify size, just do:
    // req.setChunked(true);

    vertx.fileSystem().open(filename, new AsyncResultHandler<AsyncFile>() {
      public void handle(AsyncResult<AsyncFile> ar) {
        final AsyncFile file = ar.result;
        Pump pump = Pump.createPump(file.getReadStream(), req);
        pump.start();

        file.getReadStream().endHandler(new SimpleHandler() {
          public void handle() {

            file.close(new AsyncResultHandler<Void>() {
              public void handle(AsyncResult<Void> ar) {
                if (ar.exception == null) {
                  req.end();
                  System.out.println("Sent request");
                } else {
                  ar.exception.printStackTrace(System.err);
                }
              }
View Full Code Here

            String url,
            HttpClient client,
            Handler<HttpClientRequest> setupHandler) {

        try {
            HttpClientRequest request = client.request(method, url, new Handler<HttpClientResponse>() {
                @Override
                public void handle(HttpClientResponse response) {
                    if (response.statusCode() == 200) {
                        d.getResolver().resolve(response);
                    } else {
                        d.getResolver().reject(response);
                    }
                }
            });

            request.exceptionHandler(new Handler<Throwable>() {
                @Override
                public void handle(Throwable t) {
                    reject(d, t);
                }
            });

            if (setupHandler != null) {
                setupHandler.handle(request);
            } else {
                request.end();
            }

        } catch (Throwable t) {
            reject(d, t);
        }
View Full Code Here

                            return;
                        }

                        HttpClient client = getVertx().createHttpClient().setSSL(true).setHost("verifier.login.persona.org").setPort(443);

                        HttpClientRequest clientRequest = client.post("/verify", new Handler<HttpClientResponse>() {
                            public void handle(HttpClientResponse response) {
                                // error handler
                                response.exceptionHandler(new Handler<Throwable>() {
                                    @Override
                                    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
                                                request.createSession();
                                                // OK response
                                                request.response().end(new JsonObject().putBoolean("success", true));
                                            } else {
                                                request.response().end(new JsonObject().putBoolean("success", false));
                                            }
                                        } catch (DecodeException ex) {
                                            // bogus response from verifier!
                                            request.response().end(new JsonObject().putBoolean("success", false));
                                        }
                                    }
                                });
                            }
                        });

                        clientRequest.putHeader("content-type", "application/x-www-form-urlencoded");
                        clientRequest.putHeader("content-length", Integer.toString(data.length()));
                        clientRequest.end(data);
                    }
                })
        );

        yoke.listen(8080);
View Full Code Here

        if (secure) {
            client.setSSL(true);
        }
       
        final HttpClientRequest cReq = client.request(req.method(), newUri, new Handler<HttpClientResponse>() {
          public void handle(HttpClientResponse cRes) {
            req.response().setStatusCode(cRes.statusCode());
            req.response().headers().set(cRes.headers());
            req.response().setChunked(true);
            cRes.dataHandler(new Handler<Buffer>() {
              public void handle(Buffer data) {
                req.response().write(data);
              }
            });
            cRes.endHandler(new VoidHandler() {
              public void handle() {
                req.response().end();
              }
            });
            cRes.exceptionHandler(new Handler<Throwable>() {
              public void handle(Throwable t) {
                next.handle(t);
              }
            });
          }
        });
        cReq.headers().set(req.headers());
        cReq.setChunked(true);
        req.dataHandler(new Handler<Buffer>() {
          public void handle(Buffer data) {
            cReq.write(data);
          }
        });
        req.endHandler(new VoidHandler() {
          public void handle() {
            cReq.end();
          }
        });
    }
View Full Code Here

        }

        final Set<Integer> finalExpectedStatuses = expectedStatuses;

        try {
            HttpClientRequest request = client.request(method, url, new Handler<HttpClientResponse>() {
                @Override
                public void handle(final HttpClientResponse response) {
                    // Short circuit, no need to wait for response end
                    if (writeStream == null) {
                        if (finalExpectedStatuses.contains(response.statusCode())) {
                            d.getResolver().resolve(response);
                        } else {
                            d.getResolver().reject(response);
                        }
                        return;
                    }

                    Pump.createPump(response, writeStream).start();

                    response.endHandler(new Handler<Void>() {
                        @Override
                        public void handle(Void event) {
                            if (finalExpectedStatuses.contains(response.statusCode())) {
                                d.getResolver().resolve(response);
                            } else {
                                d.getResolver().reject(response);
                            }
                        }
                    });
                }
            });

            request.exceptionHandler(new Handler<Throwable>() {
                @Override
                public void handle(Throwable t) {
                    d.getResolver().reject(t);
                }
            });

            if (setupHandler != null) {
                setupHandler.handle(request);
            } else {
                request.end();
            }

        } catch (Throwable t) {
            d.getResolver().reject(t);
        }
View Full Code Here

TOP

Related Classes of org.vertx.java.core.http.HttpClientRequest

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.