use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project ratpack by ratpack.
the class MarkupTemplateRenderer method render.
@Override
public void render(Context ctx, MarkupTemplate template) throws Exception {
String contentType = template.getContentType();
contentType = contentType == null ? ctx.get(MimeTypes.class).getContentType(template.getName()) : contentType;
try {
Template compiledTemplate = engine.createTemplateByPath(template.getName());
Writable boundTemplate = compiledTemplate.make(template.getModel());
ByteBuf byteBuf = byteBufAllocator.directBuffer();
try {
OutputStream outputStream = new ByteBufOutputStream(byteBuf);
Writer writer = new OutputStreamWriter(outputStream, CharsetUtil.encoder(StandardCharsets.UTF_8));
boundTemplate.writeTo(writer);
} catch (Exception e) {
byteBuf.release();
throw e;
}
ctx.getResponse().send(contentType, byteBuf);
} catch (IOException e) {
ctx.error(e);
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project ratpack by ratpack.
the class DefaultSession method serialize.
private ByteBuf serialize() throws Exception {
SerializedForm serializable = new SerializedForm();
serializable.entries = entries;
ByteBuf buffer = bufferAllocator.buffer();
OutputStream outputStream = new ByteBufOutputStream(buffer);
try {
defaultSerializer.serialize(SerializedForm.class, serializable, outputStream);
outputStream.close();
return buffer;
} catch (Throwable e) {
buffer.release();
throw e;
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project ratpack by ratpack.
the class MetricRegistryJsonMapper method apply.
@Override
public ByteBuf apply(MetricRegistry metricRegistry) throws Exception {
ByteBuf byteBuf = byteBufAllocator.ioBuffer();
try {
OutputStream out = new ByteBufOutputStream(byteBuf);
mapper.writeValue(out, metricRegistry);
return byteBuf;
} catch (Exception e) {
byteBuf.release();
throw e;
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project curiostack by curioswitch.
the class StorageClient method createFile.
/**
* Create a new file for uploading data to cloud storage.
*/
public ListenableFuture<FileWriter> createFile(String filename, Map<String, String> metadata, RequestContext ctx) {
FileRequest request = ImmutableFileRequest.builder().name(filename).metadata(metadata).build();
ByteBuf buf = ctx.alloc().buffer();
try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) {
OBJECT_MAPPER.writeValue((DataOutput) os, request);
} catch (IOException e) {
buf.release();
throw new UncheckedIOException("Could not serialize resource JSON to buffer.", e);
}
HttpData data = new ByteBufHttpData(buf, true);
HttpHeaders headers = HttpHeaders.of(HttpMethod.POST, uploadUrl).contentType(MediaType.JSON_UTF_8);
HttpResponse res = httpClient.execute(headers, data);
return CompletableFuturesExtra.toListenableFuture(res.aggregate(ctx.contextAwareEventLoop()).handle((msg, t) -> {
if (t != null) {
throw new RuntimeException("Unexpected error creating new file.", t);
}
HttpHeaders responseHeaders = msg.headers();
if (!responseHeaders.status().equals(HttpStatus.OK)) {
throw new RuntimeException("Non-successful response when creating new file: " + responseHeaders + "\n" + msg.content().toStringUtf8());
}
String location = responseHeaders.get(HttpHeaderNames.LOCATION);
String pathAndQuery = location.substring("https://www.googleapis.com/upload/storage/v1".length());
return new FileWriter(pathAndQuery, ctx, httpClient);
}));
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project rskj by rsksmart.
the class JsonRpcWeb3ServerHandler method processRequest.
private HttpResponse processRequest(FullHttpRequest request) throws JsonProcessingException {
HttpResponse response;
ByteBuf responseContent = Unpooled.buffer();
HttpResponseStatus responseStatus = HttpResponseStatus.OK;
try (ByteBufOutputStream os = new ByteBufOutputStream(responseContent);
ByteBufInputStream is = new ByteBufInputStream(request.content().retain())) {
int result = jsonRpcServer.handleRequest(is, os);
responseStatus = HttpResponseStatus.valueOf(DefaultHttpStatusCodeProvider.INSTANCE.getHttpStatusCode(result));
} catch (Exception e) {
LOGGER.error("Unexpected error", e);
responseContent = buildErrorContent(JSON_RPC_SERVER_ERROR_HIGH_CODE, HttpResponseStatus.INTERNAL_SERVER_ERROR.reasonPhrase());
responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR;
} finally {
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, responseStatus, responseContent);
}
return response;
}
Aggregations