Search in sources :

Example 96 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project pulsar by yahoo.

the class AdminTest method brokerStats.

@Test
void brokerStats() throws Exception {
    doReturn("client-id").when(brokerStats).clientAppId();
    Collection<Metrics> metrics = brokerStats.getMetrics();
    assertNotNull(metrics);
    LoadReport loadReport = brokerStats.getLoadReport();
    assertNotNull(loadReport);
    assertEquals(loadReport.isOverLoaded(), false);
    Collection<Metrics> mBeans = brokerStats.getMBeans();
    assertTrue(!mBeans.isEmpty());
    AllocatorStats allocatorStats = brokerStats.getAllocatorStats("default");
    assertNotNull(allocatorStats);
    Map<String, Map<String, PendingBookieOpsStats>> bookieOpsStats = brokerStats.getPendingBookieOpsStats();
    assertTrue(bookieOpsStats.isEmpty());
    StreamingOutput destination = brokerStats.getDestinations2();
    assertNotNull(destination);
    Map<Long, Collection<ResourceUnit>> resource = brokerStats.getBrokerResourceAvailability("prop", "use", "ns2");
    // size should be 1 with default resourceUnit
    assertTrue(resource.size() == 1);
}
Also used : Metrics(com.yahoo.pulsar.broker.stats.Metrics) LoadReport(com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport) AllocatorStats(com.yahoo.pulsar.common.stats.AllocatorStats) Collection(java.util.Collection) StreamingOutput(javax.ws.rs.core.StreamingOutput) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 97 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 98 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)

Example 99 with StreamingOutput

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

the class ProvenanceEventResource method getInputContent.

/**
 * Gets the content for the input of the specified event.
 *
 * @param clusterNodeId The id of the node within the cluster this content is on. Required if clustered.
 * @param id            The id of the provenance event associated with this content.
 * @return The content stream
 */
@GET
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.WILDCARD)
@Path("{id}/content/input")
@ApiOperation(value = "Gets the input content for a provenance event", response = StreamingOutput.class, authorizations = { @Authorization(value = "Read Component Data - /data/{component-type}/{uuid}") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response getInputContent(@ApiParam(value = "The id of the node where the content exists if clustered.", required = false) @QueryParam("clusterNodeId") final String clusterNodeId, @ApiParam(value = "The provenance event id.", required = true) @PathParam("id") final LongParameter id) {
    // ensure proper input
    if (id == null) {
        throw new IllegalArgumentException("The event id must be specified.");
    }
    // replicate if cluster manager
    if (isReplicateRequest()) {
        // determine where this request should be sent
        if (clusterNodeId == null) {
            throw new IllegalArgumentException("The id of the node in the cluster is required.");
        } else {
            return replicate(HttpMethod.GET, clusterNodeId);
        }
    }
    // get the uri of the request
    final String uri = generateResourceUri("provenance", "events", String.valueOf(id.getLong()), "content", "input");
    // get an input stream to the content
    final DownloadableContent content = serviceFacade.getContent(id.getLong(), uri, ContentDirection.INPUT);
    // generate a streaming response
    final StreamingOutput response = new StreamingOutput() {

        @Override
        public void write(OutputStream output) throws IOException, WebApplicationException {
            try (InputStream is = content.getContent()) {
                // stream the content to the response
                StreamUtils.copy(is, output);
                // flush the response
                output.flush();
            }
        }
    };
    // use the appropriate content type
    String contentType = content.getType();
    if (contentType == null) {
        contentType = MediaType.APPLICATION_OCTET_STREAM;
    }
    return generateOkResponse(response).type(contentType).header("Content-Disposition", String.format("attachment; filename=\"%s\"", content.getFilename())).build();
}
Also used : DownloadableContent(org.apache.nifi.web.DownloadableContent) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 100 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project pentaho-platform by pentaho.

the class FileServiceTest method testDoGetFileAsInline.

@Test
public void testDoGetFileAsInline() throws FileNotFoundException {
    /*
     * TEST 1
     */
    doReturn(true).when(fileService).isPath(anyString());
    doReturn(true).when(fileService).isPathValid(anyString());
    RepositoryDownloadWhitelist mockWhiteList = mock(RepositoryDownloadWhitelist.class);
    doReturn(mockWhiteList).when(fileService).getWhitelist();
    doReturn(true).when(mockWhiteList).accept(anyString());
    RepositoryFile mockRepoFile = mock(RepositoryFile.class);
    doReturn(mockRepoFile).when(fileService.repository).getFile(anyString());
    SimpleRepositoryFileData mockData = mock(SimpleRepositoryFileData.class);
    doReturn(mockData).when(fileService.repository).getDataForRead(any(Serializable.class), any(Class.class));
    InputStream mockInputStream = mock(InputStream.class);
    doReturn(mockInputStream).when(mockData).getInputStream();
    StreamingOutput mockStreamingOutput = mock(StreamingOutput.class);
    doReturn(mockStreamingOutput).when(fileService).getStreamingOutput(mockInputStream);
    FileService.RepositoryFileToStreamWrapper wrapper = fileService.doGetFileAsInline("test");
    verify(fileService.repository, times(1)).getFile(anyString());
    verify(mockWhiteList, times(1)).accept(anyString());
    verify(fileService, times(2)).getRepository();
    verify(fileService.repository, times(1)).getDataForRead(any(Serializable.class), any(Class.class));
    verify(mockData, times(1)).getInputStream();
    assertEquals(mockRepoFile, wrapper.getRepositoryFile());
    assertEquals(mockStreamingOutput, wrapper.getOutputStream());
    /*
     * TEST 2
     */
    doReturn(false).when(fileService).isPath(anyString());
    doReturn(mockRepoFile).when(fileService.repository).getFileById(anyString());
    wrapper = fileService.doGetFileAsInline("test");
    verify(fileService.repository, times(1)).getFileById(anyString());
    verify(fileService, times(4)).getRepository();
    assertEquals(mockRepoFile, wrapper.getRepositoryFile());
    assertEquals(mockStreamingOutput, wrapper.getOutputStream());
}
Also used : Serializable(java.io.Serializable) RepositoryDownloadWhitelist(org.pentaho.platform.repository.RepositoryDownloadWhitelist) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) StreamingOutput(javax.ws.rs.core.StreamingOutput) Test(org.junit.Test)

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