Search in sources :

Example 41 with HttpServerResponse

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();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RoutingContext(io.vertx.ext.web.RoutingContext) ProtonDelivery(io.vertx.proton.ProtonDelivery) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServer(io.vertx.core.http.HttpServer) Test(org.junit.Test)

Example 42 with HttpServerResponse

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 &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, 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();
    };
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) Predicate(java.util.function.Predicate) Vertx(io.vertx.core.Vertx) Autowired(org.springframework.beans.factory.annotation.Autowired) MIMEHeader(io.vertx.ext.web.MIMEHeader) RoutingContext(io.vertx.ext.web.RoutingContext) AbstractEndpoint(org.eclipse.hono.service.AbstractEndpoint) MessageHelper(org.eclipse.hono.util.MessageHelper) RequestResponseApiConstants(org.eclipse.hono.util.RequestResponseApiConstants) Constants(org.eclipse.hono.util.Constants) Objects(java.util.Objects) HttpServerResponse(io.vertx.core.http.HttpServerResponse) BiConsumer(java.util.function.BiConsumer) Qualifier(org.springframework.beans.factory.annotation.Qualifier) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) HttpServerResponse(io.vertx.core.http.HttpServerResponse)

Aggregations

HttpServerResponse (io.vertx.core.http.HttpServerResponse)42 Test (org.junit.Test)32 Buffer (io.vertx.core.buffer.Buffer)22 HttpClientRequest (io.vertx.core.http.HttpClientRequest)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 HttpClientOptions (io.vertx.core.http.HttpClientOptions)17 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 HttpMethod (io.vertx.core.http.HttpMethod)15 HttpServerOptions (io.vertx.core.http.HttpServerOptions)15 NetSocket (io.vertx.core.net.NetSocket)14 TimeUnit (java.util.concurrent.TimeUnit)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 ChannelFuture (io.netty.channel.ChannelFuture)13 Handler (io.vertx.core.Handler)13 HttpConnection (io.vertx.core.http.HttpConnection)13 HttpVersion (io.vertx.core.http.HttpVersion)13 TestUtils.assertIllegalStateException (io.vertx.test.core.TestUtils.assertIllegalStateException)13 ArrayList (java.util.ArrayList)13 Collections (java.util.Collections)13