Search in sources :

Example 31 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class AbstractEventBusHttpEndpoint method getDefaultResponseHandler.

/**
 * Gets a response handler that implements the default behavior for responding to an HTTP request.
 * <p>
 * The default behavior is as follows:
 * <ol>
 * <li>Set the status code on the response.</li>
 * <li>If the status code represents an error condition (i.e. the code is &gt;= 400),
 * then the JSON object passed in to the returned handler is written to the response body.</li>
 * <li>Otherwise, if the given filter evaluates to {@code true} for the status code,
 * the given custom handler is invoked (if not {@code null}), then
 * the JSON object is written to the response body and </li>
 * </ol>
 *
 * @param ctx The routing context of the request.
 * @param successfulOutcomeFilter A predicate that evaluates to {@code true} for the status code(s) representing a
 *                           successful outcome.
 * @param customHandler An (optional) handler for post processing the HTTP response, e.g. to set any additional HTTP
 *                        headers. The handler <em>must not</em> write to response body. May be {@code null}.
 * @return The created handler for processing responses.
 * @throws NullPointerException If routing context or filter is {@code null}.
 */
protected final BiConsumer<Integer, EventBusMessage> getDefaultResponseHandler(final RoutingContext ctx, final IntPredicate successfulOutcomeFilter, final BiConsumer<HttpServerResponse, EventBusMessage> customHandler) {
    Objects.requireNonNull(successfulOutcomeFilter);
    final HttpServerResponse response = ctx.response();
    return (status, result) -> {
        response.setStatusCode(status);
        if (status >= 400) {
            HttpUtils.setResponseBody(response, result.getJsonPayload());
        } else if (successfulOutcomeFilter.test(status)) {
            if (customHandler != null) {
                customHandler.accept(response, result);
            }
            HttpUtils.setResponseBody(response, result.getJsonPayload());
        }
        response.end();
    };
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) EventBusMessage(org.eclipse.hono.util.EventBusMessage) RoutingContext(io.vertx.ext.web.RoutingContext) IntPredicate(java.util.function.IntPredicate) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Objects(java.util.Objects) HttpServerResponse(io.vertx.core.http.HttpServerResponse) BiConsumer(java.util.function.BiConsumer) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) Strings(org.eclipse.hono.util.Strings) HttpServerResponse(io.vertx.core.http.HttpServerResponse)

Example 32 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class AbstractEventBusHttpEndpoint method getDefaultResponseHandler.

/**
 * Gets a response handler that implements the default behavior for responding to an HTTP request.
 * <p>
 * The default behavior is as follows:
 * <ol>
 * <li>Set the status code on the response.</li>
 * <li>If the status code represents an error condition (i.e. the code is &gt;= 400),
 * then the JSON object passed in to the returned handler is written to the response body.</li>
 * <li>Otherwise, if the given filter evaluates to {@code true} for the status code,
 * the JSON object is written to the response body and the given custom handler is
 * invoked (if not {@code null}).</li>
 * </ol>
 *
 * @param ctx The routing context of the request.
 * @param successfulOutcomeFilter A predicate that evaluates to {@code true} for the status code(s) representing a
 *                           successful outcome.
 * @param customHandler An (optional) handler for post processing the HTTP response, e.g. to set any additional HTTP
 *                        headers. The handler <em>must not</em> write to response body. May be {@code null}.
 * @return The created handler for processing responses.
 * @throws NullPointerException If routing context or filter is {@code null}.
 */
protected final BiConsumer<Integer, EventBusMessage> getDefaultResponseHandler(final RoutingContext ctx, final IntPredicate successfulOutcomeFilter, final Handler<HttpServerResponse> customHandler) {
    Objects.requireNonNull(successfulOutcomeFilter);
    final HttpServerResponse response = ctx.response();
    return (status, responseMessage) -> {
        response.setStatusCode(status);
        if (status >= 400) {
            HttpUtils.setResponseBody(response, responseMessage.getJsonPayload());
        } else if (successfulOutcomeFilter.test(status)) {
            HttpUtils.setResponseBody(response, responseMessage.getJsonPayload());
            if (customHandler != null) {
                customHandler.handle(response);
            }
        }
        response.end();
    };
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) EventBusMessage(org.eclipse.hono.util.EventBusMessage) RoutingContext(io.vertx.ext.web.RoutingContext) IntPredicate(java.util.function.IntPredicate) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Objects(java.util.Objects) HttpServerResponse(io.vertx.core.http.HttpServerResponse) BiConsumer(java.util.function.BiConsumer) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) Strings(org.eclipse.hono.util.Strings) HttpServerResponse(io.vertx.core.http.HttpServerResponse)

Aggregations

EventBusMessage (org.eclipse.hono.util.EventBusMessage)32 JsonObject (io.vertx.core.json.JsonObject)28 Future (io.vertx.core.Future)26 Handler (io.vertx.core.Handler)26 HttpURLConnection (java.net.HttpURLConnection)26 AsyncResult (io.vertx.core.AsyncResult)21 ServiceInvocationException (org.eclipse.hono.client.ServiceInvocationException)18 TestContext (io.vertx.ext.unit.TestContext)16 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)16 ServiceConfigProperties (org.eclipse.hono.config.ServiceConfigProperties)16 Test (org.junit.Test)16 Timeout (org.junit.rules.Timeout)16 RunWith (org.junit.runner.RunWith)16 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)13 CredentialsObject (org.eclipse.hono.util.CredentialsObject)13 CredentialsResult (org.eclipse.hono.util.CredentialsResult)13 BeforeClass (org.junit.BeforeClass)13 Objects (java.util.Objects)12 TenantConstants (org.eclipse.hono.util.TenantConstants)11 ClientErrorException (org.eclipse.hono.client.ClientErrorException)8