Java Code Examples for io.vertx.reactivex.ext.web.client.WebClient#create()

The following examples show how to use io.vertx.reactivex.ext.web.client.WebClient#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyResource.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Path("7")
@GET
public CompletionStage<String> hello7(@Context Vertx vertx){
	io.vertx.reactivex.core.Vertx rxVertx = io.vertx.reactivex.core.Vertx.newInstance(vertx);
	System.err.println("Creating client");
	WebClientOptions options = new WebClientOptions();
	options.setSsl(true);
	options.setTrustAll(true);
	options.setVerifyHost(false);
	WebClient client = WebClient.create(rxVertx, options);
	Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> responseHandler = client.get(443,
			"www.google.com", 
			"/robots.txt").rxSend();

	CompletableFuture<String> ret = new CompletableFuture<>();
	responseHandler
		.doAfterTerminate(() -> client.close())
		.subscribe(body -> {
		System.err.println("Got body");
		ret.complete(body.body().toString());
	});
	
	System.err.println("Created client");
	return ret;
}
 
Example 2
Source File: HttpAuthenticationProviderConfiguration.java    From graviteeio-access-management with Apache License 2.0 6 votes vote down vote up
@Bean
@Qualifier("idpHttpAuthWebClient")
public WebClient httpClient() {
    WebClientOptions httpClientOptions = new WebClientOptions();
    httpClientOptions
            .setUserAgent(DEFAULT_USER_AGENT)
            .setConnectTimeout(configuration.getConnectTimeout())
            .setMaxPoolSize(configuration.getMaxPoolSize());

    if (configuration.getAuthenticationResource().getBaseURL() != null
            && configuration.getAuthenticationResource().getBaseURL().startsWith("https://")) {
        httpClientOptions.setSsl(true);
        httpClientOptions.setTrustAll(true);
    }

    return WebClient.create(vertx, httpClientOptions);
}
 
Example 3
Source File: GetBooksHandlerTest.java    From vertx-postgresql-starter with MIT License 6 votes vote down vote up
@Before
public void setUp(TestContext testContext) {
  BookDatabaseService mockBookDatabaseService = Mockito.mock(BookDatabaseService.class);

  JsonArray mockDbResponse = new JsonArray()
    .add(new JsonObject()
      .put("id", 1)
      .put("title", "Effective Java")
      .put("category", "java")
      .put("publicationDate", "2009-01-01"))
    .add(new JsonObject()
      .put("id", 2)
      .put("title", "Thinking in Java")
      .put("category", "java")
      .put("publicationDate", "2006-02-20"));


  Mockito.when(mockBookDatabaseService.rxGetBooks(new Book())).thenReturn(Single.just(mockDbResponse));

  mockServer(1234, GET, EndPoints.GET_BOOKS, BookApis.getBooksHandler(mockBookDatabaseService), testContext);

  webClient = WebClient.create(vertx);
}
 
Example 4
Source File: MyResource.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Path("8error")
@GET
public Single<String> hello8Error(@Context io.vertx.reactivex.core.Vertx rxVertx){
	System.err.println("Creating client");
	WebClientOptions options = new WebClientOptions();
	options.setSsl(true);
	options.setTrustAll(true);
	options.setVerifyHost(false);
	WebClient client = WebClient.create(rxVertx, options);
	Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> responseHandler = client.get(443,
			"www.google.com", 
			"/robots.txt").rxSend();

	System.err.println("Created client");
	return responseHandler
			.doAfterTerminate(() -> client.close())
			.map(body -> {
		System.err.println("Got body");
		throw new MyException();
	});
}
 
Example 5
Source File: MyResource.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Path("8user")
@Produces("text/json")
@GET
public Single<DataClass> hello8User(@Context io.vertx.reactivex.core.Vertx rxVertx){
	System.err.println("Creating client");
	WebClientOptions options = new WebClientOptions();
	options.setSsl(true);
	options.setTrustAll(true);
	options.setVerifyHost(false);
	WebClient client = WebClient.create(rxVertx, options);
	Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> responseHandler = client.get(443,
			"www.google.com", 
			"/robots.txt").rxSend();

	System.err.println("Created client");
	return responseHandler.map(body -> {
		System.err.println("Got body");
		return new DataClass(body.body().toString());
	}).doAfterTerminate(() -> client.close());
}
 
Example 6
Source File: MyResource.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Path("7error")
@GET
public CompletionStage<String> hello7Error(@Context Vertx vertx){
	io.vertx.reactivex.core.Vertx rxVertx = io.vertx.reactivex.core.Vertx.newInstance(vertx);
	System.err.println("Creating client");
	WebClientOptions options = new WebClientOptions();
	options.setSsl(true);
	options.setTrustAll(true);
	options.setVerifyHost(false);
	WebClient client = WebClient.create(rxVertx, options);
	Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> responseHandler = client.get(443,
			"www.google.com", 
			"/robots.txt").rxSend();

	CompletableFuture<String> ret = new CompletableFuture<>();
	responseHandler
		.doAfterTerminate(() -> client.close())
		.subscribe(body -> {
		System.err.println("Got body");
		
		ret.completeExceptionally(new MyException());
	});
	System.err.println("Created client");
	return ret;
}
 
Example 7
Source File: DashboardWebAppVerticle.java    From vertx-in-action with MIT License 6 votes vote down vote up
private void hydrate() {
  WebClient webClient = WebClient.create(vertx);
  webClient
    .get(3001, "localhost", "/ranking-last-24-hours")
    .as(BodyCodec.jsonArray())
    .rxSend()
    .delay(5, TimeUnit.SECONDS, RxHelper.scheduler(vertx))
    .retry(5)
    .map(HttpResponse::body)
    .flattenAsFlowable(Functions.identity())
    .cast(JsonObject.class)
    .flatMapSingle(json -> whoOwnsDevice(webClient, json))
    .flatMapSingle(json -> fillWithUserProfile(webClient, json))
    .subscribe(
      this::hydrateEntryIfPublic,
      err -> logger.error("Hydratation error", err),
      () -> logger.info("Hydratation completed"));
}
 
Example 8
Source File: MyResource.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Path("6")
@GET
public void hello6(@Suspended final AsyncResponse asyncResponse,
	      // Inject the Vertx instance
	      @Context Vertx vertx){
	io.vertx.reactivex.core.Vertx rxVertx = io.vertx.reactivex.core.Vertx.newInstance(vertx);
	System.err.println("Creating client");
	WebClientOptions options = new WebClientOptions();
	options.setSsl(true);
	options.setTrustAll(true);
	options.setVerifyHost(false);
	WebClient client = WebClient.create(rxVertx, options);
	Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> responseHandler = client.get(443,
			"www.google.com", 
			"/robots.txt").rxSend();

	responseHandler
		.doAfterTerminate(() -> client.close())
		.subscribe(body -> {
		System.err.println("Got body");
		asyncResponse.resume(Response.ok(body.body().toString()).build());
	});
	
	System.err.println("Created client");
}
 
Example 9
Source File: HttpUserProviderConfiguration.java    From graviteeio-access-management with Apache License 2.0 6 votes vote down vote up
@Bean
@Qualifier("idpHttpUsersWebClient")
public WebClient httpClient() {
    WebClientOptions httpClientOptions = new WebClientOptions();
    httpClientOptions
            .setUserAgent(DEFAULT_USER_AGENT)
            .setConnectTimeout(configuration.getConnectTimeout())
            .setMaxPoolSize(configuration.getMaxPoolSize());

    if (configuration.getUsersResource().getBaseURL() != null
            && configuration.getUsersResource().getBaseURL().startsWith("https://")) {
        httpClientOptions.setSsl(true);
        httpClientOptions.setTrustAll(true);
    }

    return WebClient.create(vertx, httpClientOptions);
}
 
Example 10
Source File: CongratsVerticle.java    From vertx-in-action with MIT License 6 votes vote down vote up
@Override
public Completable rxStart() {
  mailClient = MailClient.createShared(vertx, MailerConfig.config());
  webClient = WebClient.create(vertx);

  KafkaConsumer.<String, JsonObject>create(vertx, KafkaConfig.consumerConfig("congrats-service"))
    .subscribe("daily.step.updates")
    .toFlowable()
    .filter(this::above10k)
    .distinct(KafkaConsumerRecord::key)
    .flatMapSingle(this::sendmail)
    .doOnError(err -> logger.error("Woops", err))
    .retryWhen(this::retryLater)
    .subscribe(mailResult -> logger.info("Congratulated {}", mailResult.getRecipients()));

  return Completable.complete();
}
 
Example 11
Source File: HelloResource.java    From redpipe with Apache License 2.0 5 votes vote down vote up
private Single<String> get(Vertx vertx, URI uri){
	WebClient client = WebClient.create(vertx);
	Single<HttpResponse<Buffer>> responseHandler = 
			client.get(uri.getPort(), uri.getHost(), uri.getPath()).rxSend();

	return responseHandler.map(response -> response.body().toString())
			.doAfterTerminate(() -> client.close());
}
 
Example 12
Source File: CommonConfiguration.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@Bean
@Qualifier("oidcWebClient")
public WebClient webClient() {
    WebClientOptions options = new WebClientOptions()
            .setConnectTimeout(Integer.valueOf(environment.getProperty("oidc.http.connectionTimeout", "10")) * 1000)
            .setMaxPoolSize(Integer.valueOf(environment.getProperty("oidc.http.pool.maxTotalConnection", "200")))
            .setTrustAll(Boolean.valueOf(environment.getProperty("oidc.http.client.trustAll", "true")));

    return WebClient.create(vertx,options);
}
 
Example 13
Source File: CollectorService.java    From vertx-in-action with MIT License 5 votes vote down vote up
@Override
public Completable rxStart() {
  webClient = WebClient.create(vertx);
  return vertx.createHttpServer()
    .requestHandler(this::handleRequest)
    .rxListen(8080)
    .ignoreElement();
}
 
Example 14
Source File: GithubAuthenticationProviderConfiguration.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@Bean
@Qualifier("gitHubWebClient")
public WebClient httpClient() {
    WebClientOptions httpClientOptions = new WebClientOptions();
    httpClientOptions
            .setUserAgent(DEFAULT_USER_AGENT)
            .setConnectTimeout(configuration.getConnectTimeout())
            .setMaxPoolSize(configuration.getMaxPoolSize());

    return WebClient.create(vertx, httpClientOptions);
}
 
Example 15
Source File: EventStatsVerticle.java    From vertx-in-action with MIT License 5 votes vote down vote up
@Override
public Completable rxStart() {
  webClient = WebClient.create(vertx);
  producer = KafkaProducer.create(vertx, KafkaConfig.producer());

  KafkaConsumer.<String, JsonObject>create(vertx, KafkaConfig.consumer("event-stats-throughput"))
    .subscribe("incoming.steps")
    .toFlowable()
    .buffer(5, TimeUnit.SECONDS, RxHelper.scheduler(vertx))
    .flatMapCompletable(this::publishThroughput)
    .doOnError(err -> logger.error("Woops", err))
    .retryWhen(this::retryLater)
    .subscribe();

  KafkaConsumer.<String, JsonObject>create(vertx, KafkaConfig.consumer("event-stats-user-activity-updates"))
    .subscribe("daily.step.updates")
    .toFlowable()
    .flatMapSingle(this::addDeviceOwner)
    .flatMapSingle(this::addOwnerData)
    .flatMapCompletable(this::publishUserActivityUpdate)
    .doOnError(err -> logger.error("Woops", err))
    .retryWhen(this::retryLater)
    .subscribe();

  KafkaConsumer.<String, JsonObject>create(vertx, KafkaConfig.consumer("event-stats-city-trends"))
    .subscribe("event-stats.user-activity.updates")
    .toFlowable()
    .groupBy(this::city)
    .flatMap(groupedFlowable -> groupedFlowable.buffer(5, TimeUnit.SECONDS, RxHelper.scheduler(vertx)))
    .flatMapCompletable(this::publishCityTrendUpdate)
    .doOnError(err -> logger.error("Woops", err))
    .retryWhen(this::retryLater)
    .subscribe();

  return Completable.complete();
}
 
Example 16
Source File: OAuth2GenericAuthenticationProviderConfiguration.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@Bean
@Qualifier("oauthWebClient")
public WebClient httpClient() {
    WebClientOptions httpClientOptions = new WebClientOptions();
    httpClientOptions
            .setUserAgent(DEFAULT_USER_AGENT)
            .setConnectTimeout(configuration.getConnectTimeout())
            .setMaxPoolSize(configuration.getMaxPoolSize());

    return WebClient.create(vertx, httpClientOptions);
}
 
Example 17
Source File: Helpers.java    From rxjava2-lab with Apache License 2.0 4 votes vote down vote up
public static WebClient client() {
    return WebClient.create(vertx,
            new WebClientOptions().setDefaultPort(8080).setDefaultHost("localhost")
    );
}
 
Example 18
Source File: UpsertBookByIdHandlerTest.java    From vertx-postgresql-starter with MIT License 3 votes vote down vote up
@Before
public void setUp(TestContext testContext) {
  BookDatabaseService mockBookDatabaseService = Mockito.mock(BookDatabaseService.class);

  Book book = new Book(0, "Java Concurrency in Practice", "java", "2006-05-19");

  Mockito.when(mockBookDatabaseService.rxUpsertBookById(3, book)).thenReturn(Completable.complete());

  mockServer(1234, PUT, EndPoints.UPDATE_BOOK_BY_ID, BookApis.upsertBookByIdHandler(mockBookDatabaseService), testContext);

  webClient = WebClient.create(vertx);
}
 
Example 19
Source File: DeleteBookByIdHandlerTest.java    From vertx-postgresql-starter with MIT License 3 votes vote down vote up
@Before
public void setUp(TestContext testContext) {
  BookDatabaseService mockBookDatabaseService = Mockito.mock(BookDatabaseService.class);

  int bookId = 1;

  Mockito.when(mockBookDatabaseService.rxDeleteBookById(bookId)).thenReturn(Completable.complete());

  mockServer(1234, DELETE, EndPoints.DELETE_BOOK_BY_ID, BookApis.deleteBookByIdHandler(mockBookDatabaseService), testContext);

  webClient = WebClient.create(vertx);
}
 
Example 20
Source File: AddBookHandlerTest.java    From vertx-postgresql-starter with MIT License 3 votes vote down vote up
@Before
public void setUp(TestContext testContext) {
  BookDatabaseService mockBookDatabaseService = Mockito.mock(BookDatabaseService.class);

  Book book = new Book(3, "Design Patterns", "design", "1995-01-15");

  Mockito.when(mockBookDatabaseService.rxAddNewBook(book)).thenReturn(Completable.complete());

  mockServer(1234, POST, EndPoints.ADD_NEW_BOOK, BookApis.addBookHandler(mockBookDatabaseService), testContext);

  webClient = WebClient.create(vertx);
}