Search in sources :

Example 26 with MetacardType

use of ddf.catalog.data.MetacardType in project ddf by codice.

the class HandlebarsMetacard method getAttributes.

public Set<AttributeEntry> getAttributes() {
    MetacardType metacardType = this.getMetacardType();
    Set<AttributeEntry> attributes = new TreeSet<>();
    for (AttributeDescriptor descriptor : metacardType.getAttributeDescriptors()) {
        Attribute attr = this.getAttribute(descriptor.getName());
        if (attr != null) {
            attributes.add(new AttributeEntry(attr, descriptor.getType().getAttributeFormat()));
        }
    }
    return attributes;
}
Also used : Attribute(ddf.catalog.data.Attribute) TreeSet(java.util.TreeSet) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardType(ddf.catalog.data.MetacardType)

Example 27 with MetacardType

use of ddf.catalog.data.MetacardType in project ddf by codice.

the class TestCswMarshallHelper method testWriteAllFields.

@Test
public void testWriteAllFields() {
    HierarchicalStreamWriter writer = mock(HierarchicalStreamWriter.class);
    MarshallingContext context = mock(MarshallingContext.class);
    Attribute attribute = mock(Attribute.class);
    when(attribute.getValues()).thenReturn(Arrays.asList(new String[] { "TEST1", "TEST2", "TEST3" }));
    MetacardImpl metacard = mock(MetacardImpl.class);
    MetacardType metacardType = mock(MetacardType.class);
    when(metacard.getMetacardType()).thenReturn(metacardType);
    when(metacard.getAttribute(any(String.class))).thenReturn(attribute);
    Set<AttributeDescriptor> attributeDescriptors = new HashSet<>();
    AttributeDescriptor ad = mock(AttributeDescriptor.class);
    when(ad.isMultiValued()).thenReturn(true);
    when(ad.getName()).thenReturn(CswConstants.CSW_SOURCE_QNAME.toString());
    attributeDescriptors.add(ad);
    when(metacardType.getAttributeDescriptors()).thenReturn(attributeDescriptors);
    CswMarshallHelper.writeAllFields(writer, context, metacard);
    verify(writer, times(3)).startNode(any(String.class));
    verify(writer, times(3)).setValue(any(String.class));
    verify(writer, times(3)).endNode();
}
Also used : HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) MetacardType(ddf.catalog.data.MetacardType) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 28 with MetacardType

use of ddf.catalog.data.MetacardType in project ddf by codice.

the class EndpointUtil method getMetacardTypeMap.

@SuppressWarnings("unchecked")
public Map<String, Object> getMetacardTypeMap() {
    Map<String, Object> resultTypes = new HashMap<>();
    for (MetacardType metacardType : metacardTypes) {
        Map<String, Object> attributes = new HashMap<>();
        for (AttributeDescriptor descriptor : metacardType.getAttributeDescriptors()) {
            Map<String, Object> attributeProperties = new HashMap<>();
            attributeProperties.put("type", descriptor.getType().getAttributeFormat().name());
            attributeProperties.put("multivalued", descriptor.isMultiValued());
            attributeProperties.put("id", descriptor.getName());
            attributes.put(descriptor.getName(), attributeProperties);
        }
        resultTypes.put(metacardType.getName(), attributes);
    }
    for (InjectableAttribute attribute : injectableAttributes) {
        Optional<AttributeDescriptor> lookup = attributeRegistry.lookup(attribute.attribute());
        if (!lookup.isPresent()) {
            continue;
        }
        AttributeDescriptor descriptor = lookup.get();
        Map<String, Object> attributeProperties = new HashMap<>();
        attributeProperties.put("type", descriptor.getType().getAttributeFormat().name());
        attributeProperties.put("multivalued", descriptor.isMultiValued());
        attributeProperties.put("id", descriptor.getName());
        Set<String> types = attribute.metacardTypes().isEmpty() ? resultTypes.keySet() : attribute.metacardTypes();
        for (String type : types) {
            Map<String, Object> attributes = (Map) resultTypes.getOrDefault(type, new HashMap<String, Object>());
            attributes.put(attribute.attribute(), attributeProperties);
            resultTypes.put(type, attributes);
        }
    }
    return resultTypes;
}
Also used : HashMap(java.util.HashMap) InjectableAttribute(ddf.catalog.data.InjectableAttribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) MetacardType(ddf.catalog.data.MetacardType)

Example 29 with MetacardType

use of ddf.catalog.data.MetacardType in project ddf by codice.

the class ValidationParser method parseMetacardTypes.

private List<Callable<Boolean>> parseMetacardTypes(Changeset changeset, List<Outer.MetacardType> metacardTypes) {
    List<Callable<Boolean>> staged = new ArrayList<>();
    BundleContext context = getBundleContext();
    for (Outer.MetacardType metacardType : metacardTypes) {
        Set<AttributeDescriptor> attributeDescriptors = new HashSet<>(BasicTypes.BASIC_METACARD.getAttributeDescriptors());
        Set<String> requiredAttributes = new HashSet<>();
        metacardType.attributes.forEach((attributeName, attribute) -> {
            AttributeDescriptor descriptor = attributeRegistry.lookup(attributeName).orElseThrow(() -> new IllegalStateException(String.format("Metacard type [%s] includes the attribute [%s], but that attribute is not in the attribute registry.", metacardType.type, attributeName)));
            attributeDescriptors.add(descriptor);
            if (attribute.required) {
                requiredAttributes.add(attributeName);
            }
        });
        if (!requiredAttributes.isEmpty()) {
            final MetacardValidator validator = new RequiredAttributesMetacardValidator(metacardType.type, requiredAttributes);
            staged.add(() -> {
                ServiceRegistration<MetacardValidator> registration = context.registerService(MetacardValidator.class, validator, null);
                changeset.metacardValidatorServices.add(registration);
                return registration != null;
            });
        }
        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put(NAME_PROPERTY, metacardType.type);
        MetacardType type = new MetacardTypeImpl(metacardType.type, attributeDescriptors);
        staged.add(() -> {
            ServiceRegistration<MetacardType> registration = context.registerService(MetacardType.class, type, properties);
            changeset.metacardTypeServices.add(registration);
            return registration != null;
        });
    }
    return staged;
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) Callable(java.util.concurrent.Callable) MetacardType(ddf.catalog.data.MetacardType) MetacardValidator(ddf.catalog.validation.MetacardValidator) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator) BundleContext(org.osgi.framework.BundleContext) HashSet(java.util.HashSet)

Example 30 with MetacardType

use of ddf.catalog.data.MetacardType in project ddf by codice.

the class TikaInputTransformer method transform.

@Override
public Metacard transform(InputStream input, String id) throws IOException, CatalogTransformerException {
    LOGGER.debug("Transforming input stream using Tika.");
    if (input == null) {
        throw new CatalogTransformerException("Cannot transform null input.");
    }
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            IOUtils.copy(input, fileBackedOutputStream);
        } catch (IOException e) {
            throw new CatalogTransformerException("Could not copy bytes of content message.", e);
        }
        Parser parser = new AutoDetectParser();
        ToXMLContentHandler xmlContentHandler = new ToXMLContentHandler();
        ToTextContentHandler textContentHandler = null;
        ContentHandler contentHandler;
        if (!contentMetadataExtractors.isEmpty()) {
            textContentHandler = new ToTextContentHandler();
            contentHandler = new TeeContentHandler(xmlContentHandler, textContentHandler);
        } else {
            contentHandler = xmlContentHandler;
        }
        TikaMetadataExtractor tikaMetadataExtractor = new TikaMetadataExtractor(parser, contentHandler);
        Metadata metadata;
        try (InputStream inputStreamCopy = fileBackedOutputStream.asByteSource().openStream()) {
            metadata = tikaMetadataExtractor.parseMetadata(inputStreamCopy, new ParseContext());
        }
        String metadataText = xmlContentHandler.toString();
        if (templates != null) {
            metadataText = transformToXml(metadataText);
        }
        String metacardContentType = metadata.get(Metadata.CONTENT_TYPE);
        MetacardType metacardType = getMetacardTypeFromMimeType(metacardContentType);
        if (metacardType == null) {
            metacardType = commonTikaMetacardType;
        }
        Metacard metacard;
        if (textContentHandler != null) {
            String plainText = textContentHandler.toString();
            Set<AttributeDescriptor> attributes = contentMetadataExtractors.values().stream().map(ContentMetadataExtractor::getMetacardAttributes).flatMap(Collection::stream).collect(Collectors.toSet());
            MetacardTypeImpl extendedMetacardType = new MetacardTypeImpl(metacardType.getName(), metacardType, attributes);
            metacard = MetacardCreator.createMetacard(metadata, id, metadataText, extendedMetacardType);
            for (ContentMetadataExtractor contentMetadataExtractor : contentMetadataExtractors.values()) {
                contentMetadataExtractor.process(plainText, metacard);
            }
        } else {
            metacard = MetacardCreator.createMetacard(metadata, id, metadataText, metacardType);
        }
        if (StringUtils.isNotBlank(metacardContentType)) {
            metacard.setAttribute(new AttributeImpl(Core.DATATYPE, getDatatype(metacardContentType)));
        }
        if (StringUtils.startsWith(metacardContentType, "image")) {
            try (InputStream inputStreamCopy = fileBackedOutputStream.asByteSource().openStream()) {
                createThumbnail(inputStreamCopy, metacard);
            }
        }
        LOGGER.debug("Finished transforming input stream using Tika.");
        return metacard;
    }
}
Also used : ToXMLContentHandler(org.apache.tika.sax.ToXMLContentHandler) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) CloseShieldInputStream(org.apache.tika.io.CloseShieldInputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) Metadata(org.apache.tika.metadata.Metadata) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) IOException(java.io.IOException) ToXMLContentHandler(org.apache.tika.sax.ToXMLContentHandler) TeeContentHandler(org.apache.tika.sax.TeeContentHandler) ToTextContentHandler(org.apache.tika.sax.ToTextContentHandler) ContentHandler(org.xml.sax.ContentHandler) MetacardType(ddf.catalog.data.MetacardType) Parser(org.apache.tika.parser.Parser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) ToTextContentHandler(org.apache.tika.sax.ToTextContentHandler) TikaMetadataExtractor(ddf.catalog.transformer.common.tika.TikaMetadataExtractor) Metacard(ddf.catalog.data.Metacard) ParseContext(org.apache.tika.parser.ParseContext) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) ContentMetadataExtractor(ddf.catalog.content.operation.ContentMetadataExtractor) TeeContentHandler(org.apache.tika.sax.TeeContentHandler)

Aggregations

MetacardType (ddf.catalog.data.MetacardType)88 Test (org.junit.Test)57 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)41 Metacard (ddf.catalog.data.Metacard)35 ArrayList (java.util.ArrayList)29 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)20 MetacardTypeImpl (ddf.catalog.data.impl.MetacardTypeImpl)17 HashSet (java.util.HashSet)15 Attribute (ddf.catalog.data.Attribute)12 Serializable (java.io.Serializable)10 IOException (java.io.IOException)8 Date (java.util.Date)8 HashMap (java.util.HashMap)8 List (java.util.List)8 Map (java.util.Map)8 AttributeDescriptorImpl (ddf.catalog.data.impl.AttributeDescriptorImpl)7 InputStream (java.io.InputStream)7 Set (java.util.Set)7 ContentItem (ddf.catalog.content.data.ContentItem)6 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)6