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