Search in sources :

Example 61 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project ice by JBEI.

the class FileResource method downloadSequence.

@GET
@Path("{partId}/sequence/{type}")
public Response downloadSequence(@PathParam("partId") final String partId, @PathParam("type") final String downloadType, @DefaultValue("-1") @QueryParam("remoteId") long remoteId, @QueryParam("sid") String sid) {
    if (StringUtils.isEmpty(sessionId))
        sessionId = sid;
    final String userId = getUserId(sessionId);
    if (remoteId != -1) {
        RemoteSequence sequence = new RemoteSequence(remoteId, Long.decode(partId));
        final InputStreamWrapper wrapper = sequence.get(downloadType);
        StreamingOutput stream = output -> IOUtils.copy(wrapper.getInputStream(), output);
        return addHeaders(Response.ok(stream), wrapper.getName());
    } else {
        InputStreamWrapper wrapper = new PartSequence(userId, partId).toFile(SequenceFormat.fromString(downloadType), true);
        StreamingOutput stream = output -> IOUtils.copy(wrapper.getInputStream(), output);
        return addHeaders(Response.ok(stream), wrapper.getName());
    }
}
Also used : RemoteSequence(org.jbei.ice.lib.net.RemoteSequence) AttachmentInfo(org.jbei.ice.lib.dto.entry.AttachmentInfo) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) ConfigurationSettings(org.jbei.ice.lib.config.ConfigurationSettings) Sequences(org.jbei.ice.lib.entry.sequence.Sequences) PartSequence(org.jbei.ice.lib.entry.sequence.PartSequence) Setting(org.jbei.ice.lib.dto.Setting) TraceSequences(org.jbei.ice.lib.entry.sequence.analysis.TraceSequences) ConfigurationKey(org.jbei.ice.lib.dto.ConfigurationKey) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Entries(org.jbei.ice.lib.entry.Entries) MediaType(javax.ws.rs.core.MediaType) DAOFactory(org.jbei.ice.storage.DAOFactory) Logger(org.jbei.ice.lib.common.logging.Logger) ByteArrayInputStream(java.io.ByteArrayInputStream) FileBulkUpload(org.jbei.ice.lib.bulkupload.FileBulkUpload) SequenceInfo(org.jbei.ice.lib.dto.entry.SequenceInfo) SequenceFormat(org.jbei.ice.lib.entry.sequence.SequenceFormat) EntryType(org.jbei.ice.lib.dto.entry.EntryType) Utils(org.jbei.ice.lib.utils.Utils) EntrySelection(org.jbei.ice.lib.entry.EntrySelection) Files(java.nio.file.Files) FileUtils(org.apache.commons.io.FileUtils) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) Collectors(java.util.stream.Collectors) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) Attachments(org.jbei.ice.lib.entry.attachment.Attachments) RemoteEntries(org.jbei.ice.lib.net.RemoteEntries) FormDataParam(org.glassfish.jersey.media.multipart.FormDataParam) List(java.util.List) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) Paths(java.nio.file.Paths) ShotgunSequenceDAO(org.jbei.ice.storage.hibernate.dao.ShotgunSequenceDAO) TraceSequence(org.jbei.ice.storage.model.TraceSequence) EntriesAsCSV(org.jbei.ice.lib.entry.EntriesAsCSV) InvalidFormatParserException(org.jbei.ice.lib.parsers.InvalidFormatParserException) ShotgunSequence(org.jbei.ice.storage.model.ShotgunSequence) InputStream(java.io.InputStream) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) PartSequence(org.jbei.ice.lib.entry.sequence.PartSequence) StreamingOutput(javax.ws.rs.core.StreamingOutput) RemoteSequence(org.jbei.ice.lib.net.RemoteSequence)

Example 62 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project ice by JBEI.

the class FileResource method downloadExportedFile.

@GET
@Path("/exports/{fileId}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadExportedFile(@PathParam("fileId") String fileId) {
    String userId = requireUserId();
    final java.nio.file.Path tmpFile = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY));
    String fileName = userId + "_" + fileId + "_export-data.zip";
    if (!Files.exists(Paths.get(tmpFile.toString(), "export", fileName)))
        return super.respond(Response.Status.NOT_FOUND);
    StreamingOutput stream = output -> {
        java.nio.file.Path file = Paths.get(tmpFile.toString(), "export", fileName);
        final ByteArrayInputStream input = new ByteArrayInputStream(FileUtils.readFileToByteArray(file.toFile()));
        IOUtils.copy(input, output);
    };
    return addHeaders(Response.ok(stream), "ice-export-data.zip");
}
Also used : RemoteSequence(org.jbei.ice.lib.net.RemoteSequence) AttachmentInfo(org.jbei.ice.lib.dto.entry.AttachmentInfo) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) ConfigurationSettings(org.jbei.ice.lib.config.ConfigurationSettings) Sequences(org.jbei.ice.lib.entry.sequence.Sequences) PartSequence(org.jbei.ice.lib.entry.sequence.PartSequence) Setting(org.jbei.ice.lib.dto.Setting) TraceSequences(org.jbei.ice.lib.entry.sequence.analysis.TraceSequences) ConfigurationKey(org.jbei.ice.lib.dto.ConfigurationKey) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Entries(org.jbei.ice.lib.entry.Entries) MediaType(javax.ws.rs.core.MediaType) DAOFactory(org.jbei.ice.storage.DAOFactory) Logger(org.jbei.ice.lib.common.logging.Logger) ByteArrayInputStream(java.io.ByteArrayInputStream) FileBulkUpload(org.jbei.ice.lib.bulkupload.FileBulkUpload) SequenceInfo(org.jbei.ice.lib.dto.entry.SequenceInfo) SequenceFormat(org.jbei.ice.lib.entry.sequence.SequenceFormat) EntryType(org.jbei.ice.lib.dto.entry.EntryType) Utils(org.jbei.ice.lib.utils.Utils) EntrySelection(org.jbei.ice.lib.entry.EntrySelection) Files(java.nio.file.Files) FileUtils(org.apache.commons.io.FileUtils) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) Collectors(java.util.stream.Collectors) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) Attachments(org.jbei.ice.lib.entry.attachment.Attachments) RemoteEntries(org.jbei.ice.lib.net.RemoteEntries) FormDataParam(org.glassfish.jersey.media.multipart.FormDataParam) List(java.util.List) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) Paths(java.nio.file.Paths) ShotgunSequenceDAO(org.jbei.ice.storage.hibernate.dao.ShotgunSequenceDAO) TraceSequence(org.jbei.ice.storage.model.TraceSequence) EntriesAsCSV(org.jbei.ice.lib.entry.EntriesAsCSV) InvalidFormatParserException(org.jbei.ice.lib.parsers.InvalidFormatParserException) ShotgunSequence(org.jbei.ice.storage.model.ShotgunSequence) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput)

Example 63 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project indy by Commonjava.

the class PathMappedResource method get.

@ApiOperation("Get specified path.")
@ApiResponse(code = 200, message = "Operation finished.")
@GET
@Path(CONCRETE_CONTENT_PATH)
public Response get(@PathParam("packageType") final String packageType, @ApiParam(allowableValues = "hosted,group,remote", required = true) @PathParam("type") final String type, @ApiParam(required = true) @PathParam("name") final String name, @PathParam("path") final String path, @Context final HttpServletRequest request, @Context final SecurityContext securityContext) {
    try {
        InputStream inputStream = controller.get(packageType, type, name, path);
        Response.ResponseBuilder builder = Response.ok((StreamingOutput) outputStream -> IOUtils.copy(inputStream, outputStream));
        return builder.header(ApplicationHeader.content_type.key(), mimeTyper.getContentType(path)).build();
    } catch (Exception e) {
        logger.warn("Get pathmap content failed, message: " + e.getMessage(), e);
        if (e.getMessage() != null && e.getMessage().contains("not exist")) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        responseHelper.throwError(e);
    }
    return null;
}
Also used : Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) PathParam(javax.ws.rs.PathParam) REST(org.commonjava.indy.bind.jaxrs.util.REST) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) PathMappedListResult(org.commonjava.indy.pathmapped.model.PathMappedListResult) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) ApiParam(io.swagger.annotations.ApiParam) ApplicationContent.application_json(org.commonjava.indy.util.ApplicationContent.application_json) ROOT_DIR(org.commonjava.storage.pathmapped.util.PathMapUtils.ROOT_DIR) Inject(javax.inject.Inject) ApiOperation(io.swagger.annotations.ApiOperation) HttpServletRequest(javax.servlet.http.HttpServletRequest) QueryParam(javax.ws.rs.QueryParam) SecurityManager(org.commonjava.indy.bind.jaxrs.SecurityManager) Api(io.swagger.annotations.Api) ApplicationHeader(org.commonjava.indy.util.ApplicationHeader) DELETE(javax.ws.rs.DELETE) Logger(org.slf4j.Logger) Context(javax.ws.rs.core.Context) ResponseHelper(org.commonjava.indy.bind.jaxrs.util.ResponseHelper) PathMappedDeleteResult(org.commonjava.indy.pathmapped.model.PathMappedDeleteResult) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IndyResources(org.commonjava.indy.bind.jaxrs.IndyResources) StreamingOutput(javax.ws.rs.core.StreamingOutput) MimeTyper(org.commonjava.indy.util.MimeTyper) IOUtils(org.apache.commons.io.IOUtils) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) PathMappedController(org.commonjava.indy.pathmapped.common.PathMappedController) InputStream(java.io.InputStream) InputStream(java.io.InputStream) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponse(io.swagger.annotations.ApiResponse)

Example 64 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project druid by druid-io.

the class SqlResource method doPost.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response doPost(final SqlQuery sqlQuery, @Context final HttpServletRequest req) throws IOException {
    final SqlLifecycle lifecycle = sqlLifecycleFactory.factorize();
    final String sqlQueryId = lifecycle.initialize(sqlQuery.getQuery(), sqlQuery.getContext());
    final String remoteAddr = req.getRemoteAddr();
    final String currThreadName = Thread.currentThread().getName();
    try {
        Thread.currentThread().setName(StringUtils.format("sql[%s]", sqlQueryId));
        lifecycle.setParameters(sqlQuery.getParameterList());
        lifecycle.validateAndAuthorize(req);
        // must add after lifecycle is authorized
        sqlLifecycleManager.add(sqlQueryId, lifecycle);
        lifecycle.plan();
        final SqlRowTransformer rowTransformer = lifecycle.createRowTransformer();
        final Sequence<Object[]> sequence = lifecycle.execute();
        final Yielder<Object[]> yielder0 = Yielders.each(sequence);
        try {
            final Response.ResponseBuilder responseBuilder = Response.ok((StreamingOutput) outputStream -> {
                Exception e = null;
                CountingOutputStream os = new CountingOutputStream(outputStream);
                Yielder<Object[]> yielder = yielder0;
                try (final ResultFormat.Writer writer = sqlQuery.getResultFormat().createFormatter(os, jsonMapper)) {
                    writer.writeResponseStart();
                    if (sqlQuery.includeHeader()) {
                        writer.writeHeader(rowTransformer.getRowType(), sqlQuery.includeTypesHeader(), sqlQuery.includeSqlTypesHeader());
                    }
                    while (!yielder.isDone()) {
                        final Object[] row = yielder.get();
                        writer.writeRowStart();
                        for (int i = 0; i < rowTransformer.getFieldList().size(); i++) {
                            final Object value = rowTransformer.transform(row, i);
                            writer.writeRowField(rowTransformer.getFieldList().get(i), value);
                        }
                        writer.writeRowEnd();
                        yielder = yielder.next(null);
                    }
                    writer.writeResponseEnd();
                } catch (Exception ex) {
                    e = ex;
                    log.error(ex, "Unable to send SQL response [%s]", sqlQueryId);
                    throw new RuntimeException(ex);
                } finally {
                    yielder.close();
                    endLifecycle(sqlQueryId, lifecycle, e, remoteAddr, os.getCount());
                }
            }).header(SQL_QUERY_ID_RESPONSE_HEADER, sqlQueryId);
            if (sqlQuery.includeHeader()) {
                responseBuilder.header(SQL_HEADER_RESPONSE_HEADER, SQL_HEADER_VALUE);
            }
            return responseBuilder.build();
        } catch (Throwable e) {
            // make sure to close yielder if anything happened before starting to serialize the response.
            yielder0.close();
            throw new RuntimeException(e);
        }
    } catch (QueryCapacityExceededException cap) {
        endLifecycle(sqlQueryId, lifecycle, cap, remoteAddr, -1);
        return buildNonOkResponse(QueryCapacityExceededException.STATUS_CODE, cap, sqlQueryId);
    } catch (QueryUnsupportedException unsupported) {
        endLifecycle(sqlQueryId, lifecycle, unsupported, remoteAddr, -1);
        return buildNonOkResponse(QueryUnsupportedException.STATUS_CODE, unsupported, sqlQueryId);
    } catch (QueryTimeoutException timeout) {
        endLifecycle(sqlQueryId, lifecycle, timeout, remoteAddr, -1);
        return buildNonOkResponse(QueryTimeoutException.STATUS_CODE, timeout, sqlQueryId);
    } catch (SqlPlanningException | ResourceLimitExceededException e) {
        endLifecycle(sqlQueryId, lifecycle, e, remoteAddr, -1);
        return buildNonOkResponse(BadQueryException.STATUS_CODE, e, sqlQueryId);
    } catch (ForbiddenException e) {
        endLifecycleWithoutEmittingMetrics(sqlQueryId, lifecycle);
        throw (ForbiddenException) serverConfig.getErrorResponseTransformStrategy().transformIfNeeded(// let ForbiddenExceptionMapper handle this
        e);
    } catch (RelOptPlanner.CannotPlanException e) {
        endLifecycle(sqlQueryId, lifecycle, e, remoteAddr, -1);
        SqlPlanningException spe = new SqlPlanningException(SqlPlanningException.PlanningError.UNSUPPORTED_SQL_ERROR, e.getMessage());
        return buildNonOkResponse(BadQueryException.STATUS_CODE, spe, sqlQueryId);
    }// calcite throws a java.lang.AssertionError which is type error not exception. using throwable will catch all
     catch (Throwable e) {
        log.warn(e, "Failed to handle query: %s", sqlQuery);
        endLifecycle(sqlQueryId, lifecycle, e, remoteAddr, -1);
        return buildNonOkResponse(Status.INTERNAL_SERVER_ERROR.getStatusCode(), QueryInterruptedException.wrapIfNeeded(e), sqlQueryId);
    } finally {
        Thread.currentThread().setName(currThreadName);
    }
}
Also used : SqlRowTransformer(org.apache.druid.sql.SqlRowTransformer) StreamingOutput(javax.ws.rs.core.StreamingOutput) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) CountingOutputStream(com.google.common.io.CountingOutputStream) SqlPlanningException(org.apache.druid.sql.SqlPlanningException) ForbiddenException(org.apache.druid.server.security.ForbiddenException) QueryCapacityExceededException(org.apache.druid.query.QueryCapacityExceededException) QueryUnsupportedException(org.apache.druid.query.QueryUnsupportedException) SqlLifecycle(org.apache.druid.sql.SqlLifecycle) BadQueryException(org.apache.druid.query.BadQueryException) QueryCapacityExceededException(org.apache.druid.query.QueryCapacityExceededException) SqlPlanningException(org.apache.druid.sql.SqlPlanningException) ForbiddenException(org.apache.druid.server.security.ForbiddenException) SanitizableException(org.apache.druid.common.exception.SanitizableException) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) ResourceLimitExceededException(org.apache.druid.query.ResourceLimitExceededException) QueryUnsupportedException(org.apache.druid.query.QueryUnsupportedException) Response(javax.ws.rs.core.Response) ResourceLimitExceededException(org.apache.druid.query.ResourceLimitExceededException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 65 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project druid by druid-io.

the class SqlResourceTest method doPostRaw.

// Returns either an error or a result.
private Pair<QueryException, String> doPostRaw(final SqlQuery query, final HttpServletRequest req) throws Exception {
    final Response response = resource.doPost(query, req);
    if (response.getStatus() == 200) {
        final StreamingOutput output = (StreamingOutput) response.getEntity();
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            output.write(baos);
        } catch (Exception ignored) {
        // Suppress errors and return the response so far. Similar to what the real web server would do, if it
        // started writing a 200 OK and then threw an exception in the middle.
        }
        return Pair.of(null, new String(baos.toByteArray(), StandardCharsets.UTF_8));
    } else {
        return Pair.of(JSON_MAPPER.readValue((byte[]) response.getEntity(), QueryException.class), null);
    }
}
Also used : Response(javax.ws.rs.core.Response) UnsupportedSQLQueryException(org.apache.druid.sql.calcite.planner.UnsupportedSQLQueryException) QueryException(org.apache.druid.query.QueryException) StreamingOutput(javax.ws.rs.core.StreamingOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsupportedSQLQueryException(org.apache.druid.sql.calcite.planner.UnsupportedSQLQueryException) ForbiddenException(org.apache.druid.server.security.ForbiddenException) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) QueryException(org.apache.druid.query.QueryException) QueryCapacityExceededException(org.apache.druid.query.QueryCapacityExceededException) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) IOException(java.io.IOException) RelConversionException(org.apache.calcite.tools.RelConversionException) ResourceLimitExceededException(org.apache.druid.query.ResourceLimitExceededException) QueryUnsupportedException(org.apache.druid.query.QueryUnsupportedException)

Aggregations

StreamingOutput (javax.ws.rs.core.StreamingOutput)190 OutputStream (java.io.OutputStream)84 Response (javax.ws.rs.core.Response)76 Path (javax.ws.rs.Path)53 Produces (javax.ws.rs.Produces)52 IOException (java.io.IOException)50 GET (javax.ws.rs.GET)50 File (java.io.File)45 InputStream (java.io.InputStream)45 Test (org.junit.Test)44 WebApplicationException (javax.ws.rs.WebApplicationException)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)32 List (java.util.List)26 MediaType (javax.ws.rs.core.MediaType)24 ByteArrayInputStream (java.io.ByteArrayInputStream)20 ArrayList (java.util.ArrayList)20 Consumes (javax.ws.rs.Consumes)20 HashMap (java.util.HashMap)19 POST (javax.ws.rs.POST)19 FileOutputStream (java.io.FileOutputStream)17