use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.
the class AbstractCatalogService method parseParts.
@Override
public Map.Entry<AttachmentInfo, Metacard> parseParts(Collection<Part> contentParts, String transformerParam) {
if (contentParts.size() == 1) {
Part part = Iterables.get(contentParts, 0);
try (InputStream inputStream = part.getInputStream()) {
ContentDisposition contentDisposition = new ContentDisposition(part.getHeader(HEADER_CONTENT_DISPOSITION));
return new ImmutablePair<>(attachmentParser.generateAttachmentInfo(inputStream, part.getContentType(), contentDisposition.getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME)), null);
} catch (IOException e) {
LOGGER.debug("IOException reading stream from file attachment in multipart body.", e);
}
}
Metacard metacard = null;
AttachmentInfo attachmentInfo = null;
Map<String, AttributeImpl> attributeMap = new HashMap<>();
for (Part part : contentParts) {
String name = part.getName();
String parsedName = (name.startsWith("parse.")) ? name.substring(6) : name;
try (InputStream inputStream = part.getInputStream()) {
ContentDisposition contentDisposition = new ContentDisposition(part.getHeader(HEADER_CONTENT_DISPOSITION));
switch(name) {
case "parse.resource":
attachmentInfo = attachmentParser.generateAttachmentInfo(inputStream, part.getContentType(), contentDisposition.getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME));
break;
case "parse.metadata":
metacard = parseMetacard(transformerParam, metacard, part, inputStream);
break;
default:
parseOverrideAttributes(attributeMap, parsedName, inputStream);
break;
}
} catch (IOException e) {
LOGGER.debug("Unable to get input stream for mime attachment. Ignoring override attribute: {}", name, e);
}
}
if (attachmentInfo == null) {
throw new IllegalArgumentException("No parse.resource specified in request.");
}
if (metacard == null) {
metacard = new MetacardImpl();
}
Set<AttributeDescriptor> missingDescriptors = new HashSet<>();
for (Attribute attribute : attributeMap.values()) {
if (metacard.getMetacardType().getAttributeDescriptor(attribute.getName()) == null) {
attributeRegistry.lookup(attribute.getName()).ifPresent(missingDescriptors::add);
}
metacard.setAttribute(attribute);
}
if (!missingDescriptors.isEmpty()) {
MetacardType original = metacard.getMetacardType();
MetacardImpl newMetacard = new MetacardImpl(metacard);
newMetacard.setType(new MetacardTypeImpl(original.getName(), original, missingDescriptors));
metacard = newMetacard;
}
return new ImmutablePair<>(attachmentInfo, metacard);
}
use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.
the class AbstractCatalogService method parseAttachments.
public Pair<AttachmentInfo, Metacard> parseAttachments(List<Attachment> contentParts, String transformerParam) {
if (contentParts.size() == 1) {
Attachment contentPart = contentParts.get(0);
InputStream attachmentInputStream = null;
try {
attachmentInputStream = contentPart.getDataHandler().getInputStream();
} catch (IOException e) {
LOGGER.debug("IOException reading stream from file attachment in multipart body.", e);
}
return new ImmutablePair<>(attachmentParser.generateAttachmentInfo(attachmentInputStream, contentPart.getContentType().toString(), contentPart.getContentDisposition().getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME)), null);
}
Map<String, AttributeImpl> attributeMap = new HashMap<>();
Metacard metacard = null;
AttachmentInfo attachmentInfo = null;
for (Attachment attachment : contentParts) {
String name = attachment.getContentDisposition().getParameter("name");
String parsedName = (name.startsWith("parse.")) ? name.substring(6) : name;
try {
InputStream inputStream = attachment.getDataHandler().getInputStream();
switch(name) {
case "parse.resource":
attachmentInfo = attachmentParser.generateAttachmentInfo(inputStream, attachment.getContentType().toString(), attachment.getContentDisposition().getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME));
break;
case "parse.metadata":
metacard = parseMetadata(transformerParam, metacard, attachment, inputStream);
break;
default:
parseOverrideAttributes(attributeMap, parsedName, inputStream);
break;
}
} catch (IOException e) {
LOGGER.debug("Unable to get input stream for mime attachment. Ignoring override attribute: {}", name, e);
}
}
if (attachmentInfo == null) {
throw new IllegalArgumentException("No parse.resource specified in request.");
}
if (metacard == null) {
metacard = new MetacardImpl();
}
Set<AttributeDescriptor> missingDescriptors = new HashSet<>();
for (Attribute attribute : attributeMap.values()) {
if (metacard.getMetacardType().getAttributeDescriptor(attribute.getName()) == null) {
attributeRegistry.lookup(attribute.getName()).ifPresent(missingDescriptors::add);
}
metacard.setAttribute(attribute);
}
if (!missingDescriptors.isEmpty()) {
MetacardType original = metacard.getMetacardType();
MetacardImpl newMetacard = new MetacardImpl(metacard);
newMetacard.setType(new MetacardTypeImpl(original.getName(), original, missingDescriptors));
metacard = newMetacard;
}
return new ImmutablePair<>(attachmentInfo, metacard);
}
use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.
the class CatalogServiceImplTest method testParseAttachmentsWithAttributeOverrides.
@Test
public void testParseAttachmentsWithAttributeOverrides() throws IngestException, SourceUnavailableException {
CatalogFramework framework = givenCatalogFramework();
CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry);
when(attributeRegistry.lookup(Topic.KEYWORD)).thenReturn(Optional.of(new TopicAttributes().getAttributeDescriptor(Topic.KEYWORD)));
when(attributeRegistry.lookup(Core.LOCATION)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.LOCATION)));
List<Attachment> attachments = new ArrayList<>();
ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=/path/to/metacard.txt");
Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
ContentDisposition contentDisposition1 = new ContentDisposition("form-data; name=parse.location");
Attachment attachment1 = new Attachment("parse.location", new ByteArrayInputStream("POINT(0 0)".getBytes()), contentDisposition1);
ContentDisposition contentDisposition2 = new ContentDisposition("form-data; name=parse.topic.keyword");
Attachment attachment2 = new Attachment("parse.topic.keyword", new ByteArrayInputStream("keyword1".getBytes()), contentDisposition2);
ContentDisposition contentDisposition3 = new ContentDisposition("form-data; name=parse.topic.keyword");
Attachment attachment3 = new Attachment("parse.topic.keyword", new ByteArrayInputStream("keyword2".getBytes()), contentDisposition3);
attachments.add(attachment);
attachments.add(attachment1);
attachments.add(attachment2);
attachments.add(attachment3);
Pair<AttachmentInfo, Metacard> attachmentInfoAndMetacard = catalogServiceImpl.parseAttachments(attachments, null);
Metacard metacard = attachmentInfoAndMetacard.getValue();
assertThat(metacard.getAttribute(Core.LOCATION).getValues(), hasItem("POINT(0 0)"));
assertThat(metacard.getAttribute(Topic.KEYWORD).getValues(), hasItems("keyword1", "keyword2"));
}
use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.
the class CatalogServiceImplTest method testParsePartsWithAttributeOverrides.
@Test
public void testParsePartsWithAttributeOverrides() throws Exception {
CatalogFramework framework = givenCatalogFramework();
CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry);
when(attributeRegistry.lookup(Topic.KEYWORD)).thenReturn(Optional.of(new TopicAttributes().getAttributeDescriptor(Topic.KEYWORD)));
when(attributeRegistry.lookup(Core.LOCATION)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.LOCATION)));
List<Part> parts = new ArrayList<>();
Part part = createPart("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), "form-data; name=parse.resource; filename=/path/to/metacard.txt");
Part part1 = createPart("parse.location", new ByteArrayInputStream("POINT(0 0)".getBytes()), "form-data; name=parse.location");
Part part2 = createPart("parse.topic.keyword", new ByteArrayInputStream("keyword1".getBytes()), "form-data; name=parse.topic.keyword");
Part part3 = createPart("parse.topic.keyword", new ByteArrayInputStream("keyword2".getBytes()), "form-data; name=parse.topic.keyword");
parts.add(part);
parts.add(part1);
parts.add(part2);
parts.add(part3);
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
when(httpServletRequest.getParts()).thenReturn(parts);
Map.Entry<AttachmentInfo, Metacard> attachmentInfoAndMetacard = catalogServiceImpl.parseParts(parts, null);
Metacard metacard = attachmentInfoAndMetacard.getValue();
assertThat(metacard.getAttribute(Core.LOCATION).getValues(), hasItem("POINT(0 0)"));
assertThat(metacard.getAttribute(Topic.KEYWORD).getValues(), hasItems("keyword1", "keyword2"));
}
use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.
the class CatalogServiceImplTest method testParseAttachments.
@Test
@SuppressWarnings({ "unchecked" })
public void testParseAttachments() throws IOException, CatalogTransformerException, SourceUnavailableException, IngestException, InvalidSyntaxException {
CatalogFramework framework = givenCatalogFramework();
BundleContext bundleContext = mock(BundleContext.class);
Collection<ServiceReference<InputTransformer>> serviceReferences = new ArrayList<>();
ServiceReference serviceReference = mock(ServiceReference.class);
InputTransformer inputTransformer = mock(InputTransformer.class);
MetacardImpl metacard = new MetacardImpl();
metacard.setMetadata("Some Text Again");
when(inputTransformer.transform(any())).thenReturn(metacard);
when(bundleContext.getService(serviceReference)).thenReturn(inputTransformer);
serviceReferences.add(serviceReference);
when(bundleContext.getServiceReferences(InputTransformer.class, "(id=xml)")).thenReturn(serviceReferences);
CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry) {
@Override
protected BundleContext getBundleContext() {
return bundleContext;
}
};
when(attributeRegistry.lookup(Core.METADATA)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.METADATA)));
when(attributeRegistry.lookup("foo")).thenReturn(Optional.empty());
addMatchingService(catalogServiceImpl, Collections.singletonList(getSimpleTransformer()));
List<Attachment> attachments = new ArrayList<>();
ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
attachments.add(attachment);
ContentDisposition contentDisposition1 = new ContentDisposition("form-data; name=parse.metadata; filename=C:\\DDF\\metacard.xml");
Attachment attachment1 = new Attachment("parse.metadata", new ByteArrayInputStream("Some Text Again".getBytes()), contentDisposition1);
attachments.add(attachment1);
Pair<AttachmentInfo, Metacard> attachmentInfoAndMetacard = catalogServiceImpl.parseAttachments(attachments, "xml");
assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("Some Text Again"));
ContentDisposition contentDisposition2 = new ContentDisposition("form-data; name=metadata; filename=C:\\DDF\\metacard.xml");
Attachment attachment2 = new Attachment("metadata", new ByteArrayInputStream("<meta>beta</meta>".getBytes()), contentDisposition2);
attachments.add(attachment2);
ContentDisposition contentDisposition3 = new ContentDisposition("form-data; name=foo; filename=C:\\DDF\\metacard.xml");
Attachment attachment3 = new Attachment("foo", new ByteArrayInputStream("bar".getBytes()), contentDisposition3);
attachments.add(attachment3);
attachmentInfoAndMetacard = catalogServiceImpl.parseAttachments(attachments, "xml");
assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("<meta>beta</meta>"));
assertThat(attachmentInfoAndMetacard.getValue().getAttribute("foo"), equalTo(null));
}
Aggregations