Search in sources :

Example 6 with ByteBufOutputStream

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);
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStream(java.io.OutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) Writable(groovy.lang.Writable) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) MimeTypes(ratpack.file.MimeTypes) ByteBuf(io.netty.buffer.ByteBuf) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) MarkupTemplate(ratpack.groovy.template.MarkupTemplate) Template(groovy.text.Template)

Example 7 with ByteBufOutputStream

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;
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf)

Example 8 with ByteBufOutputStream

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;
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStream(java.io.OutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf)

Example 9 with ByteBufOutputStream

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);
    }));
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ForStorage(org.curioswitch.curiostack.gcloud.storage.StorageModule.ForStorage) Singleton(javax.inject.Singleton) HttpHeaderNames(com.linecorp.armeria.common.HttpHeaderNames) MediaType(com.linecorp.armeria.common.MediaType) Inject(javax.inject.Inject) ByteBuf(io.netty.buffer.ByteBuf) HttpStatus(com.linecorp.armeria.common.HttpStatus) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) Map(java.util.Map) HttpData(com.linecorp.armeria.common.HttpData) HttpResponse(com.linecorp.armeria.common.HttpResponse) DataOutput(java.io.DataOutput) CompletableFuturesExtra(com.spotify.futures.CompletableFuturesExtra) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) HttpMethod(com.linecorp.armeria.common.HttpMethod) Immutable(org.immutables.value.Value.Immutable) ByteBufHttpData(com.linecorp.armeria.unsafe.ByteBufHttpData) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) RequestContext(com.linecorp.armeria.common.RequestContext) UncheckedIOException(java.io.UncheckedIOException) HttpClient(com.linecorp.armeria.client.HttpClient) HttpHeaders(com.linecorp.armeria.common.HttpHeaders) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) HttpHeaders(com.linecorp.armeria.common.HttpHeaders) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) HttpResponse(com.linecorp.armeria.common.HttpResponse) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ByteBuf(io.netty.buffer.ByteBuf) HttpData(com.linecorp.armeria.common.HttpData) ByteBufHttpData(com.linecorp.armeria.unsafe.ByteBufHttpData) ByteBufHttpData(com.linecorp.armeria.unsafe.ByteBufHttpData)

Example 10 with ByteBufOutputStream

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;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)52 ByteBuf (io.netty.buffer.ByteBuf)37 IOException (java.io.IOException)18 ObjectOutputStream (java.io.ObjectOutputStream)11 OutputStreamWriter (java.io.OutputStreamWriter)6 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)5 OutputStream (java.io.OutputStream)5 Test (org.junit.jupiter.api.Test)5 ObjectInputStream (java.io.ObjectInputStream)4 SneakyThrows (lombok.SneakyThrows)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)3 Writer (java.io.Writer)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)2 Bootstrap (io.netty.bootstrap.Bootstrap)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2