Search in sources :

Example 81 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project coprhd-controller by CoprHD.

the class DbStatRetrieverTest method meteringXmlServiceDBRetrieverTest.

@Test
public void meteringXmlServiceDBRetrieverTest() throws WebApplicationException, IOException, JAXBException {
    deleteIfExists(XmlTestOutputFile);
    DummyDBClient dbClient = new DummyDBClient();
    MeteringService statResource = new MeteringService();
    // statResource.setDbClient(dbClient);
    DbStatRetriever dummyDbStatRetriever = new DbStatRetriever();
    dummyDbStatRetriever.setDbClient(dbClient);
    statResource.setStatRetriever(dummyDbStatRetriever);
    DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_XML_TYPE);
    Response r = statResource.getStats("2012-01-01T00:00", header);
    Assert.assertNotNull(r);
    Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus());
    Assert.assertTrue(r.getEntity() instanceof StreamingOutput);
    StreamingOutput so = (StreamingOutput) r.getEntity();
    File of = new File(XmlTestOutputFile);
    OutputStream os = new FileOutputStream(of);
    try {
        so.write(os);
    } finally {
        os.close();
    }
    JAXBContext context = null;
    Unmarshaller unmarshaller = null;
    context = JAXBContext.newInstance(Stats.class);
    unmarshaller = context.createUnmarshaller();
    Object o = unmarshaller.unmarshal(new File(XmlTestOutputFile));
    Assert.assertTrue(o instanceof Stats);
    Stats stats = (Stats) o;
    // expected number of stats unmarshaled
    Assert.assertEquals(10, stats.stats.size());
    deleteIfExists(XmlTestOutputFile);
}
Also used : DummyHttpHeaders(com.emc.storageos.api.service.utils.DummyHttpHeaders) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DbStatRetriever(com.emc.storageos.api.service.impl.resource.utils.DbStatRetriever) StreamingOutput(javax.ws.rs.core.StreamingOutput) JAXBContext(javax.xml.bind.JAXBContext) DummyDBClient(com.emc.storageos.api.service.utils.DummyDBClient) Response(javax.ws.rs.core.Response) MeteringService(com.emc.storageos.api.service.impl.resource.MeteringService) FileOutputStream(java.io.FileOutputStream) Stats(com.emc.storageos.api.service.utils.Stats) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) Test(org.junit.Test)

Example 82 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project coprhd-controller by CoprHD.

the class MeteringServiceTest method meteringServiceTestXML.

@Test
public void meteringServiceTestXML() throws WebApplicationException, IOException, JAXBException {
    deleteIfExists(XmlTestOutputFile);
    DummyStatRetriever dbStatRetriever = new DummyStatRetriever();
    MeteringService statResource = new MeteringService();
    statResource.setStatRetriever(dbStatRetriever);
    DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_XML_TYPE);
    Response r = statResource.getStats("2012-08-08T00:00", header);
    Assert.assertNotNull(r);
    Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus());
    Assert.assertTrue(r.getEntity() instanceof StreamingOutput);
    StreamingOutput so = (StreamingOutput) r.getEntity();
    File of = new File(XmlTestOutputFile);
    OutputStream os = new FileOutputStream(of);
    so.write(os);
    os.close();
    JAXBContext context = null;
    Unmarshaller unmarshaller = null;
    context = JAXBContext.newInstance(Stats.class);
    unmarshaller = context.createUnmarshaller();
    Object o = unmarshaller.unmarshal(new File(XmlTestOutputFile));
    Assert.assertTrue(o instanceof Stats);
    Stats stats = (Stats) o;
    // expected number of stats unmarshaled
    Assert.assertEquals(100, stats.stats.size());
    deleteIfExists(XmlTestOutputFile);
}
Also used : Response(javax.ws.rs.core.Response) DummyHttpHeaders(com.emc.storageos.api.service.utils.DummyHttpHeaders) MeteringService(com.emc.storageos.api.service.impl.resource.MeteringService) DummyStatRetriever(com.emc.storageos.api.service.utils.DummyStatRetriever) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Stats(com.emc.storageos.api.service.utils.Stats) StreamingOutput(javax.ws.rs.core.StreamingOutput) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) Test(org.junit.Test)

Example 83 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project coprhd-controller by CoprHD.

the class MonitoringServiceTest method testEventRetriverXML.

@Test
public void testEventRetriverXML() throws WebApplicationException, IOException, JAXBException {
    deleteIfExists(XmlTestOutputFile);
    EventRetriever eventRetriever = new DummyEventRetriever();
    MonitoringService eventResource = new MonitoringService();
    eventResource.setEventRetriever(eventRetriever);
    DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_XML_TYPE);
    Response r = eventResource.getEvents("2012-05-05T00:00", header);
    Assert.assertNotNull(r);
    Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus());
    Assert.assertTrue(r.getEntity() instanceof StreamingOutput);
    StreamingOutput so = (StreamingOutput) r.getEntity();
    File of = new File(XmlTestOutputFile);
    OutputStream os = new FileOutputStream(of);
    so.write(os);
    os.close();
    JAXBContext context = null;
    Unmarshaller unmarshaller = null;
    context = JAXBContext.newInstance(Events.class);
    unmarshaller = context.createUnmarshaller();
    Object o = unmarshaller.unmarshal(new File(XmlTestOutputFile));
    Assert.assertTrue(o instanceof Events);
    Events events = (Events) o;
    // expected number of events unmarshaled
    Assert.assertEquals(100, events.events.size());
}
Also used : DummyHttpHeaders(com.emc.storageos.api.service.utils.DummyHttpHeaders) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) JAXBContext(javax.xml.bind.JAXBContext) Response(javax.ws.rs.core.Response) DummyEventRetriever(com.emc.storageos.api.service.utils.DummyEventRetriever) Events(com.emc.storageos.api.service.utils.Events) FileOutputStream(java.io.FileOutputStream) Unmarshaller(javax.xml.bind.Unmarshaller) MonitoringService(com.emc.storageos.api.service.impl.resource.MonitoringService) File(java.io.File) EventRetriever(com.emc.storageos.api.service.impl.resource.utils.EventRetriever) DummyEventRetriever(com.emc.storageos.api.service.utils.DummyEventRetriever) Test(org.junit.Test)

Example 84 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project coprhd-controller by CoprHD.

the class DbEventRetrieverTest method meteringServiceNullDBclientTestXML.

@Test
public void meteringServiceNullDBclientTestXML() throws WebApplicationException, IOException, JAXBException {
    deleteIfExists(XmlTestOutputFile);
    DummyDBClient dbClient = null;
    MonitoringService eventResource = new MonitoringService();
    // statResource.setDbClient(dbClient);
    DbEventRetriever dummyDbStatRetriever = new DbEventRetriever();
    dummyDbStatRetriever.setDbClient(dbClient);
    eventResource.setEventRetriever(dummyDbStatRetriever);
    DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_XML_TYPE);
    Response r = eventResource.getEvents("2012-01-02T00:00", header);
    Assert.assertNotNull(r);
    Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus());
    Assert.assertTrue(r.getEntity() instanceof StreamingOutput);
    StreamingOutput so = (StreamingOutput) r.getEntity();
    File of = new File(XmlTestOutputFile);
    OutputStream os = new FileOutputStream(of);
    try {
        so.write(os);
    } catch (InternalServerErrorException e) {
        Assert.assertTrue(e.toString().contains("DB"));
    } finally {
        os.close();
    }
}
Also used : Response(javax.ws.rs.core.Response) DummyHttpHeaders(com.emc.storageos.api.service.utils.DummyHttpHeaders) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) InternalServerErrorException(com.emc.storageos.svcs.errorhandling.resources.InternalServerErrorException) StreamingOutput(javax.ws.rs.core.StreamingOutput) DbEventRetriever(com.emc.storageos.api.service.impl.resource.utils.DbEventRetriever) MonitoringService(com.emc.storageos.api.service.impl.resource.MonitoringService) File(java.io.File) DummyDBClient(com.emc.storageos.api.service.utils.DummyDBClient) Test(org.junit.Test)

Example 85 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project neo4j-documentation by neo4j.

the class ColleaguesCypherExecutionResource method findColleagues.

@GET
@Path("/{personName}")
public Response findColleagues(@PathParam("personName") final String personName) {
    final Map<String, Object> params = MapUtil.map("personName", personName);
    StreamingOutput stream = new StreamingOutput() {

        @Override
        public void write(OutputStream os) throws IOException, WebApplicationException {
            JsonGenerator jg = objectMapper.getJsonFactory().createJsonGenerator(os, JsonEncoding.UTF8);
            jg.writeStartObject();
            jg.writeFieldName("colleagues");
            jg.writeStartArray();
            final GraphDatabaseService graphDb = dbms.database("neo4j");
            try (Transaction tx = graphDb.beginTx();
                Result result = tx.execute(colleaguesQuery(), params)) {
                while (result.hasNext()) {
                    Map<String, Object> row = result.next();
                    jg.writeString(((Node) row.get("colleague")).getProperty("name").toString());
                }
                tx.commit();
            }
            jg.writeEndArray();
            jg.writeEndObject();
            jg.flush();
            jg.close();
        }
    };
    return Response.ok().entity(stream).type(MediaType.APPLICATION_JSON).build();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) OutputStream(java.io.OutputStream) Node(org.neo4j.graphdb.Node) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) StreamingOutput(javax.ws.rs.core.StreamingOutput) Result(org.neo4j.graphdb.Result) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

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