/** * Adds a handler for adding an OpenTracing Span to the routing context. * * @param router The router to add the handler to. * @param position The position to add the tracing handler at. */ private void addTracingHandler(final Router router, final int position) { final Map<String, String> customTags = new HashMap<>(); customTags.put(Tags.COMPONENT.getKey(), getTypeName()); addCustomTags(customTags); final List<WebSpanDecorator> decorators = new ArrayList<>(); decorators.add(new ComponentMetaDataDecorator(customTags)); addCustomSpanDecorators(decorators); final TracingHandler tracingHandler = new TracingHandler(tracer, decorators); router.route().order(position).handler(tracingHandler).failureHandler(tracingHandler); }
@Override public final void doStart(final Future<Void> startFuture) { checkPortConfiguration() .compose(s -> preStartup()) .compose(s -> { final Router router = createRouter(); if (router == null) { return Future.failedFuture("no router configured"); } else { addRoutes(router); return CompositeFuture.all(bindSecureHttpServer(router), bindInsecureHttpServer(router)); } }).compose(s -> { try { onStartupSuccess(); startFuture.complete(); } catch (final Exception e) { LOG.error("error in onStartupSuccess", e); startFuture.fail(e); } }, startFuture); }
/** * Gets the options to use for creating the TLS secured http server. * <p> * Subclasses may override this method in order to customize the server. * <p> * This method returns default options with the host and port being set to the corresponding values * from the <em>config</em> properties and using a maximum chunk size of 4096 bytes. * * @return The http server options. */ protected HttpServerOptions getHttpServerOptions() { final HttpServerOptions options = new HttpServerOptions(); options.setHost(getConfig().getBindAddress()).setPort(getConfig().getPort(getPortDefaultValue())) .setMaxChunkSize(4096); addTlsKeyCertOptions(options); addTlsTrustOptions(options); return options; }
if (!isPayloadOfIndicatedType(payload, contentType)) { HttpUtils.badRequest(ctx, String.format("content type [%s] does not match payload", contentType)); } else { final String qosHeaderValue = ctx.request().getHeader(Constants.HEADER_QOS_LEVEL); final Integer qos = getQoSLevel(qosHeaderValue); if (qos != null && qos == HEADER_QOS_INVALID) { HttpUtils.badRequest(ctx, "unsupported QoS-Level header value"); } else { final Device authenticatedDevice = getAuthenticatedDevice(ctx); final Span currentSpan = tracer.buildSpan("upload " + endpointName) .asChildOf(TracingHandler.serverSpanContext(ctx)) .ignoreActiveSpan() .withTag(Tags.COMPONENT.getKey(), getTypeName()) .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT) .withTag(MessageHelper.APP_PROPERTY_TENANT_ID, tenant) final Future<JsonObject> tokenTracker = getRegistrationAssertion( tenant, deviceId, authenticatedDevice, currentSpan.context()); final Future<TenantObject> tenantConfigTracker = getTenantConfiguration(tenant, currentSpan.context()); final Future<Integer> ttdTracker = tenantConfigTracker.compose(tenantObj -> { final Integer ttdParam = HttpUtils.getTimeTilDisconnect(ctx); return getTimeUntilDisconnect(tenantObj, ttdParam).map(effectiveTtd -> { if (effectiveTtd != null) { currentSpan.setTag(MessageHelper.APP_PROPERTY_DEVICE_TTD, effectiveTtd);
} else { currentSpan.setTag(MessageHelper.APP_PROPERTY_DEVICE_TTD, ttdSecs); return getCommandConnection().createCommandConsumer( tenantId, deviceId, commandContext -> { Tags.COMPONENT.set(commandContext.getCurrentSpan(), getTypeName()); final Command command = commandContext.getCommand(); if (command.isValid()) { cancelCommandReceptionTimer(ctx); responseReady.tryComplete(); if (!responseReady.isComplete()) { addCommandReceptionTimer(ctx, responseReady, ttdSecs);
/** * Creates the router for handling requests. * <p> * This method creates a router instance with the following routes: * <ol> * <li>A default route limiting the body size of requests to the maximum payload size set in the <em>config</em> properties.</li> * </ol> * * @return The newly created router (never {@code null}). */ protected Router createRouter() { final Router router = Router.router(vertx); LOG.info("limiting size of inbound request body to {} bytes", getConfig().getMaxPayloadSize()); router.route().handler(BodyHandler.create(DEFAULT_UPLOADS_DIRECTORY).setBodyLimit(getConfig().getMaxPayloadSize())); addTracingHandler(router, -5); // add default handler for failed routes router.route().order(-1).failureHandler(new DefaultFailureHandler()); return router; }
if (!isPayloadOfIndicatedType(payload, contentType)) { HttpUtils.badRequest(ctx, String.format("content type [%s] does not match payload", contentType)); } else { final String qosHeaderValue = ctx.request().getHeader(Constants.HEADER_QOS_LEVEL); final Integer qos = getQoSLevel(qosHeaderValue); if (qos != null && qos == HEADER_QOS_INVALID) { HttpUtils.badRequest(ctx, "unsupported QoS-Level header value"); } else { final Device authenticatedDevice = getAuthenticatedDevice(ctx); final Span currentSpan = tracer.buildSpan("upload " + endpointName) .asChildOf(TracingHandler.serverSpanContext(ctx)) .ignoreActiveSpan() .withTag(Tags.COMPONENT.getKey(), getTypeName()) .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT) .withTag(MessageHelper.APP_PROPERTY_TENANT_ID, tenant) final Future<JsonObject> tokenTracker = getRegistrationAssertion( tenant, deviceId, authenticatedDevice, currentSpan.context()); final Future<TenantObject> tenantConfigTracker = getTenantConfiguration(tenant, currentSpan.context()); final Future<Integer> ttdTracker = tenantConfigTracker.compose(tenantObj -> { final Integer ttdParam = HttpUtils.getTimeTilDisconnect(ctx); return getTimeUntilDisconnect(tenantObj, ttdParam).map(effectiveTtd -> { if (effectiveTtd != null) { currentSpan.setTag(MessageHelper.APP_PROPERTY_DEVICE_TTD, effectiveTtd);
} else { currentSpan.setTag(MessageHelper.APP_PROPERTY_DEVICE_TTD, ttdSecs); return getCommandConnection().createCommandConsumer( tenantId, deviceId, commandContext -> { Tags.COMPONENT.set(commandContext.getCurrentSpan(), getTypeName()); final Command command = commandContext.getCommand(); if (command.isValid()) { cancelCommandReceptionTimer(ctx); responseReady.tryComplete(); if (!responseReady.isComplete()) { addCommandReceptionTimer(ctx, responseReady, ttdSecs);
/** * Creates the router for handling requests. * <p> * This method creates a router instance with the following routes: * <ol> * <li>A default route limiting the body size of requests to the maximum payload size set in the <em>config</em> properties.</li> * </ol> * * @return The newly created router (never {@code null}). */ protected Router createRouter() { final Router router = Router.router(vertx); LOG.info("limiting size of inbound request body to {} bytes", getConfig().getMaxPayloadSize()); router.route().handler(BodyHandler.create(DEFAULT_UPLOADS_DIRECTORY).setBodyLimit(getConfig().getMaxPayloadSize())); addTracingHandler(router, -5); // add default handler for failed routes router.route().order(-1).failureHandler(new DefaultFailureHandler()); return router; }
@Override public final void doStart(final Future<Void> startFuture) { checkPortConfiguration() .compose(s -> preStartup()) .compose(s -> { final Router router = createRouter(); if (router == null) { return Future.failedFuture("no router configured"); } else { addRoutes(router); return CompositeFuture.all(bindSecureHttpServer(router), bindInsecureHttpServer(router)); } }).compose(s -> { try { onStartupSuccess(); startFuture.complete(); } catch (final Exception e) { LOG.error("error in onStartupSuccess", e); startFuture.fail(e); } }, startFuture); }
/** * Gets the options to use for creating the TLS secured http server. * <p> * Subclasses may override this method in order to customize the server. * <p> * This method returns default options with the host and port being set to the corresponding values * from the <em>config</em> properties and using a maximum chunk size of 4096 bytes. * * @return The http server options. */ protected HttpServerOptions getHttpServerOptions() { final HttpServerOptions options = new HttpServerOptions(); options.setHost(getConfig().getBindAddress()).setPort(getConfig().getPort(getPortDefaultValue())) .setMaxChunkSize(4096); addTlsKeyCertOptions(options); addTlsTrustOptions(options); return options; }
/** * Adds a handler for adding an OpenTracing Span to the routing context. * * @param router The router to add the handler to. * @param position The position to add the tracing handler at. */ private void addTracingHandler(final Router router, final int position) { final Map<String, String> customTags = new HashMap<>(); customTags.put(Tags.COMPONENT.getKey(), getTypeName()); addCustomTags(customTags); final List<WebSpanDecorator> decorators = new ArrayList<>(); decorators.add(new ComponentMetaDataDecorator(customTags)); addCustomSpanDecorators(decorators); final TracingHandler tracingHandler = new TracingHandler(tracer, decorators); router.route().order(position).handler(tracingHandler).failureHandler(tracingHandler); }