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;
}
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();
}
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;
}
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;
}
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;
}
}
Aggregations