@Override public void sendContent(HttpResponseStatus status, ByteBuf content, HttpHeaders headers) { delegate.sendContent(status, content, headers); runHook(status); }
@Override public void sendContent(HttpResponseStatus status, BodyProducer bodyProducer, HttpHeaders headers) { delegate.sendContent(status, bodyProducer, headers); runHook(status); }
responder.sendContent(HttpResponseStatus.OK, new BodyProducer() {
responder.sendContent(HttpResponseStatus.OK, new BodyProducer() {
/** * Gets list of topics along with offsets and limit as request and returns list of messages */ @POST @Path("/metadata") public void metadata(FullHttpRequest request, HttpResponder responder) throws Exception { Map<String, GenericRecord> consumeRequests = decodeConsumeRequest(request); MessagesBodyProducer messagesBodyProducer = new MessagesBodyProducer(cConf, consumeRequests, messagingContext); responder.sendContent(HttpResponseStatus.OK, messagesBodyProducer, new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary")); }
/** * Gets list of topics along with offsets and limit as request and returns list of messages */ @POST @Path("/metadata") public void metadata(FullHttpRequest request, HttpResponder responder) throws Exception { Map<String, GenericRecord> consumeRequests = decodeConsumeRequest(request); MessagesBodyProducer messagesBodyProducer = new MessagesBodyProducer(cConf, consumeRequests, messagingContext); responder.sendContent(HttpResponseStatus.OK, messagesBodyProducer, new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary")); }
@POST @Path("poll") public void poll(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception { TopicId topicId = new NamespaceId(namespace).topic(topic); // Currently only support avro if (!"avro/binary".equals(request.headers().get(HttpHeaderNames.CONTENT_TYPE))) { throw new BadRequestException("Only avro/binary content type is supported."); } // Decode the poll request Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ByteBufInputStream(request.content()), null); DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.ConsumeRequest.SCHEMA); // Fetch the messages CloseableIterator<RawMessage> iterator = fetchMessages(datumReader.read(null, decoder), topicId); try { responder.sendContent(HttpResponseStatus.OK, new MessagesBodyProducer(iterator, messageChunkSize), new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary")); } catch (Throwable t) { iterator.close(); throw t; } }
@POST @Path("poll") public void poll(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception { TopicId topicId = new NamespaceId(namespace).topic(topic); // Currently only support avro if (!"avro/binary".equals(request.headers().get(HttpHeaderNames.CONTENT_TYPE))) { throw new BadRequestException("Only avro/binary content type is supported."); } // Decode the poll request Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ByteBufInputStream(request.content()), null); DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.ConsumeRequest.SCHEMA); // Fetch the messages CloseableIterator<RawMessage> iterator = fetchMessages(datumReader.read(null, decoder), topicId); try { responder.sendContent(HttpResponseStatus.OK, new MessagesBodyProducer(iterator, messageChunkSize), new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary")); } catch (Throwable t) { iterator.close(); throw t; } }
private void doDownloadQueryResults(HttpResponder responder, QueryHandle handle) throws ExploreException, IOException { try { if (handle.equals(QueryHandle.NO_OP) || !exploreService.getStatus(handle).getStatus().equals(QueryStatus.OpStatus.FINISHED)) { responder.sendStatus(HttpResponseStatus.CONFLICT); return; } QueryResultsBodyProducer queryResultsBodyProducer = new QueryResultsBodyProducer(exploreService, handle); responder.sendContent(HttpResponseStatus.OK, queryResultsBodyProducer, EmptyHttpHeaders.INSTANCE); } catch (IllegalArgumentException e) { LOG.debug("Got exception:", e); responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (SQLException e) { LOG.debug("Got exception:", e); responder.sendString(HttpResponseStatus.BAD_REQUEST, String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage())); } catch (HandleNotFoundException e) { if (e.isInactive()) { responder.sendString(HttpResponseStatus.CONFLICT, "Query is inactive"); } else { responder.sendStatus(HttpResponseStatus.NOT_FOUND); } } } }
private void doDownloadQueryResults(HttpResponder responder, QueryHandle handle) throws ExploreException, IOException { try { if (handle.equals(QueryHandle.NO_OP) || !exploreService.getStatus(handle).getStatus().equals(QueryStatus.OpStatus.FINISHED)) { responder.sendStatus(HttpResponseStatus.CONFLICT); return; } QueryResultsBodyProducer queryResultsBodyProducer = new QueryResultsBodyProducer(exploreService, handle); responder.sendContent(HttpResponseStatus.OK, queryResultsBodyProducer, EmptyHttpHeaders.INSTANCE); } catch (IllegalArgumentException e) { LOG.debug("Got exception:", e); responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (SQLException e) { LOG.debug("Got exception:", e); responder.sendString(HttpResponseStatus.BAD_REQUEST, String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage())); } catch (HandleNotFoundException e) { if (e.isInactive()) { responder.sendString(HttpResponseStatus.CONFLICT, "Query is inactive"); } else { responder.sendStatus(HttpResponseStatus.NOT_FOUND); } } } }
@POST @Path("/publish") public void publish(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception { TopicId topicId = new NamespaceId(namespace).topic(topic); StoreRequest storeRequest = createStoreRequest(topicId, request); // Empty payload is only allowed for transactional publish if (!storeRequest.isTransactional() && !storeRequest.hasPayload()) { throw new BadRequestException("Empty payload is only allowed for publishing transactional message. Topic: " + topicId); } // Publish the message and response with the rollback information RollbackDetail rollbackInfo = messagingService.publish(storeRequest); if (rollbackInfo == null) { // Non-tx publish doesn't have rollback info. responder.sendStatus(HttpResponseStatus.OK); return; } ByteBuf response = encodeRollbackDetail(rollbackInfo); responder.sendContent(HttpResponseStatus.OK, response, new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary")); }
@POST @Path("/publish") public void publish(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception { TopicId topicId = new NamespaceId(namespace).topic(topic); StoreRequest storeRequest = createStoreRequest(topicId, request); // Empty payload is only allowed for transactional publish if (!storeRequest.isTransactional() && !storeRequest.hasPayload()) { throw new BadRequestException("Empty payload is only allowed for publishing transactional message. Topic: " + topicId); } // Publish the message and response with the rollback information RollbackDetail rollbackInfo = messagingService.publish(storeRequest); if (rollbackInfo == null) { // Non-tx publish doesn't have rollback info. responder.sendStatus(HttpResponseStatus.OK); return; } ByteBuf response = encodeRollbackDetail(rollbackInfo); responder.sendContent(HttpResponseStatus.OK, response, new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary")); }
@POST @Path("/chunk") public void chunk(FullHttpRequest request, HttpResponder responder) { ByteBuf content = request.content().copy(); responder.sendContent(HttpResponseStatus.OK, new BodyProducer() { int count = 0; @Override public ByteBuf nextChunk() { if (count++ < 10) { return content.copy(); } return Unpooled.EMPTY_BUFFER; } @Override public void finished() { // no-op } @Override public void handleError(@Nullable Throwable cause) { // no-op } }, new DefaultHttpHeaders()); } }
/** * Calls to other responder methods in this class only cache the response to be sent. The response is actually * sent only when this method is called. * * @param keepAlive {@code true} to keep the connection open; {@code false} otherwise */ public void execute(boolean keepAlive) { Preconditions.checkState(bufferedResponse != null, "Can not call execute before one of the other responder methods are called."); try { HttpContentProducer contentProducer = bufferedResponse.getContentProducer(); HttpHeaders headers = new DefaultHttpHeaders().add(bufferedResponse.getHeaders()); headers.set(HttpHeaderNames.CONNECTION, keepAlive ? HttpHeaderValues.KEEP_ALIVE : HttpHeaderValues.CLOSE); if (!headers.contains(HttpHeaderNames.CONTENT_TYPE)) { headers.set(HttpHeaderNames.CONTENT_TYPE, bufferedResponse.getContentType()); } if (contentProducer != null) { responder.sendContent(HttpResponseStatus.valueOf(bufferedResponse.getStatus()), bodyProducerFactory.create(contentProducer, taskExecutor), headers); } else { responder.sendContent(HttpResponseStatus.valueOf(bufferedResponse.getStatus()), bufferedResponse.getContentBuffer(), headers); } emitMetrics(bufferedResponse.getStatus()); } finally { close(); } }
responder.sendContent(HttpResponseStatus.valueOf(bufferedResponse.getStatus()), new ReleasingBodyProducer(bodyProducerFactory.create(contentProducer, taskExecutor), taskExecutor), headers); } else { responder.sendContent(HttpResponseStatus.valueOf(bufferedResponse.getStatus()), bufferedResponse.getContentBuffer(), headers); taskExecutor.releaseCallResources();
return; responder.sendContent(HttpResponseStatus.OK, logsProducer, logsProducer.getResponseHeaders()); } catch (SecurityException e) { responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
return; responder.sendContent(HttpResponseStatus.OK, logsProducer, logsProducer.getResponseHeaders()); } catch (SecurityException e) { responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);