Search in sources :

Example 6 with InputTransformer

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

the class TestCswTransformProvider method testUnmarshalOtherSchema.

@Test
public void testUnmarshalOtherSchema() throws Exception {
    InputTransformer mockInputTransformer = mock(InputTransformer.class);
    when(mockInputManager.getTransformerByProperty(TransformerManager.SCHEMA, OTHER_SCHEMA)).thenReturn(mockInputTransformer);
    when(mockInputTransformer.transform(any(InputStream.class))).thenReturn(getMetacard());
    // XppReader is not namespace aware so it will read the XML and ignore the namespaces
    // WstxReader is namespace aware. It may fail for XML fragments.
    HierarchicalStreamReader reader = new XppReader(new StringReader(getRecord()), XmlPullParserFactory.newInstance().newPullParser());
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.SCHEMA);
    context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, OTHER_SCHEMA);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.unmarshal(reader, context);
    // Verify the context arguments were set correctly
    verify(mockInputManager, times(1)).getTransformerByProperty(captor.capture(), captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(OTHER_SCHEMA));
}
Also used : InputStream(java.io.InputStream) XppReader(com.thoughtworks.xstream.io.xml.XppReader) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Example 7 with InputTransformer

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

the class CswSubscriptionEndpointTest method getRecordsResponse.

private GetRecordsResponseType getRecordsResponse(int metacardCount) throws IOException, CatalogTransformerException {
    InputTransformer inputTransformer = mock(InputTransformer.class);
    when(mockInputManager.getTransformerBySchema(METACARD_SCHEMA)).thenReturn(inputTransformer);
    Metacard metacard = mock(Metacard.class);
    when(inputTransformer.transform(any(InputStream.class))).thenReturn(metacard);
    GetRecordsResponseType getRecordsResponse = new GetRecordsResponseType();
    SearchResultsType searchResults = new SearchResultsType();
    searchResults.setRecordSchema(METACARD_SCHEMA);
    getRecordsResponse.setSearchResults(searchResults);
    List<Object> any = new ArrayList<>();
    Node node = mock(Node.class);
    for (int i = 0; i < metacardCount; i++) {
        any.add(node);
    }
    searchResults.setAny(any);
    return getRecordsResponse;
}
Also used : Metacard(ddf.catalog.data.Metacard) InputStream(java.io.InputStream) SearchResultsType(net.opengis.cat.csw.v_2_0_2.SearchResultsType) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) GetRecordsResponseType(net.opengis.cat.csw.v_2_0_2.GetRecordsResponseType) InputTransformer(ddf.catalog.transform.InputTransformer)

Example 8 with InputTransformer

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

the class MetacardFactory method generateMetacard.

Metacard generateMetacard(String mimeTypeRaw, String id, String fileName, Path tmpContentPath) throws MetacardCreationException, MimeTypeParseException {
    Metacard generatedMetacard = null;
    MimeType mimeType = new MimeType(mimeTypeRaw);
    List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
    List<String> stackTraceList = new ArrayList<>();
    LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    for (InputTransformer candidate : listOfCandidates) {
        try (InputStream transformerStream = com.google.common.io.Files.asByteSource(tmpContentPath.toFile()).openStream()) {
            generatedMetacard = candidate.transform(transformerStream);
        } catch (CatalogTransformerException | IOException e) {
            List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
            stackTraceList.add(String.format("Transformer [%s] could not create metacard.", candidate));
            stackTraceList.addAll(stackTraces);
            LOGGER.debug("Transformer [{}] could not create metacard.", candidate, e);
        }
        if (generatedMetacard != null) {
            break;
        }
    }
    if (generatedMetacard == null) {
        throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeTypeRaw, StringUtils.join(stackTraceList, "\n")));
    }
    if (id != null) {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
    } else {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, uuidGenerator.generateUuid()));
    }
    if (StringUtils.isBlank(generatedMetacard.getTitle())) {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.TITLE, fileName));
    }
    return generatedMetacard;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) MimeType(javax.activation.MimeType) Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with InputTransformer

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

the class IntegrationTest method testInputAndOutput.

@Test
public void testInputAndOutput() throws CatalogTransformerException, IOException {
    Parser parser = new XmlParser();
    InputTransformer inputTransformer = new XmlInputTransformer(parser);
    MetacardMarshaller metacardMarshaller = new MetacardMarshallerImpl(parser, new PrintWriterProviderImpl());
    MetacardTransformer outputTransformer = new XmlMetacardTransformer(metacardMarshaller);
    InputStream input = getClass().getResourceAsStream("/extensibleMetacard.xml");
    Metacard metacard = inputTransformer.transform(input);
    LOGGER.info("Attributes: ");
    for (AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
        Attribute attribute = metacard.getAttribute(descriptor.getName());
        LOGGER.info("\t" + descriptor.getName() + ": " + ((attribute == null) ? attribute : attribute.getValue()));
    }
    BinaryContent output = outputTransformer.transform(metacard, mockArguments);
    String outputString = new String(output.getByteArray());
    // TODO test equivalence with XMLUnit.
    LOGGER.info(outputString);
}
Also used : XmlParser(org.codice.ddf.parser.xml.XmlParser) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) XmlMetacardTransformer(ddf.catalog.transformer.xml.XmlMetacardTransformer) Attribute(ddf.catalog.data.Attribute) MetacardMarshaller(ddf.catalog.transformer.api.MetacardMarshaller) InputStream(java.io.InputStream) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) XmlMetacardTransformer(ddf.catalog.transformer.xml.XmlMetacardTransformer) XmlInputTransformer(ddf.catalog.transformer.xml.XmlInputTransformer) InputTransformer(ddf.catalog.transform.InputTransformer) BinaryContent(ddf.catalog.data.BinaryContent) Parser(org.codice.ddf.parser.Parser) XmlParser(org.codice.ddf.parser.xml.XmlParser) MetacardMarshallerImpl(ddf.catalog.transformer.xml.MetacardMarshallerImpl) Metacard(ddf.catalog.data.Metacard) PrintWriterProviderImpl(ddf.catalog.transformer.xml.PrintWriterProviderImpl) XmlInputTransformer(ddf.catalog.transformer.xml.XmlInputTransformer) Test(org.junit.Test)

Example 10 with InputTransformer

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

the class RESTEndpoint method generateMetacard.

private Metacard generateMetacard(MimeType mimeType, String id, InputStream message, String transformerId) throws MetacardCreationException {
    Metacard generatedMetacard = null;
    List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
    List<String> stackTraceList = new ArrayList<>();
    LOGGER.trace("Entering generateMetacard.");
    LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            if (null != message) {
                IOUtils.copy(message, fileBackedOutputStream);
            } else {
                throw new MetacardCreationException("Could not copy bytes of content message.  Message was NULL.");
            }
        } catch (IOException e) {
            throw new MetacardCreationException("Could not copy bytes of content message.", e);
        }
        Iterator<InputTransformer> it = listOfCandidates.iterator();
        if (StringUtils.isNotEmpty(transformerId)) {
            BundleContext bundleContext = getBundleContext();
            Collection<ServiceReference<InputTransformer>> serviceReferences = bundleContext.getServiceReferences(InputTransformer.class, "(id=" + transformerId + ")");
            it = serviceReferences.stream().map(bundleContext::getService).iterator();
        }
        while (it.hasNext()) {
            InputTransformer transformer = it.next();
            try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
                generatedMetacard = transformer.transform(inputStreamMessageCopy);
            } catch (CatalogTransformerException | IOException e) {
                List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
                stackTraceList.add(String.format("Transformer [%s] could not create metacard.", transformer));
                stackTraceList.addAll(stackTraces);
                LOGGER.debug("Transformer [{}] could not create metacard.", transformer, e);
            }
            if (generatedMetacard != null) {
                break;
            }
        }
        if (generatedMetacard == null) {
            throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeType, StringUtils.join(stackTraceList, "\n")));
        }
        if (id != null) {
            generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
        } else {
            LOGGER.debug("Metacard had a null id");
        }
    } catch (IOException e) {
        throw new MetacardCreationException("Could not create metacard.", e);
    } catch (InvalidSyntaxException e) {
        throw new MetacardCreationException("Could not determine transformer", e);
    }
    return generatedMetacard;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) ServiceReference(org.osgi.framework.ServiceReference) Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext)

Aggregations

InputTransformer (ddf.catalog.transform.InputTransformer)29 InputStream (java.io.InputStream)23 Metacard (ddf.catalog.data.Metacard)17 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)15 IOException (java.io.IOException)13 Test (org.junit.Test)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ArrayList (java.util.ArrayList)8 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)7 MimeType (javax.activation.MimeType)7 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)6 ServiceReference (org.osgi.framework.ServiceReference)5 Result (ddf.catalog.data.Result)4 Matchers.anyString (org.mockito.Matchers.anyString)4 BundleContext (org.osgi.framework.BundleContext)4 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)3 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)3 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)3 XppReader (com.thoughtworks.xstream.io.xml.XppReader)3 MockMemoryStorageProvider (ddf.catalog.content.impl.MockMemoryStorageProvider)3