Example usage for io.vertx.core.http HttpServerResponse setChunked

List of usage examples for io.vertx.core.http HttpServerResponse setChunked

Introduction

In this page you can find the example usage for io.vertx.core.http HttpServerResponse setChunked.

Prototype

@Fluent
HttpServerResponse setChunked(boolean chunked);

Source Link

Document

If chunked is true , this response will use HTTP chunked encoding, and each call to write to the body will correspond to a new HTTP chunk sent on the wire.

Usage

From source file:com.englishtown.vertx.jersey.impl.VertxResponseWriter.java

License:Open Source License

/**
 * {@inheritDoc}//  ww w. j a  v a 2 s .co  m
 */
@Override
public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext)
        throws ContainerException {
    jerseyResponse = responseContext;
    HttpServerResponse response = vertxRequest.response();

    // Write the status
    response.setStatusCode(responseContext.getStatus());
    response.setStatusMessage(responseContext.getStatusInfo().getReasonPhrase());

    // Set the content length header
    if (contentLength != -1) {
        response.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength));
    }

    for (final Map.Entry<String, List<String>> e : responseContext.getStringHeaders().entrySet()) {
        for (final String value : e.getValue()) {
            response.putHeader(e.getKey(), value);
        }
    }

    // Run any response processors
    if (!responseProcessors.isEmpty()) {
        for (VertxResponseProcessor processor : responseProcessors) {
            processor.process(response, responseContext);
        }
    }

    // Return output stream based on whether entity is chunked
    if (responseContext.isChunked()) {
        response.setChunked(true);
        return new VertxChunkedOutputStream(response);
    } else if (responseContext.hasEntity()
            && WriteStreamOutput.class.isAssignableFrom(responseContext.getEntityClass())) {
        WriteStreamOutput writeStreamOutput = (WriteStreamOutput) responseContext.getEntity();
        writeStreamOutput.init(response, event -> end());
        isWriteStream = true;
        return new NOPOutputStream();
    } else {
        return new VertxOutputStream(response);
    }
}

From source file:com.opinionlab.woa.WallOfAwesome.java

License:Open Source License

private static Handler<RoutingContext> makeDownloadRoute() {
    return routingContext -> EXECUTOR.execute(() -> {
        try {/*  w  w w  . j a  v  a 2 s .co  m*/
            final HttpServerResponse response = routingContext.response();
            final AtomicBoolean first = new AtomicBoolean(true);
            response.putHeader("Content-Type", "text/plain");
            response.putHeader("Content-Disposition", "inline;filename=awesome.txt");

            response.setChunked(true);
            response.write("BEGIN AWESOME\n\n");
            AwesomeImap.fetchAwesome().forEach(awesome -> {
                if (!first.get()) {
                    response.write("\n\n---\n\n");
                } else {
                    first.set(false);
                }

                response.write(new ST(AWESOME_TEMPLATE).add("awesome", awesome).render());
            });
            response.write("\n\nEND AWESOME");
            response.end();
        } catch (Throwable t) {
            LOGGER.error("Unable to fetch messages.", t);
        }
    });
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example23(HttpServerRequest request) {
    HttpServerResponse response = request.response();
    response.setChunked(true);
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example24(HttpServerRequest request) {
    HttpServerResponse response = request.response();
    response.setChunked(true);
    MultiMap trailers = response.trailers();
    trailers.set("X-wibble", "woobble").set("X-quux", "flooble");
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example25(HttpServerRequest request) {
    HttpServerResponse response = request.response();
    response.setChunked(true);
    response.putTrailer("X-wibble", "woobble").putTrailer("X-quux", "flooble");
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example27(Vertx vertx) {
    vertx.createHttpServer().requestHandler(request -> {
        HttpServerResponse response = request.response();
        if (request.method() == HttpMethod.PUT) {
            response.setChunked(true);
            Pump.pump(request, response).start();
            request.endHandler(v -> response.end());
        } else {// ww  w  . j a va  2s.co m
            response.setStatusCode(400).end();
        }
    }).listen(8080);
}

From source file:io.apiman.gateway.platforms.vertx3.api.IRouteBuilder.java

License:Apache License

default <T extends Throwable> void error(RoutingContext context, HttpResponseStatus code, String message,
        T object) {/*from w w w.  j av  a2  s  .  co  m*/
    HttpServerResponse response = context.response().setStatusCode(code.code());
    response.putHeader("X-API-Gateway-Error", "true");

    if (message == null) {
        response.setStatusMessage(code.reasonPhrase());
    } else {
        response.setStatusMessage(message);
    }

    if (object != null) {
        JsonObject errorResponse = new JsonObject().put("errorType", object.getClass().getSimpleName())
                .put("message", object.getMessage());

        response.setChunked(true).putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
                .end(errorResponse.toString(), "UTF-8");
    } else {
        response.end();
    }
}

From source file:io.apiman.gateway.platforms.vertx3.http.HttpPolicyAdapter.java

License:Apache License

private static void handleError(HttpServerResponse response, Throwable error) {
    response.setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
    response.setStatusMessage(HttpResponseStatus.INTERNAL_SERVER_ERROR.reasonPhrase());
    response.headers().add("X-Gateway-Error", String.valueOf(error.getMessage())); //$NON-NLS-1$
    response.headers().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);

    EngineErrorResponse errorResponse = new EngineErrorResponse();
    errorResponse.setResponseCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
    errorResponse.setMessage(error.getMessage());
    errorResponse.setTrace(error);//from  w w  w .  j  av a 2 s .  c o  m
    response.setChunked(true);
    response.write(Json.encode(errorResponse));
    response.end();
}

From source file:io.apiman.gateway.platforms.vertx3.http.HttpPolicyAdapter.java

License:Apache License

private static void handlePolicyFailure(HttpServerResponse response, PolicyFailure failure) {
    response.headers().add("X-Policy-Failure-Type", String.valueOf(failure.getType())); //$NON-NLS-1$
    response.headers().add("X-Policy-Failure-Message", failure.getMessage()); //$NON-NLS-1$
    response.headers().add("X-Policy-Failure-Code", String.valueOf(failure.getFailureCode())); //$NON-NLS-1$
    response.headers().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);

    int code = HttpResponseStatus.INTERNAL_SERVER_ERROR.code();

    switch (failure.getType()) {
    case Authentication:
        code = HttpResponseStatus.UNAUTHORIZED.code();
        break;/*from  w  w w.j a v a2 s  .c o  m*/
    case Authorization:
        code = HttpResponseStatus.FORBIDDEN.code();
        break;
    case NotFound:
        code = HttpResponseStatus.NOT_FOUND.code();
        break;
    case Other:
        code = failure.getResponseCode();
        break;
    }

    response.setStatusCode(code);
    response.setStatusMessage(failure.getMessage());

    for (Map.Entry<String, String> entry : failure.getHeaders()) {
        response.headers().add(entry.getKey(), entry.getValue());
    }
    response.setChunked(true);
    response.end(Json.encode(failure));
}

From source file:io.flowly.core.router.BaseRouter.java

License:Open Source License

public static void writeSuccessResponse(RoutingContext routingContext, String body, boolean chunked) {
    HttpServerResponse response = routingContext.response();

    if (chunked) {
        response.setChunked(true);
    } else {//from   w  ww  .  ja  v  a 2  s . com
        setContentLength(response, body);
    }

    response.write(body).end();
}