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 >= 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();
};
}
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 >= 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();
};
}
Aggregations