Search in sources :

Example 41 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class CsvTransformer method writeMetacardsToCsv.

public static Appendable writeMetacardsToCsv(final List<Metacard> metacards, final List<AttributeDescriptor> orderedAttributeDescriptors, final Map<String, String> aliasMap) throws CatalogTransformerException {
    StringBuilder stringBuilder = new StringBuilder();
    try {
        CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.RFC4180);
        printColumnHeaders(csvPrinter, orderedAttributeDescriptors, aliasMap);
        metacards.forEach(metacard -> printMetacard(csvPrinter, metacard, orderedAttributeDescriptors));
        return csvPrinter.getOut();
    } catch (IOException ioe) {
        throw new CatalogTransformerException(ioe);
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException)

Example 42 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class ZipCompression method createZip.

private InputStream createZip(SourceResponse sourceResponse, String transformerId, Map<String, Serializable> arguments) throws CatalogTransformerException {
    ServiceReference<MetacardTransformer> serviceRef = getTransformerServiceReference(transformerId);
    MetacardTransformer transformer = bundleContext.getService(serviceRef);
    String extension = getFileExtensionFromService(serviceRef);
    if (StringUtils.isNotBlank(extension)) {
        extension = "." + extension;
    }
    try (FileBackedOutputStream fileBackedOutputStream = new FileBackedOutputStream(BUFFER_SIZE);
        ZipOutputStream zipOutputStream = new ZipOutputStream(fileBackedOutputStream)) {
        for (Result result : sourceResponse.getResults()) {
            Metacard metacard = result.getMetacard();
            BinaryContent binaryContent = getTransformedMetacard(metacard, arguments, transformer);
            if (binaryContent != null) {
                ZipEntry entry = new ZipEntry(METACARD_PATH + metacard.getId() + extension);
                zipOutputStream.putNextEntry(entry);
                zipOutputStream.write(binaryContent.getByteArray());
                zipOutputStream.closeEntry();
            } else {
                LOGGER.debug("Metacard with id [{}] was not added to zip file", metacard.getId());
            }
        }
        return fileBackedOutputStream.asByteSource().openStream();
    } catch (IOException e) {
        throw new CatalogTransformerException("An error occurred while initializing or closing output stream", e);
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) BinaryContent(ddf.catalog.data.BinaryContent) Result(ddf.catalog.data.Result)

Example 43 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class GeometryAdapter method marshalFrom.

public static GeometryElement marshalFrom(Attribute attribute) throws CatalogTransformerException {
    GeometryElement element = new GeometryElement();
    element.setName(attribute.getName());
    if (attribute.getValue() != null) {
        for (Serializable value : attribute.getValues()) {
            if (!(value instanceof String)) {
                continue;
            }
            String wkt = (String) value;
            WKTReader wktReader = new WKTReader(geometryFactory);
            Geometry jtsGeometry = null;
            try {
                jtsGeometry = wktReader.read(wkt);
            } catch (ParseException e) {
                throw new CatalogTransformerException("Could not transform Metacard to XML.  Invalid WKT.", e);
            }
            JTSToGML311GeometryConverter converter = new JTSToGML311GeometryConverter();
            @SuppressWarnings("unchecked") JAXBElement<AbstractGeometryType> gmlElement = (JAXBElement<AbstractGeometryType>) converter.createElement(jtsGeometry);
            GeometryElement.Value geoValue = new GeometryElement.Value();
            geoValue.setGeometry(gmlElement);
            element.getValue().add(geoValue);
        }
    }
    return element;
}
Also used : Serializable(java.io.Serializable) JTSToGML311GeometryConverter(org.jvnet.ogc.gml.v_3_1_1.jts.JTSToGML311GeometryConverter) AbstractGeometryType(net.opengis.gml.v_3_1_1.AbstractGeometryType) Value(ddf.catalog.transformer.xml.binding.GeometryElement.Value) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) JAXBElement(javax.xml.bind.JAXBElement) WKTReader(org.locationtech.jts.io.WKTReader) Geometry(org.locationtech.jts.geom.Geometry) Value(ddf.catalog.transformer.xml.binding.GeometryElement.Value) GeometryElement(ddf.catalog.transformer.xml.binding.GeometryElement) ParseException(org.locationtech.jts.io.ParseException)

Example 44 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class StringxmlAdapter method marshalFrom.

/**
 * @param attribute
 * @return JAXB representable attribute
 * @throws CatalogTransformerException
 */
public static StringxmlElement marshalFrom(Attribute attribute) throws CatalogTransformerException {
    StringxmlElement element = new StringxmlElement();
    element.setName(attribute.getName());
    if (attribute.getValue() != null) {
        for (Serializable value : attribute.getValues()) {
            if (!(value instanceof String)) {
                continue;
            }
            String xmlString = (String) value;
            Element anyElement = null;
            DocumentBuilder builder = null;
            try {
                synchronized (FACTORY) {
                    builder = FACTORY.newDocumentBuilder();
                    builder.setErrorHandler(null);
                }
                anyElement = builder.parse(new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8))).getDocumentElement();
            } catch (ParserConfigurationException | SAXException | IOException e) {
                throw new CatalogTransformerException(TRANSFORMATION_FAILED_ERROR_MESSAGE, e);
            }
            Value anyValue = new StringxmlElement.Value();
            anyValue.setAny(anyElement);
            element.getValue().add(anyValue);
        }
    }
    return element;
}
Also used : Serializable(java.io.Serializable) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) StringxmlElement(ddf.catalog.transformer.xml.binding.StringxmlElement) Element(org.w3c.dom.Element) Value(ddf.catalog.transformer.xml.binding.StringxmlElement.Value) StringxmlElement(ddf.catalog.transformer.xml.binding.StringxmlElement) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 45 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class CswQueryResponseTransformerTest method testMarshalAcknowledgementWithFailedTransforms.

@Test
public void testMarshalAcknowledgementWithFailedTransforms() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
    GetRecordsType query = new GetRecordsType();
    query.setResultType(ResultType.RESULTS);
    query.setMaxRecords(BigInteger.valueOf(6));
    query.setStartPosition(BigInteger.valueOf(0));
    SourceResponse sourceResponse = createSourceResponse(query, 6);
    Map<String, Serializable> args = new HashMap<>();
    args.put(CswConstants.RESULT_TYPE_PARAMETER, ResultType.RESULTS);
    args.put(CswConstants.GET_RECORDS, query);
    PrintWriter printWriter = getSimplePrintWriter();
    MetacardTransformer mockMetacardTransformer = mock(MetacardTransformer.class);
    final AtomicLong atomicLong = new AtomicLong(0);
    when(mockMetacardTransformer.transform(any(Metacard.class), anyMap())).then(invocationOnMock -> {
        if (atomicLong.incrementAndGet() == 2) {
            throw new CatalogTransformerException("");
        }
        Metacard metacard = (Metacard) invocationOnMock.getArguments()[0];
        BinaryContentImpl bci = new BinaryContentImpl(IOUtils.toInputStream(metacard.getId() + ",", StandardCharsets.UTF_8), new MimeType("application/xml"));
        return bci;
    });
    when(mockPrintWriterProvider.build((Class<Metacard>) notNull())).thenReturn(printWriter);
    when(mockTransformerManager.getTransformerBySchema(anyString())).thenReturn(mockMetacardTransformer);
    CswQueryResponseTransformer cswQueryResponseTransformer = new CswQueryResponseTransformer(mockTransformerManager, mockPrintWriterProvider);
    cswQueryResponseTransformer.init();
    BinaryContent content = cswQueryResponseTransformer.transform(sourceResponse, args);
    cswQueryResponseTransformer.destroy();
    String xml = new String(content.getByteArray());
    assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_MATCHED_ATTRIBUTE + " 6"));
    assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_RETURNED_ATTRIBUTE + " 5"));
    assertThat(xml, containsString(CswQueryResponseTransformer.NEXT_RECORD_ATTRIBUTE + " 0"));
}
Also used : Serializable(java.io.Serializable) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) HashMap(java.util.HashMap) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BinaryContent(ddf.catalog.data.BinaryContent) MimeType(javax.activation.MimeType) AtomicLong(java.util.concurrent.atomic.AtomicLong) Metacard(ddf.catalog.data.Metacard) PrintWriter(ddf.catalog.transformer.api.PrintWriter) Test(org.junit.Test)

Aggregations

CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)112 IOException (java.io.IOException)53 Metacard (ddf.catalog.data.Metacard)44 InputStream (java.io.InputStream)40 ByteArrayInputStream (java.io.ByteArrayInputStream)29 BinaryContent (ddf.catalog.data.BinaryContent)25 InputTransformer (ddf.catalog.transform.InputTransformer)21 Serializable (java.io.Serializable)21 HashMap (java.util.HashMap)21 Result (ddf.catalog.data.Result)16 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)15 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)14 ArrayList (java.util.ArrayList)13 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)12 Test (org.junit.Test)12 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)10 MimeType (javax.activation.MimeType)10 SourceResponse (ddf.catalog.operation.SourceResponse)9 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)8 List (java.util.List)8