Search in sources :

Example 71 with Future

use of io.vertx.core.Future in project hono by eclipse.

the class AbstractCredentialsServiceTest method testGetCredentialsPreservesOriginalErrorStatus.

/**
 * Verifies that when the <em>processGet</em> method returns an error result, its status
 * is adopted for the <em>get</em> method result.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetCredentialsPreservesOriginalErrorStatus(final VertxTestContext ctx) {
    final AbstractCredentialsService credentialsService = new AbstractCredentialsService() {

        @Override
        protected Future<CredentialsResult<JsonObject>> processGet(final TenantKey tenant, final CredentialKey key, final JsonObject clientContext, final Span span) {
            return Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_BAD_GATEWAY));
        }
    };
    final String tenantId = "tenant";
    final String type = CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD;
    final String authId = UUID.randomUUID().toString();
    final NoopSpan span = NoopSpan.INSTANCE;
    credentialsService.get(tenantId, type, authId, span).onComplete(ctx.succeeding(getCredentialsResult -> {
        ctx.verify(() -> {
            assertThat(getCredentialsResult.getCacheDirective()).isNotNull();
            assertThat(getCredentialsResult.getCacheDirective()).isEqualTo(CacheDirective.noCacheDirective());
            assertThat(getCredentialsResult.getStatus()).isEqualTo(HttpURLConnection.HTTP_BAD_GATEWAY);
        });
        // another test with auto-provisioning enabled
        credentialsService.setDeviceAndGatewayAutoProvisioner(getDeviceAndGatewayAutoProvisionerMock());
        credentialsService.get(tenantId, type, authId, span).onComplete(ctx.succeeding(getCredentialsResult2 -> {
            ctx.verify(() -> {
                assertThat(getCredentialsResult2.getCacheDirective()).isNotNull();
                assertThat(getCredentialsResult2.getCacheDirective()).isEqualTo(CacheDirective.noCacheDirective());
                assertThat(getCredentialsResult2.getStatus()).isEqualTo(HttpURLConnection.HTTP_BAD_GATEWAY);
            });
            ctx.completeNow();
        }));
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) VertxTestContext(io.vertx.junit5.VertxTestContext) CredentialsManagementService(org.eclipse.hono.service.management.credentials.CredentialsManagementService) CredentialsResult(org.eclipse.hono.util.CredentialsResult) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) Vertx(io.vertx.core.Vertx) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Timeout(io.vertx.junit5.Timeout) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) DeviceManagementService(org.eclipse.hono.service.management.device.DeviceManagementService) DeviceAndGatewayAutoProvisioner(org.eclipse.hono.service.management.device.DeviceAndGatewayAutoProvisioner) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TenantKey(org.eclipse.hono.deviceregistry.service.tenant.TenantKey) Span(io.opentracing.Span) JsonObject(io.vertx.core.json.JsonObject) NoopSpan(io.opentracing.noop.NoopSpan) Mockito.mock(org.mockito.Mockito.mock) NoopSpan(io.opentracing.noop.NoopSpan) TenantKey(org.eclipse.hono.deviceregistry.service.tenant.TenantKey) JsonObject(io.vertx.core.json.JsonObject) CredentialsResult(org.eclipse.hono.util.CredentialsResult) Span(io.opentracing.Span) NoopSpan(io.opentracing.noop.NoopSpan) Test(org.junit.jupiter.api.Test)

Example 72 with Future

use of io.vertx.core.Future in project hono by eclipse.

the class DelegatingRegistrationAmqpEndpoint method processAssertRequest.

private Future<Message> processAssertRequest(final Message request, final ResourceIdentifier targetAddress, final SpanContext spanContext) {
    final String tenantId = targetAddress.getTenantId();
    final String deviceId = MessageHelper.getDeviceId(request);
    final String gatewayId = MessageHelper.getGatewayId(request);
    final Span span = TracingHelper.buildServerChildSpan(tracer, spanContext, SPAN_NAME_ASSERT_DEVICE_REGISTRATION, getClass().getSimpleName()).start();
    TracingHelper.setDeviceTags(span, tenantId, deviceId);
    final Future<Message> resultFuture;
    if (tenantId == null || deviceId == null) {
        TracingHelper.logError(span, "missing tenant and/or device");
        resultFuture = Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else {
        final Future<RegistrationResult> result;
        if (gatewayId == null) {
            logger.debug("asserting registration of device [tenant: {}, device-id: {}]", tenantId, deviceId);
            result = getService().assertRegistration(tenantId, deviceId, span);
        } else {
            TracingHelper.TAG_GATEWAY_ID.set(span, gatewayId);
            logger.debug("asserting registration of device [tenant: {}, device-id: {}] for gateway [{}]", tenantId, deviceId, gatewayId);
            result = getService().assertRegistration(tenantId, deviceId, gatewayId, span);
        }
        resultFuture = result.map(res -> RegistrationConstants.getAmqpReply(RegistrationConstants.REGISTRATION_ENDPOINT, tenantId, request, res));
    }
    return finishSpanOnFutureCompletion(span, resultFuture);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) ClientErrorException(org.eclipse.hono.client.ClientErrorException) AbstractDelegatingRequestResponseEndpoint(org.eclipse.hono.service.amqp.AbstractDelegatingRequestResponseEndpoint) MessageHelper(org.eclipse.hono.util.MessageHelper) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Span(io.opentracing.Span) Message(org.apache.qpid.proton.message.Message) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) TracingHelper(org.eclipse.hono.tracing.TracingHelper) Message(org.apache.qpid.proton.message.Message) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Span(io.opentracing.Span)

Example 73 with Future

use of io.vertx.core.Future in project hono by eclipse.

the class DelegatingDeviceManagementHttpEndpoint method doSearchDevices.

private void doSearchDevices(final RoutingContext ctx) {
    final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_SEARCH_DEVICES, getClass().getSimpleName()).start();
    final String tenantId = getTenantParam(ctx);
    final Future<Integer> pageSize = getRequestParameter(ctx, RegistryManagementConstants.PARAM_PAGE_SIZE, DEFAULT_PAGE_SIZE, CONVERTER_INT, value -> value >= MIN_PAGE_SIZE && value <= MAX_PAGE_SIZE);
    final Future<Integer> pageOffset = getRequestParameter(ctx, RegistryManagementConstants.PARAM_PAGE_OFFSET, DEFAULT_PAGE_OFFSET, CONVERTER_INT, value -> value >= MIN_PAGE_OFFSET);
    final Future<List<Filter>> filters = decodeJsonFromRequestParameter(ctx, RegistryManagementConstants.PARAM_FILTER_JSON, Filter.class);
    final Future<List<Sort>> sortOptions = decodeJsonFromRequestParameter(ctx, RegistryManagementConstants.PARAM_SORT_JSON, Sort.class);
    CompositeFuture.all(pageSize, pageOffset, filters, sortOptions).onSuccess(ok -> TracingHelper.TAG_TENANT_ID.set(span, tenantId)).compose(ok -> getService().searchDevices(tenantId, pageSize.result(), pageOffset.result(), filters.result(), sortOptions.result(), span)).onSuccess(operationResult -> writeResponse(ctx, operationResult, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) AbstractDelegatingRegistryHttpEndpoint(org.eclipse.hono.service.management.AbstractDelegatingRegistryHttpEndpoint) Filter(org.eclipse.hono.service.management.Filter) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) TracingHandler(org.eclipse.hono.service.http.TracingHandler) CompositeFuture(io.vertx.core.CompositeFuture) Sort(org.eclipse.hono.service.management.Sort) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Set(java.util.Set) HttpHeaders(io.vertx.core.http.HttpHeaders) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) Optional(java.util.Optional) Span(io.opentracing.Span) Id(org.eclipse.hono.service.management.Id) List(java.util.List) Span(io.opentracing.Span)

Example 74 with Future

use of io.vertx.core.Future in project hono by eclipse.

the class DelegatingDeviceManagementHttpEndpoint method doCreateDevice.

private void doCreateDevice(final RoutingContext ctx) {
    final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_CREATE_DEVICE, getClass().getSimpleName()).start();
    final Future<String> tenantId = getRequestParameter(ctx, PARAM_TENANT_ID, getPredicate(config.getTenantIdPattern(), false));
    final Future<String> deviceId = getRequestParameter(ctx, PARAM_DEVICE_ID, getPredicate(config.getDeviceIdPattern(), true));
    final Future<Device> device = fromPayload(ctx);
    CompositeFuture.all(tenantId, deviceId, device).compose(ok -> {
        final Optional<String> did = Optional.ofNullable(deviceId.result());
        TracingHelper.TAG_TENANT_ID.set(span, tenantId.result());
        did.ifPresent(s -> TracingHelper.TAG_DEVICE_ID.set(span, s));
        logger.debug("creating device [tenant: {}, device-id: {}]", tenantId.result(), did.orElse("<auto>"));
        return getService().createDevice(tenantId.result(), did, device.result(), span);
    }).onSuccess(operationResult -> writeResponse(ctx, operationResult, (responseHeaders, status) -> {
        Optional.ofNullable(operationResult.getPayload()).map(Id::getId).ifPresent(id -> responseHeaders.set(HttpHeaders.LOCATION, String.format("/%s/%s/%s", getName(), tenantId.result(), id)));
    }, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) AbstractDelegatingRegistryHttpEndpoint(org.eclipse.hono.service.management.AbstractDelegatingRegistryHttpEndpoint) Filter(org.eclipse.hono.service.management.Filter) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) TracingHandler(org.eclipse.hono.service.http.TracingHandler) CompositeFuture(io.vertx.core.CompositeFuture) Sort(org.eclipse.hono.service.management.Sort) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Set(java.util.Set) HttpHeaders(io.vertx.core.http.HttpHeaders) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) Optional(java.util.Optional) Span(io.opentracing.Span) Id(org.eclipse.hono.service.management.Id) Optional(java.util.Optional) Id(org.eclipse.hono.service.management.Id) Span(io.opentracing.Span)

Example 75 with Future

use of io.vertx.core.Future in project hono by eclipse.

the class DelegatingTenantManagementHttpEndpoint method searchTenants.

private void searchTenants(final RoutingContext ctx) {
    final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_SEARCH_TENANT, getClass().getSimpleName()).start();
    final Future<Integer> pageSize = getRequestParameter(ctx, RegistryManagementConstants.PARAM_PAGE_SIZE, DEFAULT_PAGE_SIZE, CONVERTER_INT, value -> value >= MIN_PAGE_SIZE && value <= MAX_PAGE_SIZE);
    final Future<Integer> pageOffset = getRequestParameter(ctx, RegistryManagementConstants.PARAM_PAGE_OFFSET, DEFAULT_PAGE_OFFSET, CONVERTER_INT, value -> value >= MIN_PAGE_OFFSET);
    final Future<List<Filter>> filters = decodeJsonFromRequestParameter(ctx, RegistryManagementConstants.PARAM_FILTER_JSON, Filter.class);
    final Future<List<Sort>> sortOptions = decodeJsonFromRequestParameter(ctx, RegistryManagementConstants.PARAM_SORT_JSON, Sort.class);
    CompositeFuture.all(pageSize, pageOffset, filters, sortOptions).compose(ok -> getService().searchTenants(pageSize.result(), pageOffset.result(), filters.result(), sortOptions.result(), span)).onSuccess(operationResult -> writeResponse(ctx, operationResult, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) AbstractDelegatingRegistryHttpEndpoint(org.eclipse.hono.service.management.AbstractDelegatingRegistryHttpEndpoint) Filter(org.eclipse.hono.service.management.Filter) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) TracingHandler(org.eclipse.hono.service.http.TracingHandler) CompositeFuture(io.vertx.core.CompositeFuture) Sort(org.eclipse.hono.service.management.Sort) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Set(java.util.Set) HttpHeaders(io.vertx.core.http.HttpHeaders) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) Optional(java.util.Optional) Span(io.opentracing.Span) Id(org.eclipse.hono.service.management.Id) List(java.util.List) Span(io.opentracing.Span)

Aggregations

Future (io.vertx.core.Future)370 HttpURLConnection (java.net.HttpURLConnection)195 Handler (io.vertx.core.Handler)174 List (java.util.List)166 Objects (java.util.Objects)164 JsonObject (io.vertx.core.json.JsonObject)163 Promise (io.vertx.core.Promise)160 Vertx (io.vertx.core.Vertx)157 Buffer (io.vertx.core.buffer.Buffer)149 Optional (java.util.Optional)147 Logger (org.slf4j.Logger)136 LoggerFactory (org.slf4j.LoggerFactory)136 CompositeFuture (io.vertx.core.CompositeFuture)127 ClientErrorException (org.eclipse.hono.client.ClientErrorException)127 Map (java.util.Map)122 Span (io.opentracing.Span)117 AsyncResult (io.vertx.core.AsyncResult)112 TracingHelper (org.eclipse.hono.tracing.TracingHelper)98 Constants (org.eclipse.hono.util.Constants)97 ArrayList (java.util.ArrayList)94