/** * 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; }
/** * Gets the options to use for creating the insecure 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 getInsecureHttpServerOptions() { final HttpServerOptions options = new HttpServerOptions(); options.setHost(getConfig().getInsecurePortBindAddress()).setPort(getConfig().getInsecurePort(getInsecurePortDefaultValue())).setMaxChunkSize(4096); return options; }
/** * Exposes the HTTP adapter's configuration properties as a Spring bean. * * @return The configuration properties. */ @Bean @ConfigurationProperties(prefix = "hono.http") public HttpProtocolAdapterProperties adapterProperties() { return new HttpProtocolAdapterProperties(); }
@Override protected void addRoutes(final Router router) { if (getConfig().isAuthenticationRequired()) { final ChainAuthHandler authHandler = new HonoChainAuthHandler(); authHandler.append(new X509AuthHandler( Optional.ofNullable(clientCertAuthProvider).orElse( new X509AuthProvider(getCredentialsServiceClient(), getConfig())), getTenantServiceClient(), tracer)); authHandler.append(new HonoBasicAuthHandler( Optional.ofNullable(usernamePasswordAuthProvider).orElse( new UsernamePasswordAuthProvider(getCredentialsServiceClient(), getConfig())), getConfig().getRealm())); addTelemetryApiRoutes(router, authHandler); addEventApiRoutes(router, authHandler); addCommandResponseRoutes(router, authHandler); } else { LOG.warn("device authentication has been disabled"); LOG.warn("any device may publish data on behalf of all other devices"); addTelemetryApiRoutes(router, null); addEventApiRoutes(router, null); addCommandResponseRoutes(router, null); } }
private Future<HttpServer> bindSecureHttpServer(final Router router) { if (isSecurePortEnabled()) { final Future<HttpServer> result = Future.future(); final String bindAddress = server == null ? getConfig().getBindAddress() : "?"; if (server == null) { server = vertx.createHttpServer(getHttpServerOptions()); } server.requestHandler(router).listen(done -> { if (done.succeeded()) { LOG.info("secure http server listening on {}:{}", bindAddress, server.actualPort()); result.complete(done.result()); } else { LOG.error("error while starting up secure http server", done.cause()); result.fail(done.cause()); } }); return result; } else { return Future.succeededFuture(); } }
private Future<HttpServer> bindInsecureHttpServer(final Router router) { if (isInsecurePortEnabled()) { final Future<HttpServer> result = Future.future(); final String bindAddress = insecureServer == null ? getConfig().getInsecurePortBindAddress() : "?"; if (insecureServer == null) { insecureServer = vertx.createHttpServer(getInsecureHttpServerOptions()); } insecureServer.requestHandler(router).listen(done -> { if (done.succeeded()) { LOG.info("insecure http server listening on {}:{}", bindAddress, insecureServer.actualPort()); result.complete(done.result()); } else { LOG.error("error while starting up insecure http server", done.cause()); result.fail(done.cause()); } }); return result; } else { return Future.succeededFuture(); } }
/** * 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; }
/** * Uploads a telemetry message to Hono. * <p> * This method always sends a response to the device. The status code will be set * as specified in the * <a href="https://www.eclipse.org/hono/user-guide/http-adapter/#publish-telemetry-data-authenticated-device"> * HTTP adapter User Guide</a>. * * @param ctx The context to retrieve cookies and the HTTP response from. * @param tenant The tenant of the device that has produced the data. * @param deviceId The id of the device that has produced the data. * @param payload The message payload to send. * @param contentType The content type of the message payload. * @throws NullPointerException if any of response, tenant or device ID is {@code null}. */ public final void uploadTelemetryMessage(final RoutingContext ctx, final String tenant, final String deviceId, final Buffer payload, final String contentType) { doUploadMessage( Objects.requireNonNull(ctx), Objects.requireNonNull(tenant), Objects.requireNonNull(deviceId), payload, contentType, getTelemetrySender(tenant), TelemetryConstants.TELEMETRY_ENDPOINT); }
/** * Uploads an event message to Hono. * <p> * This method always sends a response to the device. The status code will be set * as specified in the * <a href="https://www.eclipse.org/hono/user-guide/http-adapter/#publish-an-event-authenticated-device"> * HTTP adapter User Guide</a>. * * @param ctx The context to retrieve cookies and the HTTP response from. * @param tenant The tenant of the device that has produced the data. * @param deviceId The id of the device that has produced the data. * @param payload The message payload to send. * @param contentType The content type of the message payload. * @throws NullPointerException if any of response, tenant or device ID is {@code null}. */ public final void uploadEventMessage(final RoutingContext ctx, final String tenant, final String deviceId, final Buffer payload, final String contentType) { doUploadMessage( Objects.requireNonNull(ctx), Objects.requireNonNull(tenant), Objects.requireNonNull(deviceId), payload, contentType, getEventSender(tenant), EventConstants.EVENT_ENDPOINT); }
/** * Fails the context with the error code determined from an exception. * <p> * This method invokes {@link AuthHandlerTools#processException(RoutingContext, Throwable, String)}. * * @param ctx The routing context. * @param exception The cause of failure to process the request. */ @Override protected void processException(final RoutingContext ctx, final Throwable exception) { if (ctx.response().ended()) { return; } AuthHandlerTools.processException(ctx, exception, authenticateHeader(ctx)); }
@Override protected void addRoutes(final Router router) { if (getConfig().isAuthenticationRequired()) { final ChainAuthHandler authHandler = ChainAuthHandler.create(); authHandler.append(new X509AuthHandler( new TenantServiceBasedX509Authentication(getTenantServiceClient(), tracer), Optional.ofNullable(clientCertAuthProvider).orElse( new X509AuthProvider(getCredentialsServiceClient(), getConfig(), tracer)))); authHandler.append(new HonoBasicAuthHandler( Optional.ofNullable(usernamePasswordAuthProvider).orElse( new UsernamePasswordAuthProvider(getCredentialsServiceClient(), getConfig(), tracer)), getConfig().getRealm(), tracer)); addTelemetryApiRoutes(router, authHandler); addEventApiRoutes(router, authHandler); addCommandResponseRoutes(router, authHandler); } else { LOG.warn("device authentication has been disabled"); LOG.warn("any device may publish data on behalf of all other devices"); addTelemetryApiRoutes(router, null); addEventApiRoutes(router, null); addCommandResponseRoutes(router, null); } }
/** * 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; }
/** * Gets the options to use for creating the insecure 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 getInsecureHttpServerOptions() { final HttpServerOptions options = new HttpServerOptions(); options.setHost(getConfig().getInsecurePortBindAddress()).setPort(getConfig().getInsecurePort(getInsecurePortDefaultValue())).setMaxChunkSize(4096); return options; }
private Future<HttpServer> bindSecureHttpServer(final Router router) { if (isSecurePortEnabled()) { final Future<HttpServer> result = Future.future(); final String bindAddress = server == null ? getConfig().getBindAddress() : "?"; if (server == null) { server = vertx.createHttpServer(getHttpServerOptions()); } server.requestHandler(router::accept).listen(done -> { if (done.succeeded()) { LOG.info("secure http server listening on {}:{}", bindAddress, server.actualPort()); result.complete(done.result()); } else { LOG.error("error while starting up secure http server", done.cause()); result.fail(done.cause()); } }); return result; } else { return Future.succeededFuture(); } }
/** * 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; }
/** * Uploads a telemetry message to Hono. * <p> * This method always sends a response to the device. The status code will be set * as specified in the * <a href="https://www.eclipse.org/hono/user-guide/http-adapter/#publish-telemetry-data-authenticated-device"> * HTTP adapter User Guide</a>. * * @param ctx The context to retrieve cookies and the HTTP response from. * @param tenant The tenant of the device that has produced the data. * @param deviceId The id of the device that has produced the data. * @param payload The message payload to send. * @param contentType The content type of the message payload. * @throws NullPointerException if any of response, tenant or device ID is {@code null}. */ public final void uploadTelemetryMessage(final RoutingContext ctx, final String tenant, final String deviceId, final Buffer payload, final String contentType) { doUploadMessage( Objects.requireNonNull(ctx), Objects.requireNonNull(tenant), Objects.requireNonNull(deviceId), payload, contentType, getTelemetrySender(tenant), TelemetryConstants.TELEMETRY_ENDPOINT); }
/** * Uploads an event message to Hono. * <p> * This method always sends a response to the device. The status code will be set * as specified in the * <a href="https://www.eclipse.org/hono/user-guide/http-adapter/#publish-an-event-authenticated-device"> * HTTP adapter User Guide</a>. * * @param ctx The context to retrieve cookies and the HTTP response from. * @param tenant The tenant of the device that has produced the data. * @param deviceId The id of the device that has produced the data. * @param payload The message payload to send. * @param contentType The content type of the message payload. * @throws NullPointerException if any of response, tenant or device ID is {@code null}. */ public final void uploadEventMessage(final RoutingContext ctx, final String tenant, final String deviceId, final Buffer payload, final String contentType) { doUploadMessage( Objects.requireNonNull(ctx), Objects.requireNonNull(tenant), Objects.requireNonNull(deviceId), payload, contentType, getEventSender(tenant), EventConstants.EVENT_ENDPOINT); }
/** * Exposes the HTTP adapter's configuration properties as a Spring bean. * * @return The configuration properties. */ @Bean @ConfigurationProperties(prefix = "hono.http") public HttpProtocolAdapterProperties adapterProperties() { return new HttpProtocolAdapterProperties(); }