use of io.vertx.core.http.HttpServerResponse in project hono by eclipse.
the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadEventWaitsForAcceptedOutcome.
/**
* Verifies that the adapter waits for an event being settled and accepted
* by a downstream peer before responding with a 202 status to the device.
*/
@Test
public void testUploadEventWaitsForAcceptedOutcome() {
// GIVEN an adapter with a downstream event consumer attached
final Future<ProtonDelivery> outcome = Future.future();
givenAnEventSenderForOutcome(outcome);
HttpServer server = getHttpServer(false);
AbstractVertxBasedHttpProtocolAdapter<HttpProtocolAdapterProperties> adapter = getAdapter(server, null);
// WHEN a device publishes an event
final Buffer payload = Buffer.buffer("some payload");
final HttpServerResponse response = mock(HttpServerResponse.class);
final RoutingContext ctx = newRoutingContext(payload, response);
adapter.uploadEventMessage(ctx, "tenant", "device", payload, "application/text");
// THEN the device does not get a response
verify(response, never()).end();
// until the event has been accepted
outcome.complete(mock(ProtonDelivery.class));
verify(response).setStatusCode(202);
verify(response).end();
}
use of io.vertx.core.http.HttpServerResponse in project hono by eclipse.
the class AbstractHttpEndpoint 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, JsonObject> getDefaultResponseHandler(final RoutingContext ctx, final Predicate<Integer> successfulOutcomeFilter, final Handler<HttpServerResponse> customHandler) {
Objects.requireNonNull(successfulOutcomeFilter);
final HttpServerResponse response = ctx.response();
return (status, jsonResult) -> {
response.setStatusCode(status);
if (status >= 400) {
HttpUtils.setResponseBody(response, jsonResult);
} else if (successfulOutcomeFilter.test(status)) {
HttpUtils.setResponseBody(response, jsonResult);
if (customHandler != null) {
customHandler.handle(response);
}
}
response.end();
};
}
Aggregations