Search in sources :

Example 21 with StreamingOutput

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

the class SampleResource method getRequestFile.

@POST
@Path("/requests/file")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getRequestFile(@QueryParam("sid") String sid, final ArrayList<Long> requestIds) {
    // only supports csv for now
    if (StringUtils.isEmpty(sessionId))
        sessionId = sid;
    final String userId = getUserId(sessionId);
    final ArrayList<Long> sampleRequestIds = new ArrayList<>();
    for (final Number number : requestIds) {
        sampleRequestIds.add(number.longValue());
    }
    try {
        ByteArrayOutputStream outputStream = requestRetriever.generateCSVFile(userId, sampleRequestIds);
        StreamingOutput stream = outputStream::writeTo;
        return Response.ok(stream).header("Content-Disposition", "attachment;filename=\"data.csv\"").build();
    } catch (IOException e) {
        Logger.error(e);
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : ArrayList(java.util.ArrayList) StreamingOutput(javax.ws.rs.core.StreamingOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 22 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project tika by apache.

the class TikaResource method produceText.

public StreamingOutput produceText(final InputStream is, MultivaluedMap<String, String> httpHeaders, final UriInfo info) {
    final Parser parser = createParser();
    final Metadata metadata = new Metadata();
    final ParseContext context = new ParseContext();
    fillMetadata(parser, metadata, context, httpHeaders);
    fillParseContext(context, httpHeaders, parser);
    logRequest(LOG, info, metadata);
    return new StreamingOutput() {

        public void write(OutputStream outputStream) throws IOException, WebApplicationException {
            Writer writer = new OutputStreamWriter(outputStream, UTF_8);
            BodyContentHandler body = new BodyContentHandler(new RichTextContentHandler(writer));
            parse(parser, LOG, info.getPath(), is, body, metadata, context);
        }
    };
}
Also used : BodyContentHandler(org.apache.tika.sax.BodyContentHandler) RichTextContentHandler(org.apache.tika.sax.RichTextContentHandler) OutputStream(java.io.OutputStream) Metadata(org.apache.tika.metadata.Metadata) ParseContext(org.apache.tika.parser.ParseContext) StreamingOutput(javax.ws.rs.core.StreamingOutput) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Parser(org.apache.tika.parser.Parser) HtmlParser(org.apache.tika.parser.html.HtmlParser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) DigestingParser(org.apache.tika.parser.DigestingParser)

Example 23 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project tika by apache.

the class TikaResource method produceTextMain.

public StreamingOutput produceTextMain(final InputStream is, @Context MultivaluedMap<String, String> httpHeaders, @Context final UriInfo info) {
    final Parser parser = createParser();
    final Metadata metadata = new Metadata();
    final ParseContext context = new ParseContext();
    fillMetadata(parser, metadata, context, httpHeaders);
    fillParseContext(context, httpHeaders, parser);
    logRequest(LOG, info, metadata);
    return new StreamingOutput() {

        public void write(OutputStream outputStream) throws IOException, WebApplicationException {
            Writer writer = new OutputStreamWriter(outputStream, UTF_8);
            ContentHandler handler = new BoilerpipeContentHandler(writer);
            parse(parser, LOG, info.getPath(), is, handler, metadata, context);
        }
    };
}
Also used : OutputStream(java.io.OutputStream) Metadata(org.apache.tika.metadata.Metadata) ParseContext(org.apache.tika.parser.ParseContext) StreamingOutput(javax.ws.rs.core.StreamingOutput) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BoilerpipeContentHandler(org.apache.tika.parser.html.BoilerpipeContentHandler) ExpandedTitleContentHandler(org.apache.tika.sax.ExpandedTitleContentHandler) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) RichTextContentHandler(org.apache.tika.sax.RichTextContentHandler) BoilerpipeContentHandler(org.apache.tika.parser.html.BoilerpipeContentHandler) Parser(org.apache.tika.parser.Parser) HtmlParser(org.apache.tika.parser.html.HtmlParser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) DigestingParser(org.apache.tika.parser.DigestingParser)

Aggregations

StreamingOutput (javax.ws.rs.core.StreamingOutput)23 OutputStream (java.io.OutputStream)13 IOException (java.io.IOException)8 Path (javax.ws.rs.Path)7 Produces (javax.ws.rs.Produces)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 Response (javax.ws.rs.core.Response)6 GET (javax.ws.rs.GET)5 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 Consumes (javax.ws.rs.Consumes)4 POST (javax.ws.rs.POST)4 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)4 QueryInterruptedException (io.druid.query.QueryInterruptedException)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 ByteStreams (com.google.common.io.ByteStreams)2 ISE (io.druid.java.util.common.ISE)2 Writer (java.io.Writer)2