use of io.vertx.core.AsyncResult in project hono by eclipse.
the class ForwardingDownstreamAdapter method onClientAttach.
@Override
public final void onClientAttach(final UpstreamReceiver client, final Handler<AsyncResult<Void>> resultHandler) {
if (!running) {
throw new IllegalStateException("adapter must be started first");
}
Objects.requireNonNull(client);
Objects.requireNonNull(resultHandler);
ProtonSender sender = activeSenders.get(client);
if (sender != null && sender.isOpen()) {
logger.info("reusing existing downstream sender [con: {}, link: {}]", client.getConnectionId(), client.getLinkId());
resultHandler.handle(Future.succeededFuture());
} else {
removeSender(client);
// register the result handler to be failed if the connection to the downstream container fails during
// the attempt to create a downstream sender
clientAttachHandlers.add(resultHandler);
Future<Void> tracker = Future.future();
tracker.setHandler(attempt -> {
if (attempt.succeeded()) {
logger.info("created downstream sender [con: {}, link: {}]", client.getConnectionId(), client.getLinkId());
} else {
logger.warn("can't create downstream sender [con: {}, link: {}]: {}", client.getConnectionId(), client.getLinkId(), attempt.cause().getMessage());
}
clientAttachHandlers.remove(resultHandler);
resultHandler.handle(attempt);
});
final ResourceIdentifier targetAddress = ResourceIdentifier.fromString(client.getTargetAddress());
createSender(targetAddress, replenishedSender -> handleFlow(replenishedSender, client), closeHook -> {
removeSender(client);
closeReceiver(client);
}).compose(createdSender -> {
addSender(client, createdSender);
tracker.complete();
}, tracker);
}
}
use of io.vertx.core.AsyncResult in project hono by eclipse.
the class CredentialsApiAuthProvider method authenticate.
@Override
public final void authenticate(final DeviceCredentials deviceCredentials, final Handler<AsyncResult<Device>> resultHandler) {
Objects.requireNonNull(deviceCredentials);
Objects.requireNonNull(resultHandler);
Future<Device> validationResult = Future.future();
validationResult.setHandler(resultHandler);
getCredentialsForDevice(deviceCredentials).recover(t -> {
final ServiceInvocationException e = (ServiceInvocationException) t;
if (e.getErrorCode() == HttpURLConnection.HTTP_NOT_FOUND) {
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "bad credentials"));
} else {
return Future.failedFuture(t);
}
}).map(credentialsOnRecord -> {
if (deviceCredentials.validate(credentialsOnRecord)) {
return new Device(deviceCredentials.getTenantId(), credentialsOnRecord.getDeviceId());
} else {
throw new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "invalid credentials");
}
}).setHandler(resultHandler);
}
use of io.vertx.core.AsyncResult in project vertx-web by vert-x3.
the class OpenAPI3ParametersUnitTest method testPathLabelExplodeArray.
/**
* Test: path_label_explode_array
* Expected parameters sent:
* color: .blue.black.brown
* Expected response: {"color":["blue","black","brown"]}
* @throws Exception
*/
@Test
public void testPathLabelExplodeArray() throws Exception {
routerFactory.addHandlerByOperationId("path_label_explode_array", routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
JsonObject res = new JsonObject();
RequestParameter color_path = params.pathParameter("color");
assertNotNull(color_path);
assertTrue(color_path.isArray());
res.put("color", new JsonArray(color_path.getArray().stream().map(param -> param.getString()).collect(Collectors.toList())));
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
});
CountDownLatch latch = new CountDownLatch(1);
List<Object> color_path;
color_path = new ArrayList<>();
color_path.add("blue");
color_path.add("black");
color_path.add("brown");
startServer();
apiClient.pathLabelExplodeArray(color_path, (AsyncResult<HttpResponse> ar) -> {
if (ar.succeeded()) {
assertEquals(200, ar.result().statusCode());
assertTrue("Expected: " + new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").equals(ar.result().bodyAsJsonObject()));
} else {
assertTrue(ar.cause().getMessage(), false);
}
latch.countDown();
});
awaitLatch(latch);
}
use of io.vertx.core.AsyncResult in project vertx-web by vert-x3.
the class OpenAPI3ParametersUnitTest method testPathSimpleNoexplodeArray.
/**
* Test: path_simple_noexplode_array
* Expected parameters sent:
* color: blue,black,brown
* Expected response: {"color":["blue","black","brown"]}
* @throws Exception
*/
@Test
public void testPathSimpleNoexplodeArray() throws Exception {
routerFactory.addHandlerByOperationId("path_simple_noexplode_array", routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
JsonObject res = new JsonObject();
RequestParameter color_path = params.pathParameter("color");
assertNotNull(color_path);
assertTrue(color_path.isArray());
res.put("color", new JsonArray(color_path.getArray().stream().map(param -> param.getString()).collect(Collectors.toList())));
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
});
CountDownLatch latch = new CountDownLatch(1);
List<Object> color_path;
color_path = new ArrayList<>();
color_path.add("blue");
color_path.add("black");
color_path.add("brown");
startServer();
apiClient.pathSimpleNoexplodeArray(color_path, (AsyncResult<HttpResponse> ar) -> {
if (ar.succeeded()) {
assertEquals(200, ar.result().statusCode());
assertTrue("Expected: " + new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").equals(ar.result().bodyAsJsonObject()));
} else {
assertTrue(ar.cause().getMessage(), false);
}
latch.countDown();
});
awaitLatch(latch);
}
use of io.vertx.core.AsyncResult in project vertx-web by vert-x3.
the class OpenAPI3ParametersUnitTest method testPathLabelNoexplodeObject.
/**
* Test: path_label_noexplode_object
* Expected parameters sent:
* color: .R.100.G.200.B.150
* Expected response: {"color":{"R":"100","G":"200","B":"150"}}
* @throws Exception
*/
@Test
public void testPathLabelNoexplodeObject() throws Exception {
routerFactory.addHandlerByOperationId("path_label_noexplode_object", routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
JsonObject res = new JsonObject();
RequestParameter color_path = params.pathParameter("color");
assertNotNull(color_path);
assertTrue(color_path.isObject());
Map<String, String> map = new HashMap<>();
for (String key : color_path.getObjectKeys()) map.put(key, color_path.getObjectValue(key).getString());
res.put("color", map);
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
});
CountDownLatch latch = new CountDownLatch(1);
Map<String, Object> color_path;
color_path = new HashMap<>();
color_path.put("R", "100");
color_path.put("G", "200");
color_path.put("B", "150");
startServer();
apiClient.pathLabelNoexplodeObject(color_path, (AsyncResult<HttpResponse> ar) -> {
if (ar.succeeded()) {
assertEquals(200, ar.result().statusCode());
assertTrue("Expected: " + new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}").equals(ar.result().bodyAsJsonObject()));
} else {
assertTrue(ar.cause().getMessage(), false);
}
latch.countDown();
});
awaitLatch(latch);
}
Aggregations