Search in sources :

Example 1 with BinaryOutStream

use of org.folio.rest.tools.utils.BinaryOutStream in project raml-module-builder by folio-org.

the class RestVerticle method sendResponse.

/**
 * Send the result as response.
 *
 * @param rc
 *          - where to send the result
 * @param v
 *          - the result to send
 * @param start
 *          - request's start time, using JVM's high-resolution time source, in nanoseconds
 */
private void sendResponse(RoutingContext rc, AsyncResult<Response> v, long start, String tenantId) {
    Response result = ((Response) ((AsyncResult<?>) v).result());
    if (result == null) {
        // catch all
        endRequestWithError(rc, 500, true, "Server error", new boolean[] { true });
        return;
    }
    Object entity = null;
    try {
        HttpServerResponse response = rc.response();
        int statusCode = result.getStatus();
        // a chunked Transfer header is not allowed
        if (statusCode != 204) {
            response.setChunked(true);
        }
        response.setStatusCode(statusCode);
        for (Entry<String, List<String>> entry : result.getStringHeaders().entrySet()) {
            String jointValue = Joiner.on("; ").join(entry.getValue());
            response.headers().add(entry.getKey(), jointValue);
        }
        // response.headers().add(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate");
        // forward all headers except content-type that was passed in by the client
        // since this may cause problems when for example application/octet-stream is
        // sent as part of an upload. passing this back will confuse clients as they
        // will think they are getting back a stream of data which may not be the case
        rc.request().headers().remove("Content-type");
        // should not be forwarded in cases of no content
        if (statusCode == 204) {
            rc.request().headers().remove("transfer-encoding");
        }
        mergeIntoResponseHeadersDistinct(response.headers(), rc.request().headers());
        entity = result.getEntity();
        /* entity is of type OutStream - and will be written as a string */
        if (entity instanceof OutStream) {
            response.write(MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(((OutStream) entity).getData()));
        } else /* entity is of type BinaryOutStream - and will be written as a buffer */
        if (entity instanceof BinaryOutStream) {
            response.write(Buffer.buffer(((BinaryOutStream) entity).getData()));
        } else /* data is a string so just push it out, no conversion needed */
        if (entity instanceof String) {
            response.write(Buffer.buffer((String) entity));
        } else /* catch all - anything else will be assumed to be a pojo which needs converting to json */
        if (entity != null) {
            response.write(MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(entity));
        }
    } catch (Exception e) {
        log.error(e);
    } finally {
        rc.response().end();
    }
    long end = System.nanoTime();
    StringBuilder sb = new StringBuilder();
    if (log.isDebugEnabled()) {
        try {
            sb.append(MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(entity));
        } catch (Exception e) {
            String name = "null";
            if (entity != null) {
                name = entity.getClass().getName();
            }
            log.error("writeValueAsString(" + name + ")", e);
        }
    }
    LogUtil.formatStatsLogMessage(rc.request().remoteAddress().toString(), rc.request().method().toString(), rc.request().version().toString(), rc.response().getStatusCode(), (((end - start) / 1000000)), rc.response().bytesWritten(), rc.request().path(), rc.request().query(), rc.response().getStatusMessage(), tenantId, sb.toString());
}
Also used : BinaryOutStream(org.folio.rest.tools.utils.BinaryOutStream) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Response(javax.ws.rs.core.Response) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonObject(io.vertx.core.json.JsonObject) List(java.util.List) ArrayList(java.util.ArrayList) BinaryOutStream(org.folio.rest.tools.utils.BinaryOutStream) OutStream(org.folio.rest.tools.utils.OutStream) AsyncResult(io.vertx.core.AsyncResult)

Aggregations

UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)1 AsyncResult (io.vertx.core.AsyncResult)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 JsonObject (io.vertx.core.json.JsonObject)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 MessagingException (javax.mail.MessagingException)1 Response (javax.ws.rs.core.Response)1 BinaryOutStream (org.folio.rest.tools.utils.BinaryOutStream)1 OutStream (org.folio.rest.tools.utils.OutStream)1