Search in sources :

Example 6 with AttachmentInfo

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);
}
Also used : HashMap(java.util.HashMap) Attribute(ddf.catalog.data.Attribute) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) MetacardType(ddf.catalog.data.MetacardType) Metacard(ddf.catalog.data.Metacard) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Part(javax.servlet.http.Part) HashSet(java.util.HashSet)

Example 7 with AttachmentInfo

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);
}
Also used : HashMap(java.util.HashMap) Attribute(ddf.catalog.data.Attribute) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) MetacardType(ddf.catalog.data.MetacardType) Metacard(ddf.catalog.data.Metacard) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) HashSet(java.util.HashSet)

Example 8 with AttachmentInfo

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"));
}
Also used : Metacard(ddf.catalog.data.Metacard) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) CoreAttributes(ddf.catalog.data.impl.types.CoreAttributes) CatalogFramework(ddf.catalog.CatalogFramework) ArrayList(java.util.ArrayList) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) TopicAttributes(ddf.catalog.data.impl.types.TopicAttributes) Test(org.junit.Test)

Example 9 with AttachmentInfo

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"));
}
Also used : CoreAttributes(ddf.catalog.data.impl.types.CoreAttributes) ArrayList(java.util.ArrayList) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) TopicAttributes(ddf.catalog.data.impl.types.TopicAttributes) HttpServletRequest(javax.servlet.http.HttpServletRequest) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) Part(javax.servlet.http.Part) CatalogFramework(ddf.catalog.CatalogFramework) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 10 with AttachmentInfo

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));
}
Also used : CoreAttributes(ddf.catalog.data.impl.types.CoreAttributes) ArrayList(java.util.ArrayList) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) InputTransformer(ddf.catalog.transform.InputTransformer) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) ServiceReference(org.osgi.framework.ServiceReference) Metacard(ddf.catalog.data.Metacard) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) CatalogFramework(ddf.catalog.CatalogFramework) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

AttachmentInfo (org.codice.ddf.attachment.AttachmentInfo)11 ByteArrayInputStream (java.io.ByteArrayInputStream)9 Test (org.junit.Test)9 Metacard (ddf.catalog.data.Metacard)8 CatalogFramework (ddf.catalog.CatalogFramework)6 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)6 CoreAttributes (ddf.catalog.data.impl.types.CoreAttributes)6 ArrayList (java.util.ArrayList)6 InputStream (java.io.InputStream)5 HashMap (java.util.HashMap)5 InputTransformer (ddf.catalog.transform.InputTransformer)4 Part (javax.servlet.http.Part)4 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)4 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceReference (org.osgi.framework.ServiceReference)4 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Attribute (ddf.catalog.data.Attribute)2 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)2