use of javax.ws.rs.core.StreamingOutput in project registry by hortonworks.
the class WSUtils method wrapWithStreamingOutput.
public static StreamingOutput wrapWithStreamingOutput(final InputStream inputStream) {
return new StreamingOutput() {
public void write(OutputStream os) throws IOException, WebApplicationException {
OutputStream wrappedOutputStream = os;
if (!(os instanceof BufferedOutputStream)) {
wrappedOutputStream = new BufferedOutputStream(os);
}
ByteStreams.copy(inputStream, wrappedOutputStream);
wrappedOutputStream.flush();
}
};
}
use of javax.ws.rs.core.StreamingOutput in project component-runtime by Talend.
the class ProjectResource method createZip.
@POST
@Path("zip/form")
@Produces("application/zip")
public Response createZip(@FormParam("project") final String compressedModel, @Context final Providers providers) {
final MessageBodyReader<ProjectModel> jsonReader = providers.getMessageBodyReader(ProjectModel.class, ProjectModel.class, NO_ANNOTATION, APPLICATION_JSON_TYPE);
final ProjectModel model;
try (final InputStream gzipInputStream = new ByteArrayInputStream(Base64.getUrlDecoder().decode(compressedModel))) {
model = jsonReader.readFrom(ProjectModel.class, ProjectModel.class, NO_ANNOTATION, APPLICATION_JSON_TYPE, new MultivaluedHashMap<>(), gzipInputStream);
} catch (final IOException e) {
throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorMessage(e.getMessage())).type(APPLICATION_JSON_TYPE).build());
}
final String filename = ofNullable(model.getArtifact()).orElse("zip") + ".zip";
return Response.ok().entity((StreamingOutput) out -> {
generator.generate(toRequest(model), out);
out.flush();
}).header("Content-Disposition", "inline; filename=" + filename).build();
}
use of javax.ws.rs.core.StreamingOutput in project cloudbreak by hortonworks.
the class CloudbreakEventController method getStructuredEventsZip.
@Override
public Response getStructuredEventsZip(Long stackId) {
List<StructuredEvent> events = getStructuredEventsForStack(stackId);
StreamingOutput streamingOutput = output -> {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(output)) {
zipOutputStream.putNextEntry(new ZipEntry("struct-events.json"));
zipOutputStream.write(JsonUtil.writeValueAsString(events).getBytes());
zipOutputStream.closeEntry();
}
};
return Response.ok(streamingOutput).header("content-disposition", "attachment; filename = struct-events.zip").build();
}
use of javax.ws.rs.core.StreamingOutput in project killbill by killbill.
the class JaxRsResourceBase method buildStreamingPaginationResponse.
protected <E extends Entity, J extends JsonBase> Response buildStreamingPaginationResponse(final Pagination<E> entities, final Function<E, J> toJson, final URI nextPageUri) {
final StreamingOutput json = new StreamingOutput() {
@Override
public void write(final OutputStream output) throws IOException, WebApplicationException {
final Iterator<E> iterator = entities.iterator();
try {
final JsonGenerator generator = mapper.getFactory().createGenerator(output);
generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
generator.writeStartArray();
while (iterator.hasNext()) {
final E entity = iterator.next();
final J asJson = toJson.apply(entity);
if (asJson != null) {
generator.writeObject(asJson);
}
}
generator.writeEndArray();
generator.close();
} finally {
// In case the client goes away (IOException), make sure to close the underlying DB connection
entities.close();
}
}
};
return Response.status(Status.OK).entity(json).header(HDR_PAGINATION_CURRENT_OFFSET, entities.getCurrentOffset()).header(HDR_PAGINATION_NEXT_OFFSET, entities.getNextOffset()).header(HDR_PAGINATION_TOTAL_NB_RECORDS, entities.getTotalNbRecords()).header(HDR_PAGINATION_MAX_NB_RECORDS, entities.getMaxNbRecords()).header(HDR_PAGINATION_NEXT_PAGE_URI, nextPageUri).build();
}
use of javax.ws.rs.core.StreamingOutput in project killbill by killbill.
the class AdminResource method triggerInvoiceGenerationForParkedAccounts.
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Path("/invoices")
@ApiOperation(value = "Trigger an invoice generation for all parked accounts")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successful operation") })
public Response triggerInvoiceGenerationForParkedAccounts(@QueryParam(QUERY_SEARCH_OFFSET) @DefaultValue("0") final Long offset, @QueryParam(QUERY_SEARCH_LIMIT) @DefaultValue("100") final Long limit, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request) {
final CallContext callContext = context.createCallContextNoAccountId(createdBy, reason, comment, request);
// TODO Consider adding a real invoice API post 0.18.x
final Pagination<Tag> tags = tagUserApi.searchTags(SystemTags.PARK_TAG_DEFINITION_NAME, offset, limit, callContext);
final Iterator<Tag> iterator = tags.iterator();
final StreamingOutput json = new StreamingOutput() {
@Override
public void write(final OutputStream output) throws IOException, WebApplicationException {
try {
final JsonGenerator generator = mapper.getFactory().createGenerator(output);
generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
generator.writeStartObject();
while (iterator.hasNext()) {
final Tag tag = iterator.next();
final UUID accountId = tag.getObjectId();
try {
invoiceUserApi.triggerInvoiceGeneration(accountId, clock.getUTCToday(), callContext);
generator.writeStringField(accountId.toString(), OK);
} catch (final InvoiceApiException e) {
if (e.getCode() != ErrorCode.INVOICE_NOTHING_TO_DO.getCode()) {
log.warn("Unable to trigger invoice generation for accountId='{}'", accountId);
}
generator.writeStringField(accountId.toString(), ErrorCode.fromCode(e.getCode()).toString());
}
}
generator.writeEndObject();
generator.close();
} finally {
// In case the client goes away (IOException), make sure to close the underlying DB connection
tags.close();
}
}
};
final URI nextPageUri = uriBuilder.nextPage(AdminResource.class, "triggerInvoiceGenerationForParkedAccounts", tags.getNextOffset(), limit, ImmutableMap.<String, String>of(), ImmutableMap.<String, String>of());
return Response.status(Status.OK).entity(json).header(HDR_PAGINATION_CURRENT_OFFSET, tags.getCurrentOffset()).header(HDR_PAGINATION_NEXT_OFFSET, tags.getNextOffset()).header(HDR_PAGINATION_TOTAL_NB_RECORDS, tags.getTotalNbRecords()).header(HDR_PAGINATION_MAX_NB_RECORDS, tags.getMaxNbRecords()).header(HDR_PAGINATION_NEXT_PAGE_URI, nextPageUri).build();
}
Aggregations