Search in sources :

Example 1 with XMLAuditLogMarshaller

use of com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller in project coprhd-controller by CoprHD.

the class DbAuditLogRetriever method getBulkAuditLogs.

@Override
public void getBulkAuditLogs(AuditLogRequest auditLogRequest, MediaType type, Writer writer) throws MarshallingExcetion {
    if (dbClient == null) {
        throw APIException.internalServerErrors.auditLogNoDb();
    }
    AuditLogMarshaller marshaller = null;
    if (type.equals(MediaType.APPLICATION_XML_TYPE)) {
        marshaller = new XMLAuditLogMarshaller();
        log.debug("Parser type: {}", type.toString());
    } else if (type.equals(MediaType.APPLICATION_JSON_TYPE)) {
        marshaller = new JSONAuditLogMarshaller();
        log.debug("Parser type: {}", type.toString());
    } else if (type.equals(MediaType.TEXT_PLAIN_TYPE)) {
        marshaller = new TextAuditLogMarshaller();
        log.debug("parser type: {}", type.toString());
    } else {
        log.warn("unsupported type: {}, use XML", type.toString());
        marshaller = new XMLAuditLogMarshaller();
    }
    marshaller.setLang(auditLogRequest.getLanguage());
    DateTime start = auditLogRequest.getStartTime();
    DateTime end = auditLogRequest.getEndTime();
    TimeSeriesMetadata.TimeBucket bucket = TimeSeriesMetadata.TimeBucket.HOUR;
    if (start.plusSeconds(59).isEqual(end.toInstant())) {
        bucket = TimeSeriesMetadata.TimeBucket.MINUTE;
    }
    AuditLogQueryResult result = new AuditLogQueryResult(marshaller, writer, auditLogRequest);
    marshaller.header(writer);
    log.info("Query time bucket  {} to {}", start, end);
    for (; !start.isAfter(end.toInstant()); start = start.plusHours(1)) {
        dbClient.queryTimeSeries(AuditLogTimeSeries.class, start, bucket, result, getThreadPool());
    }
    result.outputCount();
    marshaller.tailer(writer);
}
Also used : JSONAuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.JSONAuditLogMarshaller) XMLAuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller) JSONAuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.JSONAuditLogMarshaller) TimeSeriesMetadata(com.emc.storageos.db.client.TimeSeriesMetadata) XMLAuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller) DateTime(org.joda.time.DateTime) AuditLogQueryResult(com.emc.storageos.api.service.impl.resource.utils.AuditLogQueryResult)

Example 2 with XMLAuditLogMarshaller

use of com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller in project coprhd-controller by CoprHD.

the class XMLAuditLogMarshallerTest method testXmlAuditLogMarshalling.

@Test
public void testXmlAuditLogMarshalling() throws URISyntaxException, IOException, MarshallingExcetion, JAXBException {
    XMLAuditLogMarshaller xm = new XMLAuditLogMarshaller();
    AuditLog log = new AuditLog();
    log.setProductId("productId.1");
    log.setTenantId(new URI("http://tenant.1"));
    log.setUserId(new URI("http://user.1"));
    log.setServiceType("serviceType.1");
    log.setAuditType("auditType.1");
    log.setDescription("description.1");
    log.setOperationalStatus("operationalStatus.1");
    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        @Override
        public void write(int b) throws IOException {
            this.string.append((char) b);
        }

        public String toString() {
            return this.string.toString();
        }
    };
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
    xm.marshal(log, writer);
    writer.close();
    FileWriter fileWriter = new FileWriter(XmlTestOutputFile);
    fileWriter.write(output.toString());
    fileWriter.close();
    JAXBContext context = null;
    Unmarshaller unmarshaller = null;
    context = JAXBContext.newInstance(AuditLog.class);
    unmarshaller = context.createUnmarshaller();
    File f = new File(XmlTestOutputFile);
    AuditLog auditLog = (AuditLog) unmarshaller.unmarshal(f);
    Assert.assertEquals("productId.1", auditLog.getProductId().toString());
    Assert.assertEquals("http://tenant.1", auditLog.getTenantId().toString());
    Assert.assertEquals("http://user.1", auditLog.getUserId().toString());
    Assert.assertEquals("serviceType.1", auditLog.getServiceType().toString());
    Assert.assertEquals("auditType.1", auditLog.getAuditType().toString());
    Assert.assertEquals("description.1", auditLog.getDescription().toString());
    Assert.assertEquals("operationalStatus.1", auditLog.getOperationalStatus().toString());
    deleteIfExists(XmlTestOutputFile);
}
Also used : OutputStream(java.io.OutputStream) FileWriter(java.io.FileWriter) OutputStreamWriter(java.io.OutputStreamWriter) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) XMLAuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller) URI(java.net.URI) File(java.io.File) AuditLog(com.emc.storageos.db.client.model.AuditLog) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 3 with XMLAuditLogMarshaller

use of com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller in project coprhd-controller by CoprHD.

the class XMLAuditLogMarshallerTest method testXmlAuditLogMarshallingForNullLog.

@Test
public void testXmlAuditLogMarshallingForNullLog() throws URISyntaxException, IOException, MarshallingExcetion, JAXBException {
    deleteIfExists(XmlTestOutputFile);
    XMLAuditLogMarshaller jm = new XMLAuditLogMarshaller();
    AuditLog log = null;
    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        @Override
        public void write(int b) throws IOException {
            this.string.append((char) b);
        }

        public String toString() {
            return this.string.toString();
        }
    };
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
    jm.header(writer);
    jm.marshal(log, writer);
    jm.tailer(writer);
    writer.close();
    JAXBContext context = null;
    Unmarshaller unmarshaller = null;
    context = JAXBContext.newInstance(AuditLog.class);
    unmarshaller = context.createUnmarshaller();
    File f = new File(XmlTestOutputFile);
    try {
        @SuppressWarnings("unused") AuditLog auditLog = (AuditLog) unmarshaller.unmarshal(f);
    } catch (Exception e) {
        Assert.assertTrue(e.toString().contains("java.io.FileNotFoundException"));
    }
    deleteIfExists(XmlTestOutputFile);
}
Also used : OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) XMLAuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller) File(java.io.File) AuditLog(com.emc.storageos.db.client.model.AuditLog) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 4 with XMLAuditLogMarshaller

use of com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller in project coprhd-controller by CoprHD.

the class XMLAuditLogMarshallerTest method testXmlAuditLogMarshallingForIOExceptions.

@Test
public void testXmlAuditLogMarshallingForIOExceptions() throws URISyntaxException, IOException, MarshallingExcetion {
    deleteIfExists(XmlTestOutputFile);
    XMLAuditLogMarshaller jm = new XMLAuditLogMarshaller();
    AuditLog log = new AuditLog();
    log.setProductId("productId.2");
    log.setTenantId(new URI("http://tenant.2"));
    log.setUserId(new URI("http://user.2"));
    log.setServiceType("serviceType.2");
    log.setAuditType("auditType.2");
    log.setDescription("description.2");
    log.setOperationalStatus("operationalStatus.2");
    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        @Override
        public void write(int b) throws IOException {
            this.string.append((char) b);
        }

        public String toString() {
            return this.string.toString();
        }
    };
    try {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
        writer.close();
        jm.header(writer);
    } catch (MarshallingExcetion e) {
        Assert.assertTrue(e.toString().contains("XML head Streaming failed"));
    }
    try {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
        writer.close();
        jm.marshal(log, writer);
    } catch (MarshallingExcetion e) {
        Assert.assertTrue(e.toString().contains("XML Streaming Error"));
    }
    try {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
        writer.close();
        jm.tailer(writer);
    } catch (MarshallingExcetion e) {
        Assert.assertTrue(e.toString().contains("XML tail Streaming failed"));
    }
    deleteIfExists(XmlTestOutputFile);
}
Also used : MarshallingExcetion(com.emc.storageos.api.service.impl.resource.utils.MarshallingExcetion) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) XMLAuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller) URI(java.net.URI) AuditLog(com.emc.storageos.db.client.model.AuditLog) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 5 with XMLAuditLogMarshaller

use of com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller in project coprhd-controller by CoprHD.

the class DummyAuditLogRetriever method getBulkAuditLogs.

@Override
public void getBulkAuditLogs(AuditLogRequest auditLogRequest, MediaType type, Writer writer) throws MarshallingExcetion {
    AuditLogMarshaller marshaller = null;
    if (type == MediaType.APPLICATION_XML_TYPE) {
        marshaller = new XMLAuditLogMarshaller();
    } else if (type == MediaType.APPLICATION_JSON_TYPE) {
        marshaller = new JSONAuditLogMarshaller();
    }
    marshaller.header(writer);
    List<AuditLog> auditLogs = null;
    try {
        auditLogs = getDummyAuditLogs();
    } catch (URISyntaxException e) {
        _logger.error("Error getting logs", e);
    }
    for (AuditLog log : auditLogs) {
        if (type == MediaType.APPLICATION_XML_TYPE) {
            marshaller.marshal(log, writer);
        } else if (type == MediaType.APPLICATION_JSON_TYPE) {
            marshaller.marshal(log, writer);
        }
    }
    marshaller.tailer(writer);
}
Also used : JSONAuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.JSONAuditLogMarshaller) JSONAuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.JSONAuditLogMarshaller) XMLAuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller) AuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.AuditLogMarshaller) URISyntaxException(java.net.URISyntaxException) XMLAuditLogMarshaller(com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller) AuditLog(com.emc.storageos.db.client.model.AuditLog)

Aggregations

XMLAuditLogMarshaller (com.emc.storageos.api.service.impl.resource.utils.XMLAuditLogMarshaller)5 AuditLog (com.emc.storageos.db.client.model.AuditLog)4 BufferedWriter (java.io.BufferedWriter)3 OutputStream (java.io.OutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 Test (org.junit.Test)3 JSONAuditLogMarshaller (com.emc.storageos.api.service.impl.resource.utils.JSONAuditLogMarshaller)2 File (java.io.File)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 JAXBContext (javax.xml.bind.JAXBContext)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 AuditLogMarshaller (com.emc.storageos.api.service.impl.resource.utils.AuditLogMarshaller)1 AuditLogQueryResult (com.emc.storageos.api.service.impl.resource.utils.AuditLogQueryResult)1 MarshallingExcetion (com.emc.storageos.api.service.impl.resource.utils.MarshallingExcetion)1 TimeSeriesMetadata (com.emc.storageos.db.client.TimeSeriesMetadata)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 JAXBException (javax.xml.bind.JAXBException)1 DateTime (org.joda.time.DateTime)1