Example usage for com.google.common.io CountingOutputStream flush

List of usage examples for com.google.common.io CountingOutputStream flush

Introduction

In this page you can find the example usage for com.google.common.io CountingOutputStream flush.

Prototype

@Override
public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out to the stream.

Usage

From source file:io.druid.query.aggregation.atomcube.AtomCubeQueryResource.java

@POST
@Produces({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE })
@Consumes({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE, APPLICATION_SMILE })
public Response doPost(InputStream in, @QueryParam("pretty") String pretty,
        @Context final HttpServletRequest req // used only to get request content-type and remote address
) throws IOException {
    final long start = System.currentTimeMillis();
    final String reqContentType = req.getContentType();
    final boolean isSmile = SmileMediaTypes.APPLICATION_JACKSON_SMILE.equals(reqContentType)
            || APPLICATION_SMILE.equals(reqContentType);
    ObjectMapper objectMapper = isSmile ? smileMapper : jsonMapper;
    final String contentType = isSmile ? SmileMediaTypes.APPLICATION_JACKSON_SMILE : MediaType.APPLICATION_JSON;
    final ObjectWriter jsonWriter = pretty != null ? objectMapper.writerWithDefaultPrettyPrinter()
            : objectMapper.writer();//  ww  w  .  j a  va  2s  .c o  m

    AtomCubeQuery _query = objectMapper.readValue(in, AtomCubeQuery.class);
    final AtomCubeQuery atomQ = (AtomCubeQuery) _query.withId(UUID.randomUUID().toString());
    final Map<String, Object> responseContext1 = new MapMaker().makeMap();
    Sequence res = atomQ.run(this.conglomerate.findFactory(atomQ).createRunner(null), responseContext1);
    final Sequence results;
    if (res == null) {
        results = Sequences.empty();
    } else {
        results = res;
    }

    final Yielder yielder = results.toYielder(null, new YieldingAccumulator() {
        @Override
        public Object accumulate(Object accumulated, Object in) {
            yield();
            return in;
        }
    });

    final Map<String, Object> responseContext = new MapMaker().makeMap();
    Response.ResponseBuilder builder = Response.ok(new StreamingOutput() {
        @Override
        public void write(OutputStream outputStream) throws IOException, WebApplicationException {
            CountingOutputStream os = new CountingOutputStream(outputStream);
            jsonWriter.writeValue(os, yielder);

            os.flush();
            os.close();

            final long queryTime = System.currentTimeMillis() - start;
            emitter.emit(DruidMetrics.makeQueryTimeMetric(null, jsonMapper, atomQ, req.getRemoteAddr())
                    .setDimension("success", "true").build("query/time", queryTime));
            emitter.emit(DruidMetrics.makeQueryTimeMetric(null, jsonMapper, atomQ, req.getRemoteAddr())
                    .build("query/bytes", os.getCount()));

            requestLogger.log(new RequestLogLine(new DateTime(), req.getRemoteAddr(), atomQ,
                    new QueryStats(ImmutableMap.<String, Object>of("query/time", queryTime, "query/bytes",
                            os.getCount(), "success", true))));
        }
    }, contentType).header("X-Druid-Query-Id", atomQ.getId());

    String responseCtxString = jsonMapper.writeValueAsString(responseContext);

    return builder.header("X-Druid-Response-Context", responseCtxString).build();
}

From source file:org.locationtech.geogig.spring.dto.BatchObjects.java

@Override
protected void encode(OutputStream out) {

    if (packer != null && deduplicator != null) {
        CountingOutputStream counting = new CountingOutputStream(out);
        OutputStream output = counting;
        try {/*from  w w w .  j av a 2 s.c o m*/
            ObjectFunnel funnel;
            funnel = ObjectFunnels.newFunnel(output, DataStreamSerializationFactoryV1.INSTANCE);
            packer.write(funnel, want, have, false, deduplicator);
            counting.flush();
            funnel.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            deduplicator.release();
        }
    }
}

From source file:org.apache.druid.server.QueryResource.java

@POST
@Produces({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE })
@Consumes({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE, APPLICATION_SMILE })
public Response doPost(final InputStream in, @QueryParam("pretty") final String pretty,
        @Context final HttpServletRequest req // used to get request content-type,Accept header, remote address and auth-related headers
) throws IOException {
    final QueryLifecycle queryLifecycle = queryLifecycleFactory.factorize();
    Query<?> query = null;/*from   ww w  .ja v  a  2 s . co  m*/

    String acceptHeader = req.getHeader("Accept");
    if (Strings.isNullOrEmpty(acceptHeader)) {
        //default to content-type
        acceptHeader = req.getContentType();
    }

    final ResponseContext context = createContext(acceptHeader, pretty != null);

    final String currThreadName = Thread.currentThread().getName();
    try {
        queryLifecycle.initialize(readQuery(req, in, context));
        query = queryLifecycle.getQuery();
        final String queryId = query.getId();

        Thread.currentThread().setName(StringUtils.format("%s[%s_%s_%s]", currThreadName, query.getType(),
                query.getDataSource().getNames(), queryId));
        if (log.isDebugEnabled()) {
            log.debug("Got query [%s]", query);
        }

        final Access authResult = queryLifecycle.authorize(req);
        if (!authResult.isAllowed()) {
            throw new ForbiddenException(authResult.toString());
        }

        final QueryLifecycle.QueryResponse queryResponse = queryLifecycle.execute();
        final Sequence<?> results = queryResponse.getResults();
        final Map<String, Object> responseContext = queryResponse.getResponseContext();
        final String prevEtag = getPreviousEtag(req);

        if (prevEtag != null && prevEtag.equals(responseContext.get(HEADER_ETAG))) {
            return Response.notModified().build();
        }

        final Yielder<?> yielder = Yielders.each(results);

        try {
            boolean shouldFinalize = QueryContexts.isFinalize(query, true);
            boolean serializeDateTimeAsLong = QueryContexts.isSerializeDateTimeAsLong(query, false)
                    || (!shouldFinalize && QueryContexts.isSerializeDateTimeAsLongInner(query, false));
            final ObjectWriter jsonWriter = context.newOutputWriter(serializeDateTimeAsLong);
            Response.ResponseBuilder builder = Response.ok(new StreamingOutput() {
                @Override
                public void write(OutputStream outputStream) throws WebApplicationException {
                    Exception e = null;

                    CountingOutputStream os = new CountingOutputStream(outputStream);
                    try {
                        // json serializer will always close the yielder
                        jsonWriter.writeValue(os, yielder);

                        os.flush(); // Some types of OutputStream suppress flush errors in the .close() method.
                        os.close();
                    } catch (Exception ex) {
                        e = ex;
                        log.error(ex, "Unable to send query response.");
                        throw Throwables.propagate(ex);
                    } finally {
                        Thread.currentThread().setName(currThreadName);

                        queryLifecycle.emitLogsAndMetrics(e, req.getRemoteAddr(), os.getCount());

                        if (e == null) {
                            successfulQueryCount.incrementAndGet();
                        } else {
                            failedQueryCount.incrementAndGet();
                        }
                    }
                }
            }, context.getContentType()).header("X-Druid-Query-Id", queryId);

            if (responseContext.get(HEADER_ETAG) != null) {
                builder.header(HEADER_ETAG, responseContext.get(HEADER_ETAG));
                responseContext.remove(HEADER_ETAG);
            }

            DirectDruidClient.removeMagicResponseContextFields(responseContext);

            //Limit the response-context header, see https://github.com/apache/incubator-druid/issues/2331
            //Note that Response.ResponseBuilder.header(String key,Object value).build() calls value.toString()
            //and encodes the string using ASCII, so 1 char is = 1 byte
            String responseCtxString = jsonMapper.writeValueAsString(responseContext);
            if (responseCtxString.length() > RESPONSE_CTX_HEADER_LEN_LIMIT) {
                log.warn("Response Context truncated for id [%s] . Full context is [%s].", queryId,
                        responseCtxString);
                responseCtxString = responseCtxString.substring(0, RESPONSE_CTX_HEADER_LEN_LIMIT);
            }

            return builder.header("X-Druid-Response-Context", responseCtxString).build();
        } catch (Exception e) {
            // make sure to close yielder if anything happened before starting to serialize the response.
            yielder.close();
            throw Throwables.propagate(e);
        } finally {
            // do not close yielder here, since we do not want to close the yielder prior to
            // StreamingOutput having iterated over all the results
        }
    } catch (QueryInterruptedException e) {
        interruptedQueryCount.incrementAndGet();
        queryLifecycle.emitLogsAndMetrics(e, req.getRemoteAddr(), -1);
        return context.gotError(e);
    } catch (ForbiddenException e) {
        // don't do anything for an authorization failure, ForbiddenExceptionMapper will catch this later and
        // send an error response if this is thrown.
        throw e;
    } catch (Exception e) {
        failedQueryCount.incrementAndGet();
        queryLifecycle.emitLogsAndMetrics(e, req.getRemoteAddr(), -1);

        log.makeAlert(e, "Exception handling request").addData("exception", e.toString())
                .addData("query", query != null ? query.toString() : "unparseable query")
                .addData("peer", req.getRemoteAddr()).emit();

        return context.gotError(e);
    } finally {
        Thread.currentThread().setName(currThreadName);
    }
}