List of usage examples for io.netty.buffer ByteBufOutputStream ByteBufOutputStream
public ByteBufOutputStream(ByteBuf buffer)
From source file:ratpack.codahale.metrics.internal.MetricRegistryJsonMapper.java
License:Apache License
@Override public ByteBuf apply(MetricRegistry metricRegistry) throws Exception { ByteBuf byteBuf = byteBufAllocator.ioBuffer(); try {//from w ww. j a v a2 s .co m OutputStream out = new ByteBufOutputStream(byteBuf); JsonGenerator json = factory.createGenerator(out); json.writeStartObject(); json.writeNumberField("timestamp", clock.getTime()); writeTimers(json, metricRegistry.getTimers()); writeGauges(json, metricRegistry.getGauges()); writeMeters(json, metricRegistry.getMeters()); writeCounters(json, metricRegistry.getCounters()); writeHistograms(json, metricRegistry.getHistograms()); json.writeEndObject(); json.flush(); json.close(); return byteBuf; } catch (Exception e) { byteBuf.release(); throw e; } }
From source file:ratpack.dropwizard.metrics.internal.MetricRegistryJsonMapper.java
License:Apache License
@Override public ByteBuf apply(MetricRegistry metricRegistry) throws Exception { ByteBuf byteBuf = byteBufAllocator.ioBuffer(); try {/*from w w w. j a v a 2 s . c o m*/ OutputStream out = new ByteBufOutputStream(byteBuf); mapper.writeValue(out, metricRegistry); return byteBuf; } catch (Exception e) { byteBuf.release(); throw e; } }
From source file:ratpack.dropwizard.metrics.MetricsPrometheusHandler.java
License:Apache License
@Override public void handle(Context ctx) throws Exception { final ByteBufAllocator byteBufAllocator = ctx.get(ByteBufAllocator.class); ByteBuf buf = byteBufAllocator.ioBuffer(); try (Writer w = new OutputStreamWriter(new ByteBufOutputStream(buf))) { TextFormat.write004(w, ctx.get(CollectorRegistry.class).metricFamilySamples()); } catch (IOException e) { buf.release();/*from www . j av a 2s . c o m*/ throw e; } ctx.getResponse().contentType(TextFormat.CONTENT_TYPE_004).status(200).send(buf); }
From source file:ratpack.error.internal.ErrorPageRenderer.java
License:Apache License
protected void render(Context context, String pageTitle, Consumer<? super BodyWriter> body) { ByteBuf buffer = context.get(ByteBufAllocator.class).buffer(); OutputStream out = new ByteBufOutputStream(buffer); PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(out, CharsetUtil.UTF_8)); BodyWriter writer = new BodyWriter(printWriter); writer.println("<!DOCTYPE html>").println("<html>").println("<head>").print(" <title>").escape(pageTitle) .println("</title>").println(" <style type=\"text/css\">").println(style).println(" </style>") .println("</head>").println("<body>").println(" <header>").println(" <div class=\"logo\">") .println(" <div class=\"martini\">").println(" <h1>Ratpack</h1>") .println(" </div>").println(" <p>Development error page</p>").println(" </div>") .println(" </header>"); body.accept(writer);/*from w w w. j a va2 s. c om*/ writer.println("<footer>").println(" <a href=\"http://www.ratpack.io\">Ratpack.io</a>") .println("</footer>").println("</body>").println("</html>"); printWriter.close(); context.getResponse().send(HttpHeaderConstants.HTML_UTF_8, buffer); }
From source file:ratpack.groovy.template.internal.MarkupTemplateRenderer.java
License:Apache License
@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;// w ww .j av a 2s . c o m 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); } }
From source file:ratpack.gson.internal.GsonRenderer.java
License:Apache License
@Override public void render(Context ctx, GsonRender object) throws Exception { Gson gson = object.getGson();//from w ww. j av a 2 s .co m if (gson == null) { gson = ctx.maybeGet(Gson.class).orElseGet(() -> new GsonBuilder().create()); } ByteBuf buffer = ctx.get(ByteBufAllocator.class).buffer(); OutputStream outputStream = new ByteBufOutputStream(buffer); Writer writer = new OutputStreamWriter(outputStream); try { gson.toJson(object.getObject(), writer); writer.flush(); } catch (JsonIOException e) { buffer.release(); ctx.error(e); return; } ctx.getResponse().contentTypeIfNotSet(HttpHeaderConstants.JSON).send(buffer); }
From source file:ratpack.health.internal.HealthCheckResultsRenderer.java
License:Apache License
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") @Override//w w w . j av a2s. c o m public void render(Context ctx, HealthCheckResults healthCheckResults) throws Exception { ByteBuf buffer = byteBufAllocator.buffer(); try (Writer writer = new OutputStreamWriter(new BufferedOutputStream(new ByteBufOutputStream(buffer)))) { healthCheckResults.writeTo(writer); } catch (Exception e) { buffer.release(); throw e; } ctx.getResponse().contentTypeIfNotSet(HttpHeaderConstants.PLAIN_TEXT_UTF8) .status(healthCheckResults.isUnhealthy() ? 503 : 200).send(buffer); }
From source file:ratpack.jackson.internal.JsonRenderer.java
License:Apache License
@Override public void render(Context context, JsonRender object) throws Exception { ObjectWriter writer = object.getObjectWriter(); if (writer == null) { writer = defaultObjectWriter;// w ww .java 2 s. c o m } ByteBuf buffer = context.get(ByteBufAllocator.class).buffer(); OutputStream outputStream = new ByteBufOutputStream(buffer); try { writer.writeValue(outputStream, object.getObject()); } catch (JsonProcessingException e) { buffer.release(); context.error(e); return; } context.getResponse().contentTypeIfNotSet(HttpHeaderConstants.JSON).send(buffer); }
From source file:ratpack.session.internal.DefaultSession.java
License:Apache License
private ByteBuf serialize() throws Exception { SerializedForm serializable = new SerializedForm(); serializable.entries = entries;/* w w w. j ava 2 s . c o m*/ 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; } }
From source file:ratpack.session.internal.DefaultSessionAdapter.java
License:Apache License
private ByteBuf serialize() { Data data = new Data(ImmutableMap.copyOf(strings), ImmutableMap.copyOf(objects)); ByteBuf buffer = bufferAllocator.buffer(); OutputStream outputStream = new ByteBufOutputStream(buffer); try {//from w w w . j a v a 2s.co m defaultSerializer.serialize(Data.class, data, outputStream); outputStream.close(); return buffer; } catch (Throwable e) { buffer.release(); throw Exceptions.uncheck(e); } }