Search in sources :

Example 36 with InputTransformer

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

the class TestRestEndpoint method getMockInputTransformer.

protected InputTransformer getMockInputTransformer() {
    InputTransformer inputTransformer = mock(InputTransformer.class);
    Metacard generatedMetacard = getSimpleMetacard();
    try {
        when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
        when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
    } catch (IOException e) {
        LOGGER.debug("Exception occurred during test", e);
    } catch (CatalogTransformerException e) {
        LOGGER.debug("Exception occurred during test", e);
    }
    return inputTransformer;
}
Also used : Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

Example 37 with InputTransformer

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

the class TestRestEndpoint method addMatchingService.

private MimeTypeToTransformerMapper addMatchingService(RESTEndpoint rest, List<InputTransformer> sortedListOfTransformers) {
    MimeTypeToTransformerMapper matchingService = mock(MimeTypeToTransformerMapper.class);
    when(matchingService.findMatches(eq(InputTransformer.class), isA(MimeType.class))).thenReturn((List) sortedListOfTransformers);
    rest.setMimeTypeToTransformerMapper(matchingService);
    return matchingService;
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) InputTransformer(ddf.catalog.transform.InputTransformer) MimeType(javax.activation.MimeType)

Example 38 with InputTransformer

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

the class TestOpenSearchSource method getMockInputTransformer.

protected InputTransformer getMockInputTransformer() {
    InputTransformer inputTransformer = mock(InputTransformer.class);
    Metacard generatedMetacard = getSimpleMetacard();
    try {
        when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
        when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
    } catch (IOException e) {
        fail();
    } catch (CatalogTransformerException e) {
        fail();
    }
    return inputTransformer;
}
Also used : Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

Example 39 with InputTransformer

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

the class OpenSearchSource method getInputTransformer.

private InputTransformer getInputTransformer(InputStream inputStream) {
    XMLStreamReader xmlStreamReader = null;
    try {
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(inputStream);
        while (xmlStreamReader.hasNext()) {
            int next = xmlStreamReader.next();
            if (next == XMLStreamConstants.START_ELEMENT) {
                String namespaceUri = xmlStreamReader.getNamespaceURI();
                InputTransformer transformerReference = lookupTransformerReference(namespaceUri);
                if (transformerReference != null) {
                    return transformerReference;
                }
            }
        }
    } catch (XMLStreamException | InvalidSyntaxException e) {
        LOGGER.debug("Failed to parse transformer namespace", e);
    } finally {
        try {
            if (xmlStreamReader != null) {
                xmlStreamReader.close();
            }
        } catch (XMLStreamException e) {
            LOGGER.debug("failed to close namespace reader", e);
        }
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InputTransformer(ddf.catalog.transform.InputTransformer)

Example 40 with InputTransformer

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

the class InputTransformerProducer method generateMetacard.

private Metacard generateMetacard(MimeType mimeType, MimeTypeToTransformerMapper mapper, InputStream message) throws MetacardCreationException {
    LOGGER.trace("ENTERING: generateMetacard");
    List<InputTransformer> listOfCandidates = mapper.findMatches(InputTransformer.class, mimeType);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    }
    Metacard generatedMetacard = null;
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            IOUtils.copy(message, fileBackedOutputStream);
        } catch (IOException e) {
            throw new MetacardCreationException("Could not copy bytes of content message.", e);
        }
        // can create the metacard, then do not need to try any remaining InputTransformers.
        for (InputTransformer transformer : listOfCandidates) {
            try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
                generatedMetacard = transformer.transform(inputStreamMessageCopy);
            } catch (IOException | CatalogTransformerException e) {
                LOGGER.debug("Transformer [" + transformer + "] could not create metacard.", e);
            }
            if (generatedMetacard != null) {
                break;
            }
        }
        if (generatedMetacard == null) {
            throw new MetacardCreationException("Could not create metacard with mimeType " + mimeType + ". No valid transformers found.");
        }
        LOGGER.trace("EXITING: generateMetacard");
    } catch (IOException e) {
        throw new MetacardCreationException("Could not create metacard.", e);
    }
    return generatedMetacard;
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardCreationException(ddf.catalog.data.MetacardCreationException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

Aggregations

InputTransformer (ddf.catalog.transform.InputTransformer)56 InputStream (java.io.InputStream)36 Metacard (ddf.catalog.data.Metacard)29 Test (org.junit.Test)27 ByteArrayInputStream (java.io.ByteArrayInputStream)24 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)21 ArrayList (java.util.ArrayList)20 IOException (java.io.IOException)17 ServiceReference (org.osgi.framework.ServiceReference)17 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)16 BundleContext (org.osgi.framework.BundleContext)14 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 CatalogFramework (ddf.catalog.CatalogFramework)9 MimeType (javax.activation.MimeType)9 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)8 Result (ddf.catalog.data.Result)7 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)6 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)6 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)6 XppReader (com.thoughtworks.xstream.io.xml.XppReader)6